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 | 8 |
9 function Installer() { | 9 function Installer() { |
10 this._pendingInstall = null; | 10 this._pendingInstall = null; |
11 this._installStageListener = null; | |
12 this._downloadProgressListener = null; | |
11 } | 13 } |
12 | 14 |
13 Installer.prototype.install = function(url, onSuccess, onFailure) { | 15 Installer.prototype.install = function(url, onSuccess, onFailure) { |
14 if (this._pendingInstall) | 16 if (this._pendingInstall) |
15 throw 'A Chrome Web Store installation is already pending.'; | 17 throw 'A Chrome Web Store installation is already pending.'; |
16 var installId = webstoreNatives.Install(url, onSuccess, onFailure); | 18 var installId = webstoreNatives.Install( |
19 this._installStageListener != null, | |
20 this._downloadProgressListener != null, | |
not at google - send to devlin
2014/02/27 21:49:56
you can also do !!this._installStageListener etc.
Devlin
2014/02/28 18:04:51
(replaced with event.hasListeners())
| |
21 url, | |
22 onSuccess, | |
23 onFailure); | |
17 if (installId !== undefined) { | 24 if (installId !== undefined) { |
18 this._pendingInstall = { | 25 this._pendingInstall = { |
19 installId: installId, | 26 installId: installId, |
20 onSuccess: onSuccess, | 27 onSuccess: onSuccess, |
21 onFailure: onFailure | 28 onFailure: onFailure |
22 }; | 29 }; |
23 } | 30 } |
24 }; | 31 }; |
25 | 32 |
26 Installer.prototype.onInstallResponse = function(installId, success, error) { | 33 Installer.prototype.onInstallResponse = function(installId, success, error) { |
27 var pendingInstall = this._pendingInstall; | 34 var pendingInstall = this._pendingInstall; |
28 if (!pendingInstall || pendingInstall.installId != installId) { | 35 if (!pendingInstall || pendingInstall.installId != installId) { |
29 // TODO(kalman): should this be an error? | 36 // TODO(kalman): should this be an error? |
30 return; | 37 return; |
31 } | 38 } |
32 | 39 |
33 try { | 40 try { |
34 if (success && pendingInstall.onSuccess) | 41 if (success && pendingInstall.onSuccess) |
35 pendingInstall.onSuccess(); | 42 pendingInstall.onSuccess(); |
36 else if (!success && pendingInstall.onFailure) | 43 else if (!success && pendingInstall.onFailure) |
37 pendingInstall.onFailure(error); | 44 pendingInstall.onFailure(error); |
38 } catch (e) { | 45 } catch (e) { |
39 console.error('Exception in chrome.webstore.install response handler: ' + | 46 console.error('Exception in chrome.webstore.install response handler: ' + |
40 e.stack); | 47 e.stack); |
41 } finally { | 48 } finally { |
42 this._pendingInstall = null; | 49 this._pendingInstall = null; |
43 } | 50 } |
44 }; | 51 }; |
45 | 52 |
53 Installer.prototype.setInstallStageListener = function(callback) { | |
54 if (webstoreNatives.ValidateInstallStageListener(callback)) | |
55 this._installStageListener = callback; | |
56 }; | |
57 | |
58 Installer.prototype.onInstallStageChanged = function(installStage) { | |
59 if (!this._installStageListener) | |
60 return; // This could happen if we removed the listener mid-flight. | |
61 | |
62 try { | |
63 this._installStageListener(installStage); | |
64 } catch (e) { | |
65 console.error('Exception in listener set by ' + | |
66 'chrome.webstore.setInstallStageListener: ' + | |
67 e.message + ', ' + e.stack); | |
68 } | |
69 }; | |
70 | |
71 Installer.prototype.setDownloadProgressListener = function(callback) { | |
72 if (webstoreNatives.ValidateDownloadProgressListener(callback)) | |
73 this._downloadProgressListener = callback; | |
74 }; | |
75 | |
76 Installer.prototype.onDownloadProgress = function(progress) { | |
77 if (!this._downloadProgressListener) | |
78 return; // This could happen if we removed the listener mid-flight. | |
79 | |
80 try { | |
81 this._downloadProgressListener(progress); | |
82 } catch (e) { | |
83 console.error('Exception in listener set by ' + | |
84 'chrome.webstore.setDownloadProgressListener: ' + | |
85 e.message + ', ' + e.stack); | |
86 } | |
87 }; | |
88 | |
46 var installer = new Installer(); | 89 var installer = new Installer(); |
47 | 90 |
48 var chromeWebstore = { | 91 var chromeWebstore = { |
49 install: function install(url, onSuccess, onFailure) { | 92 install: function (url, onSuccess, onFailure) { |
50 installer.install(url, onSuccess, onFailure); | 93 installer.install(url, onSuccess, onFailure); |
94 }, | |
95 setInstallStageListener: function (callback) { | |
96 installer.setInstallStageListener(callback); | |
97 }, | |
98 setDownloadProgressListener: function (callback) { | |
99 installer.setDownloadProgressListener(callback); | |
51 } | 100 } |
not at google - send to devlin
2014/02/27 21:49:56
see comment in the JSON which applies here too. yo
Devlin
2014/02/28 18:04:51
Done.
| |
52 }; | 101 }; |
53 | 102 |
54 // Called by webstore_binding.cc. | 103 // This must match the name in InstallWebstoreBindings in |
55 function onInstallResponse(installId, success, error) { | |
56 installer.onInstallResponse(installId, success, error); | |
57 } | |
58 | |
59 // These must match the names in InstallWebstorebinding in | |
60 // chrome/renderer/extensions/dispatcher.cc. | 104 // chrome/renderer/extensions/dispatcher.cc. |
61 exports.chromeWebstore = chromeWebstore; | 105 exports.chromeWebstore = chromeWebstore; |
62 exports.onInstallResponse = onInstallResponse; | 106 |
107 // Called by webstore_binding.cc. | |
108 exports.onInstallResponse = | |
109 Installer.prototype.onInstallResponse.bind(installer); | |
110 exports.onInstallStageChanged = | |
111 Installer.prototype.onInstallStageChanged.bind(installer); | |
112 exports.onDownloadProgress = | |
113 Installer.prototype.onDownloadProgress.bind(installer); | |
OLD | NEW |