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 // This module implements the shared functionality for different guestview | 5 // This module implements the shared functionality for different guestview |
6 // containers, such as web_view, app_view, etc. | 6 // containers, such as web_view, app_view, etc. |
7 | 7 |
8 var DocumentNatives = requireNative('document_natives'); | 8 var DocumentNatives = requireNative('document_natives'); |
9 var GuestView = require('guestView').GuestView; | 9 var GuestView = require('guestView').GuestView; |
10 var GuestViewInternalNatives = requireNative('guest_view_internal'); | 10 var GuestViewInternalNatives = requireNative('guest_view_internal'); |
11 var IdGenerator = requireNative('id_generator'); | 11 var IdGenerator = requireNative('id_generator'); |
12 | 12 |
13 function GuestViewContainer(element, viewType) { | 13 function GuestViewContainer(element, viewType) { |
14 privates(element).internal = this; | 14 privates(element).internal = this; |
15 this.attributes = {}; | 15 this.attributes = {}; |
16 this.element = element; | 16 this.element = element; |
17 this.elementAttached = false; | 17 this.elementAttached = false; |
18 this.viewInstanceId = IdGenerator.GetNextId(); | 18 this.viewInstanceId = IdGenerator.GetNextId(); |
19 this.viewType = viewType; | 19 this.viewType = viewType; |
20 | 20 |
21 this.setupGuestProperty(); | 21 this.setupGuestProperty(); |
22 this.guest = new GuestView(viewType); | 22 this.guest = new GuestView(viewType); |
23 this.setupAttributes(); | 23 this.setupAttributes(); |
24 | 24 |
25 privates(this).browserPluginElement = this.createBrowserPluginElement(); | 25 privates(this).browserPluginElement = this.createBrowserPluginElement(); |
| 26 this.guest.setBrowserPluginElement(privates(this).browserPluginElement); |
26 this.setupFocusPropagation(); | 27 this.setupFocusPropagation(); |
27 var shadowRoot = this.element.createShadowRoot(); | 28 var shadowRoot = this.element.createShadowRoot(); |
28 shadowRoot.appendChild(privates(this).browserPluginElement); | 29 shadowRoot.appendChild(privates(this).browserPluginElement); |
29 } | 30 } |
30 | 31 |
31 // Forward public API methods from |proto| to their internal implementations. | 32 // Forward public API methods from |proto| to their internal implementations. |
32 GuestViewContainer.forwardApiMethods = function(proto, apiMethods) { | 33 GuestViewContainer.forwardApiMethods = function(proto, apiMethods) { |
33 var createProtoHandler = function(m) { | 34 var createProtoHandler = function(m) { |
34 return function(var_args) { | 35 return function(var_args) { |
35 var internal = privates(this).internal; | 36 var internal = privates(this).internal; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 85 |
85 GuestViewContainer.prototype.createBrowserPluginElement = function() { | 86 GuestViewContainer.prototype.createBrowserPluginElement = function() { |
86 // We create BrowserPlugin as a custom element in order to observe changes | 87 // We create BrowserPlugin as a custom element in order to observe changes |
87 // to attributes synchronously. | 88 // to attributes synchronously. |
88 var browserPluginElement = | 89 var browserPluginElement = |
89 new GuestViewContainer[this.viewType + 'BrowserPlugin'](); | 90 new GuestViewContainer[this.viewType + 'BrowserPlugin'](); |
90 privates(browserPluginElement).internal = this; | 91 privates(browserPluginElement).internal = this; |
91 return browserPluginElement; | 92 return browserPluginElement; |
92 }; | 93 }; |
93 | 94 |
| 95 GuestViewContainer.prototype.getBrowserPluginElement = function() { |
| 96 return privates(this).browserPluginElement; |
| 97 }; |
| 98 |
94 GuestViewContainer.prototype.setupFocusPropagation = function() { | 99 GuestViewContainer.prototype.setupFocusPropagation = function() { |
95 if (!this.element.hasAttribute('tabIndex')) { | 100 if (!this.element.hasAttribute('tabIndex')) { |
96 // GuestViewContainer needs a tabIndex in order to be focusable. | 101 // GuestViewContainer needs a tabIndex in order to be focusable. |
97 // TODO(fsamuel): It would be nice to avoid exposing a tabIndex attribute | 102 // TODO(fsamuel): It would be nice to avoid exposing a tabIndex attribute |
98 // to allow GuestViewContainer to be focusable. | 103 // to allow GuestViewContainer to be focusable. |
99 // See http://crbug.com/231664. | 104 // See http://crbug.com/231664. |
100 this.element.setAttribute('tabIndex', -1); | 105 this.element.setAttribute('tabIndex', -1); |
101 } | 106 } |
102 this.element.addEventListener('focus', function(e) { | 107 this.element.addEventListener('focus', function(e) { |
103 // Focus the BrowserPlugin when the GuestViewContainer takes focus. | 108 // Focus the BrowserPlugin when the GuestViewContainer takes focus. |
104 privates(this).browserPluginElement.focus(); | 109 privates(this).browserPluginElement.focus(); |
105 }.bind(this)); | 110 }.bind(this)); |
106 this.element.addEventListener('blur', function(e) { | 111 this.element.addEventListener('blur', function(e) { |
107 // Blur the BrowserPlugin when the GuestViewContainer loses focus. | 112 // Blur the BrowserPlugin when the GuestViewContainer loses focus. |
108 privates(this).browserPluginElement.blur(); | 113 privates(this).browserPluginElement.blur(); |
109 }.bind(this)); | 114 }.bind(this)); |
110 }; | 115 }; |
111 | 116 |
112 GuestViewContainer.prototype.attachWindow = function() { | 117 GuestViewContainer.prototype.attachWindow = function() { |
113 if (!this.internalInstanceId) { | 118 if (!this.internalInstanceId) { |
114 return true; | 119 return true; |
115 } | 120 } |
116 | 121 |
117 this.guest.attach(this.internalInstanceId, | 122 this.guest.attach(this.internalInstanceId, |
118 this.viewInstanceId, | 123 this.viewInstanceId, |
119 this.buildParams()); | 124 this.buildParams()); |
120 return true; | 125 return true; |
121 }; | 126 }; |
122 | 127 |
| 128 GuestViewContainer.prototype.onInternalInstanceID = function( |
| 129 internalInstanceId) { |
| 130 window.console.log('onInternalInstanceID: ' + internalInstanceId); |
| 131 this.internalInstanceId = internalInstanceId; |
| 132 |
| 133 // Track when the element resizes using the element resize callback. |
| 134 GuestViewInternalNatives.RegisterElementResizeCallback( |
| 135 this.internalInstanceId, this.onElementResize.bind(this)); |
| 136 |
| 137 window.console.log('this.guest.getId() = ' + |
| 138 this.guest.getId()); |
| 139 if (!this.guest.getId()) { |
| 140 return; |
| 141 } |
| 142 this.guest.attach(this.internalInstanceId, |
| 143 this.viewInstanceId, |
| 144 this.buildParams()); |
| 145 }; |
| 146 |
123 GuestViewContainer.prototype.handleBrowserPluginAttributeMutation = | 147 GuestViewContainer.prototype.handleBrowserPluginAttributeMutation = |
124 function(name, oldValue, newValue) { | 148 function(name, oldValue, newValue) { |
125 if (name == 'internalinstanceid' && !oldValue && !!newValue) { | 149 if (name == 'internalinstanceid' && !oldValue && !!newValue) { |
126 privates(this).browserPluginElement.removeAttribute('internalinstanceid'); | 150 privates(this).browserPluginElement.removeAttribute('internalinstanceid'); |
127 this.internalInstanceId = parseInt(newValue); | 151 this.onInternalInstanceID(parseInt(newValue)); |
128 | |
129 // Track when the element resizes using the element resize callback. | |
130 GuestViewInternalNatives.RegisterElementResizeCallback( | |
131 this.internalInstanceId, this.onElementResize.bind(this)); | |
132 | |
133 if (!this.guest.getId()) { | |
134 return; | |
135 } | |
136 this.guest.attach(this.internalInstanceId, | |
137 this.viewInstanceId, | |
138 this.buildParams()); | |
139 } | 152 } |
140 }; | 153 }; |
141 | 154 |
142 GuestViewContainer.prototype.onElementResize = function(newWidth, newHeight) { | 155 GuestViewContainer.prototype.onElementResize = function(newWidth, newHeight) { |
143 if (!this.guest.getId()) | 156 if (!this.guest.getId()) |
144 return; | 157 return; |
145 this.guest.setSize({normal: {width: newWidth, height: newHeight}}); | 158 this.guest.setSize({normal: {width: newWidth, height: newHeight}}); |
146 }; | 159 }; |
147 | 160 |
148 GuestViewContainer.prototype.buildParams = function() { | 161 GuestViewContainer.prototype.buildParams = function() { |
(...skipping 19 matching lines...) Expand all Loading... |
168 | 181 |
169 // Implemented by the specific view type, if needed. | 182 // Implemented by the specific view type, if needed. |
170 GuestViewContainer.prototype.buildContainerParams = function() { return {}; }; | 183 GuestViewContainer.prototype.buildContainerParams = function() { return {}; }; |
171 GuestViewContainer.prototype.onElementAttached = function() {}; | 184 GuestViewContainer.prototype.onElementAttached = function() {}; |
172 GuestViewContainer.prototype.onElementDetached = function() {}; | 185 GuestViewContainer.prototype.onElementDetached = function() {}; |
173 GuestViewContainer.prototype.setupAttributes = function() {}; | 186 GuestViewContainer.prototype.setupAttributes = function() {}; |
174 | 187 |
175 // Registers the browser plugin <object> custom element. |viewType| is the | 188 // Registers the browser plugin <object> custom element. |viewType| is the |
176 // name of the specific guestview container (e.g. 'webview'). | 189 // name of the specific guestview container (e.g. 'webview'). |
177 function registerBrowserPluginElement(viewType) { | 190 function registerBrowserPluginElement(viewType) { |
178 var proto = $Object.create(HTMLElement.prototype); | 191 var isSitePerProcess = GuestViewInternalNatives.IsSitePerProcess(); |
| 192 var proto = $Object.create(isSitePerProcess ? HTMLIFrameElement.prototype |
| 193 : HTMLElement.prototype); |
| 194 |
| 195 window.console.log('isSitePerProcess: ' + isSitePerProcess); |
179 | 196 |
180 proto.createdCallback = function() { | 197 proto.createdCallback = function() { |
181 this.setAttribute('type', 'application/browser-plugin'); | 198 if (!isSitePerProcess) { |
182 this.setAttribute('id', 'browser-plugin-' + IdGenerator.GetNextId()); | 199 this.setAttribute('type', 'application/browser-plugin'); |
| 200 this.setAttribute('id', 'browser-plugin-' + IdGenerator.GetNextId()); |
| 201 } |
183 this.style.width = '100%'; | 202 this.style.width = '100%'; |
184 this.style.height = '100%'; | 203 this.style.height = '100%'; |
185 }; | 204 }; |
186 | 205 |
187 proto.attachedCallback = function() { | 206 proto.attachedCallback = function() { |
188 // Load the plugin immediately. | 207 // Load the plugin immediately. |
189 var unused = this.nonExistentAttribute; | 208 var unused = this.nonExistentAttribute; |
190 }; | 209 }; |
191 | 210 |
192 proto.attributeChangedCallback = function(name, oldValue, newValue) { | 211 if (!isSitePerProcess) { |
193 var internal = privates(this).internal; | 212 proto.attributeChangedCallback = function(name, oldValue, newValue) { |
194 if (!internal) { | 213 var internal = privates(this).internal; |
195 return; | 214 if (!internal) { |
196 } | 215 return; |
197 internal.handleBrowserPluginAttributeMutation(name, oldValue, newValue); | 216 } |
198 }; | 217 internal.handleBrowserPluginAttributeMutation(name, oldValue, newValue); |
| 218 }; |
| 219 } |
199 | 220 |
| 221 var options = {prototype: proto}; |
| 222 options.extends = isSitePerProcess ? 'iframe' : 'object'; |
200 GuestViewContainer[viewType + 'BrowserPlugin'] = | 223 GuestViewContainer[viewType + 'BrowserPlugin'] = |
201 DocumentNatives.RegisterElement(viewType + 'browserplugin', | 224 DocumentNatives.RegisterElement(viewType + 'browserplugin', options); |
202 {extends: 'object', prototype: proto}); | |
203 | 225 |
204 delete proto.createdCallback; | 226 delete proto.createdCallback; |
205 delete proto.attachedCallback; | 227 delete proto.attachedCallback; |
206 delete proto.detachedCallback; | 228 delete proto.detachedCallback; |
207 delete proto.attributeChangedCallback; | 229 delete proto.attributeChangedCallback; |
208 }; | 230 }; |
209 | 231 |
210 // Registers the guestview container as a custom element. | 232 // Registers the guestview container as a custom element. |
211 // |guestViewContainerType| is the type of guestview container | 233 // |guestViewContainerType| is the type of guestview container |
212 // (e.g.WebViewImpl). | 234 // (e.g.WebViewImpl). |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 // Delete the callbacks so developers cannot call them and produce unexpected | 283 // Delete the callbacks so developers cannot call them and produce unexpected |
262 // behavior. | 284 // behavior. |
263 delete proto.createdCallback; | 285 delete proto.createdCallback; |
264 delete proto.attachedCallback; | 286 delete proto.attachedCallback; |
265 delete proto.detachedCallback; | 287 delete proto.detachedCallback; |
266 delete proto.attributeChangedCallback; | 288 delete proto.attributeChangedCallback; |
267 } | 289 } |
268 | 290 |
269 // Exports. | 291 // Exports. |
270 exports.GuestViewContainer = GuestViewContainer; | 292 exports.GuestViewContainer = GuestViewContainer; |
OLD | NEW |