OLD | NEW |
---|---|
(Empty) | |
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 cr.define('extensions', function() { | |
6 'use strict'; | |
7 | |
8 /** | |
9 * Returns whether or not a given |url| is associated with an extension. | |
10 * @param {string} url The url to examine. | |
11 * @param {string} extensionUrl The url of the extension. | |
12 * @return {boolean} Whether or not the url is associated with the extension. | |
13 */ | |
14 function isExtensionUrl(url, extensionUrl) { | |
15 return url.substring(0, extensionUrl.length) == extensionUrl; | |
16 } | |
17 | |
18 /** | |
19 * Get the url relative to the main extension url. If the url is | |
20 * unassociated with the extension, this will be the full url. | |
21 * @param {string} url The url to make relative. | |
22 * @param {string} extensionUrl The host for which the url is relative. | |
23 * @return {string} The url relative to the host. | |
24 */ | |
25 function getRelativeUrl(url, extensionUrl) { | |
26 return isExtensionUrl(url, extensionUrl) ? | |
27 url.substring(extensionUrl.length) : url; | |
28 } | |
29 | |
30 /** | |
31 * Clone a template within the extension error template collection. | |
32 * @param {string} templateName The class name of the template to clone. | |
33 * @return {HTMLElement} The clone of the template. | |
34 */ | |
35 function cloneTemplate(templateName) { | |
36 return $('template-collection-extension-error'). | |
37 querySelector(templateName).cloneNode(true); | |
Dan Beam
2013/08/29 00:24:30
maybe: querySelector('.' + templateName) and remov
Devlin
2013/08/29 21:02:04
Done.
| |
38 } | |
39 | |
40 /** | |
41 * Creates a new ExtensionError HTMLElement. | |
Dan Beam
2013/08/29 00:24:30
^ this is kind of a lacking description. what doe
Devlin
2013/08/29 21:02:04
Done.
| |
42 * @param {Object} error The error the element should represent. | |
43 * @constructor | |
44 * @extends {HTMLDivElement} | |
45 */ | |
46 function ExtensionError(error) { | |
47 var div = document.createElement('div'); | |
48 div.__proto__ = ExtensionError.prototype; | |
49 div.className = 'extension-error-simple-wrapper'; | |
50 div.error_ = error; | |
51 div.decorate(); | |
52 return div; | |
53 } | |
54 | |
55 ExtensionError.prototype = { | |
56 __proto__: HTMLDivElement.prototype, | |
57 | |
58 /** @override */ | |
59 decorate: function() { | |
60 var metadata = cloneTemplate('.extension-error-metadata'); | |
61 | |
62 // Add an additional class for the severity level. | |
63 if (this.error_.level == 0) | |
64 metadata.className += ' extension-error-log'; | |
65 else if (this.error_.level == 1) | |
66 metadata.className += ' extension-error-warn'; | |
67 else | |
68 metadata.className += ' extension-error-error'; | |
69 | |
70 // Add a property for the extension's base url in order to determine if | |
71 // a url belongs to the extension. | |
72 this.extensionUrl_ = | |
73 'chrome-extension://' + this.error_.extensionId + '/'; | |
74 | |
75 metadata.querySelector('.extension-error-message').innerText = | |
76 this.error_.message; | |
77 | |
78 metadata.appendChild(this.getViewSourceOrPlain_( | |
79 '(' + getRelativeUrl(this.error_.source, this.extensionUrl_) + ')', | |
80 this.error_.source)); | |
81 | |
82 this.appendChild(metadata); | |
83 }, | |
84 | |
85 /** | |
86 * If it's possible to view the source for the given |url|, then return a | |
87 * link to do so. Otherwise, return a plain-text node. | |
Dan Beam
2013/08/29 00:24:30
^ is this still returning a text node?
Devlin
2013/08/29 21:02:04
Yeah - it returns a div with just plain text (and
| |
88 * @param {string} description The innerText of the node to create; | |
89 * a human-friendly description the location (e.g., filename, line). | |
Dan Beam
2013/08/29 00:24:30
nit: i think it'd be better to just explain what s
Devlin
2013/08/29 21:02:04
Done.
| |
90 * @param {string} url The url of the resource to view. | |
91 * @return {HTMLElement} The created node, either a link or plaintext. | |
92 * @private | |
93 */ | |
94 getViewSourceOrPlain_: function(description, url) { | |
95 if (this.canViewSource_(url)) { | |
Dan Beam
2013/08/29 00:24:30
nit: no curlies
Devlin
2013/08/29 21:02:04
Done.
| |
96 var node = this.getViewSourceLink_(url); | |
97 } else { | |
98 var node = document.createElement('div'); | |
99 } | |
100 node.className = 'extension-error-view-source'; | |
Dan Beam
2013/08/29 00:24:30
what does setting the classname of a text node do?
Devlin
2013/08/29 21:02:04
It's so the css can take effect. Right now, there
| |
101 node.innerText = description; | |
102 return node; | |
103 }, | |
104 | |
105 /** | |
106 * Determine whether we can view the source of a given url. | |
107 * @param {string} url The url to the resource to view. | |
Dan Beam
2013/08/29 00:24:30
nit: the url _of_ the resource to view?
Devlin
2013/08/29 21:02:04
Done.
| |
108 * @return {boolean} Whether or not we can view the source for the url. | |
109 * @private | |
110 */ | |
111 canViewSource_: function(url) { | |
112 return isExtensionUrl(url, this.extensionUrl_) || url == 'manifest.json'; | |
113 }, | |
114 | |
115 /** | |
116 * Create a clickable node to view the source for the given url. | |
117 * @param {string} url The url to the resource to view. | |
118 * @return {HTMLElement} The clickable node to view the source. | |
119 * @private | |
120 */ | |
121 getViewSourceLink_: function(url) { | |
122 var node = document.createElement('a'); | |
123 var relativeUrl = getRelativeUrl(url, this.extensionUrl_); | |
124 | |
125 node.addEventListener('click', function(e) { | |
126 chrome.send('extensionErrorRequestFileSource', | |
127 [{'extensionId': this.error_.extensionId, | |
128 'errorMessage': this.error_.message, | |
129 'fileType': 'manifest', | |
130 'pathSuffix': relativeUrl, | |
131 'feature': this.error_.manifestKey, | |
132 'specific': this.error_.manifestSpecific}]); | |
133 }.bind(this)); | |
134 return node; | |
135 }, | |
136 }; | |
137 | |
138 /** | |
139 * An ExtensionErrorList represents the list of either runtime or manifest | |
140 * errors for a given extension. Each item in the list is an ExtensionError | |
141 * object, with information about the error. The list will display up to | |
142 * |MAX_ERRORS_TO_SHOW_| errors, with the option to expand (and then shrink) | |
143 * the displayed portion. This is included as part of the Extension node in | |
144 * the ExtensionList in chrome://extensions, if errors are present. | |
Dan Beam
2013/08/29 00:24:30
nit: this whole paragraph could be shortened to:
Devlin
2013/08/29 21:02:04
Done.
| |
145 * @constructor | |
146 * @extends {HTMLDivElement} | |
147 */ | |
148 function ExtensionErrorList(errors) { | |
149 var div = cloneTemplate('.extension-error-list'); | |
150 div.__proto__ = ExtensionErrorList.prototype; | |
151 div.errors_ = errors; | |
152 div.decorate(); | |
153 return div; | |
154 } | |
155 | |
156 ExtensionErrorList.prototype = { | |
157 __proto__: HTMLDivElement.prototype, | |
158 | |
159 /** | |
160 * @private | |
161 * @const | |
162 * @type {number} | |
163 */ | |
164 MAX_ERRORS_TO_SHOW_: 3, | |
165 | |
166 /** @override */ | |
167 decorate: function() { | |
168 this.contents_ = this.querySelector('.extension-error-list-contents'); | |
169 this.errors_.forEach(function(error) { | |
170 this.contents_.appendChild(document.createElement('li')).appendChild( | |
171 new ExtensionError(error)); | |
172 }, this); | |
173 | |
174 if (this.contents_.children.length > this.MAX_ERRORS_TO_SHOW_) { | |
175 for (var i = this.MAX_ERRORS_TO_SHOW_; | |
176 i < this.contents_.children.length; ++i) { | |
177 this.contents_.children[i].hidden = true; | |
178 } | |
179 this.initShowMoreButton_(); | |
180 } | |
181 }, | |
182 | |
183 /** | |
184 * Initialize the "Show More" button for the error list, if there are more | |
Dan Beam
2013/08/29 00:24:30
nit: Initialize the "Show More" button for the err
Devlin
2013/08/29 21:02:04
Done.
| |
185 * than |MAX_ERRORS_TO_SHOW_| errors in the list. | |
186 * @private | |
187 */ | |
188 initShowMoreButton_: function() { | |
189 var button = this.querySelector('.extension-error-list-show-more a'); | |
190 button.hidden = false; | |
191 button.isShowingAll = false; | |
192 button.addEventListener('click', function(e) { | |
193 for (var i = this.MAX_ERRORS_TO_SHOW_; | |
194 i < this.contents_.children.length; ++i) { | |
195 this.contents_.children[i].hidden = button.isShowingAll; | |
196 } | |
197 var message = button.isShowingAll ? 'extensionErrorsShowMore' : | |
198 'extensionErrorsShowFewer'; | |
199 button.innerText = loadTimeData.getString(message); | |
200 button.isShowingAll = !button.isShowingAll; | |
201 }.bind(this)); | |
202 } | |
203 }; | |
204 | |
205 return { | |
206 ExtensionErrorList: ExtensionErrorList | |
207 }; | |
208 }); | |
OLD | NEW |