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
361
362
363
364
365
366
367
368
369
|
// Copyright 2013 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.
cr.define('extensions', function() {
'use strict';
/**
* Returns whether or not a given |url| is associated with an extension.
* @param {string} url The url to examine.
* @param {string} extensionUrl The url of the extension.
* @return {boolean} Whether or not the url is associated with the extension.
*/
function isExtensionUrl(url, extensionUrl) {
return url.substring(0, extensionUrl.length) == extensionUrl;
}
/**
* Get the url relative to the main extension url. If the url is
* unassociated with the extension, this will be the full url.
* @param {string} url The url to make relative.
* @param {string} extensionUrl The host for which the url is relative.
* @return {string} The url relative to the host.
*/
function getRelativeUrl(url, extensionUrl) {
return isExtensionUrl(url, extensionUrl) ?
url.substring(extensionUrl.length) : url;
}
/**
* Clone a template within the extension error template collection.
* @param {string} templateName The class name of the template to clone.
* @return {HTMLElement} The clone of the template.
*/
function cloneTemplate(templateName) {
return $('template-collection-extension-error').
querySelector('.' + templateName).cloneNode(true);
}
/**
* Creates a new ExtensionError HTMLElement; this is used to show a
* notification to the user when an error is caused by an extension.
* @param {Object} error The error the element should represent.
* @param {string} templateName The name of the template to clone for the
* error ('extension-error-[detailed|simple]-wrapper').
* @constructor
* @extends {HTMLDivElement}
*/
function ExtensionError(error, templateName) {
var div = cloneTemplate(templateName);
div.__proto__ = ExtensionError.prototype;
div.error_ = error;
div.decorate();
return div;
}
ExtensionError.prototype = {
__proto__: HTMLDivElement.prototype,
/** @override */
decorate: function() {
var metadata = cloneTemplate('extension-error-metadata');
// Add an additional class for the severity level.
if (this.error_.level == 0)
metadata.classList.add('extension-error-severity-info');
else if (this.error_.level == 1)
metadata.classList.add('extension-error-severity-warning');
else
metadata.classList.add('extension-error-severity-fatal');
var iconNode = document.createElement('img');
iconNode.className = 'extension-error-icon';
metadata.insertBefore(iconNode, metadata.firstChild);
// Add a property for the extension's base url in order to determine if
// a url belongs to the extension.
this.extensionUrl_ =
'chrome-extension://' + this.error_.extensionId + '/';
metadata.querySelector('.extension-error-message').textContent =
this.error_.message;
metadata.appendChild(this.createViewSourceAndInspect_(
getRelativeUrl(this.error_.source, this.extensionUrl_),
this.error_.source));
// The error template may specify a <summary> to put template metadata in.
// If not, just append it to the top-level element.
var metadataContainer = this.querySelector('summary') || this;
metadataContainer.appendChild(metadata);
var detailsNode = this.querySelector('.extension-error-details');
if (detailsNode && this.error_.contextUrl)
detailsNode.appendChild(this.createContextNode_());
if (detailsNode && this.error_.stackTrace) {
var stackNode = this.createStackNode_();
if (stackNode)
detailsNode.appendChild(this.createStackNode_());
}
},
/**
* Return a div with text |description|. If it's possible to view the source
* for |url|, linkify the div to do so. Attach an inspect button if it's
* possible to open the inspector for |url|.
* @param {string} description a human-friendly description the location
* (e.g., filename, line).
* @param {string} url The url of the resource to view.
* @param {?number} line An optional line number of the resource.
* @param {?number} column An optional column number of the resource.
* @return {HTMLElement} The created node, either a link or plaintext.
* @private
*/
createViewSourceAndInspect_: function(description, url, line, column) {
var errorLinks = document.createElement('div');
errorLinks.className = 'extension-error-links';
if (this.error_.canInspect)
errorLinks.appendChild(this.createInspectLink_(url, line, column));
if (this.canViewSource_(url))
var viewSource = this.createViewSourceLink_(url, line);
else
var viewSource = document.createElement('div');
viewSource.className = 'extension-error-view-source';
viewSource.textContent = description;
errorLinks.appendChild(viewSource);
return errorLinks;
},
/**
* Determine whether we can view the source of a given url.
* @param {string} url The url of the resource to view.
* @return {boolean} Whether or not we can view the source for the url.
* @private
*/
canViewSource_: function(url) {
return isExtensionUrl(url, this.extensionUrl_) || url == 'manifest.json';
},
/**
* Determine whether or not we should display the url to the user. We don't
* want to include any of our own code in stack traces.
* @param {string} url The url in question.
* @return {boolean} True if the url should be displayed, and false
* otherwise (i.e., if it is an internal script).
*/
shouldDisplayForUrl_: function(url) {
var extensionsNamespace = 'extensions::';
// All our internal scripts are in the 'extensions::' namespace.
return url.substr(0, extensionsNamespace.length) != extensionsNamespace;
},
/**
* Create a clickable node to view the source for the given url.
* @param {string} url The url to the resource to view.
* @param {?number} line An optional line number of the resource (for
* source files).
* @return {HTMLElement} The clickable node to view the source.
* @private
*/
createViewSourceLink_: function(url, line) {
var viewSource = document.createElement('a');
viewSource.href = 'javascript:void(0)';
var relativeUrl = getRelativeUrl(url, this.extensionUrl_);
var requestFileSourceArgs = { 'extensionId': this.error_.extensionId,
'message': this.error_.message,
'pathSuffix': relativeUrl };
if (relativeUrl == 'manifest.json') {
requestFileSourceArgs.manifestKey = this.error_.manifestKey;
requestFileSourceArgs.manifestSpecific = this.error_.manifestSpecific;
} else {
// Prefer |line| if available, or default to the line of the last stack
// frame.
requestFileSourceArgs.lineNumber =
line ? line : this.getLastPosition_('lineNumber');
}
viewSource.addEventListener('click', function(e) {
chrome.send('extensionErrorRequestFileSource', [requestFileSourceArgs]);
});
viewSource.title = loadTimeData.getString('extensionErrorViewSource');
return viewSource;
},
/**
* Check the most recent stack frame to get the last position in the code.
* @param {string} type The position type, i.e. '[line|column]Number'.
* @return {?number} The last position of the given |type|, or undefined if
* there is no stack trace to check.
* @private
*/
getLastPosition_: function(type) {
var stackTrace = this.error_.stackTrace;
return stackTrace && stackTrace[0] ? stackTrace[0][type] : undefined;
},
/**
* Create an "Inspect" link, in the form of an icon.
* @param {?string} url The url of the resource to inspect; if absent, the
* render view (and no particular resource) is inspected.
* @param {?number} line An optional line number of the resource.
* @param {?number} column An optional column number of the resource.
* @return {HTMLImageElement} The created "Inspect" link for the resource.
* @private
*/
createInspectLink_: function(url, line, column) {
var linkWrapper = document.createElement('a');
linkWrapper.href = 'javascript:void(0)';
var inspectIcon = document.createElement('img');
inspectIcon.className = 'extension-error-inspect';
inspectIcon.title = loadTimeData.getString('extensionErrorInspect');
inspectIcon.addEventListener('click', function(e) {
chrome.send('extensionErrorOpenDevTools',
[{'renderProcessId': this.error_.renderProcessId,
'renderViewId': this.error_.renderViewId,
'url': url,
'lineNumber': line ? line :
this.getLastPosition_('lineNumber'),
'columnNumber': column ? column :
this.getLastPosition_('columnNumber')}]);
}.bind(this));
linkWrapper.appendChild(inspectIcon);
return linkWrapper;
},
/**
* Get the context node for this error. This will attempt to link to the
* context in which the error occurred, and can be either an extension page
* or an external page.
* @return {HTMLDivElement} The context node for the error, including the
* label and a link to the context.
* @private
*/
createContextNode_: function() {
var node = cloneTemplate('extension-error-context-wrapper');
var linkNode = node.querySelector('a');
if (isExtensionUrl(this.error_.contextUrl, this.extensionUrl_)) {
linkNode.textContent = getRelativeUrl(this.error_.contextUrl,
this.extensionUrl_);
} else {
linkNode.textContent = this.error_.contextUrl;
}
// Prepend a link to inspect the context page, if possible.
if (this.error_.canInspect)
node.insertBefore(this.createInspectLink_(), linkNode);
linkNode.href = this.error_.contextUrl;
linkNode.target = '_blank';
return node;
},
/**
* Get a node for the stack trace for this error. Each stack frame will
* include a resource url, line number, and function name (possibly
* anonymous). If possible, these frames will also be linked for viewing the
* source and inspection.
* @return {HTMLDetailsElement} The stack trace node for this error, with
* all stack frames nested in a details-summary object.
* @private
*/
createStackNode_: function() {
var node = cloneTemplate('extension-error-stack-trace');
var listNode = node.querySelector('.extension-error-stack-trace-list');
this.error_.stackTrace.forEach(function(frame) {
if (!this.shouldDisplayForUrl_(frame.url))
return;
var frameNode = document.createElement('div');
var description = getRelativeUrl(frame.url, this.extensionUrl_) +
':' + frame.lineNumber;
if (frame.functionName) {
var functionName = frame.functionName == '(anonymous function)' ?
loadTimeData.getString('extensionErrorAnonymousFunction') :
frame.functionName;
description += ' (' + functionName + ')';
}
frameNode.appendChild(this.createViewSourceAndInspect_(
description, frame.url, frame.lineNumber, frame.columnNumber));
listNode.appendChild(
document.createElement('li')).appendChild(frameNode);
}, this);
if (listNode.childElementCount == 0)
return undefined;
return node;
},
};
/**
* A variable length list of runtime or manifest errors for a given extension.
* @param {Array.<Object>} errors The list of extension errors with which
* to populate the list.
* @param {string} title The i18n key for the title of the error list, i.e.
* 'extensionErrors[Manifest,Runtime]Errors'.
* @constructor
* @extends {HTMLDivElement}
*/
function ExtensionErrorList(errors, title) {
var div = cloneTemplate('extension-error-list');
div.__proto__ = ExtensionErrorList.prototype;
div.errors_ = errors;
div.title_ = title;
div.decorate();
return div;
}
ExtensionErrorList.prototype = {
__proto__: HTMLDivElement.prototype,
/**
* @private
* @const
* @type {number}
*/
MAX_ERRORS_TO_SHOW_: 3,
/** @override */
decorate: function() {
this.querySelector('.extension-error-list-title').textContent =
loadTimeData.getString(this.title_);
this.contents_ = this.querySelector('.extension-error-list-contents');
this.errors_.forEach(function(error) {
this.contents_.appendChild(document.createElement('li')).appendChild(
new ExtensionError(error,
error.contextUrl || error.stackTrace ?
'extension-error-detailed-wrapper' :
'extension-error-simple-wrapper'));
}, this);
if (this.contents_.children.length > this.MAX_ERRORS_TO_SHOW_) {
for (var i = this.MAX_ERRORS_TO_SHOW_;
i < this.contents_.children.length; ++i) {
this.contents_.children[i].hidden = true;
}
this.initShowMoreButton_();
}
},
/**
* Initialize the "Show More" button for the error list. If there are more
* than |MAX_ERRORS_TO_SHOW_| errors in the list.
* @private
*/
initShowMoreButton_: function() {
var button = this.querySelector('.extension-error-list-show-more a');
button.hidden = false;
button.isShowingAll = false;
button.addEventListener('click', function(e) {
for (var i = this.MAX_ERRORS_TO_SHOW_;
i < this.contents_.children.length; ++i) {
this.contents_.children[i].hidden = button.isShowingAll;
}
var message = button.isShowingAll ? 'extensionErrorsShowMore' :
'extensionErrorsShowFewer';
button.textContent = loadTimeData.getString(message);
button.isShowingAll = !button.isShowingAll;
}.bind(this));
}
};
return {
ExtensionErrorList: ExtensionErrorList
};
});
|