Bug 1037329 - Part 1: Implement SystemUpdate API. r=baku

This commit is contained in:
James Cheng 2015-07-12 22:06:00 -04:00
Родитель 1c232169ab
Коммит 64cbe25e51
12 изменённых файлов: 1047 добавлений и 0 удалений

Просмотреть файл

@ -548,6 +548,12 @@ this.PermissionsTable = { geolocation: {
trusted: DENY_ACTION,
privileged: ALLOW_ACTION,
certified: ALLOW_ACTION
},
"system-update": {
app: DENY_ACTION,
trusted: DENY_ACTION,
privileged: DENY_ACTION,
certified: ALLOW_ACTION
}
};

Просмотреть файл

@ -0,0 +1,2 @@
component {e8530001-ba5b-46ab-a306-7fbeb692d0fe} SystemUpdateManager.js
contract @mozilla.org/system-update-manager;1 {e8530001-ba5b-46ab-a306-7fbeb692d0fe}

Просмотреть файл

@ -0,0 +1,262 @@
/* 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");
let 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._sendPromise(function(aResolverId) {
cpmm.sendAsyncMessage("SystemUpdate:GetProviders", {
requestId: aResolverId,
});
});
},
getActiveProvider: function() {
return this._sendPromise(function(aResolverId) {
cpmm.sendAsyncMessage("SystemUpdate:GetActiveProvider", {
requestId: aResolverId,
});
});
},
setActiveProvider: function(aUuid) {
return this._sendPromise(function(aResolverId) {
cpmm.sendAsyncMessage("SystemUpdate:SetActiveProvider", {
requestId: aResolverId,
uuid: aUuid
});
});
},
_sendPromise: function(aCallback) {
let self = this;
return this.createPromise(function(aResolve, aReject) {
let resolverId = self.getPromiseResolverId({resolve: aResolve,
reject: aReject});
aCallback(resolverId);
});
}
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SystemUpdateManager]);

Просмотреть файл

@ -0,0 +1,382 @@
/* 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;
this.EXPORTED_SYMBOLS = ["SystemUpdateService"];
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
const CATEGORY_SYSTEM_UPDATE_PROVIDER = "system-update-provider";
const PROVIDER_ACTIVITY_IDLE = 0;
const PROVIDER_ACTIVITY_CHECKING = 1;
const PROVIDER_ACTIVITY_DOWNLOADING = 1 << 1;
const PROVIDER_ACTIVITY_APPLYING = 1 << 2;
let debug = Services.prefs.getBoolPref("dom.system_update.debug")
? (aMsg) => dump("-*- SystemUpdateService.jsm : " + aMsg + "\n")
: (aMsg) => {};
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
"@mozilla.org/parentprocessmessagemanager;1",
"nsIMessageBroadcaster");
function ActiveProvider(aProvider) {
this.id = aProvider.id;
this._instance = Cc[aProvider.contractId].getService(Ci.nsISystemUpdateProvider);
this._instance.setListener(this);
}
ActiveProvider.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsISystemUpdateListener]),
_activity: PROVIDER_ACTIVITY_IDLE,
destroy: function() {
if (this._instance) {
this._instance.unsetListener();
this._instance = null;
}
this.id = null;
},
checkForUpdate: function() {
this._execFuncIfNotInActivity(PROVIDER_ACTIVITY_CHECKING,
this._instance.checkForUpdate);
},
startDownload: function() {
this._execFuncIfNotInActivity(PROVIDER_ACTIVITY_DOWNLOADING,
this._instance.startDownload);
},
stopDownload: function() {
this._execFuncIfNotInActivity(PROVIDER_ACTIVITY_DOWNLOADING,
this._instance.stopDownload);
},
applyUpdate: function() {
this._execFuncIfNotInActivity(PROVIDER_ACTIVITY_APPLYING,
this._instance.applyUpdate);
},
setParameter: function(aName, aValue) {
return this._instance.setParameter(aName, aValue);
},
getParameter: function(aName) {
return this._instance.getParameter(aName);
},
// nsISystemUpdateListener
onUpdateAvailable: function(aType, aVersion, aDescription, aBuildDate, aSize) {
this._execFuncIfActiveAndInAction(PROVIDER_ACTIVITY_CHECKING, function() {
ppmm.broadcastAsyncMessage("SystemUpdate:OnUpdateAvailable", {
uuid: this.id,
packageInfo: {
type: aType,
version: aVersion,
description: aDescription,
buildDate: aBuildDate,
size: aSize,
}
});
this._unsetActivity(PROVIDER_ACTIVITY_CHECKING);
}.bind(this));
},
onProgress: function(aLoaded, aTotal) {
this._execFuncIfActiveAndInAction(PROVIDER_ACTIVITY_DOWNLOADING, function() {
ppmm.broadcastAsyncMessage("SystemUpdate:OnProgress", {
uuid: this.id,
loaded: aLoaded,
total: aTotal,
});
}.bind(this));
},
onUpdateReady: function() {
this._execFuncIfActiveAndInAction(PROVIDER_ACTIVITY_DOWNLOADING, function() {
ppmm.broadcastAsyncMessage("SystemUpdate:OnUpdateReady", {
uuid: this.id,
});
this._unsetActivity(PROVIDER_ACTIVITY_DOWNLOADING);
}.bind(this));
},
onError: function(aErrMsg) {
if (!SystemUpdateService._isActiveProviderId(this.id)) {
return;
}
ppmm.broadcastAsyncMessage("SystemUpdate:OnError", {
uuid: this.id,
message: aErrMsg,
});
this._activity = PROVIDER_ACTIVITY_IDLE;
},
isIdle: function() {
return this._activity === PROVIDER_ACTIVITY_IDLE;
},
_isInActivity: function(aActivity) {
return (this._activity & aActivity) !== PROVIDER_ACTIVITY_IDLE;
},
_setActivity: function(aActivity) {
this._activity |= aActivity;
},
_unsetActivity: function(aActivity) {
this._activity &= ~aActivity;
},
_execFuncIfNotInActivity: function(aActivity, aFunc) {
if (!this._isInActivity(aActivity)) {
this._setActivity(aActivity);
aFunc();
}
},
_execFuncIfActiveAndInAction: function(aActivity, aFunc) {
if (!SystemUpdateService._isActiveProviderId(this.id)) {
return;
}
if (this._isInActivity(aActivity)) {
aFunc();
}
},
};
this.SystemUpdateService = {
_providers: [],
_activeProvider: null,
_updateActiveProvider: function(aProvider) {
if (this._activeProvider) {
this._activeProvider.destroy();
}
this._activeProvider = new ActiveProvider(aProvider);
},
_isActiveProviderId: function(aId) {
return (this._activeProvider && this._activeProvider.id === aId);
},
init: function() {
debug("init");
let messages = ["SystemUpdate:GetProviders",
"SystemUpdate:GetActiveProvider",
"SystemUpdate:SetActiveProvider",
"SystemUpdate:CheckForUpdate",
"SystemUpdate:StartDownload",
"SystemUpdate:StopDownload",
"SystemUpdate:ApplyUpdate",
"SystemUpdate:SetParameter",
"SystemUpdate:GetParameter"];
messages.forEach((function(aMsgName) {
ppmm.addMessageListener(aMsgName, this);
}).bind(this));
// load available provider list
let catMan = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
let entries = catMan.enumerateCategory(CATEGORY_SYSTEM_UPDATE_PROVIDER);
while (entries.hasMoreElements()) {
let name = entries.getNext().QueryInterface(Ci.nsISupportsCString).data;
let [contractId, id] = catMan.getCategoryEntry(CATEGORY_SYSTEM_UPDATE_PROVIDER, name).split(",");
this._providers.push({
id: id,
name: name,
contractId: contractId
});
}
debug("available providers: " + JSON.stringify(this._providers));
// setup default active provider
let defaultActive;
try {
defaultActive = Services.prefs.getCharPref("dom.system_update.active");
} catch (e) {}
if (defaultActive) {
let defaultProvider = this._providers.find(function(aProvider) {
return aProvider.contractId === defaultActive;
});
if (defaultProvider) {
this._updateActiveProvider(defaultProvider);
}
}
},
addProvider: function(aClassId, aContractId, aName) {
debug("addProvider");
//did not allow null or empty string to add.
if(!aClassId || !aContractId || !aName) {
return;
}
let existedProvider = this._providers.find(function(provider) {
return provider.id === aClassId;
});
//skip if adding the existed provider.
if (existedProvider) {
debug("existing providers: " + JSON.stringify(existedProvider));
return;
}
//dynamically add the provider info to list.
this._providers.push({
id: aClassId,
name: aName,
contractId: aContractId
});
debug("available providers: " + JSON.stringify(this._providers));
},
getProviders: function(aData, aMm) {
debug("getProviders");
aData.providers = [];
for (let provider of this._providers) {
aData.providers.push({
name: provider.name,
uuid: provider.id
});
}
aMm.sendAsyncMessage("SystemUpdate:GetProviders:Result:OK", aData);
},
getActiveProvider: function(aData, aMm) {
debug("getActiveProvider");
let self = this;
let providerInfo = this._providers.find(function(provider) {
return self._isActiveProviderId(provider.id);
});
if (!providerInfo) {
aData.error = "NotFoundError";
aMm.sendAsyncMessage("SystemUpdate:GetActiveProvider:Result:Error", aData);
return;
}
aData.provider = {
name: providerInfo.name,
uuid: providerInfo.id
};
aMm.sendAsyncMessage("SystemUpdate:GetActiveProvider:Result:OK", aData);
},
setActiveProvider: function(aData, aMm) {
debug("setActiveProvider");
let self = this;
let selectedProvider = this._providers.find(function(provider) {
return provider.id === aData.uuid;
});
if (!selectedProvider) {
aData.error = "DataError";
aMm.sendAsyncMessage("SystemUpdate:SetActiveProvider:Result:Error", aData);
return;
}
if (!this._isActiveProviderId(selectedProvider.id)) {
// not allow changing active provider while there is an ongoing update activity
if (this.activeProvider && !this._activeProvider.isIdle()) {
aData.error = "DataError";
aMm.sendAsyncMessage("SystemUpdate:SetActiveProvider:Result:Error", aData);
return;
}
this._updateActiveProvider(selectedProvider);
Services.prefs.setCharPref("dom.system_update.active", selectedProvider.contractId);
}
aData.provider = {
name: selectedProvider.name,
uuid: selectedProvider.id
};
aMm.sendAsyncMessage("SystemUpdate:SetActiveProvider:Result:OK", aData);
},
receiveMessage: function(aMessage) {
if (!aMessage.target.assertPermission("system-update")) {
debug("receive message " + aMessage.name +
" from a content process with no 'system-update' privileges.");
return null;
}
let msg = aMessage.data || {};
let mm = aMessage.target;
switch (aMessage.name) {
case "SystemUpdate:GetProviders": {
this.getProviders(msg, mm);
break;
}
case "SystemUpdate:GetActiveProvider": {
this.getActiveProvider(msg, mm);
break;
}
case "SystemUpdate:SetActiveProvider": {
this.setActiveProvider(msg, mm);
break;
}
case "SystemUpdate:CheckForUpdate": {
if (this._isActiveProviderId(msg.uuid)) {
this._activeProvider.checkForUpdate();
}
break;
}
case "SystemUpdate:StartDownload": {
if (this._isActiveProviderId(msg.uuid)) {
this._activeProvider.startDownload();
}
break;
}
case "SystemUpdate:StopDownload": {
if (this._isActiveProviderId(msg.uuid)) {
this._activeProvider.stopDownload();
}
break;
}
case "SystemUpdate:ApplyUpdate": {
if (this._isActiveProviderId(msg.uuid)) {
this._activeProvider.applyUpdate();
}
break;
}
case "SystemUpdate:SetParameter": {
if (this._isActiveProviderId(msg.uuid)) {
return this._activeProvider.setParameter(msg.name, msg.value);
}
break;
}
case "SystemUpdate:GetParameter": {
if (this._isActiveProviderId(msg.uuid)) {
return this._activeProvider.getParameter(msg.name);
}
break;
}
}
},
};
SystemUpdateService.init();

Просмотреть файл

@ -19,6 +19,7 @@ elif toolkit == 'gonk':
XPIDL_SOURCES += [
'nsIOSFileConstantsService.idl',
'nsISystemUpdateProvider.idl',
]
XPIDL_MODULE = 'dom_system'
@ -39,6 +40,12 @@ UNIFIED_SOURCES += [
EXTRA_COMPONENTS += [
'NetworkGeolocationProvider.js',
'NetworkGeolocationProvider.manifest',
'SystemUpdate.manifest',
'SystemUpdateManager.js',
]
EXTRA_JS_MODULES += [
'SystemUpdateService.jsm',
]
FAIL_ON_WARNINGS = True
@ -57,3 +64,4 @@ DEFINES['DLL_PREFIX'] = '"%s"' % CONFIG['DLL_PREFIX']
DEFINES['DLL_SUFFIX'] = '"%s"' % CONFIG['DLL_SUFFIX']
MOCHITEST_CHROME_MANIFESTS += ['tests/chrome.ini']
MOCHITEST_MANIFESTS += ['tests/mochitest.ini']

Просмотреть файл

@ -0,0 +1,73 @@
/* 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/. */
#include "nsISupports.idl"
[scriptable, uuid(775edbf5-b4a9-400c-b0ad-ea3c3a027097)]
interface nsISystemUpdateListener : nsISupports
{
/**
* callback for notifying an update package is available for download.
*/
void onUpdateAvailable(in DOMString type,
in DOMString version,
in DOMString description,
in unsigned long long buildDate,
in unsigned long long size);
/**
* callback for notifying the download progress.
*/
void onProgress(in unsigned long long loaded, in unsigned long long total);
/**
* callback for notifying an update package is ready to apply.
*/
void onUpdateReady();
/**
* callback for notifying any error while
* checking/downloading/applying an update package.
*/
void onError(in DOMString errMsg);
};
[scriptable, uuid(c9b7c166-b9cf-4396-a6de-39275e1c0a36)]
interface nsISystemUpdateProvider : nsISupports
{
void checkForUpdate();
void startDownload();
void stopDownload();
void applyUpdate();
/**
* Set the available parameter to the update provider.
* The available parameter is implementation-dependent.
* e.g. "update-url", "last-update-date", "update-status", "update-interval"
*
* @param name The number of languages.
* @param languages An array of languages.
* @return true when setting an available parameter,
* false when setting an unavailable parameter.
*/
bool setParameter(in DOMString name, in DOMString value);
/**
* Get the available parameter from the update provider.
* The available parameter is implementation-dependent.
*
* @param name The available parameter.
* @return The corresponding value to the name.
* Return null if try to get unavailable parameter.
*/
DOMString getParameter(in DOMString name);
/**
* NOTE TO IMPLEMENTORS:
* Need to consider if it is necessary to fire the pending event when
* registering the listener.
* (E.g. UpdateAvailable or UpdateReady event.)
*/
void setListener(in nsISystemUpdateListener listener);
void unsetListener();
};

Просмотреть файл

@ -0,0 +1,6 @@
[DEFAULT]
skip-if = buildapp != 'b2g'
support-files =
preload-SystemUpdateManager-jsm.js
[test_system_update_enabled.html]

Просмотреть файл

@ -0,0 +1,80 @@
/* 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, manager: Cm} = Components;
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
const cid = '{17a84227-28f4-453d-9b80-9ae75a5682e0}';
const contractId = '@mozilla.org/test-update-provider;1';
function TestUpdateProvider() {}
TestUpdateProvider.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsISystemUpdateProvider]),
checkForUpdate: function() {
dump('check for update');
this._listener.onUpdateAvailable('test-type', 'test-version', 'test-description', Date.now().valueOf(), 5566);
},
startDownload: function() {
dump('test start download');
this._listener.onProgress(10, 100);
},
stopDownload: function() {
dump('test stop download');
},
applyUpdate: function() {
dump('apply update');
},
setParameter: function(name, value) {
dump('set parameter');
return (name === 'dummy' && value === 'dummy-value');
},
getParameter: function(name) {
dump('get parameter');
if (name === 'dummy') {
return 'dummy-value';
}
},
setListener: function(listener) {
this._listener = listener;
},
unsetListener: function() {
this._listener = null;
},
};
let factory = {
createInstance: function(outer, iid) {
if (outer) {
throw Components.results.NS_ERROR_NO_AGGREGATION;
}
return new TestUpdateProvider().QueryInterface(iid);
},
lockFactory: function(aLock) {
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
};
Cm.nsIComponentRegistrar.registerFactory(Components.ID(cid), '', contractId, factory);
let cm = Cc['@mozilla.org/categorymanager;1'].getService(Ci.nsICategoryManager);
cm.addCategoryEntry('system-update-provider', 'DummyProvider',
contractId + ',' + cid, false, true);
Cu.import('resource://gre/modules/SystemUpdateService.jsm');
this.SystemUpdateService.addProvider('{17a84227-28f4-453d-9b80-9ae75a5682e0}',
'@mozilla.org/test-update-provider;1',
'DummyProvider');

Просмотреть файл

@ -0,0 +1,175 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1037329
-->
<head>
<meta charset="utf-8">
<title>System Update API Test</title>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1037329">Test System Update API</a>
<script type="application/javascript;version=1.8">
'use strict';
SimpleTest.waitForExplicitFinish();
function setup() {
window.gUrl = SimpleTest.getTestFileURL('preload-SystemUpdateManager-jsm.js');
window.gScript = SpecialPowers.loadChromeScript(gUrl);
return Promise.resolve();
}
function testGetProviders() {
return new Promise(function(resolve, reject) {
navigator.updateManager.getProviders().then(function(providerInfos) {
info('num of providers: ' + providerInfos.length);
for (let providerInfo of providerInfos) {
info('provider info: ' + JSON.stringify(providerInfo));
}
resolve(providerInfos);
});
});
}
function testSetActiveProvider(providerInfos) {
return new Promise(function(resolve, reject) {
//Find the mock provider for our testing provider instead.
//Set the mock provider as active provider.
let targetProvider = providerInfos[0];
for(let provider of providerInfos) {
if(provider.uuid == "{17a84227-28f4-453d-9b80-9ae75a5682e0}") {
info('target provider uuid: ' + provider.uuid);
targetProvider = provider;
break;
}
}
is("{17a84227-28f4-453d-9b80-9ae75a5682e0}", targetProvider.uuid, 'get the dynamically added provider');
navigator.updateManager.setActiveProvider(targetProvider.uuid).then(function(activeProvider) {
info('active provider info: ' + JSON.stringify(activeProvider.info));
is(activeProvider.name, targetProvider.name, 'expected name of active provider');
is(activeProvider.uuid, targetProvider.uuid, 'expected uuid of active provider');
resolve({name : activeProvider.name, uuid : activeProvider.uuid});
});
});
}
function testGetActiveProvider(providerInfo) {
info('test GetActiveProvider');
return new Promise(function(resolve, reject) {
navigator.updateManager.getActiveProvider().then(function(activeProvider) {
is(activeProvider.name, providerInfo.name, 'expected name of active provider');
is(activeProvider.uuid, providerInfo.uuid, 'expected uuid of active provider');
resolve(activeProvider);
});
});
}
function testCheckForUpdate(provider) {
info('test CheckForUpdate');
return new Promise(function(resolve, reject) {
provider.addEventListener('updateavailable', function(event) {
ok(true, 'receive updateavailable event');
info('event: ' + JSON.stringify(event.detail));
resolve(provider);
});
provider.checkForUpdate();
});
}
function testStartDownload(provider) {
info('test StartDownload');
return new Promise(function(resolve, reject) {
provider.addEventListener('progress', function(event) {
ok(true, 'receive progress event');
is(event.loaded, 10, 'expected loaded');
is(event.total, 100, 'expected total');
resolve(provider);
});
provider.startDownload();
});
}
function testStopDownload(provider) {
info('test StopDownload');
return new Promise(function(resolve, reject) {
provider.stopDownload();
resolve(provider);
});
}
function testApplyUpdate(provider) {
info('test ApplyUpdate');
return new Promise(function(resolve, reject) {
provider.applyUpdate();
resolve(provider);
});
}
function testGetParameter(provider) {
info('test GetParameter');
return new Promise(function(resolve, reject) {
let dummy = provider.getParameter('dummy');
is(dummy, 'dummy-value', 'expected parameter');
resolve(provider);
});
}
function testSetParameter(provider) {
info('test SetParameter');
return new Promise(function(resolve, reject) {
provider.setParameter('dummy', 'dummy-value');
resolve();
});
}
function testSetActiveProviderError() {
info('test setActiveProvider error');
return new Promise(function(resolve, reject) {
navigator.updateManager.setActiveProvider('something not exsited').then(function(provider) {
ok(false, 'should not success');
resolve();
}, function(reason) {
info('error message: ' + reason);
ok(true, 'expected error while setActiveProvider');
resolve();
});
});
}
function runTest() {
ok(navigator.updateManager, 'should have navigator.updateManager');
setup()
.then(testGetProviders)
.then(testSetActiveProvider)
.then(testGetActiveProvider)
.then(testCheckForUpdate)
.then(testStartDownload)
.then(testStopDownload)
.then(testApplyUpdate)
.then(testGetParameter)
.then(testSetParameter)
.then(testSetActiveProviderError)
.then(function() {
info('test finished');
gScript.destroy();
SimpleTest.finish();
});
}
SpecialPowers.pushPermissions([
{type: 'system-update', allow: true, context: document},
], function() {
SpecialPowers.pushPrefEnv({
'set': [
['dom.system_update.enabled', true],
['dom.system_update.debug', true],
['dom.system_update.active', '@mozilla.org/test-update-provider;1'],
]
}, runTest);
}
);
</script>
</pre>
</body>
</html>

Просмотреть файл

@ -0,0 +1,48 @@
/* 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/. */
dictionary SystemUpdateProviderInfo {
DOMString name = "";
DOMString uuid = "";
};
dictionary SystemUpdatePackageInfo {
DOMString type = "";
DOMString version = "";
DOMString description = "";
DOMTimeStamp buildDate = 0;
unsigned long long size = 0;
};
[JSImplementation="@mozilla.org/system-update-provider;1",
CheckPermissions="system-update",
Pref="dom.system_update.enabled"]
interface SystemUpdateProvider : EventTarget {
readonly attribute DOMString name;
readonly attribute DOMString uuid;
attribute EventHandler onupdateavailable;
attribute EventHandler onprogress;
attribute EventHandler onupdateready;
attribute EventHandler onerror;
void checkForUpdate();
void startDownload();
void stopDownload();
void applyUpdate();
boolean setParameter(DOMString name, DOMString value);
DOMString getParameter(DOMString name);
};
[NavigatorProperty="updateManager",
JSImplementation="@mozilla.org/system-update-manager;1",
CheckPermissions="system-update",
Pref="dom.system_update.enabled"]
interface SystemUpdateManager {
Promise<sequence<SystemUpdateProviderInfo>> getProviders();
Promise<SystemUpdateProvider> setActiveProvider(DOMString uuid);
Promise<SystemUpdateProvider> getActiveProvider();
};

Просмотреть файл

@ -508,6 +508,7 @@ WEBIDL_FILES = [
'SVGViewElement.webidl',
'SVGZoomAndPan.webidl',
'SVGZoomEvent.webidl',
'SystemUpdate.webidl',
'Telephony.webidl',
'TelephonyCall.webidl',
'TelephonyCallGroup.webidl',

Просмотреть файл

@ -4804,6 +4804,10 @@ pref("dom.caches.enabled", true);
pref("camera.control.low_memory_thresholdMB", 404);
#endif
// SystemUpdate API
pref("dom.system_update.enabled", false);
pref("dom.system_update.debug", false);
// UDPSocket API
pref("dom.udpsocket.enabled", false);