1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Manage the installation of apps.
8 * @param {string} itemId Item id to be installed.
9 * @param {!CWSWidgetContainer.PlatformDelegate} delegate Delegate for accessing
10 * Chrome platform APIs.
14 function AppInstaller(itemId, delegate) {
15 /** @private {!CWSWidgetContainer.PlatformDelegate} */
16 this.delegate_ = delegate;
17 this.itemId_ = itemId;
18 this.callback_ = null;
27 AppInstaller.Result = {
28 SUCCESS: 'AppInstaller.success',
29 CANCELLED: 'AppInstaller.cancelled',
30 ERROR: 'AppInstaller.error'
32 Object.freeze(AppInstaller.Result);
35 * Error message for user cancellation. This must be match with the constant
36 * 'kUserCancelledError' in C/B/extensions/webstore_standalone_installer.cc.
41 AppInstaller.USER_CANCELLED_ERROR_STR_ = 'User cancelled install';
44 * Start an installation.
45 * @param {function(AppInstaller.Result, string)} callback Called when the
46 * installation is finished.
48 AppInstaller.prototype.install = function(callback) {
49 this.callback_ = callback;
50 this.delegate_.installWebstoreItem(
52 this.onInstallCompleted_.bind(this));
56 * Prevents {@code this.callback_} from being called.
58 AppInstaller.prototype.cancel = function() {
59 // TODO(tbarzic): Would it make sense to uninstall the app on success if the
60 // app instaler is cancelled instead of just invalidating the callback?
61 this.callback_ = null;
65 * Called when the installation is completed.
67 * @param {?string} error Null if the installation is success,
68 * otherwise error message.
71 AppInstaller.prototype.onInstallCompleted_ = function(error) {
75 var installerResult = AppInstaller.Result.SUCCESS;
78 error == AppInstaller.USER_CANCELLED_ERROR_STR_ ?
79 AppInstaller.Result.CANCELLED :
80 AppInstaller.Result.ERROR;
82 this.callback_(installerResult, error || '');
83 this.callback_ = null;