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', 'partition']; | 10 var WEB_VIEW_ATTRIBUTES = ['src', 'partition']; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 // Map attribute modifications on the <webview> tag to property changes in | 83 // Map attribute modifications on the <webview> tag to property changes in |
84 // the underlying <object> node. | 84 // the underlying <object> node. |
85 var handleMutation = this.handleMutation_.bind(this); | 85 var handleMutation = this.handleMutation_.bind(this); |
86 var observer = new WebKitMutationObserver(function(mutations) { | 86 var observer = new WebKitMutationObserver(function(mutations) { |
87 mutations.forEach(handleMutation); | 87 mutations.forEach(handleMutation); |
88 }); | 88 }); |
89 observer.observe( | 89 observer.observe( |
90 this.node_, | 90 this.node_, |
91 {attributes: true, attributeFilter: WEB_VIEW_ATTRIBUTES}); | 91 {attributes: true, attributeFilter: WEB_VIEW_ATTRIBUTES}); |
92 | 92 |
93 var handleObjectMutation = this.handleObjectMutation_.bind(this); | |
94 var objectObserver = new WebKitMutationObserver(function(mutations) { | |
95 mutations.forEach(handleObjectMutation); | |
96 }); | |
97 objectObserver.observe( | |
98 this.objectNode_, | |
99 {attributes: true, attributeFilter: WEB_VIEW_ATTRIBUTES}); | |
100 | |
93 var objectNode = this.objectNode_; | 101 var objectNode = this.objectNode_; |
94 // Expose getters and setters for the attributes. | 102 // Expose getters and setters for the attributes. |
95 WEB_VIEW_ATTRIBUTES.forEach(function(attributeName) { | 103 WEB_VIEW_ATTRIBUTES.forEach(function(attributeName) { |
96 Object.defineProperty(this.node_, attributeName, { | 104 Object.defineProperty(this.node_, attributeName, { |
97 get: function() { | 105 get: function() { |
98 return objectNode[attributeName]; | 106 return objectNode[attributeName]; |
99 }, | 107 }, |
100 set: function(value) { | 108 set: function(value) { |
101 objectNode[attributeName] = value; | 109 objectNode[attributeName] = value; |
102 }, | 110 }, |
(...skipping 16 matching lines...) Expand all Loading... | |
119 | 127 |
120 for (var eventName in WEB_VIEW_EVENTS) { | 128 for (var eventName in WEB_VIEW_EVENTS) { |
121 this.setupEvent_(eventName, WEB_VIEW_EVENTS[eventName]); | 129 this.setupEvent_(eventName, WEB_VIEW_EVENTS[eventName]); |
122 } | 130 } |
123 } | 131 } |
124 | 132 |
125 /** | 133 /** |
126 * @private | 134 * @private |
127 */ | 135 */ |
128 WebView.prototype.handleMutation_ = function(mutation) { | 136 WebView.prototype.handleMutation_ = function(mutation) { |
129 this.node_[mutation.attributeName] = | 137 this.objectNode_[mutation.attributeName] = |
130 this.node_.getAttribute(mutation.attributeName); | 138 this.node_.getAttribute(mutation.attributeName); |
131 }; | 139 }; |
132 | 140 |
133 /** | 141 /** |
134 * @private | 142 * @private |
135 */ | 143 */ |
144 WebView.prototype.handleObjectMutation_ = function(mutation) { | |
145 console.log('observed object mutation'); | |
sadrul
2012/12/04 01:28:46
remove this?
Fady Samuel
2012/12/04 01:57:30
Done.
| |
146 this.node_.setAttribute(mutation.attributeName, | |
147 this.objectNode_.getAttribute(mutation.attributeName)); | |
148 }; | |
149 | |
150 /** | |
151 * @private | |
152 */ | |
136 WebView.prototype.setupEvent_ = function(eventname, attribs) { | 153 WebView.prototype.setupEvent_ = function(eventname, attribs) { |
137 var node = this.node_; | 154 var node = this.node_; |
138 this.objectNode_.addEventListener('-internal-' + eventname, function(e) { | 155 this.objectNode_.addEventListener('-internal-' + eventname, function(e) { |
139 var evt = new Event(eventname); | 156 var evt = new Event(eventname); |
140 var detail = e.detail ? JSON.parse(e.detail) : {}; | 157 var detail = e.detail ? JSON.parse(e.detail) : {}; |
141 attribs.forEach(function(attribName) { | 158 attribs.forEach(function(attribName) { |
142 evt[attribName] = detail[attribName]; | 159 evt[attribName] = detail[attribName]; |
143 }); | 160 }); |
144 node.dispatchEvent(evt); | 161 node.dispatchEvent(evt); |
145 }); | 162 }); |
146 } | 163 } |
OLD | NEW |