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. |
| 6 // |
| 7 // The actual tag is implemented via the browser plugin. The internals of this |
| 8 // are hidden via Shadow DOM. |
| 9 |
| 10 var BROWSER_TAG_ATTRIBUTES = ['src', 'width', 'height']; |
| 11 |
| 12 // Handle <browser> tags already in the document. |
| 13 window.addEventListener('DOMContentLoaded', function() { |
| 14 var browserNodes = document.body.querySelectorAll('browser'); |
| 15 for (var i = 0, browserNode; browserNode = browserNodes[i]; i++) { |
| 16 new BrowserTag(browserNode); |
| 17 } |
| 18 }); |
| 19 |
| 20 // Handle <browser> tags added later. |
| 21 var observer = new WebKitMutationObserver(function(mutations) { |
| 22 mutations.forEach(function(mutation) { |
| 23 for (var i = 0, addedNode; addedNode = mutation.addedNodes[i]; i++) { |
| 24 if (addedNode.tagName == 'BROWSER') { |
| 25 new BrowserTag(addedNode); |
| 26 } |
| 27 } |
| 28 }); |
| 29 }); |
| 30 observer.observe(document, {subtree: true, childList: true}); |
| 31 |
| 32 /** |
| 33 * @constructor |
| 34 */ |
| 35 function BrowserTag(node) { |
| 36 this.node_ = node; |
| 37 var shadowRoot = new WebKitShadowRoot(node); |
| 38 |
| 39 this.objectNode_ = document.createElement('object'); |
| 40 this.objectNode_.type = 'application/browser-plugin'; |
| 41 BROWSER_TAG_ATTRIBUTES.forEach(this.copyAttribute_, this); |
| 42 shadowRoot.appendChild(this.objectNode_); |
| 43 |
| 44 // Map attribute modifications on the <browser> tag to changes on the |
| 45 // underlying <object> node. |
| 46 var handleMutation = this.handleMutation_.bind(this); |
| 47 var observer = new WebKitMutationObserver(function(mutations) { |
| 48 mutations.forEach(handleMutation); |
| 49 }); |
| 50 observer.observe( |
| 51 this.node_, |
| 52 {attributes: true, attributeFilter: BROWSER_TAG_ATTRIBUTES}); |
| 53 |
| 54 // Expose getters and setters for the attributes. |
| 55 BROWSER_TAG_ATTRIBUTES.forEach(function(attributeName) { |
| 56 Object.defineProperty(this.node_, attributeName, { |
| 57 get: function() { |
| 58 var value = node.getAttribute(attributeName); |
| 59 var numericValue = parseInt(value, 10); |
| 60 return isNaN(numericValue) ? value : numericValue; |
| 61 }, |
| 62 set: function(value) { |
| 63 node.setAttribute(attributeName, value); |
| 64 }, |
| 65 enumerable: true |
| 66 }); |
| 67 }, this); |
| 68 }; |
| 69 |
| 70 /** |
| 71 * @private |
| 72 */ |
| 73 BrowserTag.prototype.handleMutation_ = function(mutation) { |
| 74 switch (mutation.attributeName) { |
| 75 case 'src': |
| 76 this.objectNode_.postMessage(this.node_.getAttribute('src')); |
| 77 break; |
| 78 default: |
| 79 this.copyAttribute_(mutation.attributeName); |
| 80 break; |
| 81 } |
| 82 }; |
| 83 |
| 84 /** |
| 85 * @private |
| 86 */ |
| 87 BrowserTag.prototype.copyAttribute_ = function(attributeName) { |
| 88 this.objectNode_.setAttribute( |
| 89 attributeName, this.node_.getAttribute(attributeName)); |
| 90 }; |
OLD | NEW |