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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'chrome://resources/cr_elements/cr_button/cr_button.js';
import 'chrome://resources/cr_elements/cr_icon_button/cr_icon_button.js';
import 'chrome://resources/cr_elements/cr_icons.css.js';
import 'chrome://resources/cr_elements/cr_toggle/cr_toggle.js';
import 'chrome://resources/cr_elements/cr_hidden_style.css.js';
import 'chrome://resources/cr_elements/icons.html.js';
import 'chrome://resources/cr_elements/cr_shared_style.css.js';
import 'chrome://resources/cr_elements/cr_shared_vars.css.js';
import 'chrome://resources/js/action_link.js';
import 'chrome://resources/cr_elements/action_link.css.js';
import './icons.html.js';
import './shared_style.css.js';
import './shared_vars.css.js';
import './strings.m.js';
import 'chrome://resources/polymer/v3_0/iron-flex-layout/iron-flex-layout-classes.js';
import 'chrome://resources/polymer/v3_0/iron-icon/iron-icon.js';
import 'chrome://resources/polymer/v3_0/paper-tooltip/paper-tooltip.js';
import {ChromeEvent} from '/tools/typescript/definitions/chrome_event.js';
import {getToastManager} from 'chrome://resources/cr_elements/cr_toast/cr_toast_manager.js';
import {CrToggleElement} from 'chrome://resources/cr_elements/cr_toggle/cr_toggle.js';
import {assert, assertNotReached} from 'chrome://resources/js/assert_ts.js';
import {I18nMixin} from 'chrome://resources/cr_elements/i18n_mixin.js';
import {flush, PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {getTemplate} from './item.html.js';
import {ItemMixin} from './item_mixin.js';
import {computeInspectableViewLabel, EnableControl, getEnableControl, getItemSource, getItemSourceString, isEnabled, sortViews, SourceType, userCanChangeEnablement} from './item_util.js';
import {navigation, Page} from './navigation_helper.js';
export interface ItemDelegate {
deleteItem(id: string): void;
setItemEnabled(id: string, isEnabled: boolean): void;
setItemAllowedIncognito(id: string, isAllowedIncognito: boolean): void;
setItemAllowedOnFileUrls(id: string, isAllowedOnFileUrls: boolean): void;
setItemHostAccess(id: string, hostAccess: chrome.developerPrivate.HostAccess):
void;
setItemCollectsErrors(id: string, collectsErrors: boolean): void;
inspectItemView(id: string, view: chrome.developerPrivate.ExtensionView):
void;
openUrl(url: string): void;
reloadItem(id: string): Promise<void>;
repairItem(id: string): void;
showItemOptionsPage(extension: chrome.developerPrivate.ExtensionInfo): void;
showInFolder(id: string): void;
getExtensionSize(id: string): Promise<string>;
addRuntimeHostPermission(id: string, host: string): Promise<void>;
removeRuntimeHostPermission(id: string, host: string): Promise<void>;
setShowAccessRequestsInToolbar(id: string, showRequests: boolean): void;
// TODO(tjudkins): This function is not specific to items, so should be pulled
// out to a more generic place when we need to access it from elsewhere.
recordUserAction(metricName: string): void;
getItemStateChangedTarget():
ChromeEvent<(data: chrome.developerPrivate.EventData) => void>;
}
export interface ExtensionsItemElement {
$: {
a11yAssociation: HTMLElement,
detailsButton: HTMLElement,
enableToggle: CrToggleElement,
name: HTMLElement,
removeButton: HTMLElement,
};
}
const ExtensionsItemElementBase = I18nMixin(ItemMixin(PolymerElement));
export class ExtensionsItemElement extends ExtensionsItemElementBase {
static get is() {
return 'extensions-item';
}
static get template() {
return getTemplate();
}
static get properties() {
return {
// The item's delegate, or null.
delegate: Object,
// Whether or not dev mode is enabled.
inDevMode: {
type: Boolean,
value: false,
},
// The underlying ExtensionInfo itself. Public for use in declarative
// bindings.
data: Object,
// Whether or not the expanded view of the item is shown.
showingDetails_: {
type: Boolean,
value: false,
},
// First inspectable view after sorting.
firstInspectView_: {
type: Object,
computed: 'computeFirstInspectView_(data.views)',
},
};
}
static get observers() {
return ['observeIdVisibility_(inDevMode, showingDetails_, data.id)'];
}
delegate: ItemDelegate;
inDevMode: boolean;
data: chrome.developerPrivate.ExtensionInfo;
private showingDetails_: boolean;
private firstInspectView_: chrome.developerPrivate.ExtensionView;
/** Prevents reloading the same item while it's already being reloaded. */
private isReloading_: boolean = false;
private fire_(eventName: string, detail?: any) {
this.dispatchEvent(
new CustomEvent(eventName, {bubbles: true, composed: true, detail}));
}
getDetailsButton() {
return this.$.detailsButton;
}
/** @return The "Errors" button, if it exists. */
getErrorsButton(): HTMLElement|null {
return this.shadowRoot!.querySelector('#errors-button');
}
private observeIdVisibility_() {
flush();
const idElement = this.shadowRoot!.querySelector('#extension-id');
if (idElement) {
assert(this.data);
idElement.textContent = this.i18n('itemId', this.data.id);
}
}
private shouldShowErrorsButton_(): boolean {
// When the error console is disabled (happens when
// --disable-error-console command line flag is used or when in the
// Stable/Beta channel), |installWarnings| is populated.
if (this.data.installWarnings && this.data.installWarnings.length > 0) {
return true;
}
// When error console is enabled |installedWarnings| is not populated.
// Instead |manifestErrors| and |runtimeErrors| are used.
return this.data.manifestErrors.length > 0 ||
this.data.runtimeErrors.length > 0;
}
private onRemoveTap_() {
this.delegate.deleteItem(this.data.id);
}
private onEnableToggleChange_() {
this.delegate.setItemEnabled(this.data.id, this.$.enableToggle.checked);
this.$.enableToggle.checked = this.isEnabled_();
}
private onErrorsTap_() {
if (this.data.installWarnings && this.data.installWarnings.length > 0) {
this.fire_('show-install-warnings', this.data.installWarnings);
return;
}
navigation.navigateTo({page: Page.ERRORS, extensionId: this.data.id});
}
private onDetailsTap_() {
navigation.navigateTo({page: Page.DETAILS, extensionId: this.data.id});
}
private computeFirstInspectView_(): chrome.developerPrivate.ExtensionView {
return sortViews(this.data.views)[0];
}
private onInspectTap_() {
this.delegate.inspectItemView(this.data.id, this.firstInspectView_);
}
private onExtraInspectTap_() {
navigation.navigateTo({page: Page.DETAILS, extensionId: this.data.id});
}
private onReloadTap_() {
// Don't reload if in the middle of an update.
if (this.isReloading_) {
return;
}
this.isReloading_ = true;
const toastManager = getToastManager();
// Keep the toast open indefinitely.
toastManager.duration = 0;
toastManager.show(this.i18n('itemReloading'));
this.delegate.reloadItem(this.data.id)
.then(
() => {
toastManager.hide();
toastManager.duration = 3000;
toastManager.show(this.i18n('itemReloaded'));
this.isReloading_ = false;
},
loadError => {
this.fire_('load-error', loadError);
toastManager.hide();
this.isReloading_ = false;
});
}
private onRepairTap_() {
this.delegate.repairItem(this.data.id);
}
private isEnabled_(): boolean {
return isEnabled(this.data.state);
}
private isEnableToggleEnabled_(): boolean {
return userCanChangeEnablement(this.data);
}
/** @return Whether the reload button should be shown. */
private showReloadButton_(): boolean {
return getEnableControl(this.data) === EnableControl.RELOAD;
}
/** @return Whether the repair button should be shown. */
private showRepairButton_(): boolean {
return getEnableControl(this.data) === EnableControl.REPAIR;
}
/** @return Whether the enable toggle should be shown. */
private showEnableToggle_(): boolean {
return getEnableControl(this.data) === EnableControl.ENABLE_TOGGLE;
}
private computeClasses_(): string {
let classes = this.isEnabled_() ? 'enabled' : 'disabled';
if (this.inDevMode) {
classes += ' dev-mode';
}
return classes;
}
private computeSourceIndicatorIcon_(): string {
switch (getItemSource(this.data)) {
case SourceType.POLICY:
return 'extensions-icons:business';
case SourceType.SIDELOADED:
return 'extensions-icons:input';
case SourceType.UNKNOWN:
// TODO(dpapad): Ask UX for a better icon for this case.
return 'extensions-icons:input';
case SourceType.UNPACKED:
return 'extensions-icons:unpacked';
case SourceType.WEBSTORE:
case SourceType.INSTALLED_BY_DEFAULT:
return '';
default:
assertNotReached();
}
}
private computeSourceIndicatorText_(): string {
if (this.data.locationText) {
return this.data.locationText;
}
const sourceType = getItemSource(this.data);
return sourceType === SourceType.WEBSTORE ? '' :
getItemSourceString(sourceType);
}
private computeInspectViewsHidden_(): boolean {
return !this.data.views || this.data.views.length === 0;
}
private computeFirstInspectTitle_(): string {
// Note: theoretically, this wouldn't be called without any inspectable
// views (because it's in a dom-if="!computeInspectViewsHidden_()").
// However, due to the recycling behavior of iron list, it seems that
// sometimes it can. Even when it is, the UI behaves properly, but we
// need to handle the case gracefully.
return this.data.views.length > 0 ?
computeInspectableViewLabel(this.firstInspectView_) :
'';
}
private computeFirstInspectLabel_(): string {
const label = this.computeFirstInspectTitle_();
return label && this.data.views.length > 1 ? label + ',' : label;
}
private computeExtraViewsHidden_(): boolean {
return this.data.views.length <= 1;
}
private computeDevReloadButtonHidden_(): boolean {
// Only display the reload spinner if the extension is unpacked and
// enabled or disabled for reload. If an extension fails to reload (due to
// e.g. a parsing error), it will
// remain disabled with the "reloading" reason. We show the reload button
// when it's disabled for reload to enable developers to reload the fixed
// version. (Note that trying to reload an extension that is currently
// trying to reload is a no-op.) For other
// disableReasons, there's no point in reloading a disabled extension, and
// we'll show a crashed reload button if it's terminated.
const showIcon =
this.data.location === chrome.developerPrivate.Location.UNPACKED &&
(this.data.state === chrome.developerPrivate.ExtensionState.ENABLED ||
this.data.disableReasons.reloading);
return !showIcon;
}
private computeExtraInspectLabel_(): string {
return this.i18n(
'itemInspectViewsExtra', (this.data.views.length - 1).toString());
}
private hasSevereWarnings_(): boolean {
return this.data.disableReasons.corruptInstall ||
this.data.disableReasons.suspiciousInstall ||
this.data.runtimeWarnings.length > 0 || !!this.data.blacklistText;
}
private showDescription_(): boolean {
return !this.hasSevereWarnings_() &&
!this.data.showSafeBrowsingAllowlistWarning;
}
private showAllowlistWarning_(): boolean {
// Only show the allowlist warning if there are no other warnings. The item
// card has a fixed height and the content might get cropped if too many
// warnings are displayed. This should be a rare edge case and the allowlist
// warning will still be shown in the item detail view.
return this.data.showSafeBrowsingAllowlistWarning &&
!this.hasSevereWarnings_();
}
}
declare global {
interface HTMLElementTagNameMap {
'extensions-item': ExtensionsItemElement;
}
}
customElements.define(ExtensionsItemElement.is, ExtensionsItemElement);
|