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/. */
8 //dump('DEBUG RequestSyncManager: ' + 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');
15 Cu.import('resource://gre/modules/RequestSyncApp.jsm');
16 Cu.import('resource://gre/modules/RequestSyncTask.jsm');
18 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
19 "@mozilla.org/childprocessmessagemanager;1",
22 function RequestSyncManager() {
26 RequestSyncManager.prototype = {
27 __proto__: DOMRequestIpcHelper.prototype,
29 classDescription: 'RequestSyncManager XPCOM Component',
30 classID: Components.ID('{e6f55080-e549-4e30-9d00-15f240fb763c}'),
31 contractID: '@mozilla.org/dom/request-sync-manager;1',
32 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference,
34 Ci.nsIDOMGlobalPropertyInitializer]),
36 _messages: [ "RequestSyncManager:Registrations:Return",
37 "RequestSyncManager:SetPolicy:Return",
38 "RequestSyncManager:RunTask:Return" ],
40 init: function(aWindow) {
43 // DOMRequestIpcHelper.initHelper sets this._window
44 this.initDOMRequestHelper(aWindow, this._messages);
47 sendMessage: function(aMsg, aObj) {
49 return this.createPromise(function(aResolve, aReject) {
51 self.getPromiseResolverId({ resolve: aResolve, reject: aReject });
52 cpmm.sendAsyncMessage(aMsg, aObj, null,
53 self._window.document.nodePrincipal);
57 registrations: function() {
58 debug('registrations');
59 return this.sendMessage("RequestSyncManager:Registrations", {});
62 setPolicy: function(aTask, aOrigin, aManifestURL, aIsInBrowserElement,
63 aState, aOverwrittenMinInterval) {
66 return this.sendMessage("RequestSyncManager:SetPolicy",
69 manifestURL: aManifestURL,
70 isInBrowserElement: aIsInBrowserElement,
72 overwrittenMinInterval: aOverwrittenMinInterval });
75 runTask: function(aTask, aOrigin, aManifestURL, aIsInBrowserElement) {
78 return this.sendMessage("RequestSyncManager:RunTask",
81 manifestURL: aManifestURL,
82 isInBrowserElement: aIsInBrowserElement });
85 registrationsResult: function(aData) {
86 debug("registrationsResult");
88 let results = new this._window.Array();
89 for (let i = 0; i < aData.length; ++i) {
90 if (!("app" in aData[i])) {
91 dump("ERROR - Serialization error in RequestSyncManager.\n");
95 let app = new RequestSyncApp(aData[i].app);
97 this._window.RequestSyncApp._create(this._window, app);
99 let task = new RequestSyncTask(this, this._window, exposedApp, aData[i]);
101 this._window.RequestSyncTask._create(this._window, task);
103 results.push(exposedTask);
108 receiveMessage: function(aMessage) {
109 debug('receiveMessage');
111 let req = this.getPromiseResolver(aMessage.data.requestID);
116 if ('error' in aMessage.data) {
117 req.reject(Cu.cloneInto(aMessage.data.error, this._window));
121 if (aMessage.name == 'RequestSyncManager:Registrations:Return') {
122 req.resolve(this.registrationsResult(aMessage.data.results));
126 if ('results' in aMessage.data) {
127 req.resolve(Cu.cloneInto(aMessage.data.results, this._window));
135 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RequestSyncManager]);