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 this.node_.setAttribute(mutation.attributeName, |
| 146 this.objectNode_.getAttribute(mutation.attributeName)); |
| 147 }; |
| 148 |
| 149 /** |
| 150 * @private |
| 151 */ |
136 WebView.prototype.setupEvent_ = function(eventname, attribs) { | 152 WebView.prototype.setupEvent_ = function(eventname, attribs) { |
137 var node = this.node_; | 153 var node = this.node_; |
138 this.objectNode_.addEventListener('-internal-' + eventname, function(e) { | 154 this.objectNode_.addEventListener('-internal-' + eventname, function(e) { |
139 var evt = new Event(eventname); | 155 var evt = new Event(eventname); |
140 var detail = e.detail ? JSON.parse(e.detail) : {}; | 156 var detail = e.detail ? JSON.parse(e.detail) : {}; |
141 attribs.forEach(function(attribName) { | 157 attribs.forEach(function(attribName) { |
142 evt[attribName] = detail[attribName]; | 158 evt[attribName] = detail[attribName]; |
143 }); | 159 }); |
144 node.dispatchEvent(evt); | 160 node.dispatchEvent(evt); |
145 }); | 161 }); |
146 } | 162 } |
OLD | NEW |