dom/apps/src/Webapps.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Cc = Components.classes;
michael@0 6 const Ci = Components.interfaces;
michael@0 7 const Cu = Components.utils;
michael@0 8 const Cr = Components.results;
michael@0 9
michael@0 10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 11 Cu.import("resource://gre/modules/Services.jsm");
michael@0 12 Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
michael@0 13 Cu.import("resource://gre/modules/AppsUtils.jsm");
michael@0 14 Cu.import("resource://gre/modules/BrowserElementPromptService.jsm");
michael@0 15
michael@0 16 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
michael@0 17 "@mozilla.org/childprocessmessagemanager;1",
michael@0 18 "nsIMessageSender");
michael@0 19
michael@0 20 function convertAppsArray(aApps, aWindow) {
michael@0 21 let apps = new aWindow.Array();
michael@0 22 for (let i = 0; i < aApps.length; i++) {
michael@0 23 let app = aApps[i];
michael@0 24 apps.push(createApplicationObject(aWindow, app));
michael@0 25 }
michael@0 26
michael@0 27 return apps;
michael@0 28 }
michael@0 29
michael@0 30 function WebappsRegistry() {
michael@0 31 }
michael@0 32
michael@0 33 WebappsRegistry.prototype = {
michael@0 34 __proto__: DOMRequestIpcHelper.prototype,
michael@0 35
michael@0 36 receiveMessage: function(aMessage) {
michael@0 37 let msg = aMessage.json;
michael@0 38 if (msg.oid != this._id)
michael@0 39 return
michael@0 40 let req = this.getRequest(msg.requestID);
michael@0 41 if (!req)
michael@0 42 return;
michael@0 43 let app = msg.app;
michael@0 44 switch (aMessage.name) {
michael@0 45 case "Webapps:Install:Return:OK":
michael@0 46 this.removeMessageListeners("Webapps:Install:Return:KO");
michael@0 47 Services.DOMRequest.fireSuccess(req, createApplicationObject(this._window, app));
michael@0 48 cpmm.sendAsyncMessage("Webapps:Install:Return:Ack",
michael@0 49 { manifestURL : app.manifestURL });
michael@0 50 break;
michael@0 51 case "Webapps:Install:Return:KO":
michael@0 52 this.removeMessageListeners(aMessage.name);
michael@0 53 Services.DOMRequest.fireError(req, msg.error || "DENIED");
michael@0 54 break;
michael@0 55 case "Webapps:GetSelf:Return:OK":
michael@0 56 this.removeMessageListeners(aMessage.name);
michael@0 57 if (msg.apps.length) {
michael@0 58 app = msg.apps[0];
michael@0 59 Services.DOMRequest.fireSuccess(req, createApplicationObject(this._window, app));
michael@0 60 } else {
michael@0 61 Services.DOMRequest.fireSuccess(req, null);
michael@0 62 }
michael@0 63 break;
michael@0 64 case "Webapps:CheckInstalled:Return:OK":
michael@0 65 this.removeMessageListeners(aMessage.name);
michael@0 66 Services.DOMRequest.fireSuccess(req, msg.app);
michael@0 67 break;
michael@0 68 case "Webapps:GetInstalled:Return:OK":
michael@0 69 this.removeMessageListeners(aMessage.name);
michael@0 70 Services.DOMRequest.fireSuccess(req, convertAppsArray(msg.apps, this._window));
michael@0 71 break;
michael@0 72 }
michael@0 73 this.removeRequest(msg.requestID);
michael@0 74 },
michael@0 75
michael@0 76 _getOrigin: function(aURL) {
michael@0 77 let uri = Services.io.newURI(aURL, null, null);
michael@0 78 return uri.prePath;
michael@0 79 },
michael@0 80
michael@0 81 // Checks that the URL scheme is appropriate (http or https) and
michael@0 82 // asynchronously fire an error on the DOM Request if it isn't.
michael@0 83 _validateURL: function(aURL, aRequest) {
michael@0 84 let uri;
michael@0 85 let res;
michael@0 86
michael@0 87 try {
michael@0 88 uri = Services.io.newURI(aURL, null, null);
michael@0 89 if (uri.schemeIs("http") || uri.schemeIs("https")) {
michael@0 90 res = uri.spec;
michael@0 91 }
michael@0 92 } catch(e) {
michael@0 93 Services.DOMRequest.fireErrorAsync(aRequest, "INVALID_URL");
michael@0 94 return false;
michael@0 95 }
michael@0 96
michael@0 97 // The scheme is incorrect, fire DOMRequest error.
michael@0 98 if (!res) {
michael@0 99 Services.DOMRequest.fireErrorAsync(aRequest, "INVALID_URL");
michael@0 100 return false;
michael@0 101 }
michael@0 102
michael@0 103 return uri.spec;
michael@0 104 },
michael@0 105
michael@0 106 // Checks that we run as a foreground page, and fire an error on the
michael@0 107 // DOM Request if we aren't.
michael@0 108 _ensureForeground: function(aRequest) {
michael@0 109 let docShell = this._window.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 110 .getInterface(Ci.nsIWebNavigation)
michael@0 111 .QueryInterface(Ci.nsIDocShell);
michael@0 112 if (docShell.isActive) {
michael@0 113 return true;
michael@0 114 }
michael@0 115
michael@0 116 Services.DOMRequest.fireErrorAsync(aRequest, "BACKGROUND_APP");
michael@0 117 return false;
michael@0 118 },
michael@0 119
michael@0 120 _prepareInstall: function(aURL, aRequest, aParams, isPackage) {
michael@0 121 let installURL = this._window.location.href;
michael@0 122 let requestID = this.getRequestId(aRequest);
michael@0 123 let receipts = (aParams && aParams.receipts &&
michael@0 124 Array.isArray(aParams.receipts)) ? aParams.receipts
michael@0 125 : [];
michael@0 126 let categories = (aParams && aParams.categories &&
michael@0 127 Array.isArray(aParams.categories)) ? aParams.categories
michael@0 128 : [];
michael@0 129
michael@0 130 let principal = this._window.document.nodePrincipal;
michael@0 131
michael@0 132 return { app: {
michael@0 133 installOrigin: this._getOrigin(installURL),
michael@0 134 origin: this._getOrigin(aURL),
michael@0 135 manifestURL: aURL,
michael@0 136 receipts: receipts,
michael@0 137 categories: categories
michael@0 138 },
michael@0 139
michael@0 140 from: installURL,
michael@0 141 oid: this._id,
michael@0 142 requestID: requestID,
michael@0 143 appId: principal.appId,
michael@0 144 isBrowser: principal.isInBrowserElement,
michael@0 145 isPackage: isPackage
michael@0 146 };
michael@0 147 },
michael@0 148
michael@0 149 // mozIDOMApplicationRegistry implementation
michael@0 150
michael@0 151 install: function(aURL, aParams) {
michael@0 152 let request = this.createRequest();
michael@0 153
michael@0 154 let uri = this._validateURL(aURL, request);
michael@0 155
michael@0 156 if (uri && this._ensureForeground(request)) {
michael@0 157 this.addMessageListeners("Webapps:Install:Return:KO");
michael@0 158 cpmm.sendAsyncMessage("Webapps:Install",
michael@0 159 this._prepareInstall(uri, request, aParams, false));
michael@0 160 }
michael@0 161
michael@0 162 return request;
michael@0 163 },
michael@0 164
michael@0 165 getSelf: function() {
michael@0 166 let request = this.createRequest();
michael@0 167 this.addMessageListeners("Webapps:GetSelf:Return:OK");
michael@0 168 cpmm.sendAsyncMessage("Webapps:GetSelf", { origin: this._getOrigin(this._window.location.href),
michael@0 169 appId: this._window.document.nodePrincipal.appId,
michael@0 170 oid: this._id,
michael@0 171 requestID: this.getRequestId(request) });
michael@0 172 return request;
michael@0 173 },
michael@0 174
michael@0 175 checkInstalled: function(aManifestURL) {
michael@0 176 let manifestURL = Services.io.newURI(aManifestURL, null, this._window.document.baseURIObject);
michael@0 177 this._window.document.nodePrincipal.checkMayLoad(manifestURL, true, false);
michael@0 178
michael@0 179 let request = this.createRequest();
michael@0 180
michael@0 181 this.addMessageListeners("Webapps:CheckInstalled:Return:OK");
michael@0 182 cpmm.sendAsyncMessage("Webapps:CheckInstalled", { origin: this._getOrigin(this._window.location.href),
michael@0 183 manifestURL: manifestURL.spec,
michael@0 184 oid: this._id,
michael@0 185 requestID: this.getRequestId(request) });
michael@0 186 return request;
michael@0 187 },
michael@0 188
michael@0 189 getInstalled: function() {
michael@0 190 let request = this.createRequest();
michael@0 191 this.addMessageListeners("Webapps:GetInstalled:Return:OK");
michael@0 192 cpmm.sendAsyncMessage("Webapps:GetInstalled", { origin: this._getOrigin(this._window.location.href),
michael@0 193 oid: this._id,
michael@0 194 requestID: this.getRequestId(request) });
michael@0 195 return request;
michael@0 196 },
michael@0 197
michael@0 198 get mgmt() {
michael@0 199 if (!this.hasMgmtPrivilege) {
michael@0 200 return null;
michael@0 201 }
michael@0 202
michael@0 203 if (!this._mgmt)
michael@0 204 this._mgmt = new WebappsApplicationMgmt(this._window);
michael@0 205 return this._mgmt;
michael@0 206 },
michael@0 207
michael@0 208 uninit: function() {
michael@0 209 this._mgmt = null;
michael@0 210 cpmm.sendAsyncMessage("Webapps:UnregisterForMessages",
michael@0 211 ["Webapps:Install:Return:OK"]);
michael@0 212 },
michael@0 213
michael@0 214 installPackage: function(aURL, aParams) {
michael@0 215 let request = this.createRequest();
michael@0 216
michael@0 217 let uri = this._validateURL(aURL, request);
michael@0 218
michael@0 219 if (uri && this._ensureForeground(request)) {
michael@0 220 this.addMessageListeners("Webapps:Install:Return:KO");
michael@0 221 cpmm.sendAsyncMessage("Webapps:InstallPackage",
michael@0 222 this._prepareInstall(uri, request, aParams, true));
michael@0 223 }
michael@0 224
michael@0 225 return request;
michael@0 226 },
michael@0 227
michael@0 228 // nsIDOMGlobalPropertyInitializer implementation
michael@0 229 init: function(aWindow) {
michael@0 230 this.initDOMRequestHelper(aWindow, "Webapps:Install:Return:OK");
michael@0 231
michael@0 232 let util = this._window.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 233 .getInterface(Ci.nsIDOMWindowUtils);
michael@0 234 this._id = util.outerWindowID;
michael@0 235 cpmm.sendAsyncMessage("Webapps:RegisterForMessages",
michael@0 236 { messages: ["Webapps:Install:Return:OK"]});
michael@0 237
michael@0 238 let principal = aWindow.document.nodePrincipal;
michael@0 239 let perm = Services.perms
michael@0 240 .testExactPermissionFromPrincipal(principal, "webapps-manage");
michael@0 241
michael@0 242 // Only pages with the webapps-manage permission set can get access to
michael@0 243 // the mgmt object.
michael@0 244 this.hasMgmtPrivilege = perm == Ci.nsIPermissionManager.ALLOW_ACTION;
michael@0 245 },
michael@0 246
michael@0 247 classID: Components.ID("{fff440b3-fae2-45c1-bf03-3b5a2e432270}"),
michael@0 248
michael@0 249 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference,
michael@0 250 Ci.nsIObserver,
michael@0 251 Ci.mozIDOMApplicationRegistry,
michael@0 252 Ci.mozIDOMApplicationRegistry2,
michael@0 253 Ci.nsIDOMGlobalPropertyInitializer]),
michael@0 254
michael@0 255 classInfo: XPCOMUtils.generateCI({classID: Components.ID("{fff440b3-fae2-45c1-bf03-3b5a2e432270}"),
michael@0 256 contractID: "@mozilla.org/webapps;1",
michael@0 257 interfaces: [Ci.mozIDOMApplicationRegistry,
michael@0 258 Ci.mozIDOMApplicationRegistry2],
michael@0 259 flags: Ci.nsIClassInfo.DOM_OBJECT,
michael@0 260 classDescription: "Webapps Registry"})
michael@0 261 }
michael@0 262
michael@0 263 /**
michael@0 264 * mozIDOMApplication object
michael@0 265 */
michael@0 266
michael@0 267 // A simple cache for the wrapped manifests.
michael@0 268 let manifestCache = {
michael@0 269 _cache: { },
michael@0 270
michael@0 271 // Gets an entry from the cache, and populates the cache if needed.
michael@0 272 get: function mcache_get(aManifestURL, aManifest, aWindow, aInnerWindowID) {
michael@0 273 if (!(aManifestURL in this._cache)) {
michael@0 274 this._cache[aManifestURL] = { };
michael@0 275 }
michael@0 276
michael@0 277 let winObjs = this._cache[aManifestURL];
michael@0 278 if (!(aInnerWindowID in winObjs)) {
michael@0 279 winObjs[aInnerWindowID] = Cu.cloneInto(aManifest, aWindow);
michael@0 280 }
michael@0 281
michael@0 282 return winObjs[aInnerWindowID];
michael@0 283 },
michael@0 284
michael@0 285 // Invalidates an entry in the cache.
michael@0 286 evict: function mcache_evict(aManifestURL, aInnerWindowID) {
michael@0 287 if (aManifestURL in this._cache) {
michael@0 288 let winObjs = this._cache[aManifestURL];
michael@0 289 if (aInnerWindowID in winObjs) {
michael@0 290 delete winObjs[aInnerWindowID];
michael@0 291 }
michael@0 292
michael@0 293 if (Object.keys(winObjs).length == 0) {
michael@0 294 delete this._cache[aManifestURL];
michael@0 295 }
michael@0 296 }
michael@0 297 },
michael@0 298
michael@0 299 observe: function(aSubject, aTopic, aData) {
michael@0 300 // Clear the cache on memory pressure.
michael@0 301 this._cache = { };
michael@0 302 },
michael@0 303
michael@0 304 init: function() {
michael@0 305 Services.obs.addObserver(this, "memory-pressure", false);
michael@0 306 }
michael@0 307 };
michael@0 308
michael@0 309 function createApplicationObject(aWindow, aApp) {
michael@0 310 let app = Cc["@mozilla.org/webapps/application;1"].createInstance(Ci.mozIDOMApplication);
michael@0 311 app.wrappedJSObject.init(aWindow, aApp);
michael@0 312 return app;
michael@0 313 }
michael@0 314
michael@0 315 function WebappsApplication() {
michael@0 316 this.wrappedJSObject = this;
michael@0 317 }
michael@0 318
michael@0 319 WebappsApplication.prototype = {
michael@0 320 __proto__: DOMRequestIpcHelper.prototype,
michael@0 321
michael@0 322 init: function(aWindow, aApp) {
michael@0 323 this._window = aWindow;
michael@0 324 let principal = this._window.document.nodePrincipal;
michael@0 325 this._appStatus = principal.appStatus;
michael@0 326 this.origin = aApp.origin;
michael@0 327 this._manifest = aApp.manifest;
michael@0 328 this._updateManifest = aApp.updateManifest;
michael@0 329 this.manifestURL = aApp.manifestURL;
michael@0 330 this.receipts = aApp.receipts;
michael@0 331 this.installOrigin = aApp.installOrigin;
michael@0 332 this.installTime = aApp.installTime;
michael@0 333 this.installState = aApp.installState || "installed";
michael@0 334 this.removable = aApp.removable;
michael@0 335 this.lastUpdateCheck = aApp.lastUpdateCheck ? aApp.lastUpdateCheck
michael@0 336 : Date.now();
michael@0 337 this.updateTime = aApp.updateTime ? aApp.updateTime
michael@0 338 : aApp.installTime;
michael@0 339 this.progress = NaN;
michael@0 340 this.downloadAvailable = aApp.downloadAvailable;
michael@0 341 this.downloading = aApp.downloading;
michael@0 342 this.readyToApplyDownload = aApp.readyToApplyDownload;
michael@0 343 this.downloadSize = aApp.downloadSize || 0;
michael@0 344
michael@0 345 this._onprogress = null;
michael@0 346 this._ondownloadsuccess = null;
michael@0 347 this._ondownloaderror = null;
michael@0 348 this._ondownloadavailable = null;
michael@0 349 this._ondownloadapplied = null;
michael@0 350
michael@0 351 this._downloadError = null;
michael@0 352
michael@0 353 this.initDOMRequestHelper(aWindow, [
michael@0 354 { name: "Webapps:CheckForUpdate:Return:KO", weakRef: true },
michael@0 355 { name: "Webapps:Connect:Return:OK", weakRef: true },
michael@0 356 { name: "Webapps:Connect:Return:KO", weakRef: true },
michael@0 357 { name: "Webapps:FireEvent", weakRef: true },
michael@0 358 { name: "Webapps:GetConnections:Return:OK", weakRef: true },
michael@0 359 { name: "Webapps:UpdateState", weakRef: true }
michael@0 360 ]);
michael@0 361
michael@0 362 cpmm.sendAsyncMessage("Webapps:RegisterForMessages", {
michael@0 363 messages: ["Webapps:FireEvent",
michael@0 364 "Webapps:UpdateState"],
michael@0 365 app: {
michael@0 366 id: this.id,
michael@0 367 manifestURL: this.manifestURL,
michael@0 368 installState: this.installState,
michael@0 369 downloading: this.downloading
michael@0 370 }
michael@0 371 });
michael@0 372 },
michael@0 373
michael@0 374 get manifest() {
michael@0 375 return manifestCache.get(this.manifestURL,
michael@0 376 this._manifest,
michael@0 377 this._window,
michael@0 378 this.innerWindowID);
michael@0 379 },
michael@0 380
michael@0 381 get updateManifest() {
michael@0 382 return this.updateManifest =
michael@0 383 this._updateManifest ? Cu.cloneInto(this._updateManifest, this._window)
michael@0 384 : null;
michael@0 385 },
michael@0 386
michael@0 387 set onprogress(aCallback) {
michael@0 388 this._onprogress = aCallback;
michael@0 389 },
michael@0 390
michael@0 391 get onprogress() {
michael@0 392 return this._onprogress;
michael@0 393 },
michael@0 394
michael@0 395 set ondownloadsuccess(aCallback) {
michael@0 396 this._ondownloadsuccess = aCallback;
michael@0 397 },
michael@0 398
michael@0 399 get ondownloadsuccess() {
michael@0 400 return this._ondownloadsuccess;
michael@0 401 },
michael@0 402
michael@0 403 set ondownloaderror(aCallback) {
michael@0 404 this._ondownloaderror = aCallback;
michael@0 405 },
michael@0 406
michael@0 407 get ondownloaderror() {
michael@0 408 return this._ondownloaderror;
michael@0 409 },
michael@0 410
michael@0 411 set ondownloadavailable(aCallback) {
michael@0 412 this._ondownloadavailable = aCallback;
michael@0 413 },
michael@0 414
michael@0 415 get ondownloadavailable() {
michael@0 416 return this._ondownloadavailable;
michael@0 417 },
michael@0 418
michael@0 419 set ondownloadapplied(aCallback) {
michael@0 420 this._ondownloadapplied = aCallback;
michael@0 421 },
michael@0 422
michael@0 423 get ondownloadapplied() {
michael@0 424 return this._ondownloadapplied;
michael@0 425 },
michael@0 426
michael@0 427 get downloadError() {
michael@0 428 return new this._window.DOMError(this._downloadError || '');
michael@0 429 },
michael@0 430
michael@0 431 download: function() {
michael@0 432 cpmm.sendAsyncMessage("Webapps:Download",
michael@0 433 { manifestURL: this.manifestURL });
michael@0 434 },
michael@0 435
michael@0 436 cancelDownload: function() {
michael@0 437 cpmm.sendAsyncMessage("Webapps:CancelDownload",
michael@0 438 { manifestURL: this.manifestURL });
michael@0 439 },
michael@0 440
michael@0 441 checkForUpdate: function() {
michael@0 442 let request = this.createRequest();
michael@0 443
michael@0 444 cpmm.sendAsyncMessage("Webapps:CheckForUpdate",
michael@0 445 { manifestURL: this.manifestURL,
michael@0 446 oid: this._id,
michael@0 447 requestID: this.getRequestId(request) });
michael@0 448 return request;
michael@0 449 },
michael@0 450
michael@0 451 launch: function(aStartPoint) {
michael@0 452 let request = this.createRequest();
michael@0 453 this.addMessageListeners(["Webapps:Launch:Return:OK",
michael@0 454 "Webapps:Launch:Return:KO"]);
michael@0 455 cpmm.sendAsyncMessage("Webapps:Launch", { origin: this.origin,
michael@0 456 manifestURL: this.manifestURL,
michael@0 457 startPoint: aStartPoint || "",
michael@0 458 oid: this._id,
michael@0 459 timestamp: Date.now(),
michael@0 460 requestID: this.getRequestId(request) });
michael@0 461 return request;
michael@0 462 },
michael@0 463
michael@0 464 clearBrowserData: function() {
michael@0 465 let request = this.createRequest();
michael@0 466 let browserChild =
michael@0 467 BrowserElementPromptService.getBrowserElementChildForWindow(this._window);
michael@0 468 if (browserChild) {
michael@0 469 this.addMessageListeners("Webapps:ClearBrowserData:Return");
michael@0 470 browserChild.messageManager.sendAsyncMessage(
michael@0 471 "Webapps:ClearBrowserData",
michael@0 472 { manifestURL: this.manifestURL,
michael@0 473 oid: this._id,
michael@0 474 requestID: this.getRequestId(request) }
michael@0 475 );
michael@0 476 } else {
michael@0 477 Services.DOMRequest.fireErrorAsync(request, "NO_CLEARABLE_BROWSER");
michael@0 478 }
michael@0 479 return request;
michael@0 480 },
michael@0 481
michael@0 482 connect: function(aKeyword, aRules) {
michael@0 483 return this.createPromise(function (aResolve, aReject) {
michael@0 484 cpmm.sendAsyncMessage("Webapps:Connect",
michael@0 485 { keyword: aKeyword,
michael@0 486 rules: aRules,
michael@0 487 manifestURL: this.manifestURL,
michael@0 488 outerWindowID: this._id,
michael@0 489 requestID: this.getPromiseResolverId({
michael@0 490 resolve: aResolve,
michael@0 491 reject: aReject
michael@0 492 })});
michael@0 493 }.bind(this));
michael@0 494 },
michael@0 495
michael@0 496 getConnections: function() {
michael@0 497 return this.createPromise(function (aResolve, aReject) {
michael@0 498 cpmm.sendAsyncMessage("Webapps:GetConnections",
michael@0 499 { manifestURL: this.manifestURL,
michael@0 500 outerWindowID: this._id,
michael@0 501 requestID: this.getPromiseResolverId({
michael@0 502 resolve: aResolve,
michael@0 503 reject: aReject
michael@0 504 })});
michael@0 505 }.bind(this));
michael@0 506 },
michael@0 507
michael@0 508 addReceipt: function(receipt) {
michael@0 509 let request = this.createRequest();
michael@0 510
michael@0 511 this.addMessageListeners(["Webapps:AddReceipt:Return:OK",
michael@0 512 "Webapps:AddReceipt:Return:KO"]);
michael@0 513
michael@0 514 cpmm.sendAsyncMessage("Webapps:AddReceipt", { manifestURL: this.manifestURL,
michael@0 515 receipt: receipt,
michael@0 516 oid: this._id,
michael@0 517 requestID: this.getRequestId(request) });
michael@0 518
michael@0 519 return request;
michael@0 520 },
michael@0 521
michael@0 522 removeReceipt: function(receipt) {
michael@0 523 let request = this.createRequest();
michael@0 524
michael@0 525 this.addMessageListeners(["Webapps:RemoveReceipt:Return:OK",
michael@0 526 "Webapps:RemoveReceipt:Return:KO"]);
michael@0 527
michael@0 528 cpmm.sendAsyncMessage("Webapps:RemoveReceipt", { manifestURL: this.manifestURL,
michael@0 529 receipt: receipt,
michael@0 530 oid: this._id,
michael@0 531 requestID: this.getRequestId(request) });
michael@0 532
michael@0 533 return request;
michael@0 534 },
michael@0 535
michael@0 536 replaceReceipt: function(oldReceipt, newReceipt) {
michael@0 537 let request = this.createRequest();
michael@0 538
michael@0 539 this.addMessageListeners(["Webapps:ReplaceReceipt:Return:OK",
michael@0 540 "Webapps:ReplaceReceipt:Return:KO"]);
michael@0 541
michael@0 542 cpmm.sendAsyncMessage("Webapps:ReplaceReceipt", { manifestURL: this.manifestURL,
michael@0 543 newReceipt: newReceipt,
michael@0 544 oldReceipt: oldReceipt,
michael@0 545 oid: this._id,
michael@0 546 requestID: this.getRequestId(request) });
michael@0 547
michael@0 548 return request;
michael@0 549 },
michael@0 550
michael@0 551 uninit: function() {
michael@0 552 this._onprogress = null;
michael@0 553 cpmm.sendAsyncMessage("Webapps:UnregisterForMessages", [
michael@0 554 "Webapps:FireEvent",
michael@0 555 "Webapps:UpdateState"
michael@0 556 ]);
michael@0 557
michael@0 558 manifestCache.evict(this.manifestURL, this.innerWindowID);
michael@0 559 },
michael@0 560
michael@0 561 _fireEvent: function(aName) {
michael@0 562 let handler = this["_on" + aName];
michael@0 563 if (handler) {
michael@0 564 let event = new this._window.MozApplicationEvent(aName, {
michael@0 565 application: this
michael@0 566 });
michael@0 567 try {
michael@0 568 handler.handleEvent(event);
michael@0 569 } catch (ex) {
michael@0 570 dump("Event handler expection " + ex + "\n");
michael@0 571 }
michael@0 572 }
michael@0 573 },
michael@0 574
michael@0 575 _updateState: function(aMsg) {
michael@0 576 if (aMsg.app) {
michael@0 577 for (let prop in aMsg.app) {
michael@0 578 this[prop] = aMsg.app[prop];
michael@0 579 }
michael@0 580 }
michael@0 581
michael@0 582 if (aMsg.error) {
michael@0 583 this._downloadError = aMsg.error;
michael@0 584 }
michael@0 585
michael@0 586 if (aMsg.manifest) {
michael@0 587 this._manifest = aMsg.manifest;
michael@0 588 manifestCache.evict(this.manifestURL, this.innerWindowID);
michael@0 589 }
michael@0 590 },
michael@0 591
michael@0 592 receiveMessage: function(aMessage) {
michael@0 593 let msg = aMessage.json;
michael@0 594 let req;
michael@0 595 if (aMessage.name == "Webapps:Connect:Return:OK" ||
michael@0 596 aMessage.name == "Webapps:Connect:Return:KO" ||
michael@0 597 aMessage.name == "Webapps:GetConnections:Return:OK") {
michael@0 598 req = this.takePromiseResolver(msg.requestID);
michael@0 599 } else {
michael@0 600 req = this.takeRequest(msg.requestID);
michael@0 601 }
michael@0 602
michael@0 603 // ondownload* callbacks should be triggered on all app instances
michael@0 604 if ((msg.oid != this._id || !req) &&
michael@0 605 aMessage.name !== "Webapps:FireEvent" &&
michael@0 606 aMessage.name !== "Webapps:UpdateState") {
michael@0 607 return;
michael@0 608 }
michael@0 609
michael@0 610 switch (aMessage.name) {
michael@0 611 case "Webapps:Launch:Return:KO":
michael@0 612 this.removeMessageListeners(["Webapps:Launch:Return:OK",
michael@0 613 "Webapps:Launch:Return:KO"]);
michael@0 614 Services.DOMRequest.fireError(req, "APP_INSTALL_PENDING");
michael@0 615 break;
michael@0 616 case "Webapps:Launch:Return:OK":
michael@0 617 this.removeMessageListeners(["Webapps:Launch:Return:OK",
michael@0 618 "Webapps:Launch:Return:KO"]);
michael@0 619 Services.DOMRequest.fireSuccess(req, null);
michael@0 620 break;
michael@0 621 case "Webapps:CheckForUpdate:Return:KO":
michael@0 622 Services.DOMRequest.fireError(req, msg.error);
michael@0 623 break;
michael@0 624 case "Webapps:FireEvent":
michael@0 625 if (msg.manifestURL != this.manifestURL) {
michael@0 626 return;
michael@0 627 }
michael@0 628
michael@0 629 // The parent might ask childs to trigger more than one event in one
michael@0 630 // shot, so in order to avoid needless IPC we allow an array for the
michael@0 631 // 'eventType' IPC message field.
michael@0 632 if (!Array.isArray(msg.eventType)) {
michael@0 633 msg.eventType = [msg.eventType];
michael@0 634 }
michael@0 635
michael@0 636 msg.eventType.forEach((aEventType) => {
michael@0 637 if ("_on" + aEventType in this) {
michael@0 638 this._fireEvent(aEventType);
michael@0 639 } else {
michael@0 640 dump("Unsupported event type " + aEventType + "\n");
michael@0 641 }
michael@0 642 });
michael@0 643
michael@0 644 if (req) {
michael@0 645 Services.DOMRequest.fireSuccess(req, this.manifestURL);
michael@0 646 }
michael@0 647 break;
michael@0 648 case "Webapps:UpdateState":
michael@0 649 if (msg.manifestURL != this.manifestURL) {
michael@0 650 return;
michael@0 651 }
michael@0 652
michael@0 653 this._updateState(msg);
michael@0 654 break;
michael@0 655 case "Webapps:ClearBrowserData:Return":
michael@0 656 this.removeMessageListeners(aMessage.name);
michael@0 657 Services.DOMRequest.fireSuccess(req, null);
michael@0 658 break;
michael@0 659 case "Webapps:Connect:Return:OK":
michael@0 660 let messagePorts = [];
michael@0 661 msg.messagePortIDs.forEach((aPortID) => {
michael@0 662 let port = new this._window.MozInterAppMessagePort(aPortID);
michael@0 663 messagePorts.push(port);
michael@0 664 });
michael@0 665 req.resolve(messagePorts);
michael@0 666 break;
michael@0 667 case "Webapps:Connect:Return:KO":
michael@0 668 req.reject("No connections registered");
michael@0 669 break;
michael@0 670 case "Webapps:GetConnections:Return:OK":
michael@0 671 let connections = [];
michael@0 672 msg.connections.forEach((aConnection) => {
michael@0 673 let connection =
michael@0 674 new this._window.MozInterAppConnection(aConnection.keyword,
michael@0 675 aConnection.pubAppManifestURL,
michael@0 676 aConnection.subAppManifestURL);
michael@0 677 connections.push(connection);
michael@0 678 });
michael@0 679 req.resolve(connections);
michael@0 680 break;
michael@0 681 case "Webapps:AddReceipt:Return:OK":
michael@0 682 this.removeMessageListeners(["Webapps:AddReceipt:Return:OK",
michael@0 683 "Webapps:AddReceipt:Return:KO"]);
michael@0 684 this.receipts = msg.receipts;
michael@0 685 Services.DOMRequest.fireSuccess(req, null);
michael@0 686 break;
michael@0 687 case "Webapps:AddReceipt:Return:KO":
michael@0 688 this.removeMessageListeners(["Webapps:AddReceipt:Return:OK",
michael@0 689 "Webapps:AddReceipt:Return:KO"]);
michael@0 690 Services.DOMRequest.fireError(req, msg.error);
michael@0 691 break;
michael@0 692 case "Webapps:RemoveReceipt:Return:OK":
michael@0 693 this.removeMessageListeners(["Webapps:RemoveReceipt:Return:OK",
michael@0 694 "Webapps:RemoveReceipt:Return:KO"]);
michael@0 695 this.receipts = msg.receipts;
michael@0 696 Services.DOMRequest.fireSuccess(req, null);
michael@0 697 break;
michael@0 698 case "Webapps:RemoveReceipt:Return:KO":
michael@0 699 this.removeMessageListeners(["Webapps:RemoveReceipt:Return:OK",
michael@0 700 "Webapps:RemoveReceipt:Return:KO"]);
michael@0 701 Services.DOMRequest.fireError(req, msg.error);
michael@0 702 break;
michael@0 703 case "Webapps:ReplaceReceipt:Return:OK":
michael@0 704 this.removeMessageListeners(["Webapps:ReplaceReceipt:Return:OK",
michael@0 705 "Webapps:ReplaceReceipt:Return:KO"]);
michael@0 706 this.receipts = msg.receipts;
michael@0 707 Services.DOMRequest.fireSuccess(req, null);
michael@0 708 break;
michael@0 709 case "Webapps:ReplaceReceipt:Return:KO":
michael@0 710 this.removeMessageListeners(["Webapps:ReplaceReceipt:Return:OK",
michael@0 711 "Webapps:ReplaceReceipt:Return:KO"]);
michael@0 712 Services.DOMRequest.fireError(req, msg.error);
michael@0 713 break;
michael@0 714 }
michael@0 715 },
michael@0 716
michael@0 717 classID: Components.ID("{723ed303-7757-4fb0-b261-4f78b1f6bd22}"),
michael@0 718
michael@0 719 QueryInterface: XPCOMUtils.generateQI([Ci.mozIDOMApplication,
michael@0 720 Ci.nsISupportsWeakReference,
michael@0 721 Ci.nsIObserver]),
michael@0 722
michael@0 723 classInfo: XPCOMUtils.generateCI({classID: Components.ID("{723ed303-7757-4fb0-b261-4f78b1f6bd22}"),
michael@0 724 contractID: "@mozilla.org/webapps/application;1",
michael@0 725 interfaces: [Ci.mozIDOMApplication],
michael@0 726 flags: Ci.nsIClassInfo.DOM_OBJECT,
michael@0 727 classDescription: "Webapps Application"})
michael@0 728 }
michael@0 729
michael@0 730 /**
michael@0 731 * mozIDOMApplicationMgmt object
michael@0 732 */
michael@0 733 function WebappsApplicationMgmt(aWindow) {
michael@0 734 this.initDOMRequestHelper(aWindow, ["Webapps:GetAll:Return:OK",
michael@0 735 "Webapps:GetAll:Return:KO",
michael@0 736 "Webapps:Uninstall:Return:OK",
michael@0 737 "Webapps:Uninstall:Broadcast:Return:OK",
michael@0 738 "Webapps:Uninstall:Return:KO",
michael@0 739 "Webapps:Install:Return:OK",
michael@0 740 "Webapps:GetNotInstalled:Return:OK"]);
michael@0 741
michael@0 742 cpmm.sendAsyncMessage("Webapps:RegisterForMessages",
michael@0 743 {
michael@0 744 messages: ["Webapps:Install:Return:OK",
michael@0 745 "Webapps:Uninstall:Return:OK",
michael@0 746 "Webapps:Uninstall:Broadcast:Return:OK"]
michael@0 747 }
michael@0 748 );
michael@0 749
michael@0 750 this._oninstall = null;
michael@0 751 this._onuninstall = null;
michael@0 752 }
michael@0 753
michael@0 754 WebappsApplicationMgmt.prototype = {
michael@0 755 __proto__: DOMRequestIpcHelper.prototype,
michael@0 756 __exposedProps__: {
michael@0 757 applyDownload: "r",
michael@0 758 getAll: "r",
michael@0 759 getNotInstalled: "r",
michael@0 760 oninstall: "rw",
michael@0 761 onuninstall: "rw"
michael@0 762 },
michael@0 763
michael@0 764 uninit: function() {
michael@0 765 this._oninstall = null;
michael@0 766 this._onuninstall = null;
michael@0 767 cpmm.sendAsyncMessage("Webapps:UnregisterForMessages",
michael@0 768 ["Webapps:Install:Return:OK",
michael@0 769 "Webapps:Uninstall:Return:OK",
michael@0 770 "Webapps:Uninstall:Broadcast:Return:OK"]);
michael@0 771 },
michael@0 772
michael@0 773 applyDownload: function(aApp) {
michael@0 774 if (!aApp.readyToApplyDownload) {
michael@0 775 return;
michael@0 776 }
michael@0 777
michael@0 778 cpmm.sendAsyncMessage("Webapps:ApplyDownload",
michael@0 779 { manifestURL: aApp.manifestURL });
michael@0 780 },
michael@0 781
michael@0 782 uninstall: function(aApp) {
michael@0 783 dump("-- webapps.js uninstall " + aApp.manifestURL + "\n");
michael@0 784 let request = this.createRequest();
michael@0 785 cpmm.sendAsyncMessage("Webapps:Uninstall", { origin: aApp.origin,
michael@0 786 manifestURL: aApp.manifestURL,
michael@0 787 oid: this._id,
michael@0 788 requestID: this.getRequestId(request) });
michael@0 789 return request;
michael@0 790 },
michael@0 791
michael@0 792 getAll: function() {
michael@0 793 let request = this.createRequest();
michael@0 794 cpmm.sendAsyncMessage("Webapps:GetAll", { oid: this._id,
michael@0 795 requestID: this.getRequestId(request) });
michael@0 796 return request;
michael@0 797 },
michael@0 798
michael@0 799 getNotInstalled: function() {
michael@0 800 let request = this.createRequest();
michael@0 801 cpmm.sendAsyncMessage("Webapps:GetNotInstalled", { oid: this._id,
michael@0 802 requestID: this.getRequestId(request) });
michael@0 803 return request;
michael@0 804 },
michael@0 805
michael@0 806 get oninstall() {
michael@0 807 return this._oninstall;
michael@0 808 },
michael@0 809
michael@0 810 get onuninstall() {
michael@0 811 return this._onuninstall;
michael@0 812 },
michael@0 813
michael@0 814 set oninstall(aCallback) {
michael@0 815 this._oninstall = aCallback;
michael@0 816 },
michael@0 817
michael@0 818 set onuninstall(aCallback) {
michael@0 819 this._onuninstall = aCallback;
michael@0 820 },
michael@0 821
michael@0 822 receiveMessage: function(aMessage) {
michael@0 823 var msg = aMessage.json;
michael@0 824 let req = this.getRequest(msg.requestID);
michael@0 825 // We want Webapps:Install:Return:OK and Webapps:Uninstall:Broadcast:Return:OK
michael@0 826 // to be broadcasted to all instances of mozApps.mgmt.
michael@0 827 if (!((msg.oid == this._id && req) ||
michael@0 828 aMessage.name == "Webapps:Install:Return:OK" ||
michael@0 829 aMessage.name == "Webapps:Uninstall:Broadcast:Return:OK")) {
michael@0 830 return;
michael@0 831 }
michael@0 832 switch (aMessage.name) {
michael@0 833 case "Webapps:GetAll:Return:OK":
michael@0 834 Services.DOMRequest.fireSuccess(req, convertAppsArray(msg.apps, this._window));
michael@0 835 break;
michael@0 836 case "Webapps:GetAll:Return:KO":
michael@0 837 Services.DOMRequest.fireError(req, "DENIED");
michael@0 838 break;
michael@0 839 case "Webapps:GetNotInstalled:Return:OK":
michael@0 840 Services.DOMRequest.fireSuccess(req, convertAppsArray(msg.apps, this._window));
michael@0 841 break;
michael@0 842 case "Webapps:Install:Return:OK":
michael@0 843 if (this._oninstall) {
michael@0 844 let app = msg.app;
michael@0 845 let event = new this._window.MozApplicationEvent("applicationinstall",
michael@0 846 { application : createApplicationObject(this._window, app) });
michael@0 847 this._oninstall.handleEvent(event);
michael@0 848 }
michael@0 849 break;
michael@0 850 case "Webapps:Uninstall:Broadcast:Return:OK":
michael@0 851 if (this._onuninstall) {
michael@0 852 let detail = {
michael@0 853 manifestURL: msg.manifestURL,
michael@0 854 origin: msg.origin
michael@0 855 };
michael@0 856 let event = new this._window.MozApplicationEvent("applicationuninstall",
michael@0 857 { application : createApplicationObject(this._window, detail) });
michael@0 858 this._onuninstall.handleEvent(event);
michael@0 859 }
michael@0 860 break;
michael@0 861 case "Webapps:Uninstall:Return:OK":
michael@0 862 Services.DOMRequest.fireSuccess(req, msg.origin);
michael@0 863 break;
michael@0 864 case "Webapps:Uninstall:Return:KO":
michael@0 865 Services.DOMRequest.fireError(req, "NOT_INSTALLED");
michael@0 866 break;
michael@0 867 }
michael@0 868 if (aMessage.name !== "Webapps:Uninstall:Broadcast:Return:OK") {
michael@0 869 this.removeRequest(msg.requestID);
michael@0 870 }
michael@0 871 },
michael@0 872
michael@0 873 classID: Components.ID("{8c1bca96-266f-493a-8d57-ec7a95098c15}"),
michael@0 874
michael@0 875 QueryInterface: XPCOMUtils.generateQI([Ci.mozIDOMApplicationMgmt,
michael@0 876 Ci.nsISupportsWeakReference,
michael@0 877 Ci.nsIObserver]),
michael@0 878
michael@0 879 classInfo: XPCOMUtils.generateCI({classID: Components.ID("{8c1bca96-266f-493a-8d57-ec7a95098c15}"),
michael@0 880 contractID: "@mozilla.org/webapps/application-mgmt;1",
michael@0 881 interfaces: [Ci.mozIDOMApplicationMgmt],
michael@0 882 flags: Ci.nsIClassInfo.DOM_OBJECT,
michael@0 883 classDescription: "Webapps Application Mgmt"})
michael@0 884 }
michael@0 885
michael@0 886 manifestCache.init();
michael@0 887
michael@0 888 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WebappsRegistry,
michael@0 889 WebappsApplication]);

mercurial