Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | this.EXPORTED_SYMBOLS = ["WebappsUpdater"]; |
michael@0 | 8 | |
michael@0 | 9 | const Cc = Components.classes; |
michael@0 | 10 | const Cu = Components.utils; |
michael@0 | 11 | |
michael@0 | 12 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 13 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 14 | |
michael@0 | 15 | XPCOMUtils.defineLazyServiceGetter(this, "settings", |
michael@0 | 16 | "@mozilla.org/settingsService;1", |
michael@0 | 17 | "nsISettingsService"); |
michael@0 | 18 | |
michael@0 | 19 | XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", |
michael@0 | 20 | "resource://gre/modules/SystemAppProxy.jsm"); |
michael@0 | 21 | |
michael@0 | 22 | function debug(aStr) { |
michael@0 | 23 | //dump("--*-- WebappsUpdater: " + aStr); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | this.WebappsUpdater = { |
michael@0 | 27 | _checkingApps: false, |
michael@0 | 28 | |
michael@0 | 29 | handleContentStart: function() { |
michael@0 | 30 | }, |
michael@0 | 31 | |
michael@0 | 32 | sendChromeEvent: function(aType, aDetail) { |
michael@0 | 33 | let detail = aDetail || {}; |
michael@0 | 34 | detail.type = aType; |
michael@0 | 35 | |
michael@0 | 36 | let sent = SystemAppProxy.dispatchEvent(detail); |
michael@0 | 37 | if (!sent) { |
michael@0 | 38 | debug("Warning: Couldn't send update event " + aType + |
michael@0 | 39 | ": no content browser. Will send again when content becomes available."); |
michael@0 | 40 | return false; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | return true; |
michael@0 | 44 | }, |
michael@0 | 45 | |
michael@0 | 46 | _appsUpdated: function(aApps) { |
michael@0 | 47 | debug("appsUpdated: " + aApps.length + " apps to update"); |
michael@0 | 48 | let lock = settings.createLock(); |
michael@0 | 49 | lock.set("apps.updateStatus", "check-complete", null); |
michael@0 | 50 | this.sendChromeEvent("apps-update-check", { apps: aApps }); |
michael@0 | 51 | this._checkingApps = false; |
michael@0 | 52 | }, |
michael@0 | 53 | |
michael@0 | 54 | // Trigger apps update check and wait for all to be done before |
michael@0 | 55 | // notifying gaia. |
michael@0 | 56 | updateApps: function() { |
michael@0 | 57 | debug("updateApps (" + this._checkingApps + ")"); |
michael@0 | 58 | // Don't start twice. |
michael@0 | 59 | if (this._checkingApps) { |
michael@0 | 60 | return; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | this._checkingApps = true; |
michael@0 | 64 | |
michael@0 | 65 | let self = this; |
michael@0 | 66 | |
michael@0 | 67 | let window = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 68 | let all = window.navigator.mozApps.mgmt.getAll(); |
michael@0 | 69 | |
michael@0 | 70 | all.onsuccess = function() { |
michael@0 | 71 | let appsCount = this.result.length; |
michael@0 | 72 | let appsChecked = 0; |
michael@0 | 73 | let appsToUpdate = []; |
michael@0 | 74 | this.result.forEach(function updateApp(aApp) { |
michael@0 | 75 | let update = aApp.checkForUpdate(); |
michael@0 | 76 | update.onsuccess = function() { |
michael@0 | 77 | if (aApp.downloadAvailable) { |
michael@0 | 78 | appsToUpdate.push(aApp.manifestURL); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | appsChecked += 1; |
michael@0 | 82 | if (appsChecked == appsCount) { |
michael@0 | 83 | self._appsUpdated(appsToUpdate); |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | update.onerror = function() { |
michael@0 | 87 | appsChecked += 1; |
michael@0 | 88 | if (appsChecked == appsCount) { |
michael@0 | 89 | self._appsUpdated(appsToUpdate); |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | }); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | all.onerror = function() { |
michael@0 | 96 | // Could not get the app list, just notify to update nothing. |
michael@0 | 97 | self._appsUpdated([]); |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | }; |