Bumping manifests a=b2g-bump
[gecko.git] / dom / requestsync / RequestSyncScheduler.js
blob774636248939b83b9dcc392ff13c6b762b4c7c6b
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 'use strict';
7 function debug(s) {
8   //dump('DEBUG RequestSyncScheduler: ' + s + '\n');
11 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
13 Cu.import('resource://gre/modules/DOMRequestHelper.jsm');
14 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
16 XPCOMUtils.defineLazyServiceGetter(this, 'cpmm',
17                                    '@mozilla.org/childprocessmessagemanager;1',
18                                    'nsIMessageSender');
20 function RequestSyncScheduler() {
21   debug('created');
24 RequestSyncScheduler.prototype = {
25   __proto__: DOMRequestIpcHelper.prototype,
27   classDescription: 'RequestSyncScheduler XPCOM Component',
28   classID: Components.ID('{8ee5ab74-15c4-478f-9d32-67627b9f0f1a}'),
29   contractID: '@mozilla.org/dom/request-sync-scheduler;1',
30   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference,
31                                          Ci.nsIObserver,
32                                          Ci.nsIDOMGlobalPropertyInitializer]),
34   _messages: [ 'RequestSync:Register:Return',
35                'RequestSync:Unregister:Return',
36                'RequestSync:Registrations:Return',
37                'RequestSync:Registration:Return' ],
39   init: function(aWindow) {
40     debug('init');
42     // DOMRequestIpcHelper.initHelper sets this._window
43     this.initDOMRequestHelper(aWindow, this._messages);
44   },
46   register: function(aTask, aParams) {
47     debug('register');
48     return this.sendMessage('RequestSync:Register',
49                             { task: aTask, params: aParams });
50   },
52   unregister: function(aTask) {
53     debug('unregister');
54     return this.sendMessage('RequestSync:Unregister',
55                             { task: aTask });
56   },
58   registrations: function() {
59     debug('registrations');
60     return this.sendMessage('RequestSync:Registrations', {});
61   },
63   registration: function(aTask) {
64     debug('registration');
65     return this.sendMessage('RequestSync:Registration',
66                             { task: aTask });
67   },
69   sendMessage: function(aMsg, aObj) {
70     let self = this;
71     return this.createPromise(function(aResolve, aReject) {
72       aObj.requestID =
73         self.getPromiseResolverId({ resolve: aResolve, reject: aReject });
74       cpmm.sendAsyncMessage(aMsg, aObj, null,
75                             self._window.document.nodePrincipal);
76     });
77   },
79   receiveMessage: function(aMessage) {
80     debug('receiveMessage');
82     let req = this.getPromiseResolver(aMessage.data.requestID);
83     if (!req) {
84       return;
85     }
87     if ('error' in aMessage.data) {
88       req.reject(Cu.cloneInto(aMessage.data.error, this._window));
89       return;
90     }
92     if ('results' in aMessage.data) {
93       req.resolve(Cu.cloneInto(aMessage.data.results, this._window));
94       return;
95     }
97     req.resolve();
98   }
101 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RequestSyncScheduler]);