OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Custom binding for the webstore API. | 5 // Custom binding for the webstore API. |
6 | 6 |
7 var webstoreNatives = requireNative('webstore'); | 7 var webstoreNatives = requireNative('webstore'); |
8 var Event = require('event_bindings').Event; | 8 |
9 var onInstallStageChanged; | |
10 var onDownloadProgress; | |
9 | 11 |
10 function Installer() { | 12 function Installer() { |
11 this._pendingInstall = null; | 13 this._pendingInstall = null; |
12 this.onInstallStageChanged = | |
13 new Event(null, [{name: 'stage', type: 'string'}], {unmanaged: true}); | |
14 this.onDownloadProgress = | |
15 new Event(null, [{name: 'progress', type: 'number'}], {unmanaged: true}); | |
16 } | 14 } |
17 | 15 |
18 Installer.prototype.install = function(url, onSuccess, onFailure) { | 16 Installer.prototype.install = function(url, onSuccess, onFailure) { |
19 if (this._pendingInstall) | 17 if (this._pendingInstall) |
20 throw new Error('A Chrome Web Store installation is already pending.'); | 18 throw new Error('A Chrome Web Store installation is already pending.'); |
21 if (url !== undefined && typeof(url) !== 'string') { | 19 |
20 // With native bindings, these calls go through argument validation, which | |
21 // sets optional/missing arguments to null. The native webstore bindings | |
22 // expect either present or undefined, so transform null to undefined. | |
23 if (url === null) | |
24 url = undefined; | |
25 if (onSuccess === null) | |
26 onSuccess = undefined; | |
27 if (onFailure === null) | |
28 onFailure = undefined; | |
29 | |
30 if (url !== undefined && typeof url !== 'string') { | |
22 throw new Error( | 31 throw new Error( |
23 'The Chrome Web Store item link URL parameter must be a string.'); | 32 'The Chrome Web Store item link URL parameter must be a string.'); |
24 } | 33 } |
25 if (onSuccess !== undefined && typeof(onSuccess) !== 'function') | 34 if (onSuccess !== undefined && typeof onSuccess !== 'function') { |
26 throw new Error('The success callback parameter must be a function.'); | 35 throw new Error('The success callback parameter must be a function.'); |
27 if (onFailure !== undefined && typeof(onFailure) !== 'function') | 36 } |
37 if (onFailure !== undefined && typeof onFailure !== 'function') { | |
28 throw new Error('The failure callback parameter must be a function.'); | 38 throw new Error('The failure callback parameter must be a function.'); |
39 } | |
29 | 40 |
30 // Since we call Install() with a bool for if we have listeners, listeners | 41 // Since we call Install() with a bool for if we have listeners, listeners |
31 // must be set prior to the inline installation starting (this is also | 42 // must be set prior to the inline installation starting (this is also |
32 // noted in the Event documentation in | 43 // noted in the Event documentation in |
33 // chrome/common/extensions/api/webstore.json). | 44 // chrome/common/extensions/api/webstore.json). |
34 var installId = webstoreNatives.Install( | 45 var installId = webstoreNatives.Install( |
35 this.onInstallStageChanged.hasListeners(), | 46 onInstallStageChanged.hasListeners(), |
36 this.onDownloadProgress.hasListeners(), | 47 onDownloadProgress.hasListeners(), |
37 url, | 48 url, |
38 onSuccess, | 49 onSuccess, |
39 onFailure); | 50 onFailure); |
40 if (installId !== undefined) { | 51 if (installId !== undefined) { |
41 this._pendingInstall = { | 52 this._pendingInstall = { |
42 installId: installId, | 53 installId: installId, |
43 onSuccess: onSuccess, | 54 onSuccess: onSuccess, |
44 onFailure: onFailure | 55 onFailure: onFailure |
45 }; | 56 }; |
46 } | 57 } |
(...skipping 14 matching lines...) Expand all Loading... | |
61 pendingInstall.onFailure(error, resultCode); | 72 pendingInstall.onFailure(error, resultCode); |
62 } catch (e) { | 73 } catch (e) { |
63 console.error('Exception in chrome.webstore.install response handler: ' + | 74 console.error('Exception in chrome.webstore.install response handler: ' + |
64 e.stack); | 75 e.stack); |
65 } finally { | 76 } finally { |
66 this._pendingInstall = null; | 77 this._pendingInstall = null; |
67 } | 78 } |
68 }; | 79 }; |
69 | 80 |
70 Installer.prototype.onInstallStageChanged = function(installStage) { | 81 Installer.prototype.onInstallStageChanged = function(installStage) { |
71 this.onInstallStageChanged.dispatch(installStage); | 82 onInstallStageChanged.dispatch(installStage); |
72 }; | 83 }; |
73 | 84 |
74 Installer.prototype.onDownloadProgress = function(progress) { | 85 Installer.prototype.onDownloadProgress = function(progress) { |
75 this.onDownloadProgress.dispatch(progress); | 86 onDownloadProgress.dispatch(progress); |
76 }; | 87 }; |
77 | 88 |
78 var installer = new Installer(); | 89 var installer = new Installer(); |
79 | 90 |
80 var chromeWebstore = { | |
81 install: function (url, onSuccess, onFailure) { | |
82 installer.install(url, onSuccess, onFailure); | |
83 }, | |
84 onInstallStageChanged: installer.onInstallStageChanged, | |
85 onDownloadProgress: installer.onDownloadProgress | |
86 }; | |
87 | 91 |
88 exports.$set('binding', chromeWebstore); | 92 if (apiBridge) { |
93 apiBridge.registerCustomHook(function(api) { | |
94 var apiFunctions = api.apiFunctions; | |
lazyboy
2017/04/17 20:48:04
Unused?
Devlin
2017/04/19 01:07:42
Whoops! Removed.
| |
95 api.apiFunctions.setHandleRequest('install', | |
96 function(url, onSuccess, onFailure) { | |
97 installer.install(url, onSuccess, onFailure); | |
98 }); | |
99 | |
100 onInstallStageChanged = api.compiledApi.onInstallStageChanged; | |
101 onDownloadProgress = api.compiledApi.onDownloadProgress; | |
102 }); | |
103 } else { | |
104 var Event = require('event_bindings').Event; | |
105 onInstallStageChanged = | |
106 new Event(null, [{name: 'stage', type: 'string'}], {unmanaged: true}); | |
107 onDownloadProgress = | |
108 new Event(null, [{name: 'progress', type: 'number'}], {unmanaged: true}); | |
109 | |
110 var chromeWebstore = { | |
111 install: function (url, onSuccess, onFailure) { | |
112 installer.install(url, onSuccess, onFailure); | |
113 }, | |
114 onInstallStageChanged: onInstallStageChanged, | |
115 onDownloadProgress: onDownloadProgress, | |
116 }; | |
117 exports.$set('binding', chromeWebstore); | |
118 } | |
89 | 119 |
90 // Called by webstore_bindings.cc. | 120 // Called by webstore_bindings.cc. |
91 exports.onInstallResponse = | 121 exports.$set('onInstallResponse', |
92 Installer.prototype.onInstallResponse.bind(installer); | 122 $Function.bind(Installer.prototype.onInstallResponse, installer)); |
93 exports.onInstallStageChanged = | 123 exports.$set('onInstallStageChanged', |
94 Installer.prototype.onInstallStageChanged.bind(installer); | 124 $Function.bind(Installer.prototype.onInstallStageChanged, |
95 exports.onDownloadProgress = | 125 installer)); |
96 Installer.prototype.onDownloadProgress.bind(installer); | 126 exports.$set('onDownloadProgress', |
127 $Function.bind(Installer.prototype.onDownloadProgress, installer)); | |
OLD | NEW |