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 var utils = require('utils'); | |
12 | |
13 var EXTENSION_OPTIONS_ATTRIBUTES = { | |
14 'autosize': 'on', | |
15 'maxheight': 600, | |
16 'maxwidth': 800, | |
17 'minheight': 32, | |
18 'minwidth': 80 | |
19 }; | |
11 | 20 |
12 function ExtensionOptionsInternal(extensionoptionsNode) { | 21 function ExtensionOptionsInternal(extensionoptionsNode) { |
13 privates(extensionoptionsNode).internal = this; | 22 privates(extensionoptionsNode).internal = this; |
14 this.extensionoptionsNode = extensionoptionsNode; | 23 this.extensionoptionsNode = extensionoptionsNode; |
15 this.viewInstanceId = IdGenerator.GetNextId(); | 24 this.viewInstanceId = IdGenerator.GetNextId(); |
16 | 25 |
17 // on* Event handlers. | 26 // on* Event handlers. |
18 this.eventHandlers = {}; | 27 this.eventHandlers = {}; |
19 new ExtensionOptionsEvents(this, this.viewInstanceId); | 28 new ExtensionOptionsEvents(this, this.viewInstanceId); |
20 | 29 |
21 this.setupNodeProperties(); | 30 this.setupNodeProperties(); |
22 | 31 |
23 if (this.parseExtensionAttribute()) | 32 if (this.parseExtensionAttribute()) |
24 this.init(); | 33 this.init(); |
25 }; | 34 }; |
26 | 35 |
27 ExtensionOptionsInternal.prototype.attachWindow = function(instanceId) { | 36 ExtensionOptionsInternal.prototype.attachWindow = function(instanceId) { |
28 this.instanceId = instanceId; | 37 this.instanceId = instanceId; |
29 var params = { | 38 var params = { |
30 'instanceId': this.viewInstanceId, | 39 'instanceId': this.viewInstanceId, |
31 } | 40 } |
32 return this.browserPluginNode['-internal-attach'](instanceId, params); | 41 return this.browserPluginNode['-internal-attach'](instanceId, params); |
33 }; | 42 }; |
34 | 43 |
35 ExtensionOptionsInternal.prototype.createBrowserPluginNode = function() { | 44 ExtensionOptionsInternal.prototype.createBrowserPluginNode = function() { |
36 var browserPluginNode = new ExtensionOptionsInternal.BrowserPlugin(); | 45 var browserPluginNode = new ExtensionOptionsInternal.BrowserPlugin(); |
37 privates(browserPluginNode).internal = this; | 46 privates(browserPluginNode).internal = this; |
47 | |
48 utils.forEach(EXTENSION_OPTIONS_ATTRIBUTES, function(attributeName, value) { | |
49 if (this.extensionoptionsNode.hasAttribute(attributeName)) { | |
50 browserPluginNode.setAttribute( | |
51 attributeName, this.extensionoptionsNode.getAttribute(attributeName)); | |
52 } else { | |
53 browserPluginNode.setAttribute( | |
54 attributeName, EXTENSION_OPTIONS_ATTRIBUTES[attributeName]); | |
not at google - send to devlin
2014/07/31 20:26:00
use |value| not the whole "EXTENSION_OPTIONS_ATTRI
| |
55 } | |
56 }, this); | |
38 return browserPluginNode; | 57 return browserPluginNode; |
39 }; | 58 }; |
40 | 59 |
41 ExtensionOptionsInternal.prototype.createGuest = function() { | 60 ExtensionOptionsInternal.prototype.createGuest = function() { |
42 var params = { | 61 var params = { |
43 'extensionId': this.extensionId, | 62 'extensionId': this.extensionId, |
44 }; | 63 }; |
45 var self = this; | 64 var self = this; |
46 GuestViewInternal.createGuest( | 65 GuestViewInternal.createGuest( |
47 'extensionoptions', | 66 'extensionoptions', |
48 params, | 67 params, |
49 function(instanceId) { | 68 function(instanceId) { |
50 if (instanceId == 0) { | 69 if (instanceId == 0) { |
51 self.initCalled = false; | 70 self.initCalled = false; |
52 } else { | 71 } else { |
53 self.attachWindow(instanceId); | 72 self.attachWindow(instanceId); |
54 } | 73 } |
55 }); | 74 }); |
56 }; | 75 }; |
57 | 76 |
58 ExtensionOptionsInternal.prototype.dispatchEvent = | 77 ExtensionOptionsInternal.prototype.dispatchEvent = |
59 function(extensionOptionsEvent) { | 78 function(extensionOptionsEvent) { |
60 return this.extensionoptionsNode.dispatchEvent(extensionOptionsEvent); | 79 return this.extensionoptionsNode.dispatchEvent(extensionOptionsEvent); |
61 }; | 80 }; |
62 | 81 |
63 ExtensionOptionsInternal.prototype.handleExtensionOptionsAttributeMutation = | 82 ExtensionOptionsInternal.prototype.handleExtensionOptionsAttributeMutation = |
64 function(name, oldValue, newValue) { | 83 function(name, oldValue, newValue) { |
65 if (name != 'extension') | |
66 return; | |
67 // We treat null attribute (attribute removed) and the empty string as | 84 // We treat null attribute (attribute removed) and the empty string as |
68 // one case. | 85 // one case. |
69 oldValue = oldValue || ''; | 86 oldValue = oldValue || ''; |
70 newValue = newValue || ''; | 87 newValue = newValue || ''; |
71 | 88 |
72 if (oldValue === newValue) | 89 if (oldValue === newValue) |
73 return; | 90 return; |
74 this.extensionId = newValue; | |
75 | 91 |
76 // Create new guest view if one hasn't been created for this element. | 92 if (name == 'extension') { |
77 if (!this.instanceId && this.parseExtensionAttribute()) | 93 this.extensionId = newValue; |
78 this.init(); | 94 // 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 | 95 if (!this.instanceId && this.parseExtensionAttribute()) |
80 // that functionality. | 96 this.init(); |
97 // TODO(ericzeng): Implement navigation to another guest view if we want | |
98 // that functionality. | |
99 return; | |
100 } | |
101 | |
102 if (this.browserPluginNode.hasOwnProperty(name)) { | |
103 this.browserPluginNode[name] = newValue; | |
104 } else { | |
105 this.browserPluginNode.setAttribute(name, newValue); | |
106 } | |
81 }; | 107 }; |
82 | 108 |
83 ExtensionOptionsInternal.prototype.init = function() { | 109 ExtensionOptionsInternal.prototype.init = function() { |
84 if (this.initCalled) | 110 if (this.initCalled) |
85 return; | 111 return; |
86 | 112 |
87 this.initCalled = true; | 113 this.initCalled = true; |
88 this.browserPluginNode = this.createBrowserPluginNode(); | 114 this.browserPluginNode = this.createBrowserPluginNode(); |
89 var shadowRoot = this.extensionoptionsNode.createShadowRoot(); | 115 var shadowRoot = this.extensionoptionsNode.createShadowRoot(); |
90 shadowRoot.appendChild(this.browserPluginNode); | 116 shadowRoot.appendChild(this.browserPluginNode); |
91 this.createGuest(); | 117 this.createGuest(); |
92 }; | 118 }; |
93 | 119 |
120 ExtensionOptionsInternal.prototype.onSizeChanged = function(width, height) { | |
121 this.browserPluginNode.style.width = width + 'px'; | |
122 this.browserPluginNode.style.height = height + 'px'; | |
123 }; | |
124 | |
94 ExtensionOptionsInternal.prototype.parseExtensionAttribute = function() { | 125 ExtensionOptionsInternal.prototype.parseExtensionAttribute = function() { |
95 if (this.extensionoptionsNode.hasAttribute('extension')) { | 126 if (this.extensionoptionsNode.hasAttribute('extension')) { |
96 var extensionId = this.extensionoptionsNode.getAttribute('extension'); | 127 var extensionId = this.extensionoptionsNode.getAttribute('extension'); |
97 // Only allow extensions to embed their own options page (if it has one). | 128 // Only allow extensions to embed their own options page (if it has one). |
98 if (chrome.runtime.id == extensionId && | 129 if (chrome.runtime.id == extensionId && |
99 chrome.runtime.getManifest().hasOwnProperty('options_page')) { | 130 chrome.runtime.getManifest().hasOwnProperty('options_page')) { |
100 this.extensionId = extensionId; | 131 this.extensionId = extensionId; |
101 return true; | 132 return true; |
102 } | 133 } |
103 } | 134 } |
(...skipping 16 matching lines...) Expand all Loading... | |
120 eventName, self.eventHandlers[propertyName]); | 151 eventName, self.eventHandlers[propertyName]); |
121 self.eventHandlers[propertyName] = value; | 152 self.eventHandlers[propertyName] = value; |
122 if (value) | 153 if (value) |
123 extensionoptionsNode.addEventListener(eventName, value); | 154 extensionoptionsNode.addEventListener(eventName, value); |
124 }, | 155 }, |
125 enumerable: true | 156 enumerable: true |
126 }); | 157 }); |
127 }; | 158 }; |
128 | 159 |
129 ExtensionOptionsInternal.prototype.setupNodeProperties = function() { | 160 ExtensionOptionsInternal.prototype.setupNodeProperties = function() { |
161 utils.forEach(EXTENSION_OPTIONS_ATTRIBUTES, function(attributeName) { | |
162 Object.defineProperty(this.extensionoptionsNode, attributeName, { | |
163 get: function() { | |
164 if (this.browserPluginNode.hasOwnProperty(attributeName)) | |
165 return this.browserPluginNode[attributeName]; | |
166 return this.browserPluginNode.getAttribute(attributeName); | |
167 }, | |
168 set: function(value) { | |
169 if (this.browserPluginNode.hasOwnProperty(attributeName)) { | |
170 // Give the BrowserPlugin first stab at the attribute so that it can | |
171 // throw an exception if there is a problem. This attribute will then | |
172 // be propagated back to the <extensionoptions>. | |
173 this.browserPluginNode[attributeName] = value; | |
174 } else { | |
175 this.browserPluginNode.setAttribute(attributeName, value); | |
176 } | |
177 }, | |
178 enumerable: true | |
179 }); | |
180 }, this); | |
181 | |
130 var self = this; | 182 var self = this; |
131 this.extensionId = this.extensionoptionsNode.getAttribute('extension'); | |
132 Object.defineProperty(this.extensionoptionsNode, 'extension', { | 183 Object.defineProperty(this.extensionoptionsNode, 'extension', { |
133 get: function() { | 184 get: function() { |
134 return self.extensionId; | 185 return self.extensionId; |
135 }, | 186 }, |
136 set: function(value) { | 187 set: function(value) { |
137 self.extensionoptionsNode.setAttribute('extension', value); | 188 self.extensionoptionsNode.setAttribute('extension', value); |
138 }, | 189 }, |
139 enumerable: true | 190 enumerable: true |
140 }); | 191 }); |
141 }; | 192 }; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 | 241 |
191 var useCapture = true; | 242 var useCapture = true; |
192 window.addEventListener('readystatechange', function listener(event) { | 243 window.addEventListener('readystatechange', function listener(event) { |
193 if (document.readyState == 'loading') | 244 if (document.readyState == 'loading') |
194 return; | 245 return; |
195 | 246 |
196 registerBrowserPluginElement(); | 247 registerBrowserPluginElement(); |
197 registerExtensionOptionsElement(); | 248 registerExtensionOptionsElement(); |
198 window.removeEventListener(event.type, listener, useCapture); | 249 window.removeEventListener(event.type, listener, useCapture); |
199 }, useCapture); | 250 }, useCapture); |
OLD | NEW |