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.
4
5'use strict';
6
7/**
8 * Manage the installation of apps.
9 *
10 * @param {string} itemId Item id to be installed.
11 * @constructor
12 * @extends {cr.EventType}
13 */
14function AppInstaller(itemId) {
15  this.itemId_ = itemId;
16  this.callback_ = null;
17
18  Object.seal(this);
19}
20
21AppInstaller.prototype = {
22};
23
24/**
25 * Type of result.
26 *
27 * @enum {string}
28 * @const
29 */
30AppInstaller.Result = {
31  SUCCESS: 'AppInstaller.success',
32  CANCELLED: 'AppInstaller.cancelled',
33  ERROR: 'AppInstaller.error'
34};
35Object.freeze(AppInstaller.Result);
36
37/**
38 * Error message for user cancellation. This must be match with the constant
39 * 'kUserCancelledError' in C/B/extensions/webstore_standalone_installer.cc.
40 * @type {string}
41 * @const
42 * @private
43 */
44AppInstaller.USER_CANCELLED_ERROR_STR_ = 'User cancelled install';
45
46/**
47 * Start an installation.
48 * @param {function(boolean, string)} callback Called when the installation is
49 *     finished.
50 */
51AppInstaller.prototype.install = function(callback) {
52  this.callback_ = callback;
53  chrome.fileBrowserPrivate.installWebstoreItem(
54      this.itemId_,
55      function() {
56        this.onInstallCompleted_(chrome.runtime.lastError);
57      }.bind(this));
58};
59
60/**
61 * Called when the installation is completed.
62 *
63 * @param {{message: string}?} error Null if the installation is success,
64 *     otherwise an object which contains error message.
65 * @private
66 */
67AppInstaller.prototype.onInstallCompleted_ = function(error) {
68  var installerResult = AppInstaller.Result.SUCCESS;
69  var errorMessage = '';
70  if (error) {
71    installerResult =
72        error.message == AppInstaller.USER_CANCELLED_ERROR_STR_ ?
73        AppInstaller.Result.CANCELLED :
74        AppInstaller.Result.ERROR;
75    errorMessage = error.message;
76  }
77  this.callback_(installerResult, errorMessage);
78  this.callback_ = null;
79};
80