summaryrefslogtreecommitdiff
path: root/dom/system/SystemUpdateManager.js
blob: a944767ece8df97ecbc891ce4bf8e77707ad1fe2 (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
/* 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 {classes: Cc, interfaces: Ci, utils: Cu} = Components;

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

var debug = Services.prefs.getBoolPref("dom.system_update.debug")
              ? (aMsg) => dump("-*- SystemUpdateManager.js : " + aMsg + "\n")
              : (aMsg) => {};

const SYSTEMUPDATEPROVIDER_CID = Components.ID("{11fbea3d-fd94-459a-b8fb-557fe19e473a}");
const SYSTEMUPDATEMANAGER_CID = Components.ID("{e8530001-ba5b-46ab-a306-7fbeb692d0fe}");
const SYSTEMUPDATEMANAGER_CONTRACTID = "@mozilla.org/system-update-manager;1";

XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
                                   "@mozilla.org/childprocessmessagemanager;1",
                                   "nsISyncMessageSender");

function SystemUpdateProvider(win, provider) {
  this.initDOMRequestHelper(win, [
    {name: "SystemUpdate:OnUpdateAvailable", weakRef: true},
    {name: "SystemUpdate:OnProgress", weakRef: true},
    {name: "SystemUpdate:OnUpdateReady", weakRef: true},
    {name: "SystemUpdate:OnError", weakRef: true},
  ]);
  this._provider = Cu.cloneInto(provider, win);
}

SystemUpdateProvider.prototype = {
  __proto__: DOMRequestIpcHelper.prototype,

  classID: SYSTEMUPDATEPROVIDER_CID,
  QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference,
                                         Ci.nsIObserver]),

  receiveMessage: function(aMsg) {
    if (!aMsg || !aMsg.json) {
      return;
    }

    let json = aMsg.json;

    if (json.uuid !== this._provider.uuid) {
      return;
    }

    debug("receive msg: " + aMsg.name);
    switch (aMsg.name) {
      case "SystemUpdate:OnUpdateAvailable": {
        let detail = {
          detail: {
            packageInfo: json.packageInfo
          }
        };
        let event = new this._window.CustomEvent("updateavailable",
                                                  Cu.cloneInto(detail, this._window));
        this.__DOM_IMPL__.dispatchEvent(event);
        break;
      }
      case "SystemUpdate:OnProgress": {
        let event = new this._window.ProgressEvent("progress", {lengthComputable: true,
                                                                loaded: json.loaded,
                                                                total: json.total});
        this.__DOM_IMPL__.dispatchEvent(event);
        break;
      }
      case "SystemUpdate:OnUpdateReady": {
        let event = new this._window.Event("updateready");
        this.__DOM_IMPL__.dispatchEvent(event);
        break;
      }
      case "SystemUpdate:OnError": {
        let event = new this._window.ErrorEvent("error", {message: json.message});
        this.__DOM_IMPL__.dispatchEvent(event);
        break;
      }
    }
  },

  destroy: function() {
    this.destroyDOMRequestHelper();
  },

  get name() {
    return this._provider.name;
  },

  get uuid() {
    return this._provider.uuid;
  },

  get onupdateavailable() {
    return this.__DOM_IMPL__.getEventHandler("onupdateavailable");
  },
  set onupdateavailable(aHandler) {
    this.__DOM_IMPL__.setEventHandler("onupdateavailable", aHandler);
  },
  get onprogress() {
    return this.__DOM_IMPL__.getEventHandler("onprogress");
  },
  set onprogress(aHandler) {
    this.__DOM_IMPL__.setEventHandler("onprogress", aHandler);
  },
  get onupdateready() {
    return this.__DOM_IMPL__.getEventHandler("onupdateready");
  },
  set onupdateready(aHandler) {
    this.__DOM_IMPL__.setEventHandler("onupdateready", aHandler);
  },
  get onerror() {
    return this.__DOM_IMPL__.getEventHandler("onerror");
  },
  set onerror(aHandler) {
    this.__DOM_IMPL__.setEventHandler("onerror", aHandler);
  },

  checkForUpdate: function() {
    let self = this;
    cpmm.sendAsyncMessage("SystemUpdate:CheckForUpdate", {
      uuid: self._provider.uuid
    });
  },
  startDownload: function() {
    let self = this;
    cpmm.sendAsyncMessage("SystemUpdate:StartDownload", {
      uuid: self._provider.uuid
    });
  },
  stopDownload: function() {
    let self = this;
    cpmm.sendAsyncMessage("SystemUpdate:StopDownload", {
      uuid: self._provider.uuid
    });
  },
  applyUpdate: function() {
    let self = this;
    cpmm.sendAsyncMessage("SystemUpdate:ApplyUpdate", {
      uuid: self._provider.uuid
    });
  },
  setParameter: function(aName, aValue) {
    let self = this;
    return cpmm.sendSyncMessage("SystemUpdate:SetParameter", {
      uuid: self._provider.uuid,
      name: aName,
      value: aValue
    })[0];
  },
  getParameter: function(aName) {
    let self = this;
    return cpmm.sendSyncMessage("SystemUpdate:GetParameter", {
      uuid: self._provider.uuid,
      name: aName
    })[0];
  },
};

function SystemUpdateManager() {}

SystemUpdateManager.prototype = {
  __proto__: DOMRequestIpcHelper.prototype,

  classID: SYSTEMUPDATEMANAGER_CID,
  QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference,
                                         Ci.nsIObserver,
                                         Ci.nsIDOMGlobalPropertyInitializer]),

  receiveMessage: function(aMsg) {
    if (!aMsg || !aMsg.json) {
      return;
    }

    let json = aMsg.json;
    let resolver = this.takePromiseResolver(json.requestId);

    if (!resolver) {
      return;
    }

    debug("receive msg: " + aMsg.name);
    switch (aMsg.name) {
      case "SystemUpdate:GetProviders:Result:OK": {
        resolver.resolve(Cu.cloneInto(json.providers, this._window));
        break;
      }
      case "SystemUpdate:SetActiveProvider:Result:OK":
      case "SystemUpdate:GetActiveProvider:Result:OK": {
        let updateProvider = new SystemUpdateProvider(this._window, json.provider);
        resolver.resolve(this._window.SystemUpdateProvider._create(this._window,
                                                                   updateProvider));
        break;
      }
      case "SystemUpdate:GetProviders:Result:Error":
      case "SystemUpdate:GetActiveProvider:Result:Error":
      case "SystemUpdate:SetActiveProvider:Result:Error": {
        resolver.reject(json.error);
        break;
      }
    }
  },

  init: function(aWindow) {
    this.initDOMRequestHelper(aWindow, [
      {name: "SystemUpdate:GetProviders:Result:OK", weakRef: true},
      {name: "SystemUpdate:GetProviders:Result:Error", weakRef: true},
      {name: "SystemUpdate:GetActiveProvider:Result:OK", weakRef: true},
      {name: "SystemUpdate:GetActiveProvider:Result:Error", weakRef: true},
      {name: "SystemUpdate:SetActiveProvider:Result:OK", weakRef: true},
      {name: "SystemUpdate:SetActiveProvider:Result:Error", weakRef: true},
    ]);
  },

  uninit: function() {
    let self = this;

    this.forEachPromiseResolver(function(aKey) {
      self.takePromiseResolver(aKey).reject("SystemUpdateManager got destroyed");
    });
  },

  getProviders: function() {
    return this.createPromiseWithId(function(aResolverId) {
      cpmm.sendAsyncMessage("SystemUpdate:GetProviders", {
        requestId: aResolverId,
      });
    });
  },

  getActiveProvider: function() {
    return this.createPromiseWithId(function(aResolverId) {
      cpmm.sendAsyncMessage("SystemUpdate:GetActiveProvider", {
        requestId: aResolverId,
      });
    });
  },

  setActiveProvider: function(aUuid) {
    return this.createPromiseWithId(function(aResolverId) {
      cpmm.sendAsyncMessage("SystemUpdate:SetActiveProvider", {
        requestId: aResolverId,
        uuid: aUuid
      });
    });
  }
};

this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SystemUpdateManager]);