| 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 |
| 9 function Installer() { | 10 function Installer() { |
| 10 this._pendingInstall = null; | 11 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}); |
| 11 } | 16 } |
| 12 | 17 |
| 13 Installer.prototype.install = function(url, onSuccess, onFailure) { | 18 Installer.prototype.install = function(url, onSuccess, onFailure) { |
| 14 if (this._pendingInstall) | 19 if (this._pendingInstall) |
| 15 throw 'A Chrome Web Store installation is already pending.'; | 20 throw new Error('A Chrome Web Store installation is already pending.'); |
| 16 var installId = webstoreNatives.Install(url, onSuccess, onFailure); | 21 if (url !== undefined && typeof(url) !== 'string') { |
| 22 throw new Error( |
| 23 'The Chrome Web Store item link URL parameter must be a string.'); |
| 24 } |
| 25 if (onSuccess !== undefined && typeof(onSuccess) !== 'function') |
| 26 throw new Error('The success callback parameter must be a function.'); |
| 27 if (onFailure !== undefined && typeof(onFailure) !== 'function') |
| 28 throw new Error('The failure callback parameter must be a function.'); |
| 29 |
| 30 // 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 |
| 32 // noted in the Event documentation in |
| 33 // chrome/common/extensions/api/webstore.json). |
| 34 var installId = webstoreNatives.Install( |
| 35 this.onInstallStageChanged.hasListeners(), |
| 36 this.onDownloadProgress.hasListeners(), |
| 37 url, |
| 38 onSuccess, |
| 39 onFailure); |
| 17 if (installId !== undefined) { | 40 if (installId !== undefined) { |
| 18 this._pendingInstall = { | 41 this._pendingInstall = { |
| 19 installId: installId, | 42 installId: installId, |
| 20 onSuccess: onSuccess, | 43 onSuccess: onSuccess, |
| 21 onFailure: onFailure | 44 onFailure: onFailure |
| 22 }; | 45 }; |
| 23 } | 46 } |
| 24 }; | 47 }; |
| 25 | 48 |
| 26 Installer.prototype.onInstallResponse = function(installId, success, error) { | 49 Installer.prototype.onInstallResponse = function(installId, success, error) { |
| 27 var pendingInstall = this._pendingInstall; | 50 var pendingInstall = this._pendingInstall; |
| 28 if (!pendingInstall || pendingInstall.installId != installId) { | 51 if (!pendingInstall || pendingInstall.installId != installId) { |
| 29 // TODO(kalman): should this be an error? | 52 // TODO(kalman): should this be an error? |
| 30 return; | 53 return; |
| 31 } | 54 } |
| 32 | 55 |
| 33 try { | 56 try { |
| 34 if (success && pendingInstall.onSuccess) | 57 if (success && pendingInstall.onSuccess) |
| 35 pendingInstall.onSuccess(); | 58 pendingInstall.onSuccess(); |
| 36 else if (!success && pendingInstall.onFailure) | 59 else if (!success && pendingInstall.onFailure) |
| 37 pendingInstall.onFailure(error); | 60 pendingInstall.onFailure(error); |
| 38 } catch (e) { | 61 } catch (e) { |
| 39 console.error('Exception in chrome.webstore.install response handler: ' + | 62 console.error('Exception in chrome.webstore.install response handler: ' + |
| 40 e.stack); | 63 e.stack); |
| 41 } finally { | 64 } finally { |
| 42 this._pendingInstall = null; | 65 this._pendingInstall = null; |
| 43 } | 66 } |
| 44 }; | 67 }; |
| 45 | 68 |
| 69 Installer.prototype.onInstallStageChanged = function(installStage) { |
| 70 this.onInstallStageChanged.dispatch(installStage); |
| 71 }; |
| 72 |
| 73 Installer.prototype.onDownloadProgress = function(progress) { |
| 74 this.onDownloadProgress.dispatch(progress); |
| 75 }; |
| 76 |
| 46 var installer = new Installer(); | 77 var installer = new Installer(); |
| 47 | 78 |
| 48 var chromeWebstore = { | 79 var chromeWebstore = { |
| 49 install: function install(url, onSuccess, onFailure) { | 80 install: function (url, onSuccess, onFailure) { |
| 50 installer.install(url, onSuccess, onFailure); | 81 installer.install(url, onSuccess, onFailure); |
| 51 } | 82 }, |
| 83 onInstallStageChanged: installer.onInstallStageChanged, |
| 84 onDownloadProgress: installer.onDownloadProgress |
| 52 }; | 85 }; |
| 53 | 86 |
| 54 // Called by webstore_binding.cc. | 87 // 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. | 88 // chrome/renderer/extensions/dispatcher.cc. |
| 61 exports.chromeWebstore = chromeWebstore; | 89 exports.chromeWebstore = chromeWebstore; |
| 62 exports.onInstallResponse = onInstallResponse; | 90 |
| 91 // Called by webstore_bindings.cc. |
| 92 exports.onInstallResponse = |
| 93 Installer.prototype.onInstallResponse.bind(installer); |
| 94 exports.onInstallStageChanged = |
| 95 Installer.prototype.onInstallStageChanged.bind(installer); |
| 96 exports.onDownloadProgress = |
| 97 Installer.prototype.onDownloadProgress.bind(installer); |
| OLD | NEW |