summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/extensions/item_util.js
blob: b619d2cdb8ed52a153b0f740db76786970996233 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2017 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.

// Closure compiler won't let this be declared inside cr.define().
/** @enum {string} */
const SourceType = {
  WEBSTORE: 'webstore',
  POLICY: 'policy',
  SIDELOADED: 'sideloaded',
  UNPACKED: 'unpacked',
  UNKNOWN: 'unknown',
};

cr.define('extensions', function() {
  /**
   * Returns true if the extension is enabled, including terminated
   * extensions.
   * @param {!chrome.developerPrivate.ExtensionState} state
   * @return {boolean}
   */
  function isEnabled(state) {
    switch (state) {
      case chrome.developerPrivate.ExtensionState.ENABLED:
      case chrome.developerPrivate.ExtensionState.TERMINATED:
        return true;
      case chrome.developerPrivate.ExtensionState.BLACKLISTED:
      case chrome.developerPrivate.ExtensionState.DISABLED:
        return false;
    }
    assertNotReached();
  }

  /**
   * @param {!chrome.developerPrivate.ExtensionInfo} extensionInfo
   * @return {boolean} Whether the extension is controlled.
   */
  function isControlled(extensionInfo) {
    return !!extensionInfo.controlledInfo;
  }

  /**
   * Returns true if the user can change whether or not the extension is
   * enabled.
   * @param {!chrome.developerPrivate.ExtensionInfo} item
   * @return {boolean}
   */
  function userCanChangeEnablement(item) {
    // User doesn't have permission.
    if (!item.userMayModify) {
      return false;
    }
    // Item is forcefully disabled.
    if (item.disableReasons.corruptInstall ||
        item.disableReasons.suspiciousInstall ||
        item.disableReasons.updateRequired) {
      return false;
    }
    // An item with dependent extensions can't be disabled (it would bork the
    // dependents).
    if (item.dependentExtensions.length > 0) {
      return false;
    }
    // Blacklisted can't be enabled, either.
    if (item.state == chrome.developerPrivate.ExtensionState.BLACKLISTED) {
      return false;
    }

    return true;
  }

  /**
   * @param {!chrome.developerPrivate.ExtensionInfo} item
   * @return {SourceType}
   */
  function getItemSource(item) {
    if (item.controlledInfo &&
        item.controlledInfo.type ==
            chrome.developerPrivate.ControllerType.POLICY) {
      return SourceType.POLICY;
    }

    switch (item.location) {
      case chrome.developerPrivate.Location.THIRD_PARTY:
        return SourceType.SIDELOADED;
      case chrome.developerPrivate.Location.UNPACKED:
        return SourceType.UNPACKED;
      case chrome.developerPrivate.Location.UNKNOWN:
        return SourceType.UNKNOWN;
      case chrome.developerPrivate.Location.FROM_STORE:
        return SourceType.WEBSTORE;
    }

    assertNotReached(item.location);
  }

  /**
   * @param {SourceType} source
   * @return {string}
   */
  function getItemSourceString(source) {
    switch (source) {
      case SourceType.POLICY:
        return loadTimeData.getString('itemSourcePolicy');
      case SourceType.SIDELOADED:
        return loadTimeData.getString('itemSourceSideloaded');
      case SourceType.UNPACKED:
        return loadTimeData.getString('itemSourceUnpacked');
      case SourceType.WEBSTORE:
        return loadTimeData.getString('itemSourceWebstore');
      case SourceType.UNKNOWN:
        // Nothing to return. Calling code should use
        // chrome.developerPrivate.ExtensionInfo's |locationText| instead.
        return '';
    }
    assertNotReached();
  }

  /**
   * Computes the human-facing label for the given inspectable view.
   * @param {!chrome.developerPrivate.ExtensionView} view
   * @return {string}
   */
  function computeInspectableViewLabel(view) {
    // Trim the "chrome-extension://<id>/".
    const url = new URL(view.url);
    let label = view.url;
    if (url.protocol == 'chrome-extension:') {
      label = url.pathname.substring(1);
    }
    if (label == '_generated_background_page.html') {
      label = loadTimeData.getString('viewBackgroundPage');
    }
    // Add any qualifiers.
    if (view.incognito) {
      label += ' ' + loadTimeData.getString('viewIncognito');
    }
    if (view.renderProcessId == -1) {
      label += ' ' + loadTimeData.getString('viewInactive');
    }
    if (view.isIframe) {
      label += ' ' + loadTimeData.getString('viewIframe');
    }

    return label;
  }

  return {
    isControlled: isControlled,
    isEnabled: isEnabled,
    userCanChangeEnablement: userCanChangeEnablement,
    getItemSource: getItemSource,
    getItemSourceString: getItemSourceString,
    computeInspectableViewLabel: computeInspectableViewLabel
  };
});