OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Shim that simulates a <webview> tag via Mutation Observers. | 5 // Shim that simulates a <webview> tag via Mutation Observers. |
6 // | 6 // |
7 // The actual tag is implemented via the browser plugin. The internals of this | 7 // The actual tag is implemented via the browser plugin. The internals of this |
8 // are hidden via Shadow DOM. | 8 // are hidden via Shadow DOM. |
9 | 9 |
10 var watchForTag = require("tagWatcher").watchForTag; | 10 var watchForTag = require("tagWatcher").watchForTag; |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 // contentWindow.postMessage until http://crbug.com/152006 is fixed. | 119 // contentWindow.postMessage until http://crbug.com/152006 is fixed. |
120 if (objectNode.contentWindow) | 120 if (objectNode.contentWindow) |
121 return objectNode.contentWindow.self; | 121 return objectNode.contentWindow.self; |
122 console.error('contentWindow is not available at this time. ' + | 122 console.error('contentWindow is not available at this time. ' + |
123 'It will become available when the page has finished loading.'); | 123 'It will become available when the page has finished loading.'); |
124 }, | 124 }, |
125 // No setter. | 125 // No setter. |
126 enumerable: true | 126 enumerable: true |
127 }); | 127 }); |
128 | 128 |
| 129 this.setupExecuteScript_(); |
| 130 |
129 for (var eventName in WEB_VIEW_EVENTS) { | 131 for (var eventName in WEB_VIEW_EVENTS) { |
130 this.setupEvent_(eventName, WEB_VIEW_EVENTS[eventName]); | 132 this.setupEvent_(eventName, WEB_VIEW_EVENTS[eventName]); |
131 } | 133 } |
132 this.maybeSetupPermissionEvent_(); | 134 this.maybeSetupPermissionEvent_(); |
133 } | 135 } |
134 | 136 |
135 /** | 137 /** |
136 * @private | 138 * @private |
137 */ | 139 */ |
138 WebView.prototype.handleMutation_ = function(mutation) { | 140 WebView.prototype.handleMutation_ = function(mutation) { |
(...skipping 25 matching lines...) Expand all Loading... |
164 // loop. Thus, we avoid this loop by only updating the <webview> attribute | 166 // loop. Thus, we avoid this loop by only updating the <webview> attribute |
165 // if the BrowserPlugin attributes differs from it. | 167 // if the BrowserPlugin attributes differs from it. |
166 var oldValue = this.node_.getAttribute(mutation.attributeName); | 168 var oldValue = this.node_.getAttribute(mutation.attributeName); |
167 var newValue = this.objectNode_.getAttribute(mutation.attributeName); | 169 var newValue = this.objectNode_.getAttribute(mutation.attributeName); |
168 if (newValue != oldValue) { | 170 if (newValue != oldValue) { |
169 this.node_.setAttribute(mutation.attributeName, newValue); | 171 this.node_.setAttribute(mutation.attributeName, newValue); |
170 } | 172 } |
171 } | 173 } |
172 }; | 174 }; |
173 | 175 |
| 176 WebView.prototype.setupExecuteScript_ = function() { |
| 177 var node = this.node_; |
| 178 var callbackMap = new Array(); |
| 179 var self = this; |
| 180 node['executeScript'] = function(injectDetails, resultCallback) { |
| 181 var result = self.objectNode_.executeScript(injectDetails); |
| 182 callbackMap[result] = resultCallback; |
| 183 }; |
| 184 this.objectNode_.addEventListener('-internal-scriptresponse', function(e) { |
| 185 var detail = JSON.parse(e.detail); |
| 186 if (callbackMap[detail.requestId]) { |
| 187 console.log(detail.result); |
| 188 callbackMap[detail.requestId].apply(self.node_, detail.result); |
| 189 delete callbackMap[detail.requestId]; |
| 190 } |
| 191 }); |
| 192 }; |
| 193 |
174 /** | 194 /** |
175 * @private | 195 * @private |
176 */ | 196 */ |
177 WebView.prototype.setupEvent_ = function(eventname, attribs) { | 197 WebView.prototype.setupEvent_ = function(eventname, attribs) { |
178 var node = this.node_; | 198 var node = this.node_; |
179 this.objectNode_.addEventListener('-internal-' + eventname, function(e) { | 199 this.objectNode_.addEventListener('-internal-' + eventname, function(e) { |
180 var evt = new Event(eventname, { bubbles: true }); | 200 var evt = new Event(eventname, { bubbles: true }); |
181 var detail = e.detail ? JSON.parse(e.detail) : {}; | 201 var detail = e.detail ? JSON.parse(e.detail) : {}; |
182 attribs.forEach(function(attribName) { | 202 attribs.forEach(function(attribName) { |
183 evt[attribName] = detail[attribName]; | 203 evt[attribName] = detail[attribName]; |
184 }); | 204 }); |
185 node.dispatchEvent(evt); | 205 node.dispatchEvent(evt); |
186 }); | 206 }); |
187 }; | 207 }; |
188 | 208 |
189 /** | 209 /** |
190 * Implemented when experimental permission is available. | 210 * Implemented when experimental permission is available. |
191 * @private | 211 * @private |
192 */ | 212 */ |
193 WebView.prototype.maybeSetupPermissionEvent_ = function() {}; | 213 WebView.prototype.maybeSetupPermissionEvent_ = function() {}; |
194 | 214 |
195 exports.WebView = WebView; | 215 exports.WebView = WebView; |
OLD | NEW |