Chromium Code Reviews| 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 var DocumentNatives = requireNative('document_natives'); | 5 var DocumentNatives = requireNative('document_natives'); |
| 6 var ExtensionOptionsEvents = | 6 var ExtensionOptionsEvents = |
| 7 require('extensionOptionsEvents').ExtensionOptionsEvents; | 7 require('extensionOptionsEvents').ExtensionOptionsEvents; |
| 8 var GuestViewInternal = | 8 var GuestViewInternal = |
| 9 require('binding').Binding.create('guestViewInternal').generate(); | 9 require('binding').Binding.create('guestViewInternal').generate(); |
| 10 var IdGenerator = requireNative('id_generator'); | 10 var IdGenerator = requireNative('id_generator'); |
| 11 | 11 |
| 12 var EXTENSION_OPTIONS_ATTRIBUTES = { | |
| 13 'autosize': 'on', | |
| 14 'maxheight': 600, | |
| 15 'maxwidth': 800, | |
| 16 'minheight': 32, | |
| 17 'minwidth': 80 | |
| 18 }; | |
| 19 | |
| 12 function ExtensionOptionsInternal(extensionoptionsNode) { | 20 function ExtensionOptionsInternal(extensionoptionsNode) { |
| 13 privates(extensionoptionsNode).internal = this; | 21 privates(extensionoptionsNode).internal = this; |
| 14 this.extensionoptionsNode = extensionoptionsNode; | 22 this.extensionoptionsNode = extensionoptionsNode; |
| 15 this.viewInstanceId = IdGenerator.GetNextId(); | 23 this.viewInstanceId = IdGenerator.GetNextId(); |
| 16 | 24 |
| 17 // on* Event handlers. | 25 // on* Event handlers. |
| 18 this.eventHandlers = {}; | 26 this.eventHandlers = {}; |
| 19 new ExtensionOptionsEvents(this, this.viewInstanceId); | 27 new ExtensionOptionsEvents(this, this.viewInstanceId); |
| 20 | 28 |
| 21 this.setupNodeProperties(); | 29 this.setupNodeProperties(); |
| 22 | 30 |
| 23 if (this.parseExtensionAttribute()) | 31 if (this.parseExtensionAttribute()) |
| 24 this.init(); | 32 this.init(); |
| 25 }; | 33 }; |
| 26 | 34 |
| 27 ExtensionOptionsInternal.prototype.attachWindow = function(instanceId) { | 35 ExtensionOptionsInternal.prototype.attachWindow = function(instanceId) { |
| 28 this.instanceId = instanceId; | 36 this.instanceId = instanceId; |
| 29 var params = { | 37 var params = { |
| 30 'instanceId': this.viewInstanceId, | 38 'instanceId': this.viewInstanceId, |
| 31 } | 39 } |
| 32 return this.browserPluginNode['-internal-attach'](instanceId, params); | 40 return this.browserPluginNode['-internal-attach'](instanceId, params); |
| 33 }; | 41 }; |
| 34 | 42 |
| 35 ExtensionOptionsInternal.prototype.createBrowserPluginNode = function() { | 43 ExtensionOptionsInternal.prototype.createBrowserPluginNode = function() { |
| 36 var browserPluginNode = new ExtensionOptionsInternal.BrowserPlugin(); | 44 var browserPluginNode = new ExtensionOptionsInternal.BrowserPlugin(); |
| 37 privates(browserPluginNode).internal = this; | 45 privates(browserPluginNode).internal = this; |
| 46 | |
| 47 for (var attributeName in EXTENSION_OPTIONS_ATTRIBUTES) { | |
| 48 if (!EXTENSION_OPTIONS_ATTRIBUTES.hasOwnProperty(attributeName)) { | |
| 49 continue; | |
| 50 } | |
|
not at google - send to devlin
2014/07/31 00:26:06
better than using "var .. in" and then checking ha
ericzeng
2014/07/31 18:29:57
Done.
| |
| 51 | |
| 52 if (this.extensionoptionsNode.hasAttribute(attributeName)) { | |
| 53 browserPluginNode.setAttribute( | |
| 54 attributeName, this.extensionoptionsNode.getAttribute(attributeName)); | |
| 55 } else { | |
| 56 browserPluginNode.setAttribute( | |
| 57 attributeName, EXTENSION_OPTIONS_ATTRIBUTES.attributeName); | |
|
not at google - send to devlin
2014/07/31 00:26:06
(you'd need EXTENSION_OPTIONS_ATTRIBUTES[attribute
ericzeng
2014/07/31 18:29:57
Done.
| |
| 58 } | |
| 59 } | |
| 60 | |
| 61 $Array.forEach(EXTENSION_OPTIONS_ATTRIBUTES, function(attributeName) { | |
|
not at google - send to devlin
2014/07/31 00:26:06
does $Array.forEach work on objects like this?
ericzeng
2014/07/31 18:29:57
I forgot to delete this block of code, the loop ab
not at google - send to devlin
2014/07/31 19:46:47
forEach doesn't work for objects, and EXTENSION_OP
| |
| 62 // Only copy attributes that have been assigned values, rather than copying | |
| 63 // a series of undefined attributes to BrowserPlugin. | |
| 64 if (this.extensionoptionsNode.hasAttribute(attributeName)) { | |
| 65 browserPluginNode.setAttribute( | |
| 66 attributeName, this.extensionoptionsNode.getAttribute(attributeName)); | |
| 67 } | |
| 68 }, this); | |
| 69 | |
| 38 return browserPluginNode; | 70 return browserPluginNode; |
| 39 }; | 71 }; |
| 40 | 72 |
| 41 ExtensionOptionsInternal.prototype.createGuest = function() { | 73 ExtensionOptionsInternal.prototype.createGuest = function() { |
| 42 var params = { | 74 var params = { |
| 43 'extensionId': this.extensionId, | 75 'extensionId': this.extensionId, |
| 44 }; | 76 }; |
| 45 var self = this; | 77 var self = this; |
| 46 GuestViewInternal.createGuest( | 78 GuestViewInternal.createGuest( |
| 47 'extensionoptions', | 79 'extensionoptions', |
| 48 params, | 80 params, |
| 49 function(instanceId) { | 81 function(instanceId) { |
| 50 if (instanceId == 0) { | 82 if (instanceId == 0) { |
| 51 self.initCalled = false; | 83 self.initCalled = false; |
| 52 } else { | 84 } else { |
| 53 self.attachWindow(instanceId); | 85 self.attachWindow(instanceId); |
| 54 } | 86 } |
| 55 }); | 87 }); |
| 56 }; | 88 }; |
| 57 | 89 |
| 58 ExtensionOptionsInternal.prototype.dispatchEvent = | 90 ExtensionOptionsInternal.prototype.dispatchEvent = |
| 59 function(extensionOptionsEvent) { | 91 function(extensionOptionsEvent) { |
| 60 return this.extensionoptionsNode.dispatchEvent(extensionOptionsEvent); | 92 return this.extensionoptionsNode.dispatchEvent(extensionOptionsEvent); |
| 61 }; | 93 }; |
| 62 | 94 |
| 63 ExtensionOptionsInternal.prototype.handleExtensionOptionsAttributeMutation = | 95 ExtensionOptionsInternal.prototype.handleExtensionOptionsAttributeMutation = |
| 64 function(name, oldValue, newValue) { | 96 function(name, oldValue, newValue) { |
| 65 if (name != 'extension') | |
| 66 return; | |
| 67 // We treat null attribute (attribute removed) and the empty string as | 97 // We treat null attribute (attribute removed) and the empty string as |
| 68 // one case. | 98 // one case. |
| 69 oldValue = oldValue || ''; | 99 oldValue = oldValue || ''; |
| 70 newValue = newValue || ''; | 100 newValue = newValue || ''; |
| 71 | 101 |
| 72 if (oldValue === newValue) | 102 if (oldValue === newValue) |
| 73 return; | 103 return; |
| 74 this.extensionId = newValue; | |
| 75 | 104 |
| 76 // Create new guest view if one hasn't been created for this element. | 105 if (name == 'extension') { |
| 77 if (!this.instanceId && this.parseExtensionAttribute()) | 106 this.extensionId = newValue; |
| 78 this.init(); | 107 // Create new guest view if one hasn't been created for this element. |
| 79 // TODO(ericzeng): Implement navigation to another guest view if we want | 108 if (!this.instanceId && this.parseExtensionAttribute()) |
| 80 // that functionality. | 109 this.init(); |
| 110 // TODO(ericzeng): Implement navigation to another guest view if we want | |
| 111 // that functionality. | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 if (this.browserPluginNode.hasOwnProperty(name)) { | |
| 116 this.browserPluginNode[name] = newValue; | |
| 117 } else { | |
| 118 this.browserPluginNode.setAttribute(name, newValue); | |
| 119 } | |
| 81 }; | 120 }; |
| 82 | 121 |
| 83 ExtensionOptionsInternal.prototype.init = function() { | 122 ExtensionOptionsInternal.prototype.init = function() { |
| 84 if (this.initCalled) | 123 if (this.initCalled) |
| 85 return; | 124 return; |
| 86 | 125 |
| 87 this.initCalled = true; | 126 this.initCalled = true; |
| 88 this.browserPluginNode = this.createBrowserPluginNode(); | 127 this.browserPluginNode = this.createBrowserPluginNode(); |
| 89 var shadowRoot = this.extensionoptionsNode.createShadowRoot(); | 128 var shadowRoot = this.extensionoptionsNode.createShadowRoot(); |
| 90 shadowRoot.appendChild(this.browserPluginNode); | 129 shadowRoot.appendChild(this.browserPluginNode); |
| 91 this.createGuest(); | 130 this.createGuest(); |
| 92 }; | 131 }; |
| 93 | 132 |
| 133 ExtensionOptionsInternal.prototype.onSizeChanged = function(width, height) { | |
| 134 this.browserPluginNode.style.width = width + 'px'; | |
| 135 this.browserPluginNode.style.height = height + 'px'; | |
| 136 }; | |
| 137 | |
| 94 ExtensionOptionsInternal.prototype.parseExtensionAttribute = function() { | 138 ExtensionOptionsInternal.prototype.parseExtensionAttribute = function() { |
| 95 if (this.extensionoptionsNode.hasAttribute('extension')) { | 139 if (this.extensionoptionsNode.hasAttribute('extension')) { |
| 96 var extensionId = this.extensionoptionsNode.getAttribute('extension'); | 140 var extensionId = this.extensionoptionsNode.getAttribute('extension'); |
| 97 // Only allow extensions to embed their own options page (if it has one). | 141 // Only allow extensions to embed their own options page (if it has one). |
| 98 if (chrome.runtime.id == extensionId && | 142 if (chrome.runtime.id == extensionId && |
| 99 chrome.runtime.getManifest().hasOwnProperty('options_page')) { | 143 chrome.runtime.getManifest().hasOwnProperty('options_page')) { |
| 100 this.extensionId = extensionId; | 144 this.extensionId = extensionId; |
| 101 return true; | 145 return true; |
| 102 } | 146 } |
| 103 } | 147 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 120 eventName, self.eventHandlers[propertyName]); | 164 eventName, self.eventHandlers[propertyName]); |
| 121 self.eventHandlers[propertyName] = value; | 165 self.eventHandlers[propertyName] = value; |
| 122 if (value) | 166 if (value) |
| 123 extensionoptionsNode.addEventListener(eventName, value); | 167 extensionoptionsNode.addEventListener(eventName, value); |
| 124 }, | 168 }, |
| 125 enumerable: true | 169 enumerable: true |
| 126 }); | 170 }); |
| 127 }; | 171 }; |
| 128 | 172 |
| 129 ExtensionOptionsInternal.prototype.setupNodeProperties = function() { | 173 ExtensionOptionsInternal.prototype.setupNodeProperties = function() { |
| 174 $Array.forEach(EXTENSION_OPTIONS_ATTRIBUTES, function(attributeName) { | |
|
not at google - send to devlin
2014/07/31 00:26:06
likewise
| |
| 175 Object.defineProperty(this.extensionoptionsNode, attributeName, { | |
| 176 get: function() { | |
| 177 if (this.browserPluginNode.hasOwnProperty(attributeName)) | |
| 178 return this.browserPluginNode[attributeName]; | |
| 179 return this.browserPluginNode.getAttribute(attributeName); | |
| 180 }, | |
| 181 set: function(value) { | |
| 182 if (this.browserPluginNode.hasOwnProperty(attributeName)) { | |
| 183 // Give the BrowserPlugin first stab at the attribute so that it can | |
| 184 // throw an exception if there is a problem. This attribute will then | |
| 185 // be propagated back to the <extensionoptions>. | |
| 186 this.browserPluginNode[attributeName] = value; | |
| 187 } else { | |
| 188 this.browserPluginNode.setAttribute(attributeName, value); | |
| 189 } | |
| 190 }, | |
| 191 enumerable: true | |
| 192 }); | |
| 193 }, this); | |
| 194 | |
| 130 var self = this; | 195 var self = this; |
| 131 this.extensionId = this.extensionoptionsNode.getAttribute('extension'); | |
| 132 Object.defineProperty(this.extensionoptionsNode, 'extension', { | 196 Object.defineProperty(this.extensionoptionsNode, 'extension', { |
| 133 get: function() { | 197 get: function() { |
| 134 return self.extensionId; | 198 return self.extensionId; |
| 135 }, | 199 }, |
| 136 set: function(value) { | 200 set: function(value) { |
| 137 self.extensionoptionsNode.setAttribute('extension', value); | 201 self.extensionoptionsNode.setAttribute('extension', value); |
| 138 }, | 202 }, |
| 139 enumerable: true | 203 enumerable: true |
| 140 }); | 204 }); |
| 141 }; | 205 }; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 | 254 |
| 191 var useCapture = true; | 255 var useCapture = true; |
| 192 window.addEventListener('readystatechange', function listener(event) { | 256 window.addEventListener('readystatechange', function listener(event) { |
| 193 if (document.readyState == 'loading') | 257 if (document.readyState == 'loading') |
| 194 return; | 258 return; |
| 195 | 259 |
| 196 registerBrowserPluginElement(); | 260 registerBrowserPluginElement(); |
| 197 registerExtensionOptionsElement(); | 261 registerExtensionOptionsElement(); |
| 198 window.removeEventListener(event.type, listener, useCapture); | 262 window.removeEventListener(event.type, listener, useCapture); |
| 199 }, useCapture); | 263 }, useCapture); |
| OLD | NEW |