summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/resources/extensions/webstore_custom_bindings.js
blob: 19c8f0fbacf33aa3d539e383511191155f03a840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Custom bindings for the webstore API.

var webstoreNatives = requireNative('webstore');

function Installer() {
  this._pendingInstall = null;
}

Installer.prototype.install = function(url, onSuccess, onFailure) {
  if (this._pendingInstall) {
    throw 'A Chrome Web Store installation is already pending.';
  }
  var installId = webstoreNatives.Install(url, onSuccess, onFailure);
  if (installId !== undefined) {
    this._pendingInstall = {
      installId: installId,
      onSuccess: onSuccess,
      onFailure: onFailure
    };
  }
};

Installer.prototype.onInstallResponse = function(installId, success, error) {
  var pendingInstall = this._pendingInstall;
  if (!pendingInstall || pendingInstall.installId != installId) {
    // TODO(kalman): should this be an error?
    return;
  }

  try {
    if (success && pendingInstall.onSuccess)
      pendingInstall.onSuccess();
    else if (!success && pendingInstall.onFailure)
      pendingInstall.onFailure(error);
  } finally {
    this._pendingInstall = null;
  }
};

var installer = new Installer();

var chromeWebstore = {
  install: function install(url, onSuccess, onFailure) {
    installer.install(url, onSuccess, onFailure);
  }
};

// Called by webstore_bindings.cc.
var chromeHiddenWebstore = {
  onInstallResponse: function(installId, success, error) {
    installer.onInstallResponse(installId, success, error);
  }
};

// These must match the names in InstallWebstoreBindings in
// chrome/renderer/extensions/dispatcher.cc.
exports.chromeWebstore = chromeWebstore;
exports.chromeHiddenWebstore = chromeHiddenWebstore;