Chromium Code Reviews| 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 <browser> tag via Mutation Observers. | 5 // Shim that simulates a <browser> 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 BROWSER_TAG_ATTRIBUTES = ['src', 'width', 'height']; | 10 var BROWSER_TAG_ATTRIBUTES = ['src', 'width', 'height']; |
| 11 | 11 |
| 12 // All exposed api methods for <browser>, these are forwarded to the browser | |
| 13 // plugin. | |
| 14 var BROWSER_TAG_API_METHODS = [ | |
| 15 'addEventListener', | |
| 16 'back', | |
| 17 'forward', | |
| 18 'getProcessId', | |
| 19 'go', | |
| 20 'reload', | |
| 21 'removeEventListener', | |
| 22 'stop', | |
| 23 'terminate' | |
| 24 ]; | |
| 25 | |
| 12 window.addEventListener('DOMContentLoaded', function() { | 26 window.addEventListener('DOMContentLoaded', function() { |
| 13 // Handle <browser> tags already in the document. | 27 // Handle <browser> tags already in the document. |
| 14 var browserNodes = document.body.querySelectorAll('browser'); | 28 var browserNodes = document.body.querySelectorAll('browser'); |
| 15 for (var i = 0, browserNode; browserNode = browserNodes[i]; i++) { | 29 for (var i = 0, browserNode; browserNode = browserNodes[i]; i++) { |
| 16 new BrowserTag(browserNode); | 30 new BrowserTag(browserNode); |
| 17 } | 31 } |
| 18 | 32 |
| 19 // Handle <browser> tags added later. | 33 // Handle <browser> tags added later. |
| 20 var documentObserver = new WebKitMutationObserver(function(mutations) { | 34 var documentObserver = new WebKitMutationObserver(function(mutations) { |
| 21 mutations.forEach(function(mutation) { | 35 mutations.forEach(function(mutation) { |
| 22 for (var i = 0, addedNode; addedNode = mutation.addedNodes[i]; i++) { | 36 for (var i = 0, addedNode; addedNode = mutation.addedNodes[i]; i++) { |
| 23 if (addedNode.tagName == 'BROWSER') { | 37 if (addedNode.tagName == 'BROWSER') { |
| 24 new BrowserTag(addedNode); | 38 new BrowserTag(addedNode); |
| 25 } | 39 } |
| 26 } | 40 } |
| 27 }); | 41 }); |
| 28 }); | 42 }); |
| 29 documentObserver.observe(document, {subtree: true, childList: true}); | 43 documentObserver.observe(document, {subtree: true, childList: true}); |
| 30 }); | 44 }); |
| 31 | 45 |
| 32 /** | 46 /** |
| 33 * @constructor | 47 * @constructor |
| 34 */ | 48 */ |
| 35 function BrowserTag(node) { | 49 function BrowserTag(node) { |
| 36 this.node_ = node; | 50 this.node_ = node; |
| 37 var shadowRoot = new WebKitShadowRoot(node); | 51 var shadowRoot = new WebKitShadowRoot(node); |
| 38 | 52 |
| 39 this.objectNode_ = document.createElement('object'); | 53 this.objectNode_ = document.createElement('object'); |
| 40 this.objectNode_.type = 'application/browser-plugin'; | 54 this.objectNode_.type = 'application/browser-plugin'; |
| 55 var objectNode = this.objectNode_; | |
| 41 BROWSER_TAG_ATTRIBUTES.forEach(this.copyAttribute_, this); | 56 BROWSER_TAG_ATTRIBUTES.forEach(this.copyAttribute_, this); |
| 57 BROWSER_TAG_API_METHODS.forEach(function(apiMethod) { | |
| 58 node[apiMethod] = function(var_args) { | |
| 59 return objectNode[apiMethod].apply(objectNode, arguments); | |
|
sadrul
2012/10/12 22:03:49
node[apiMethod] = objectNode[apiMethod].bind(objec
Mihai Parparita -not on Chrome
2012/10/12 22:19:41
+1
lazyboy
2012/10/12 23:21:16
Cool, thanks.
| |
| 60 }; | |
| 61 }); | |
|
Mihai Parparita -not on Chrome
2012/10/12 22:19:41
You just pass in this as an argument to forEach, a
lazyboy
2012/10/12 23:21:16
Yes, now doing this;
I didn't do it b/c I was acce
| |
| 42 shadowRoot.appendChild(this.objectNode_); | 62 shadowRoot.appendChild(this.objectNode_); |
| 43 | 63 |
| 44 // Map attribute modifications on the <browser> tag to changes on the | 64 // Map attribute modifications on the <browser> tag to changes on the |
| 45 // underlying <object> node. | 65 // underlying <object> node. |
| 46 var handleMutation = this.handleMutation_.bind(this); | 66 var handleMutation = this.handleMutation_.bind(this); |
| 47 var observer = new WebKitMutationObserver(function(mutations) { | 67 var observer = new WebKitMutationObserver(function(mutations) { |
| 48 mutations.forEach(handleMutation); | 68 mutations.forEach(handleMutation); |
| 49 }); | 69 }); |
| 50 observer.observe( | 70 observer.observe( |
| 51 this.node_, | 71 this.node_, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 } | 105 } |
| 86 }; | 106 }; |
| 87 | 107 |
| 88 /** | 108 /** |
| 89 * @private | 109 * @private |
| 90 */ | 110 */ |
| 91 BrowserTag.prototype.copyAttribute_ = function(attributeName) { | 111 BrowserTag.prototype.copyAttribute_ = function(attributeName) { |
| 92 this.objectNode_.setAttribute( | 112 this.objectNode_.setAttribute( |
| 93 attributeName, this.node_.getAttribute(attributeName)); | 113 attributeName, this.node_.getAttribute(attributeName)); |
| 94 }; | 114 }; |
| OLD | NEW |