OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // Shim that simulates a <browser> tag via Mutation Observers and the trick | |
6 // documented at http://ajaxian.com/archives/web-ninja-interview-erik-arvidsson | |
7 // to subclass HTML elements. | |
8 // | |
9 // The actual tag is implemented via the browser plugin. The internals of this | |
10 // are hidden via Shadow DOM. | |
11 | |
12 var BROWSER_TAG_ATTRIBUTES = ['src', 'width', 'height']; | |
13 | |
14 // Handle <browser> tags already in the document. | |
15 window.addEventListener('DOMContentLoaded', function() { | |
16 var browserNodes = document.body.getElementsByTagName('browser'); | |
17 for (var i = 0, browserNode; browserNode = browserNodes[i]; i++) { | |
18 BrowserTag.decorate(browserNode); | |
19 } | |
20 }); | |
21 | |
22 // Handle <browser> tags added later. | |
23 var observer = new WebKitMutationObserver(function(mutations) { | |
24 mutations.forEach(function(mutation) { | |
25 for (var i = 0, addedNode; addedNode = mutation.addedNodes[i]; i++) { | |
26 if (addedNode.tagName == 'BROWSER') { | |
27 BrowserTag.decorate(addedNode); | |
28 } | |
29 } | |
30 }); | |
31 }); | |
32 observer.observe(document, {subtree: true, childList: true}); | |
33 | |
34 /** | |
35 * @constructor | |
36 */ | |
37 function BrowserTag() { | |
38 BrowserTag.decorate_(document.createElement('browser')); | |
39 } | |
40 | |
41 /** | |
42 * @param {Node} node | |
43 */ | |
44 BrowserTag.decorate = function(node) { | |
45 // Make sure we don't decorate twice. | |
46 if (node instanceof BrowserTag) { | |
47 return; | |
48 } | |
49 node.__proto__ = BrowserTag.prototype; | |
50 node.decorate_(); | |
51 } | |
52 | |
53 BrowserTag.prototype.__proto__ = HTMLElement.prototype; | |
54 | |
55 /** | |
56 * @private | |
57 */ | |
58 BrowserTag.prototype.decorate_ = function() { | |
59 this.shadowRoot_ = new WebKitShadowRoot(this); | |
dglazkov
2012/06/21 16:22:07
Is it ok for these members to be enumerable? They
Mihai Parparita -not on Chrome
2012/06/25 20:29:43
Switched to using Object.defineProperty so that th
| |
60 | |
61 this.objectNode_ = document.createElement('object'); | |
62 // Force compositing, since the plugin only works for accelerated pages. | |
63 this.objectNode_.style.webkitTransform = 'translateZ(0)'; | |
Fady Samuel
2012/06/21 23:14:46
This should be unnecessary. We just landed a patch
Mihai Parparita -not on Chrome
2012/06/25 20:29:43
Removed.
| |
64 this.objectNode_.setAttribute('type', 'application/browser-plugin'); | |
65 BROWSER_TAG_ATTRIBUTES.forEach(this.copyAttribute_, this); | |
66 this.shadowRoot_.appendChild(this.objectNode_); | |
67 | |
68 var handleMutation = this.handleMutation_.bind(this); | |
69 this.observer_ = new WebKitMutationObserver(function(mutations) { | |
70 mutations.forEach(handleMutation); | |
71 }); | |
72 this.observer_.observe( | |
73 this, | |
74 {attributes: true, attributeFilter: BROWSER_TAG_ATTRIBUTES}); | |
75 } | |
76 | |
77 /** | |
78 * @private | |
79 */ | |
80 BrowserTag.prototype.handleMutation_ = function(mutation) { | |
81 switch (mutation.attributeName) { | |
82 case 'src': | |
83 this.objectNode_.postMessage(this.getAttribute('src')); | |
84 break; | |
85 default: | |
86 this.copyAttribute_(mutation.attributeName); | |
87 break; | |
88 } | |
89 }; | |
90 | |
91 /** | |
92 * @private | |
93 */ | |
94 BrowserTag.prototype.copyAttribute_ = function(attributeName) { | |
95 this.objectNode_.setAttribute( | |
96 attributeName, this.getAttribute(attributeName)); | |
97 }; | |
OLD | NEW |