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 GuestView = require('guestView').GuestView; | 8 var GuestView = require('guestView').GuestView; |
9 var GuestViewContainer = require('guestViewContainer').GuestViewContainer; | 9 var GuestViewContainer = require('guestViewContainer').GuestViewContainer; |
10 var GuestViewInternal = | 10 var GuestViewInternal = |
(...skipping 19 matching lines...) Expand all Loading... |
30 // setupEventProperty is normally called in extension_options_events.js to | 30 // setupEventProperty is normally called in extension_options_events.js to |
31 // register events, but the createfailed event is registered here because | 31 // register events, but the createfailed event is registered here because |
32 // the event is fired from here instead of through | 32 // the event is fired from here instead of through |
33 // extension_options_events.js. | 33 // extension_options_events.js. |
34 this.setupEventProperty('createfailed'); | 34 this.setupEventProperty('createfailed'); |
35 new ExtensionOptionsEvents(this, this.viewInstanceId); | 35 new ExtensionOptionsEvents(this, this.viewInstanceId); |
36 | 36 |
37 this.autosizeDeferred = false; | 37 this.autosizeDeferred = false; |
38 | 38 |
39 this.setupElementProperties(); | 39 this.setupElementProperties(); |
40 this.parseExtensionAttribute(); | |
41 }; | 40 }; |
42 | 41 |
43 ExtensionOptionsImpl.prototype.__proto__ = GuestViewContainer.prototype; | 42 ExtensionOptionsImpl.prototype.__proto__ = GuestViewContainer.prototype; |
44 | 43 |
45 ExtensionOptionsImpl.VIEW_TYPE = 'ExtensionOptions'; | 44 ExtensionOptionsImpl.VIEW_TYPE = 'ExtensionOptions'; |
46 | 45 |
47 // Add extra functionality to |this.element|. | 46 // Add extra functionality to |this.element|. |
48 ExtensionOptionsImpl.setupElement = function(proto) { | 47 ExtensionOptionsImpl.setupElement = function(proto) { |
49 var apiMethods = [ | 48 var apiMethods = [ |
50 'setDeferAutoSize', | 49 'setDeferAutoSize', |
51 'resumeDeferredAutoSize' | 50 'resumeDeferredAutoSize' |
52 ]; | 51 ]; |
53 | 52 |
54 // Forward proto.foo* method calls to ExtensionOptionsImpl.foo*. | 53 // Forward proto.foo* method calls to ExtensionOptionsImpl.foo*. |
55 GuestViewContainer.forwardApiMethods(proto, apiMethods); | 54 GuestViewContainer.forwardApiMethods(proto, apiMethods); |
56 } | 55 } |
57 | 56 |
58 ExtensionOptionsImpl.prototype.onElementAttached = function() { | 57 ExtensionOptionsImpl.prototype.onElementAttached = function() { |
59 this.parseExtensionAttribute(); | |
60 this.createGuest(); | 58 this.createGuest(); |
61 } | 59 } |
62 | 60 |
63 ExtensionOptionsImpl.prototype.buildAttachParams = function() { | 61 ExtensionOptionsImpl.prototype.buildContainerParams = function() { |
64 var params = { | 62 var params = { |
65 'autosize': this.element.hasAttribute('autosize'), | 63 'autosize': this.element.hasAttribute('autosize'), |
66 'maxheight': parseInt(this.maxheight || 0), | 64 'maxheight': parseInt(this.maxheight || 0), |
67 'maxwidth': parseInt(this.maxwidth || 0), | 65 'maxwidth': parseInt(this.maxwidth || 0), |
68 'minheight': parseInt(this.minheight || 0), | 66 'minheight': parseInt(this.minheight || 0), |
69 'minwidth': parseInt(this.minwidth || 0) | 67 'minwidth': parseInt(this.minwidth || 0), |
| 68 'extensionId': this.element.getAttribute('extension') |
70 }; | 69 }; |
71 return params; | 70 return params; |
72 }; | 71 }; |
73 | 72 |
74 ExtensionOptionsImpl.prototype.createGuest = function() { | 73 ExtensionOptionsImpl.prototype.createGuest = function() { |
75 if (!this.elementAttached) { | 74 if (!this.elementAttached) { |
76 return; | 75 return; |
77 } | 76 } |
78 var params = { | |
79 'extensionId': this.extensionId, | |
80 }; | |
81 | 77 |
82 this.guest.create(params, function() { | 78 // Destroy the old guest if one exists. |
| 79 this.guest.destroy(); |
| 80 |
| 81 this.guest.create(this.buildParams(), function() { |
83 if (!this.guest.getId()) { | 82 if (!this.guest.getId()) { |
84 // Fire a createfailed event here rather than in ExtensionOptionsGuest | 83 // Fire a createfailed event here rather than in ExtensionOptionsGuest |
85 // because the guest will not be created, and cannot fire an event. | 84 // because the guest will not be created, and cannot fire an event. |
86 this.initCalled = false; | |
87 var createFailedEvent = new Event('createfailed', { bubbles: true }); | 85 var createFailedEvent = new Event('createfailed', { bubbles: true }); |
88 this.dispatchEvent(createFailedEvent); | 86 this.dispatchEvent(createFailedEvent); |
89 } else { | 87 } else { |
90 this.attachWindow(); | 88 this.attachWindow(); |
91 } | 89 } |
92 }.bind(this)); | 90 }.bind(this)); |
93 }; | 91 }; |
94 | 92 |
95 ExtensionOptionsImpl.prototype.dispatchEvent = | 93 ExtensionOptionsImpl.prototype.dispatchEvent = |
96 function(extensionOptionsEvent) { | 94 function(extensionOptionsEvent) { |
97 return this.element.dispatchEvent(extensionOptionsEvent); | 95 return this.element.dispatchEvent(extensionOptionsEvent); |
98 }; | 96 }; |
99 | 97 |
100 ExtensionOptionsImpl.prototype.handleAttributeMutation = | 98 ExtensionOptionsImpl.prototype.handleAttributeMutation = |
101 function(name, oldValue, newValue) { | 99 function(name, oldValue, newValue) { |
102 // We treat null attribute (attribute removed) and the empty string as | 100 // We treat null attribute (attribute removed) and the empty string as |
103 // one case. | 101 // one case. |
104 oldValue = oldValue || ''; | 102 oldValue = oldValue || ''; |
105 newValue = newValue || ''; | 103 newValue = newValue || ''; |
106 | 104 |
107 if (oldValue === newValue) | 105 if (oldValue === newValue) |
108 return; | 106 return; |
109 | 107 |
110 if (name == 'extension' && !oldValue && !!newValue) { | 108 if (name == 'extension') { |
111 this.extensionId = newValue; | 109 this.createGuest(); |
112 // If the browser plugin is not ready then don't create the guest until | |
113 // it is ready (in handleBrowserPluginAttributeMutation). | |
114 if (!this.internalInstanceId) | |
115 return; | |
116 | |
117 // If a guest view does not exist then create one. | |
118 if (!this.guest.getId()) { | |
119 this.createGuest(); | |
120 return; | |
121 } | |
122 // TODO(ericzeng): Implement navigation to another guest view if we want | |
123 // that functionality. | |
124 } else if (AUTO_SIZE_ATTRIBUTES.hasOwnProperty(name) > -1) { | 110 } else if (AUTO_SIZE_ATTRIBUTES.hasOwnProperty(name) > -1) { |
125 this[name] = newValue; | 111 this[name] = newValue; |
126 this.resetSizeConstraintsIfInvalid(); | 112 this.resetSizeConstraintsIfInvalid(); |
127 | 113 |
128 if (!this.guest.getId()) | 114 if (!this.guest.getId()) |
129 return; | 115 return; |
130 | 116 |
131 this.guest.setSize({ | 117 this.guest.setSize({ |
132 'enableAutoSize': this.element.hasAttribute('autosize'), | 118 'enableAutoSize': this.element.hasAttribute('autosize'), |
133 'min': { | 119 'min': { |
(...skipping 15 matching lines...) Expand all Loading... |
149 newWidth: newWidth, | 135 newWidth: newWidth, |
150 newHeight: newHeight, | 136 newHeight: newHeight, |
151 oldWidth: oldWidth, | 137 oldWidth: oldWidth, |
152 oldHeight: oldHeight | 138 oldHeight: oldHeight |
153 }; | 139 }; |
154 } else { | 140 } else { |
155 this.resize(newWidth, newHeight, oldWidth, oldHeight); | 141 this.resize(newWidth, newHeight, oldWidth, oldHeight); |
156 } | 142 } |
157 }; | 143 }; |
158 | 144 |
159 ExtensionOptionsImpl.prototype.parseExtensionAttribute = function() { | |
160 if (this.element.hasAttribute('extension')) { | |
161 this.extensionId = this.element.getAttribute('extension'); | |
162 return true; | |
163 } | |
164 return false; | |
165 }; | |
166 | |
167 ExtensionOptionsImpl.prototype.resize = | 145 ExtensionOptionsImpl.prototype.resize = |
168 function(newWidth, newHeight, oldWidth, oldHeight) { | 146 function(newWidth, newHeight, oldWidth, oldHeight) { |
169 this.element.style.width = newWidth + 'px'; | 147 this.element.style.width = newWidth + 'px'; |
170 this.element.style.height = newHeight + 'px'; | 148 this.element.style.height = newHeight + 'px'; |
171 | 149 |
172 // Do not allow the options page's dimensions to shrink. This ensures that the | 150 // Do not allow the options page's dimensions to shrink. This ensures that the |
173 // options page has a consistent UI. If the new size is larger than the | 151 // options page has a consistent UI. If the new size is larger than the |
174 // minimum, make that the new minimum size. | 152 // minimum, make that the new minimum size. |
175 if (newWidth > this.minwidth) | 153 if (newWidth > this.minwidth) |
176 this.minwidth = newWidth; | 154 this.minwidth = newWidth; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 this.element.setAttribute(attributeName, value); | 208 this.element.setAttribute(attributeName, value); |
231 }.bind(this), | 209 }.bind(this), |
232 enumerable: true | 210 enumerable: true |
233 }); | 211 }); |
234 }, this); | 212 }, this); |
235 | 213 |
236 this.resetSizeConstraintsIfInvalid(); | 214 this.resetSizeConstraintsIfInvalid(); |
237 | 215 |
238 Object.defineProperty(this.element, 'extension', { | 216 Object.defineProperty(this.element, 'extension', { |
239 get: function() { | 217 get: function() { |
240 return this.extensionId; | 218 return this.element.getAttribute('extension'); |
241 }.bind(this), | 219 }.bind(this), |
242 set: function(value) { | 220 set: function(value) { |
243 this.element.setAttribute('extension', value); | 221 this.element.setAttribute('extension', value); |
244 }.bind(this), | 222 }.bind(this), |
245 enumerable: true | 223 enumerable: true |
246 }); | 224 }); |
247 }; | 225 }; |
248 | 226 |
249 ExtensionOptionsImpl.prototype.resetSizeConstraintsIfInvalid = function () { | 227 ExtensionOptionsImpl.prototype.resetSizeConstraintsIfInvalid = function () { |
250 if (this.minheight > this.maxheight || this.minheight < 0) { | 228 if (this.minheight > this.maxheight || this.minheight < 0) { |
(...skipping 28 matching lines...) Expand all Loading... |
279 ExtensionOptionsImpl.prototype.resumeDeferredAutoSize = function() { | 257 ExtensionOptionsImpl.prototype.resumeDeferredAutoSize = function() { |
280 if (this.autosizeDeferred) { | 258 if (this.autosizeDeferred) { |
281 this.resize(this.deferredAutoSizeState.newWidth, | 259 this.resize(this.deferredAutoSizeState.newWidth, |
282 this.deferredAutoSizeState.newHeight, | 260 this.deferredAutoSizeState.newHeight, |
283 this.deferredAutoSizeState.oldWidth, | 261 this.deferredAutoSizeState.oldWidth, |
284 this.deferredAutoSizeState.oldHeight); | 262 this.deferredAutoSizeState.oldHeight); |
285 } | 263 } |
286 }; | 264 }; |
287 | 265 |
288 GuestViewContainer.registerElement(ExtensionOptionsImpl); | 266 GuestViewContainer.registerElement(ExtensionOptionsImpl); |
OLD | NEW |