1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/b2g/components/WebappsUpdater.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,100 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +this.EXPORTED_SYMBOLS = ["WebappsUpdater"]; 1.11 + 1.12 +const Cc = Components.classes; 1.13 +const Cu = Components.utils; 1.14 + 1.15 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.16 +Cu.import("resource://gre/modules/Services.jsm"); 1.17 + 1.18 +XPCOMUtils.defineLazyServiceGetter(this, "settings", 1.19 + "@mozilla.org/settingsService;1", 1.20 + "nsISettingsService"); 1.21 + 1.22 +XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", 1.23 + "resource://gre/modules/SystemAppProxy.jsm"); 1.24 + 1.25 +function debug(aStr) { 1.26 + //dump("--*-- WebappsUpdater: " + aStr); 1.27 +} 1.28 + 1.29 +this.WebappsUpdater = { 1.30 + _checkingApps: false, 1.31 + 1.32 + handleContentStart: function() { 1.33 + }, 1.34 + 1.35 + sendChromeEvent: function(aType, aDetail) { 1.36 + let detail = aDetail || {}; 1.37 + detail.type = aType; 1.38 + 1.39 + let sent = SystemAppProxy.dispatchEvent(detail); 1.40 + if (!sent) { 1.41 + debug("Warning: Couldn't send update event " + aType + 1.42 + ": no content browser. Will send again when content becomes available."); 1.43 + return false; 1.44 + } 1.45 + 1.46 + return true; 1.47 + }, 1.48 + 1.49 + _appsUpdated: function(aApps) { 1.50 + debug("appsUpdated: " + aApps.length + " apps to update"); 1.51 + let lock = settings.createLock(); 1.52 + lock.set("apps.updateStatus", "check-complete", null); 1.53 + this.sendChromeEvent("apps-update-check", { apps: aApps }); 1.54 + this._checkingApps = false; 1.55 + }, 1.56 + 1.57 + // Trigger apps update check and wait for all to be done before 1.58 + // notifying gaia. 1.59 + updateApps: function() { 1.60 + debug("updateApps (" + this._checkingApps + ")"); 1.61 + // Don't start twice. 1.62 + if (this._checkingApps) { 1.63 + return; 1.64 + } 1.65 + 1.66 + this._checkingApps = true; 1.67 + 1.68 + let self = this; 1.69 + 1.70 + let window = Services.wm.getMostRecentWindow("navigator:browser"); 1.71 + let all = window.navigator.mozApps.mgmt.getAll(); 1.72 + 1.73 + all.onsuccess = function() { 1.74 + let appsCount = this.result.length; 1.75 + let appsChecked = 0; 1.76 + let appsToUpdate = []; 1.77 + this.result.forEach(function updateApp(aApp) { 1.78 + let update = aApp.checkForUpdate(); 1.79 + update.onsuccess = function() { 1.80 + if (aApp.downloadAvailable) { 1.81 + appsToUpdate.push(aApp.manifestURL); 1.82 + } 1.83 + 1.84 + appsChecked += 1; 1.85 + if (appsChecked == appsCount) { 1.86 + self._appsUpdated(appsToUpdate); 1.87 + } 1.88 + } 1.89 + update.onerror = function() { 1.90 + appsChecked += 1; 1.91 + if (appsChecked == appsCount) { 1.92 + self._appsUpdated(appsToUpdate); 1.93 + } 1.94 + } 1.95 + }); 1.96 + } 1.97 + 1.98 + all.onerror = function() { 1.99 + // Could not get the app list, just notify to update nothing. 1.100 + self._appsUpdated([]); 1.101 + } 1.102 + } 1.103 +};