Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7712)

Unified Diff: chrome/renderer/resources/extensions/browser_tag.js

Issue 10598006: Browser tag shim (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Ready for review Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/renderer/resources/extensions/browser_tag.js
diff --git a/chrome/renderer/resources/extensions/browser_tag.js b/chrome/renderer/resources/extensions/browser_tag.js
new file mode 100644
index 0000000000000000000000000000000000000000..1c1708ce53c7207c7d95a90ef7559486bed1d3d1
--- /dev/null
+++ b/chrome/renderer/resources/extensions/browser_tag.js
@@ -0,0 +1,97 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Shim that simulates a <browser> tag via Mutation Observers and the trick
+// documented at http://ajaxian.com/archives/web-ninja-interview-erik-arvidsson
+// to subclass HTML elements.
+//
+// The actual tag is implemented via the browser plugin. The internals of this
+// are hidden via Shadow DOM.
+
+var BROWSER_TAG_ATTRIBUTES = ['src', 'width', 'height'];
+
+// Handle <browser> tags already in the document.
+window.addEventListener('DOMContentLoaded', function() {
+ var browserNodes = document.body.getElementsByTagName('browser');
+ for (var i = 0, browserNode; browserNode = browserNodes[i]; i++) {
+ BrowserTag.decorate(browserNode);
+ }
+});
+
+// Handle <browser> tags added later.
+var observer = new WebKitMutationObserver(function(mutations) {
+ mutations.forEach(function(mutation) {
+ for (var i = 0, addedNode; addedNode = mutation.addedNodes[i]; i++) {
+ if (addedNode.tagName == 'BROWSER') {
+ BrowserTag.decorate(addedNode);
+ }
+ }
+ });
+});
+observer.observe(document, {subtree: true, childList: true});
+
+/**
+ * @constructor
+ */
+function BrowserTag() {
+ BrowserTag.decorate_(document.createElement('browser'));
+}
+
+/**
+ * @param {Node} node
+ */
+BrowserTag.decorate = function(node) {
+ // Make sure we don't decorate twice.
+ if (node instanceof BrowserTag) {
+ return;
+ }
+ node.__proto__ = BrowserTag.prototype;
+ node.decorate_();
+}
+
+BrowserTag.prototype.__proto__ = HTMLElement.prototype;
+
+/**
+ * @private
+ */
+BrowserTag.prototype.decorate_ = function() {
+ 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
+
+ this.objectNode_ = document.createElement('object');
+ // Force compositing, since the plugin only works for accelerated pages.
+ 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.
+ this.objectNode_.setAttribute('type', 'application/browser-plugin');
+ BROWSER_TAG_ATTRIBUTES.forEach(this.copyAttribute_, this);
+ this.shadowRoot_.appendChild(this.objectNode_);
+
+ var handleMutation = this.handleMutation_.bind(this);
+ this.observer_ = new WebKitMutationObserver(function(mutations) {
+ mutations.forEach(handleMutation);
+ });
+ this.observer_.observe(
+ this,
+ {attributes: true, attributeFilter: BROWSER_TAG_ATTRIBUTES});
+}
+
+/**
+ * @private
+ */
+BrowserTag.prototype.handleMutation_ = function(mutation) {
+ switch (mutation.attributeName) {
+ case 'src':
+ this.objectNode_.postMessage(this.getAttribute('src'));
+ break;
+ default:
+ this.copyAttribute_(mutation.attributeName);
+ break;
+ }
+};
+
+/**
+ * @private
+ */
+BrowserTag.prototype.copyAttribute_ = function(attributeName) {
+ this.objectNode_.setAttribute(
+ attributeName, this.getAttribute(attributeName));
+};

Powered by Google App Engine
This is Rietveld 408576698