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 WEB_VIEW_ATTRIBUTES = ['src']; | 10 var WEB_VIEW_ATTRIBUTES = ['src']; |
11 | 11 |
12 var WEB_VIEW_READONLY_ATTRIBUTES = ['contentWindow']; | 12 var WEB_VIEW_READONLY_ATTRIBUTES = ['contentWindow']; |
13 | 13 |
14 // All exposed api methods for <webview>, these are forwarded to the browser | 14 // All exposed api methods for <webview>, these are forwarded to the browser |
15 // plugin. | 15 // plugin. |
16 var WEB_VIEW_API_METHODS = [ | 16 var WEB_VIEW_API_METHODS = [ |
17 'addEventListener', | 17 'back', |
18 'back', | 18 'canGoBack', |
19 'canGoBack', | 19 'canGoForward', |
20 'canGoForward', | 20 'forward', |
21 'forward', | 21 'getProcessId', |
22 'getProcessId', | 22 'go', |
23 'go', | 23 'reload', |
24 'reload', | 24 'stop', |
25 'removeEventListener', | 25 'terminate' |
26 'stop', | 26 ]; |
27 'terminate' | 27 |
28 ]; | 28 var WEB_VIEW_EVENTS = { |
29 'exit' : [ 'processId', 'reason' ], | |
arv (Not doing code reviews)
2012/11/12 22:58:07
no ws after [ nor before ]
sadrul
2012/11/12 23:03:21
Done.
| |
30 'loadabort' : [ 'url', 'isTopLevel', 'reason' ], | |
31 'loadcommit' : [ 'url', 'isTopLevel' ], | |
32 'loadredirect' : [ 'oldurl', 'newurl', 'isTopLevel' ], | |
33 'loadstart' : [ 'url', 'isTopLevel' ], | |
34 'loadstop' : [], | |
35 'sizechanged': [ 'oldHeight', 'oldWidth', 'newHeight', 'newWidth' ], | |
36 }; | |
29 | 37 |
30 window.addEventListener('DOMContentLoaded', function() { | 38 window.addEventListener('DOMContentLoaded', function() { |
31 // Handle <webview> tags already in the document. | 39 // Handle <webview> tags already in the document. |
32 var webViewNodes = document.body.querySelectorAll('webview'); | 40 var webViewNodes = document.body.querySelectorAll('webview'); |
33 for (var i = 0, webViewNode; webViewNode = webViewNodes[i]; i++) { | 41 for (var i = 0, webViewNode; webViewNode = webViewNodes[i]; i++) { |
34 new WebView(webViewNode); | 42 new WebView(webViewNode); |
35 } | 43 } |
36 | 44 |
37 // Handle <webview> tags added later. | 45 // Handle <webview> tags added later. |
38 var documentObserver = new WebKitMutationObserver(function(mutations) { | 46 var documentObserver = new WebKitMutationObserver(function(mutations) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 WEB_VIEW_READONLY_ATTRIBUTES.forEach(function(attributeName) { | 112 WEB_VIEW_READONLY_ATTRIBUTES.forEach(function(attributeName) { |
105 Object.defineProperty(this.node_, attributeName, { | 113 Object.defineProperty(this.node_, attributeName, { |
106 get: function() { | 114 get: function() { |
107 // Read these attributes from the plugin <object>. | 115 // Read these attributes from the plugin <object>. |
108 return objectNode[attributeName]; | 116 return objectNode[attributeName]; |
109 }, | 117 }, |
110 // No setter. | 118 // No setter. |
111 enumerable: true | 119 enumerable: true |
112 }) | 120 }) |
113 }, this); | 121 }, this); |
122 | |
123 for (eventname in WEB_VIEW_EVENTS) { | |
arv (Not doing code reviews)
2012/11/12 22:58:07
eventName
arv (Not doing code reviews)
2012/11/12 22:58:07
missing var
You should really use strict mode for
sadrul
2012/11/12 23:03:21
Done.
sadrul
2012/11/12 23:03:21
Done.
| |
124 this.setupEvent_(eventname, WEB_VIEW_EVENTS[eventname]); | |
125 } | |
114 }; | 126 }; |
115 | 127 |
116 /** | 128 /** |
117 * @private | 129 * @private |
118 */ | 130 */ |
119 WebView.prototype.handleMutation_ = function(mutation) { | 131 WebView.prototype.handleMutation_ = function(mutation) { |
120 switch (mutation.attributeName) { | 132 switch (mutation.attributeName) { |
121 case 'src': | 133 case 'src': |
122 // We need to set .src directly on the shadow element so that | 134 // We need to set .src directly on the shadow element so that |
123 // BrowserPluginBindings catches this as src attribute mutation. The | 135 // BrowserPluginBindings catches this as src attribute mutation. The |
124 // bindings would catch 'SetAttribute' method call with src as argument | 136 // bindings would catch 'SetAttribute' method call with src as argument |
125 // otherwise. | 137 // otherwise. |
126 this.objectNode_.src = this.node_.getAttribute('src'); | 138 this.objectNode_.src = this.node_.getAttribute('src'); |
127 break; | 139 break; |
128 default: | 140 default: |
129 this.copyAttribute_(mutation.attributeName); | 141 this.copyAttribute_(mutation.attributeName); |
130 break; | 142 break; |
131 } | 143 } |
132 }; | 144 }; |
133 | 145 |
134 /** | 146 /** |
135 * @private | 147 * @private |
136 */ | 148 */ |
137 WebView.prototype.copyAttribute_ = function(attributeName) { | 149 WebView.prototype.copyAttribute_ = function(attributeName) { |
138 this.objectNode_.setAttribute( | 150 this.objectNode_.setAttribute( |
139 attributeName, this.node_.getAttribute(attributeName)); | 151 attributeName, this.node_.getAttribute(attributeName)); |
140 }; | 152 }; |
153 | |
154 /** | |
155 * @private | |
156 */ | |
157 WebView.prototype.setupEvent_ = function(eventname, attribs) { | |
158 var node = this.node_; | |
159 this.objectNode_.addEventListener('-internal-' + eventname, function(e) { | |
160 var evt = new Event(eventname); | |
161 var detail = e.detail ? JSON.parse(e.detail) : {}; | |
162 attribs.forEach(function(attribname) { | |
arv (Not doing code reviews)
2012/11/12 22:58:07
attribName
sadrul
2012/11/12 23:03:21
Done.
| |
163 evt[attribname] = detail[attribname]; | |
164 }); | |
165 node.dispatchEvent(evt); | |
166 }); | |
167 } | |
OLD | NEW |