OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('extensions', function() { | 5 cr.define('extensions', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * Construct an ExtensionLoadError around the given |div|. | 9 * Construct an ExtensionLoadError around the given |div|. |
10 * @param {HTMLDivElement} div The HTML div for the extension load error. | 10 * @param {HTMLDivElement} div The HTML div for the extension load error. |
11 * @constructor | 11 * @constructor |
12 */ | 12 */ |
13 function ExtensionLoadError(div) { | 13 function ExtensionLoadError(div) { |
14 div.__proto__ = ExtensionLoadError.prototype; | 14 div.__proto__ = ExtensionLoadError.prototype; |
15 div.init(); | 15 div.init(); |
16 return div; | 16 return div; |
17 } | 17 } |
18 | 18 |
| 19 /** |
| 20 * Construct a Failure. |
| 21 * @param {string} filePath The path to the unpacked extension. |
| 22 * @param {string} reason The reason the extension failed to load. |
| 23 * @param {Object} manifest An object with three strings: beforeHighlight, |
| 24 * afterHighlight, and highlight. These represent three portions of the |
| 25 * file's content to display - the portion which is most relevant and |
| 26 * should be emphasized (highlight), and the parts both before and after |
| 27 * this portion. These may be empty. |
| 28 * @param {HTMLLIElement} listElement The HTML element used for displaying the |
| 29 * failure path for the additional failures UI. |
| 30 * @constructor |
| 31 */ |
| 32 function Failure(filePath, reason, manifest, listElement) { |
| 33 this.path = filePath; |
| 34 this.reason = reason; |
| 35 this.manifest = manifest; |
| 36 this.listElement = listElement; |
| 37 } |
| 38 |
19 ExtensionLoadError.prototype = { | 39 ExtensionLoadError.prototype = { |
20 __proto__: HTMLDivElement.prototype, | 40 __proto__: HTMLDivElement.prototype, |
21 | 41 |
22 /** | 42 /** |
23 * Initialize the ExtensionLoadError div. | 43 * Initialize the ExtensionLoadError div. |
24 */ | 44 */ |
25 init: function() { | 45 init: function() { |
26 /** | 46 /** |
27 * The element which displays the path of the extension. | 47 * The element which displays the path of the extension. |
28 * @type {HTMLSpanElement} | 48 * @type {HTMLSpanElement} |
29 * @private | 49 * @private |
30 */ | 50 */ |
31 this.path_ = this.querySelector('#extension-load-error-path'); | 51 this.path_ = this.querySelector('#extension-load-error-path'); |
32 | 52 |
33 /** | 53 /** |
34 * The element which displays the reason the extension failed to load. | 54 * The element which displays the reason the extension failed to load. |
35 * @type {HTMLSpanElement} | 55 * @type {HTMLSpanElement} |
36 * @private | 56 * @private |
37 */ | 57 */ |
38 this.reason_ = this.querySelector('#extension-load-error-reason'); | 58 this.reason_ = this.querySelector('#extension-load-error-reason'); |
39 | 59 |
40 /** | 60 /** |
41 * The element which displays the manifest code. | 61 * The element which displays the manifest code. |
42 * @type {ExtensionCode} | 62 * @type {ExtensionCode} |
43 * @private | 63 * @private |
44 */ | 64 */ |
45 this.manifest_ = new extensions.ExtensionCode( | 65 this.manifest_ = new extensions.ExtensionCode( |
46 this.querySelector('#extension-load-error-manifest')); | 66 this.querySelector('#extension-load-error-manifest')); |
47 | 67 |
| 68 /** |
| 69 * The element which displays information about additional errors. |
| 70 * @type {HTMLULElement} |
| 71 * @private |
| 72 */ |
| 73 this.additional_ = this.querySelector('#extension-load-error-additional'); |
| 74 this.additional_.list = this.additional_.getElementsByTagName('ul')[0]; |
| 75 |
| 76 /** |
| 77 * An array of Failures for keeping track of multiple active failures. |
| 78 * @type {Array.<Failure>} |
| 79 * @private |
| 80 */ |
| 81 this.failures_ = []; |
| 82 |
48 this.querySelector('#extension-load-error-retry-button').addEventListener( | 83 this.querySelector('#extension-load-error-retry-button').addEventListener( |
49 'click', function(e) { | 84 'click', function(e) { |
50 chrome.send('extensionLoaderRetry'); | 85 chrome.send('extensionLoaderRetry'); |
51 this.hide_(); | 86 this.remove_(); |
52 }.bind(this)); | 87 }.bind(this)); |
53 | 88 |
54 this.querySelector('#extension-load-error-give-up-button'). | 89 this.querySelector('#extension-load-error-give-up-button'). |
55 addEventListener('click', function(e) { | 90 addEventListener('click', function(e) { |
56 this.hide_(); | 91 chrome.send('extensionLoaderIgnoreFailure'); |
| 92 this.remove_(); |
57 }.bind(this)); | 93 }.bind(this)); |
| 94 |
| 95 chrome.send('extensionLoaderDisplayFailures'); |
58 }, | 96 }, |
59 | 97 |
60 /** | 98 /** |
61 * Display the load error to the user. | 99 * Add a failure to failures_ array. If there is already a displayed |
62 * @param {string} path The path from which the extension was loaded. | 100 * failure, display the additional failures element. |
63 * @param {string} reason The reason the extension failed to load. | 101 * @param {Array.<Object>} failures Array of failures containing paths, |
64 * @param {string} manifest The manifest object, with highlighted regions. | 102 * errors, and manifests. |
| 103 * @private |
65 */ | 104 */ |
66 show: function(path, reason, manifest) { | 105 add_: function(failures) { |
67 this.path_.textContent = path; | 106 // If a failure is already being displayed, unhide the last item. |
68 this.reason_.textContent = reason; | 107 if (this.failures_.length > 0) |
| 108 this.failures_[this.failures_.length - 1].listElement.hidden = false; |
| 109 for (var i = 0; i < failures.length; ++i) { |
| 110 var listItem = document.createElement('li'); |
| 111 listItem.textContent = failures[i].path; |
| 112 this.additional_.list.appendChild(listItem); |
| 113 failures[i].listElement = listItem; |
| 114 this.failures_.push(failures[i]); |
| 115 } |
| 116 // Hide the last item because the UI is displaying its information. |
| 117 this.failures_[this.failures_.length - 1].listElement.hidden = true; |
| 118 this.show_(); |
| 119 }, |
69 | 120 |
70 manifest.message = reason; | 121 /** |
| 122 * Remove a failure from |failures_| array. If this was the last failure, |
| 123 * hide the error UI. If this was the last additional failure, hide |
| 124 * the additional failures UI. |
| 125 * @private |
| 126 */ |
| 127 remove_: function() { |
| 128 this.additional_.list.removeChild( |
| 129 this.failures_[this.failures_.length - 1].listElement); |
| 130 this.failures_.pop(); |
| 131 if (this.failures_.length > 0) { |
| 132 this.failures_[this.failures_.length - 1].listElement.hidden = true; |
| 133 this.show_(); |
| 134 } else { |
| 135 this.hidden = true; |
| 136 } |
| 137 }, |
| 138 |
| 139 /** |
| 140 * Display the load error to the user. The last failure gets its manifest |
| 141 * and error displayed, while additional failures have their path names |
| 142 * displayed in the additional failures element. |
| 143 * @private |
| 144 */ |
| 145 show_: function() { |
| 146 assert(this.failures_.length >= 1); |
| 147 var failure = this.failures_[this.failures_.length - 1]; |
| 148 this.path_.textContent = failure.path; |
| 149 this.reason_.textContent = failure.reason; |
| 150 |
| 151 failure.manifest.message = failure.reason; |
71 this.manifest_.populate( | 152 this.manifest_.populate( |
72 manifest, | 153 failure.manifest, |
73 loadTimeData.getString('extensionLoadCouldNotLoadManifest')); | 154 loadTimeData.getString('extensionLoadCouldNotLoadManifest')); |
74 this.hidden = false; | 155 this.hidden = false; |
75 this.manifest_.scrollToError(); | 156 this.manifest_.scrollToError(); |
76 }, | |
77 | 157 |
78 /** | 158 this.additional_.hidden = this.failures_.length == 1; |
79 * Hide the extension load error. | |
80 * @private | |
81 */ | |
82 hide_: function() { | |
83 this.hidden = true; | |
84 } | 159 } |
85 }; | 160 }; |
86 | 161 |
87 /** | 162 /** |
88 * The ExtensionLoader is the class in charge of loading unpacked extensions. | 163 * The ExtensionLoader is the class in charge of loading unpacked extensions. |
89 * @constructor | 164 * @constructor |
90 */ | 165 */ |
91 function ExtensionLoader() { | 166 function ExtensionLoader() { |
92 /** | 167 /** |
93 * The ExtensionLoadError to show any errors from loading an unpacked | 168 * The ExtensionLoadError to show any errors from loading an unpacked |
(...skipping 10 matching lines...) Expand all Loading... |
104 /** | 179 /** |
105 * Begin the sequence of loading an unpacked extension. If an error is | 180 * Begin the sequence of loading an unpacked extension. If an error is |
106 * encountered, this object will get notified via notifyFailed(). | 181 * encountered, this object will get notified via notifyFailed(). |
107 */ | 182 */ |
108 loadUnpacked: function() { | 183 loadUnpacked: function() { |
109 chrome.send('extensionLoaderLoadUnpacked'); | 184 chrome.send('extensionLoaderLoadUnpacked'); |
110 }, | 185 }, |
111 | 186 |
112 /** | 187 /** |
113 * Notify the ExtensionLoader that loading an unpacked extension failed. | 188 * Notify the ExtensionLoader that loading an unpacked extension failed. |
114 * Show the ExtensionLoadError. | 189 * Add the failure to failures_ and show the ExtensionLoadError. |
115 * @param {string} filePath The path to the unpacked extension. | 190 * @param {Array.<Object>} failures Array of failures containing paths, |
116 * @param {string} reason The reason the extension failed to load. | 191 * errors, and manifests. |
117 * @param {Object} manifest An object with three strings: beforeHighlight, | |
118 * afterHighlight, and highlight. These represent three portions of the | |
119 * file's content to display - the portion which is most relevant and | |
120 * should be emphasized (highlight), and the parts both before and after | |
121 * this portion. These may be empty. | |
122 */ | 192 */ |
123 notifyFailed: function(filePath, reason, manifest) { | 193 notifyFailed: function(failures) { |
124 this.loadError_.show(filePath, reason, manifest); | 194 this.loadError_.add_(failures); |
125 } | 195 }, |
126 }; | 196 }; |
127 | 197 |
128 /* | 198 /* |
129 * A static forwarding function for ExtensionLoader.notifyFailed. | 199 * A static forwarding function for ExtensionLoader.notifyFailed. |
130 * @param {string} filePath The path to the unpacked extension. | 200 * @param {Array.<Object>} failures Array of failures containing paths, |
131 * @param {string} reason The reason the extension failed to load. | 201 * errors, and manifests. |
132 * @param {Object} manifest The manifest of the failed extension. | |
133 * @see ExtensionLoader.notifyFailed | 202 * @see ExtensionLoader.notifyFailed |
134 */ | 203 */ |
135 ExtensionLoader.notifyLoadFailed = function(filePath, reason, manifest) { | 204 ExtensionLoader.notifyLoadFailed = function(failures) { |
136 ExtensionLoader.getInstance().notifyFailed(filePath, reason, manifest); | 205 ExtensionLoader.getInstance().notifyFailed(failures); |
137 }; | 206 }; |
138 | 207 |
139 return { | 208 return { |
140 ExtensionLoader: ExtensionLoader | 209 ExtensionLoader: ExtensionLoader |
141 }; | 210 }; |
142 }); | 211 }); |
OLD | NEW |