summaryrefslogtreecommitdiffstats
path: root/dom/settings/SettingsRequestManager.jsm
blob: fced802472d70b8bd404a472aaeb6c5f1f338e55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;

Cu.importGlobalProperties(['File']);

this.EXPORTED_SYMBOLS = ["SettingsRequestManager"];

Cu.import("resource://gre/modules/SettingsDB.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/PermissionsTable.jsm");

var DEBUG = false;
var VERBOSE = false;
var TRACK = false;

try {
  DEBUG   =
    Services.prefs.getBoolPref("dom.mozSettings.SettingsRequestManager.debug.enabled");
  VERBOSE =
    Services.prefs.getBoolPref("dom.mozSettings.SettingsRequestManager.verbose.enabled");
  TRACK =
    Services.prefs.getBoolPref("dom.mozSettings.trackTasksUsage");
} catch (ex) { }

var allowForceReadOnly = false;
try {
  allowForceReadOnly = Services.prefs.getBoolPref("dom.mozSettings.allowForceReadOnly");
} catch (ex) { }

function debug(s) {
  dump("-*- SettingsRequestManager: " + s + "\n");
}

var inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
                  .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;

const kXpcomShutdownObserverTopic      = "xpcom-shutdown";
const kInnerWindowDestroyed            = "inner-window-destroyed";
const kMozSettingsChangedObserverTopic = "mozsettings-changed";
const kSettingsReadSuffix              = "-read";
const kSettingsWriteSuffix             = "-write";
const kSettingsClearPermission         = "settings-clear";
const kAllSettingsReadPermission       = "settings" + kSettingsReadSuffix;
const kAllSettingsWritePermission      = "settings" + kSettingsWriteSuffix;
// Any application with settings permissions, be it for all settings
// or a single one, will need to be able to access the settings API.
// The settings-api permission allows an app to see the mozSettings
// API in order to create locks and queue tasks. Whether these tasks
// will be allowed depends on the exact permissions the app has.
const kSomeSettingsReadPermission      = "settings-api" + kSettingsReadSuffix;
const kSomeSettingsWritePermission     = "settings-api" + kSettingsWriteSuffix;

// Time, in seconds, to consider the API is starting to jam
var kSoftLockupDelta = 30;
try {
  kSoftLockupDelta = Services.prefs.getIntPref("dom.mozSettings.softLockupDelta");
} catch (ex) { }

XPCOMUtils.defineLazyServiceGetter(this, "mrm",
                                   "@mozilla.org/memory-reporter-manager;1",
                                   "nsIMemoryReporterManager");
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
                                   "@mozilla.org/parentprocessmessagemanager;1",
                                   "nsIMessageBroadcaster");
XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
                                   "@mozilla.org/uuid-generator;1",
                                   "nsIUUIDGenerator");
XPCOMUtils.defineLazyServiceGetter(this, "gSettingsService",
                                   "@mozilla.org/settingsService;1",
                                   "nsISettingsService");

var SettingsPermissions = {
  checkPermission: function(aPrincipal, aPerm) {
    if (!aPrincipal) {
      Cu.reportError("SettingsPermissions.checkPermission was passed a null principal. Denying all permissions.");
      return false;
    }
    if (aPrincipal.origin == "[System Principal]" ||
        Services.perms.testExactPermissionFromPrincipal(aPrincipal, aPerm) == Ci.nsIPermissionManager.ALLOW_ACTION) {
      return true;
    }
    return false;
  },
  hasAllReadPermission: function(aPrincipal) {
    return this.checkPermission(aPrincipal, kAllSettingsReadPermission);
  },
  hasAllWritePermission: function(aPrincipal) {
    return this.checkPermission(aPrincipal, kAllSettingsWritePermission);
  },
  hasSomeReadPermission: function(aPrincipal) {
    return this.checkPermission(aPrincipal, kSomeSettingsReadPermission);
  },
  hasSomeWritePermission: function(aPrincipal) {
    return this.checkPermission(aPrincipal, kSomeSettingsWritePermission);
  },
  hasClearPermission: function(aPrincipal) {
    return this.checkPermission(aPrincipal, kSettingsClearPermission);
  },
  hasReadPermission: function(aPrincipal, aSettingsName) {
    return this.hasAllReadPermission(aPrincipal) || this.checkPermission(aPrincipal, "settings:" + aSettingsName + kSettingsReadSuffix);
  },
  hasWritePermission: function(aPrincipal, aSettingsName) {
    return this.hasAllWritePermission(aPrincipal) || this.checkPermission(aPrincipal, "settings:" + aSettingsName + kSettingsWriteSuffix);
  }
};


function SettingsLockInfo(aDB, aMsgMgr, aPrincipal, aLockID, aIsServiceLock, aWindowID, aLockStack) {
  return {
    // ID Shared with the object on the child side
    lockID: aLockID,
    // Is this a content lock or a settings service lock?
    isServiceLock: aIsServiceLock,
    // Which inner window ID
    windowID: aWindowID,
    // Where does this lock comes from
    lockStack: aLockStack,
    // Tasks to be run once the lock is at the head of the queue
    tasks: [],
    // This is set to true once a transaction is ready to run, but is not at the
    // head of the lock queue.
    consumable: false,
    // Holds values that are requested to be set until the lock lifetime ends,
    // then commits them to the DB.
    queuedSets: {},
    // Internal transaction object
    _transaction: undefined,
    // Message manager that controls the lock
    _mm: aMsgMgr,
    // If true, it means a permissions check failed, so just fail everything now
    _failed: false,
    // If we're slated to run finalize, set this to make sure we don't
    // somehow run other events afterward.
    finalizing: false,
    // Lets us know if we can use this lock for a clear command
    canClear: true,
    // Lets us know if this lock has been used to clear at any point.
    hasCleared: false,
    // forceReadOnly sets whether we want to do a read only transaction. Define
    // true by default, and let queueTask() set this to false if we queue any
    // "set" task. Since users of settings locks will queue all tasks before
    // any idb transaction is created, we know we will have all needed
    // information to set this before creating a transaction.
    forceReadOnly: true,
    // Principal the lock was created under. We assume that the lock
    // will continue to exist under this principal for the duration of
    // its lifetime.
    principal: aPrincipal,
    getObjectStore: function() {
      if (VERBOSE) debug("Getting transaction for " + this.lockID);
      let store;
      // Test for transaction validity via trying to get the
      // datastore. If it doesn't work, assume the transaction is
      // closed, create a new transaction and try again.
      if (this._transaction) {
        try {
          store = this._transaction.objectStore(SETTINGSSTORE_NAME);
        } catch (e) {
          if (e.name == "InvalidStateError") {
            if (VERBOSE) debug("Current transaction for " + this.lockID + " closed, trying to create new one.");
          } else {
            if (DEBUG) debug("Unexpected exception, throwing: " + e);
            throw e;
          }
        }
      }
      // Create one transaction with a global permission. This may be
      // slightly slower on apps with full settings permissions, but
      // it means we don't have to do our own transaction order
      // bookkeeping.
      let canReadOnly = allowForceReadOnly && this.forceReadOnly;
      if (canReadOnly || !SettingsPermissions.hasSomeWritePermission(this.principal)) {
        if (VERBOSE) debug("Making READONLY transaction for " + this.lockID);
        this._transaction = aDB._db.transaction(SETTINGSSTORE_NAME, "readonly");
      } else {
        if (VERBOSE) debug("Making READWRITE transaction for " + this.lockID);
        this._transaction = aDB._db.transaction(SETTINGSSTORE_NAME, "readwrite");
      }
      this._transaction.oncomplete = function() {
        if (VERBOSE) debug("Transaction for lock " + this.lockID + " closed");
      }.bind(this);
      this._transaction.onabort = function () {
        if (DEBUG) debug("Transaction for lock " + this.lockID + " aborted");
        this._failed = true;
      }.bind(this);
      try {
        store = this._transaction.objectStore(SETTINGSSTORE_NAME);
      } catch (e) {
          if (e.name == "InvalidStateError") {
            if (DEBUG) debug("Cannot create objectstore on transaction for " + this.lockID);
            return null;
          } else {
            if (DEBUG) debug("Unexpected exception, throwing: " + e);
            throw e;
          }
      }
      return store;
    }
  };
}

var SettingsRequestManager = {
  // Access to the settings DB
  settingsDB: new SettingsDB(),
  // Remote messages to listen for from child
  messages: ["child-process-shutdown", "Settings:Get", "Settings:Set",
             "Settings:Clear", "Settings:Run", "Settings:Finalize",
             "Settings:CreateLock", "Settings:RegisterForMessages"],
  // Map of LockID to SettingsLockInfo objects
  lockInfo: {},
  // Storing soft lockup detection infos
  softLockup: {
    lockId: null, // last lock dealt with
    lockTs: null  // last time of dealing with
  },
  // Queue of LockIDs. The LockID on the front of the queue is the only lock
  // that will have requests processed, all other locks will queue requests
  // until they hit the front of the queue.
  settingsLockQueue: [],
  children: [],
  // Since we need to call observers at times when we may not have
  // just received a message from a child process, we cache principals
  // for message managers and check permissions on them before we send
  // settings notifications to child processes.
  observerPrincipalCache: new Map(),
  totalProcessed: 0,
  tasksConsumed: {},
  totalSetProcessed: 0,
  tasksSetConsumed: {},
  totalGetProcessed: 0,
  tasksGetConsumed: {},

  init: function() {
    if (VERBOSE) debug("init");
    this.settingsDB.init();
    this.messages.forEach((function(msgName) {
      ppmm.addMessageListener(msgName, this);
    }).bind(this));
    Services.obs.addObserver(this, kXpcomShutdownObserverTopic, false);
    Services.obs.addObserver(this, kInnerWindowDestroyed, false);
    mrm.registerStrongReporter(this);
  },

  _serializePreservingBinaries: function _serializePreservingBinaries(aObject) {
    function needsUUID(aValue) {
      if (!aValue || !aValue.constructor) {
        return false;
      }
      return (aValue.constructor.name == "Date") || (aValue instanceof File) ||
             (aValue instanceof Ci.nsIDOMBlob);
    }
    // We need to serialize settings objects, otherwise they can change between
    // the set() call and the enqueued request being processed. We can't simply
    // parse(stringify(obj)) because that breaks things like Blobs, Files and
    // Dates, so we use stringify's replacer and parse's reviver parameters to
    // preserve binaries.
    let binaries = Object.create(null);
    let stringified = JSON.stringify(aObject, function(key, value) {
      value = this.settingsDB.prepareValue(value);
      if (needsUUID(value)) {
        let uuid = uuidgen.generateUUID().toString();
        binaries[uuid] = value;
        return uuid;
      }
      return value;
    }.bind(this));
    return JSON.parse(stringified, function(key, value) {
      if (value in binaries) {
        return binaries[value];
      }
      return value;
    });
  },

  queueTask: function(aOperation, aData) {
    if (VERBOSE) debug("Queueing task: " + aOperation);

    let defer = {};

    let lock = this.lockInfo[aData.lockID];

    if (!lock) {
      return Promise.reject({error: "Lock already dead, cannot queue task"});
    }

    if (aOperation == "set") {
      aData.settings = this._serializePreservingBinaries(aData.settings);
    }

    if (aOperation === "set" || aOperation === "clear") {
      lock.forceReadOnly = false;
    }

    lock.tasks.push({
      operation: aOperation,
      data: aData,
      defer: defer
    });

    let promise = new Promise(function(resolve, reject) {
      defer.resolve = resolve;
      defer.reject = reject;
    });

    return promise;
  },

  // Due to the fact that we're skipping the database in some places
  // by keeping a local "set" value cache, resolving some calls
  // without a call to the database would mean we could potentially
  // receive promise responses out of expected order if a get is
  // called before a set. Therefore, we wrap our resolve in a null
  // get, which means it will resolves afer the rest of the calls
  // queued to the DB.
  queueTaskReturn: function(aTask, aReturnValue) {
    if (VERBOSE) debug("Making task queuing transaction request.");
    let data = aTask.data;
    let lock = this.lockInfo[data.lockID];
    let store = lock.getObjectStore(lock.principal);
    if (!store) {
      if (DEBUG) debug("Rejecting task queue on lock " + aTask.data.lockID);
      return Promise.reject({task: aTask, error: "Cannot get object store"});
    }
    // Due to the fact that we're skipping the database, resolving
    // this without a call to the database would mean we could
    // potentially receive promise responses out of expected order if
    // a get is called before a set. Therefore, we wrap our resolve in
    // a null get, which means it will resolves afer the rest of the
    // calls queued to the DB.
    let getReq = store.get(0);

    let defer = {};
    let promiseWrapper = new Promise(function(resolve, reject) {
      defer.resolve = resolve;
      defer.reject = reject;
    });

    getReq.onsuccess = function(event) {
      return defer.resolve(aReturnValue);
    };
    getReq.onerror = function() {
      return defer.reject({task: aTask, error: getReq.error.name});
    };
    return promiseWrapper;
  },
  
  taskGet: function(aTask) {
    if (VERBOSE) debug("Running Get task on lock " + aTask.data.lockID);

    // Check that we have permissions for getting the value
    let data = aTask.data;
    let lock = this.lockInfo[data.lockID];

    if (!lock) {
      return Promise.reject({task: aTask, error: "Lock died, can't finalize"});
    }
    if (lock._failed) {
      if (DEBUG) debug("Lock failed. All subsequent requests will fail.");
      return Promise.reject({task: aTask, error: "Lock failed, all requests now failing."});
    }

    if (lock.hasCleared) {
      if (DEBUG) debug("Lock was used for a clear command. All subsequent requests will fail.");
      return Promise.reject({task: aTask, error: "Lock was used for a clear command. All subsequent requests will fail."});
    }

    lock.canClear = false;
    
    if (!SettingsPermissions.hasReadPermission(lock.principal, data.name)) {
      if (DEBUG) debug("get not allowed for " + data.name);
      lock._failed = true;
      return Promise.reject({task: aTask, error: "No permission to get " + data.name});
    }

    // If the value was set during this transaction, use the cached value
    if (data.name in lock.queuedSets) {
      if (VERBOSE) debug("Returning cached set value " + lock.queuedSets[data.name] + " for " + data.name);
      let local_results = {};
      local_results[data.name] = lock.queuedSets[data.name];
      return this.queueTaskReturn(aTask, {task: aTask, results: local_results});
    }

    // Create/Get transaction and make request
    if (VERBOSE) debug("Making get transaction request for " + data.name);
    let store = lock.getObjectStore(lock.principal);
    if (!store) {
      if (DEBUG) debug("Rejecting Get task on lock " + aTask.data.lockID);
      return Promise.reject({task: aTask, error: "Cannot get object store"});
    }

    if (VERBOSE) debug("Making get request for " + data.name);
    let getReq = (data.name === "*") ? store.mozGetAll() : store.mozGetAll(data.name);

    let defer = {};
    let promiseWrapper = new Promise(function(resolve, reject) {
      defer.resolve = resolve;
      defer.reject = reject;
    });

    getReq.onsuccess = function(event) {
      if (VERBOSE) debug("Request for '" + data.name + "' successful. " +
            "Record count: " + event.target.result.length);

      if (event.target.result.length == 0) {
        if (VERBOSE) debug("MOZSETTINGS-GET-WARNING: " + data.name + " is not in the database.\n");
      }

      let results = {};

      for (let i in event.target.result) {
        let result = event.target.result[i];
        let name = result.settingName;
        if (VERBOSE) debug(name + ": " + result.userValue +", " + result.defaultValue);
        let value = result.userValue !== undefined ? result.userValue : result.defaultValue;
        results[name] = value;
      }
      return defer.resolve({task: aTask, results: results});
    };
    getReq.onerror = function() {
      return defer.reject({task: aTask, error: getReq.error.name});
    };
    return promiseWrapper;
  },

  taskSet: function(aTask) {
    let data = aTask.data;
    let lock = this.lockInfo[data.lockID];
    let keys = Object.getOwnPropertyNames(data.settings);

    if (!lock) {
      return Promise.reject({task: aTask, error: "Lock died, can't finalize"});
    }
    if (lock._failed) {
      if (DEBUG) debug("Lock failed. All subsequent requests will fail.");
      return Promise.reject({task: aTask, error: "Lock failed a permissions check, all requests now failing."});
    }

    if (lock.hasCleared) {
      if (DEBUG) debug("Lock was used for a clear command. All subsequent requests will fail.");
      return Promise.reject({task: aTask, error: "Lock was used for a clear command. All other requests will fail."});
    }

    lock.canClear = false;

    // If we have no keys, resolve
    if (keys.length === 0) {
      if (DEBUG) debug("No keys to change entered!");
      return Promise.resolve({task: aTask});
    }

    for (let i = 0; i < keys.length; i++) {
      if (!SettingsPermissions.hasWritePermission(lock.principal, keys[i])) {
        if (DEBUG) debug("set not allowed on " + keys[i]);
        lock._failed = true;
        return Promise.reject({task: aTask, error: "No permission to set " + keys[i]});
      }
    }

    for (let i = 0; i < keys.length; i++) {
      let key = keys[i];
      if (VERBOSE) debug("key: " + key + ", val: " + JSON.stringify(data.settings[key]) + ", type: " + typeof(data.settings[key]));
      lock.queuedSets[key] = data.settings[key];
    }

    return this.queueTaskReturn(aTask, {task: aTask});
  },

  startRunning: function(aLockID) {
    let lock = this.lockInfo[aLockID];

    if (!lock) {
      if (DEBUG) debug("Lock no longer alive, cannot start running");
      return;
    }

    lock.consumable = true;
    if (aLockID == this.settingsLockQueue[0] || this.settingsLockQueue.length == 0) {
      // If a lock is currently at the head of the queue, run all tasks for
      // it.
      if (VERBOSE) debug("Start running tasks for " + aLockID);
      this.queueConsume();
    } else {
      // If a lock isn't at the head of the queue, but requests to be run,
      // simply mark it as consumable, which means it will automatically run
      // once it comes to the head of the queue.
      if (VERBOSE) debug("Queuing tasks for " + aLockID + " while waiting for " + this.settingsLockQueue[0]);
    }
  },

  queueConsume: function() {
    if (this.settingsLockQueue.length > 0 && this.lockInfo[this.settingsLockQueue[0]].consumable) {
      Services.tm.currentThread.dispatch(SettingsRequestManager.consumeTasks.bind(this), Ci.nsIThread.DISPATCH_NORMAL);
    }
  },

  finalizeSets: function(aTask) {
    let data = aTask.data;
    if (VERBOSE) debug("Finalizing tasks for lock " + data.lockID);
    let lock = this.lockInfo[data.lockID];

    if (!lock) {
      return Promise.reject({task: aTask, error: "Lock died, can't finalize"});
    }
    lock.finalizing = true;
    if (lock._failed) {
      this.removeLock(data.lockID);
      return Promise.reject({task: aTask, error: "Lock failed a permissions check, all requests now failing."});
    }
    // If we have cleared, there is no reason to continue finalizing
    // this lock. Just resolve promise with task and move on.
    if (lock.hasCleared) {
      if (VERBOSE) debug("Clear was called on lock, skipping finalize");
      this.removeLock(data.lockID);
      return Promise.resolve({task: aTask});
    }
    let keys = Object.getOwnPropertyNames(lock.queuedSets);
    if (keys.length === 0) {
      if (VERBOSE) debug("Nothing to finalize. Exiting.");
      this.removeLock(data.lockID);
      return Promise.resolve({task: aTask});
    }

    let store = lock.getObjectStore(lock.principal);
    if (!store) {
      if (DEBUG) debug("Rejecting Set task on lock " + aTask.data.lockID);
      this.removeLock(data.lockID);
      return Promise.reject({task: aTask, error: "Cannot get object store"});
    }

    // Due to the fact there may have multiple set operations to clear, and
    // they're all async, callbacks are gathered into promises, and the promises
    // are processed with Promises.all().
    let checkPromises = [];
    let finalValues = {};
    for (let i = 0; i < keys.length; i++) {
      let key = keys[i];
      if (VERBOSE) debug("key: " + key + ", val: " + lock.queuedSets[key] + ", type: " + typeof(lock.queuedSets[key]));
      let checkDefer = {};
      let checkPromise = new Promise(function(resolve, reject) {
        checkDefer.resolve = resolve;
        checkDefer.reject = reject;
      });

      // Get operation is used to fill in the default value, assuming there is
      // one. For the moment, if a value doesn't exist in the settings DB, we
      // allow the user to add it, and just pass back a null default value.
      let checkKeyRequest = store.get(key);
      checkKeyRequest.onsuccess = function (event) {
        let userValue = lock.queuedSets[key];
        let defaultValue;
        if (!event.target.result) {
          defaultValue = null;
          if (VERBOSE) debug("MOZSETTINGS-GET-WARNING: " + key + " is not in the database.\n");
        } else {
          defaultValue = event.target.result.defaultValue;
        }
        let obj = {settingName: key, defaultValue: defaultValue, userValue: userValue};
        finalValues[key] = {defaultValue: defaultValue, userValue: userValue};
        let setReq = store.put(obj);
        setReq.onsuccess = function() {
          if (VERBOSE) debug("Set successful!");
          if (VERBOSE) debug("key: " + key + ", val: " + finalValues[key] + ", type: " + typeof(finalValues[key]));
          return checkDefer.resolve({task: aTask});
        };
        setReq.onerror = function() {
          return checkDefer.reject({task: aTask, error: setReq.error.name});
        };
      }.bind(this);
      checkKeyRequest.onerror = function(event) {
        return checkDefer.reject({task: aTask, error: checkKeyRequest.error.name});
      };
      checkPromises.push(checkPromise);
    }

    let defer = {};
    let promiseWrapper = new Promise(function(resolve, reject) {
      defer.resolve = resolve;
      defer.reject = reject;
    });

    // Once all transactions are done, or any have failed, remove the lock and
    // start processing the tasks from the next lock in the queue.
    Promise.all(checkPromises).then(function() {
      // If all commits were successful, notify observers
      for (let i = 0; i < keys.length; i++) {
        this.sendSettingsChange(keys[i], finalValues[keys[i]].userValue, lock.isServiceLock);
      }
      this.removeLock(data.lockID);
      defer.resolve({task: aTask});
    }.bind(this), function(ret) {
      this.removeLock(data.lockID);
      defer.reject({task: aTask, error: "Set transaction failure"});
    }.bind(this));
    return promiseWrapper;
  },

  // Clear is only expected to be called via tests, and if a lock
  // calls clear, it should be the only thing the lock does. This
  // allows us to not have to deal with the possibility of query
  // integrity checking. Clear should never be called in the wild,
  // even by certified apps, which is why it has its own permission
  // (settings-clear).
  taskClear: function(aTask) {
    if (VERBOSE) debug("Clearing");
    let data = aTask.data;
    let lock = this.lockInfo[data.lockID];

    if (lock._failed) {
      if (DEBUG) debug("Lock failed, all requests now failing.");
      return Promise.reject({task: aTask, error: "Lock failed, all requests now failing."});
    }

    if (!lock.canClear) {
      if (DEBUG) debug("Lock tried to clear after queuing other tasks. Failing.");
      lock._failed = true;
      return Promise.reject({task: aTask, error: "Cannot call clear after queuing other tasks, all requests now failing."});
    }

    if (!SettingsPermissions.hasClearPermission(lock.principal)) {
      if (DEBUG) debug("clear not allowed");
      lock._failed = true;
      return Promise.reject({task: aTask, error: "No permission to clear DB"});
    }

    lock.hasCleared = true;

    let store = lock.getObjectStore(lock.principal);
    if (!store) {
      if (DEBUG) debug("Rejecting Clear task on lock " + aTask.data.lockID);
      return Promise.reject({task: aTask, error: "Cannot get object store"});
    }
    let defer = {};
    let promiseWrapper = new Promise(function(resolve, reject) {
      defer.resolve = resolve;
      defer.reject = reject;
    });

    let clearReq = store.clear();
    clearReq.onsuccess = function() {
      return defer.resolve({task: aTask});
    };
    clearReq.onerror = function() {
      return defer.reject({task: aTask});
    };
    return promiseWrapper;
  },

  ensureConnection : function() {
    if (VERBOSE) debug("Ensuring Connection");
    let defer = {};
    let promiseWrapper = new Promise(function(resolve, reject) {
      defer.resolve = resolve;
      defer.reject = reject;
    });
    this.settingsDB.ensureDB(
      function() { defer.resolve(); },
      function(error) {
        if (DEBUG) debug("Cannot open Settings DB. Trying to open an old version?\n");
        defer.reject(error);
      }
    );
    return promiseWrapper;
  },

  runTasks: function(aLockID) {
    if (VERBOSE) debug("Running tasks for " + aLockID);
    let lock = this.lockInfo[aLockID];
    if (!lock) {
      if (DEBUG) debug("Lock no longer alive, cannot run tasks");
      return;
    }
    let currentTask = lock.tasks.shift();
    let promises = [];
    if (TRACK) {
      if (this.tasksConsumed[aLockID] === undefined) {
        this.tasksConsumed[aLockID] = 0;
        this.tasksGetConsumed[aLockID] = 0;
        this.tasksSetConsumed[aLockID] = 0;
      }
    }
    while (currentTask) {
      if (VERBOSE) debug("Running Operation " + currentTask.operation);
      if (lock.finalizing) {
        // We should really never get to this point, but if we do,
        // fail every task that happens.
        Cu.reportError("Settings lock trying to run more tasks after finalizing. Ignoring tasks, but this is bad. Lock: " + aLockID);
        currentTask.defer.reject("Cannot call new task after finalizing");
      } else {
      let p;
      this.totalProcessed++;
      if (TRACK) {
        this.tasksConsumed[aLockID]++;
      }
      switch (currentTask.operation) {
        case "get":
          this.totalGetProcessed++;
          if (TRACK) {
            this.tasksGetConsumed[aLockID]++;
          }
          p = this.taskGet(currentTask);
          break;
        case "set":
          this.totalSetProcessed++;
          if (TRACK) {
            this.tasksSetConsumed[aLockID]++;
          }
          p = this.taskSet(currentTask);
          break;
        case "clear":
          p = this.taskClear(currentTask);
          break;
        case "finalize":
          p = this.finalizeSets(currentTask);
          break;
        default:
          if (DEBUG) debug("Invalid operation: " + currentTask.operation);
          p.reject("Invalid operation: " + currentTask.operation);
      }
      p.then(function(ret) {
        ret.task.defer.resolve("results" in ret ? ret.results : null);
      }.bind(currentTask), function(ret) {
        ret.task.defer.reject(ret.error);
      });
      promises.push(p);
      }
      currentTask = lock.tasks.shift();
    }
  },

  consumeTasks: function() {
    if (this.settingsLockQueue.length == 0) {
      if (VERBOSE) debug("Nothing to run!");
      return;
    }

    let lockID = this.settingsLockQueue[0];
    if (VERBOSE) debug("Consuming tasks for " + lockID);
    let lock = this.lockInfo[lockID];

    // If a process dies, we should clean up after it via the
    // child-process-shutdown event. But just in case we don't, we want to make
    // sure we never block on consuming.
    if (!lock) {
      if (DEBUG) debug("Lock no longer alive, cannot consume tasks");
      this.queueConsume();
      return;
    }

    if (!lock.consumable || lock.tasks.length === 0) {
      if (VERBOSE) debug("No more tasks to run or not yet consuamble.");
      return;
    }

    lock.consumable = false;
    this.ensureConnection().then(
      function(task) {
        this.runTasks(lockID);
        this.updateSoftLockup(lockID);
      }.bind(this), function(ret) {
        dump("-*- SettingsRequestManager: SETTINGS DATABASE ERROR: Cannot make DB connection!\n");
    });
  },

  observe: function(aSubject, aTopic, aData) {
    if (VERBOSE) debug("observe: " + aTopic);
    switch (aTopic) {
      case kXpcomShutdownObserverTopic:
        this.messages.forEach((function(msgName) {
          ppmm.removeMessageListener(msgName, this);
        }).bind(this));
        Services.obs.removeObserver(this, kXpcomShutdownObserverTopic);
        ppmm = null;
        mrm.unregisterStrongReporter(this);
        break;

      case kInnerWindowDestroyed:
        let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
        this.forceFinalizeChildLocksNonOOP(wId);
        break;

      default:
        if (DEBUG) debug("Wrong observer topic: " + aTopic);
        break;
    }
  },

  collectReports: function(aCallback, aData, aAnonymize) {
    for (let lockId of Object.keys(this.lockInfo)) {
      let lock = this.lockInfo[lockId];
      let length = lock.tasks.length;

      if (length === 0) {
        continue;
      }

      let path = "settings-locks/tasks/lock(id=" + lockId + ")/";

      aCallback.callback("", path + "alive",
                         Ci.nsIMemoryReporter.KIND_OTHER,
                         Ci.nsIMemoryReporter.UNITS_COUNT,
                         length,
                         "Alive tasks for this lock",
                         aData);
    }

    aCallback.callback("",
                       "settings-locks/tasks-total/processed",
                       Ci.nsIMemoryReporter.KIND_OTHER,
                       Ci.nsIMemoryReporter.UNITS_COUNT,
                       this.totalProcessed,
                       "The total number of tasks that were executed.",
                       aData);

    aCallback.callback("",
                       "settings-locks/tasks-total/set",
                       Ci.nsIMemoryReporter.KIND_OTHER,
                       Ci.nsIMemoryReporter.UNITS_COUNT,
                       this.totalSetProcessed,
                       "The total number of set tasks that were executed.",
                       aData);

    aCallback.callback("",
                       "settings-locks/tasks-total/get",
                       Ci.nsIMemoryReporter.KIND_OTHER,
                       Ci.nsIMemoryReporter.UNITS_COUNT,
                       this.totalGetProcessed,
                       "The total number of get tasks that were executed.",
                       aData);

    // if TRACK is not enabled, then, no details are available
    if (!TRACK) {
      return;
    }

    for (let lockId of Object.keys(this.tasksConsumed)) {
      let lock = this.lockInfo[lockId];
      let length = 0;
      if (lock) {
        length = lock.tasks.length;
      }

      let path = "settings-locks/tasks/lock(id=" + lockId + ")/";

      aCallback.callback("", path + "set",
                         Ci.nsIMemoryReporter.KIND_OTHER,
                         Ci.nsIMemoryReporter.UNITS_COUNT,
                         this.tasksSetConsumed[lockId],
                         "Set tasks for this lock.",
                         aData);

      aCallback.callback("", path + "get",
                         Ci.nsIMemoryReporter.KIND_OTHER,
                         Ci.nsIMemoryReporter.UNITS_COUNT,
                         this.tasksGetConsumed[lockId],
                         "Get tasks for this lock.",
                         aData);

      aCallback.callback("", path + "processed",
                         Ci.nsIMemoryReporter.KIND_OTHER,
                         Ci.nsIMemoryReporter.UNITS_COUNT,
                         this.tasksConsumed[lockId],
                         "Number of tasks that were executed.",
                         aData);
    }
  },

  sendSettingsChange: function(aKey, aValue, aIsServiceLock) {
    this.broadcastMessage("Settings:Change:Return:OK",
      { key: aKey, value: aValue });
    var setting = {
      key: aKey,
      value: aValue,
      isInternalChange: aIsServiceLock
    };
    setting.wrappedJSObject = setting;
    Services.obs.notifyObservers(setting, kMozSettingsChangedObserverTopic, "");
  },

  broadcastMessage: function broadcastMessage(aMsgName, aContent) {
    if (VERBOSE) debug("Broadcast");
    this.children.forEach(function(msgMgr) {
      let principal = this.observerPrincipalCache.get(msgMgr);
      if (!principal) {
        if (DEBUG) debug("Cannot find principal for message manager to check permissions");
      }
      else if (SettingsPermissions.hasReadPermission(principal, aContent.key)) {
        try {
          msgMgr.sendAsyncMessage(aMsgName, aContent);
        } catch (e) {
          if (DEBUG) debug("Failed sending message: " + aMsgName);
        }
      }
    }.bind(this));
    if (VERBOSE) debug("Finished Broadcasting");
  },

  addObserver: function(aMsgMgr, aPrincipal) {
    if (VERBOSE) debug("Add observer for " + aPrincipal.origin);
    if (this.children.indexOf(aMsgMgr) == -1) {
      this.children.push(aMsgMgr);
      this.observerPrincipalCache.set(aMsgMgr, aPrincipal);
    }
  },

  removeObserver: function(aMsgMgr) {
    if (VERBOSE) {
      let principal = this.observerPrincipalCache.get(aMsgMgr);
      if (principal) {
        debug("Remove observer for " + principal.origin);
      }
    }
    let index = this.children.indexOf(aMsgMgr);
    if (index != -1) {
      this.children.splice(index, 1);
      this.observerPrincipalCache.delete(aMsgMgr);
    }
    if (VERBOSE) debug("Principal/MessageManager pairs left in observer cache: " + this.observerPrincipalCache.size);
  },

  removeLock: function(aLockID) {
    if (VERBOSE) debug("Removing lock " + aLockID);
    if (this.lockInfo[aLockID]) {
      let transaction = this.lockInfo[aLockID]._transaction;
      if (transaction) {
        try {
          transaction.abort();
        } catch (e) {
          if (e.name == "InvalidStateError") {
            if (VERBOSE) debug("Transaction for " + aLockID + " closed already");
          } else {
            if (DEBUG) debug("Unexpected exception, throwing: " + e);
            throw e;
          }
        }
      }
      delete this.lockInfo[aLockID];
    }
    let index = this.settingsLockQueue.indexOf(aLockID);
    if (index > -1) {
      this.settingsLockQueue.splice(index, 1);
    }
    // If index is 0, the lock we just removed was at the head of
    // the queue, so possibly queue the next lock if it's
    // consumable.
    if (index == 0) {
      this.queueConsume();
    }
  },

  hasLockFinalizeTask: function(lock) {
    // Go in reverse order because finalize should be the last one
    for (let task_index = lock.tasks.length; task_index >= 0; task_index--) {
      if (lock.tasks[task_index]
          && lock.tasks[task_index].operation === "finalize") {
        return true;
      }
    }
    return false;
  },

  enqueueForceFinalize: function(lock) {
    if (!this.hasLockFinalizeTask(lock)) {
      if (VERBOSE) debug("Alive lock has pending tasks: " + lock.lockID);
      this.queueTask("finalize", {lockID: lock.lockID}).then(
        function() {
          if (VERBOSE) debug("Alive lock " + lock.lockID + " succeeded to force-finalize");
        },
        function(error) {
          if (DEBUG) debug("Alive lock " + lock.lockID + " failed to force-finalize due to error: " + error);
        }
      );
      // Finalize is considered a task running situation, but it also needs to
      // queue a task.
      this.startRunning(lock.lockID);
    }
  },

  forceFinalizeChildLocksNonOOP: function(windowId) {
    if (VERBOSE) debug("Forcing finalize on child locks, non OOP");

    for (let lockId of Object.keys(this.lockInfo)) {
      let lock = this.lockInfo[lockId];
      if (lock.windowID === windowId) {
        this.enqueueForceFinalize(lock);
      }
    }
  },

  forceFinalizeChildLocksOOP: function(aMsgMgr) {
    if (VERBOSE) debug("Forcing finalize on child locks, OOP");

    for (let lockId of Object.keys(this.lockInfo)) {
      let lock = this.lockInfo[lockId];
      if (lock._mm === aMsgMgr) {
        this.enqueueForceFinalize(lock);
      }
    }
  },

  updateSoftLockup: function(aLockId) {
    if (VERBOSE) debug("Treating lock " + aLockId + ", so updating soft lockup infos ...");

    this.softLockup = {
      lockId: aLockId,
      lockTs: new Date()
    };
  },

  checkSoftLockup: function() {
    if (VERBOSE) debug("Checking for soft lockup ...");

    if (this.settingsLockQueue.length === 0) {
      if (VERBOSE) debug("Empty settings lock queue, no soft lockup ...");
      return;
    }

    let head = this.settingsLockQueue[0];
    if (head !== this.softLockup.lockId) {
      if (VERBOSE) debug("Non matching head of settings lock queue, no soft lockup ...");
      return;
    }

    let delta = (new Date() - this.softLockup.lockTs) / 1000;
    if (delta < kSoftLockupDelta) {
      if (VERBOSE) debug("Matching head of settings lock queue, but delta (" + delta + ") < 30 secs, no soft lockup ...");
      return;
    }

    let msgBlocked = "Settings queue head blocked at " + head +
                     " for " + delta + " secs, Settings API may be soft lockup. Lock from: " +
                     this.lockInfo[head].lockStack;
    Cu.reportError(msgBlocked);
    if (DEBUG) debug(msgBlocked);
  },

  receiveMessage: function(aMessage) {
    if (VERBOSE) debug("receiveMessage " + aMessage.name + ": " + JSON.stringify(aMessage.data));

    let msg = aMessage.data;
    let mm = aMessage.target;

    function returnMessage(name, data) {
      if (mm) {
        try {
          mm.sendAsyncMessage(name, data);
        } catch (e) {
          if (DEBUG) debug("Return message failed, " + name + ": " + e);
        }
      } else {
        try {
          gSettingsService.receiveMessage({ name: name, data: data });
	} catch (e) {
          if (DEBUG) debug("Direct return message failed, " + name + ": " + e);
	}
      }
    }

    // For all message types that expect a lockID, we check to make
    // sure that we're accessing a lock that's part of our process. If
    // not, consider it a security violation and kill the app. Killing
    // based on creating a colliding lock ID happens as part of
    // CreateLock check below.
    switch (aMessage.name) {
      case "Settings:Get":
      case "Settings:Set":
      case "Settings:Clear":
      case "Settings:Run":
      case "Settings:Finalize":
        this.checkSoftLockup();
        let kill_process = false;
        if (!msg.lockID) {
          Cu.reportError("Process sending request for lock that does not exist. Killing.");
          kill_process = true;
        }
        else if (!this.lockInfo[msg.lockID]) {
          if (DEBUG) debug("Cannot find lock ID " + msg.lockID);
          // This doesn't kill, because we can have things that file
          // finalize, then die, and we may get the observer
          // notification before we get the IPC messages.
          return;
        }
        else if (mm != this.lockInfo[msg.lockID]._mm) {
          Cu.reportError("Process trying to access settings lock from another process. Killing.");
          kill_process = true;
        }
        if (kill_process) {
          // Kill the app by checking for a non-existent permission
          aMessage.target.assertPermission("message-manager-mismatch-kill");
          return;
        }
      default:
      break;
    }

    switch (aMessage.name) {
      case "child-process-shutdown":
        if (VERBOSE) debug("Child process shutdown received.");
        this.forceFinalizeChildLocksOOP(mm);
        this.removeObserver(mm);
        break;
      case "Settings:RegisterForMessages":
        if (!SettingsPermissions.hasSomeReadPermission(aMessage.principal)) {
          Cu.reportError("Settings message " + aMessage.name +
                         " from a content process with no 'settings-api-read' privileges.");
          aMessage.target.assertPermission("message-manager-no-read-kill");
          return;
        }
        this.addObserver(mm, aMessage.principal);
        break;
      case "Settings:UnregisterForMessages":
        this.removeObserver(mm);
        break;
      case "Settings:CreateLock":
        if (VERBOSE) debug("Received CreateLock for " + msg.lockID + " from " + aMessage.principal.origin + " window: " + msg.windowID);
        // If we try to create a lock ID that collides with one
        // already in the system, consider it a security violation and
        // kill.
        if (msg.lockID in this.settingsLockQueue) {
          Cu.reportError("Trying to queue a lock with the same ID as an already queued lock. Killing app.");
          aMessage.target.assertPermission("lock-id-duplicate-kill");
          return;
        }

        if (this.softLockup.lockId === null) {
          this.updateSoftLockup(msg.lockID);
        }

        this.settingsLockQueue.push(msg.lockID);
        this.lockInfo[msg.lockID] = SettingsLockInfo(this.settingsDB,
                                                     mm,
                                                     aMessage.principal,
                                                     msg.lockID,
                                                     msg.isServiceLock,
                                                     msg.windowID,
                                                     msg.lockStack);
        break;
      case "Settings:Get":
        if (VERBOSE) debug("Received getRequest from " + msg.lockID);
        this.queueTask("get", msg).then(function(settings) {
            returnMessage("Settings:Get:OK", {
              lockID: msg.lockID,
              requestID: msg.requestID,
              settings: settings
            });
          }, function(error) {
            if (DEBUG) debug("getRequest FAILED " + msg.name);
            returnMessage("Settings:Get:KO", {
              lockID: msg.lockID,
              requestID: msg.requestID,
              errorMsg: error
            });
        });
        break;
      case "Settings:Set":
        if (VERBOSE) debug("Received Set Request from " + msg.lockID);
        this.queueTask("set", msg).then(function(settings) {
          returnMessage("Settings:Set:OK", {
            lockID: msg.lockID,
            requestID: msg.requestID
          });
        }, function(error) {
          returnMessage("Settings:Set:KO", {
            lockID: msg.lockID,
            requestID: msg.requestID,
            errorMsg: error
          });
        });
        break;
      case "Settings:Clear":
        if (VERBOSE) debug("Received Clear Request from " + msg.lockID);
        this.queueTask("clear", msg).then(function() {
          returnMessage("Settings:Clear:OK", {
            lockID: msg.lockID,
            requestID: msg.requestID
          });
        }, function(error) {
          returnMessage("Settings:Clear:KO", {
            lockID: msg.lockID,
            requestID: msg.requestID,
            errorMsg: error
          });
        });
        break;
      case "Settings:Finalize":
        if (VERBOSE) debug("Received Finalize");
        this.queueTask("finalize", msg).then(function() {
          returnMessage("Settings:Finalize:OK", {
            lockID: msg.lockID
          });
        }, function(error) {
          returnMessage("Settings:Finalize:KO", {
            lockID: msg.lockID,
            errorMsg: error
          });
        });
      // YES THIS IS SUPPOSED TO FALL THROUGH. Finalize is considered a task
      // running situation, but it also needs to queue a task.
      case "Settings:Run":
        if (VERBOSE) debug("Received Run");
        this.startRunning(msg.lockID);
        break;
      default:
        if (DEBUG) debug("Wrong message: " + aMessage.name);
    }
  }
};

// This code should ALWAYS be living only on the parent side.
if (!inParent) {
  debug("SettingsRequestManager should be living on parent side.");
  throw Cr.NS_ERROR_ABORT;
} else {
  this.SettingsRequestManager = SettingsRequestManager;
  SettingsRequestManager.init();
}