OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 cr.define('extensions', function() { |
| 6 'use strict'; |
| 7 |
| 8 /** |
| 9 * Clear all the content of a given element. |
| 10 * @param {HTMLElement} element The element to be cleared. |
| 11 */ |
| 12 function clearElement(element) { |
| 13 while (element.firstChild) |
| 14 element.removeChild(element.firstChild); |
| 15 } |
| 16 |
| 17 /** |
| 18 * Construct an ExtensionLoadError around the given |div|. |
| 19 * @param {HTMLDivElement} div The HTML div for the extension load error. |
| 20 * @constructor |
| 21 */ |
| 22 function ExtensionLoadError(div) { |
| 23 div.__proto__ = ExtensionLoadError.prototype; |
| 24 div.init(); |
| 25 return div; |
| 26 } |
| 27 |
| 28 ExtensionLoadError.prototype = { |
| 29 __proto__: HTMLDivElement.prototype, |
| 30 |
| 31 init: function() { |
| 32 this.path_ = this.querySelector('#extension-load-error-path'); |
| 33 this.reason_ = this.querySelector('#extension-load-error-reason'); |
| 34 this.manifest_ = this.querySelector('#extension-load-error-manifest'); |
| 35 |
| 36 console.log('initd'); |
| 37 this.querySelector('#extension-load-error-retry-button').addEventListener( |
| 38 'click', function(e) { |
| 39 chrome.send('extensionLoaderRetry'); |
| 40 console.log('Retry Clicked!'); |
| 41 this.hide_(); |
| 42 }.bind(this)); |
| 43 |
| 44 this.querySelector('#extension-load-error-give-up-button'). |
| 45 addEventListener('click', function(e) { |
| 46 this.hide_(); |
| 47 }.bind(this)); |
| 48 }, |
| 49 |
| 50 show: function(path, reason, manifest) { |
| 51 this.path_.textContent = path; |
| 52 this.reason_.textContent = reason; |
| 53 |
| 54 clearElement(this.manifest_); |
| 55 if (manifest.highlight) { |
| 56 var createSpan = function(source, isHighlighted) { |
| 57 var span = document.createElement('span'); |
| 58 span.className = |
| 59 isHighlighted ? 'highlighted-source' : 'normal-source'; |
| 60 span.textContent = source; |
| 61 return span; |
| 62 }; |
| 63 |
| 64 if (manifest.beforeHighlight) { |
| 65 this.manifest_.appendChild( |
| 66 createSpan(manifest.beforeHighlight, false)); |
| 67 } |
| 68 |
| 69 if (manifest.highlight) { |
| 70 this.manifest_.appendChild( |
| 71 createSpan(manifest.highlight, true)); |
| 72 } |
| 73 |
| 74 if (manifest.afterHighlight) { |
| 75 this.manifest_.appendChild( |
| 76 createSpan(manifest.afterHighlight, false)); |
| 77 } |
| 78 this.manifest_.hidden = false; |
| 79 } else { |
| 80 this.manifest_.hidden = true; |
| 81 } |
| 82 |
| 83 this.hidden = false; |
| 84 }, |
| 85 |
| 86 hide_: function() { |
| 87 this.hidden = true; |
| 88 } |
| 89 }; |
| 90 |
| 91 function ExtensionLoader() { |
| 92 this.__proto__ = ExtensionLoader.prototype; |
| 93 this.init(); |
| 94 } |
| 95 |
| 96 cr.addSingletonGetter(ExtensionLoader); |
| 97 |
| 98 /** |
| 99 * The class in charge of loading unpacked extensions. |
| 100 */ |
| 101 ExtensionLoader.prototype = { |
| 102 /** |
| 103 * Perform first-time initialization. |
| 104 */ |
| 105 init: function() { |
| 106 console.log('Initing ExtensionLoader'); |
| 107 /** |
| 108 * The ExtensionLoadError to show any errors from loading an unpacked |
| 109 * extension. |
| 110 * @type {ExtensionLoadError} |
| 111 * @private |
| 112 */ |
| 113 this.loadError_ = new ExtensionLoadError($('extension-load-error')); |
| 114 }, |
| 115 |
| 116 /** |
| 117 * Begin the sequence of loading an unpacked extension. If an error is |
| 118 * encountered, this object will get notified via notifyFailed(). |
| 119 */ |
| 120 loadUnpacked: function() { |
| 121 chrome.send('extensionLoaderLoadUnpacked'); |
| 122 }, |
| 123 |
| 124 /** |
| 125 * Notify the ExtensionLoader that loading an unpacked extension failed. |
| 126 * Show the ExtensionLoadError. |
| 127 * @param {string} filePath The path to the unpacked extension. |
| 128 * @param {string} reason The reason the extension failed to load. |
| 129 * @param {Object} manifest An object with three strings: beforeHighlight, |
| 130 * afterHighlight, and highlight. These represent three portions of the |
| 131 * file's content to display - the portion which is most relevant and |
| 132 * should be emphasized (highlight), and the parts both before and after |
| 133 * this portion. These may be empty. |
| 134 */ |
| 135 notifyFailed: function(filePath, reason, manifest) { |
| 136 this.loadError_.show(filePath, reason, manifest); |
| 137 } |
| 138 }; |
| 139 |
| 140 /* |
| 141 * A static forwarding function for ExtensionLoader.notifyFailed. |
| 142 * @param {string} filePath The path to the unpacked extension. |
| 143 * @param {string} reason The reason the extension failed to load. |
| 144 * @param {Object} manifest The manifest of the failed extension. |
| 145 * @see ExtensionLoader.notifyFailed |
| 146 */ |
| 147 ExtensionLoader.notifyLoadFailed = function(filePath, reason, manifest) { |
| 148 ExtensionLoader.getInstance().notifyFailed(filePath, reason, manifest); |
| 149 }; |
| 150 |
| 151 return { |
| 152 ExtensionLoader: ExtensionLoader |
| 153 }; |
| 154 }); |
OLD | NEW |