Chromium Code Reviews| 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. | |
|
Aaron Boodman
2012/07/02 20:52:35
Is the plan to actually implement a <browser> tag
Mihai Parparita -not on Chrome
2012/07/02 22:05:14
Perhaps, but I don't think Apple would be too rece
Aaron Boodman
2012/07/02 23:04:37
I don't think so. I wondered if you were consideri
| |
| 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']; | |
|
Aaron Boodman
2012/07/02 20:52:35
I can't remember, but I think that these names mig
Mihai Parparita -not on Chrome
2012/07/02 22:05:14
The module system wrap everything in an anonymous
Aaron Boodman
2012/07/02 23:04:37
Cool.
| |
| 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 this.shadowRoot_ = new WebKitShadowRoot(node); | |
|
arv (Not doing code reviews)
2012/07/02 21:04:28
No need to make this a property
Mihai Parparita -not on Chrome
2012/07/02 22:05:14
Done. The observer didn't need to be a property ei
| |
| 38 | |
| 39 this.objectNode_ = document.createElement('object'); | |
| 40 this.objectNode_.type = 'application/browser-plugin'; | |
| 41 BROWSER_TAG_ATTRIBUTES.forEach(this.copyAttribute_, this); | |
| 42 this.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 this.observer_ = new WebKitMutationObserver(function(mutations) { | |
| 48 mutations.forEach(handleMutation); | |
| 49 }); | |
| 50 this.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 } | |
|
arv (Not doing code reviews)
2012/07/02 21:04:28
missing semicolon
Mihai Parparita -not on Chrome
2012/07/02 22:05:14
Done.
| |
| OLD | NEW |