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

Side by Side Diff: chrome/renderer/resources/extensions/browser_tag.js

Issue 11139005: Expose Browser Plugin API in <browser> Tag Shim. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments + add test. Created 8 years, 2 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/test/data/extensions/platform_apps/browser_tag/main.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_;
Mihai Parparita -not on Chrome 2012/10/13 00:13:53 This is no longer needed.
lazyboy 2012/10/13 02:44:08 Removed.
41 BROWSER_TAG_ATTRIBUTES.forEach(this.copyAttribute_, this); 56 BROWSER_TAG_ATTRIBUTES.forEach(this.copyAttribute_, this);
57
42 shadowRoot.appendChild(this.objectNode_); 58 shadowRoot.appendChild(this.objectNode_);
43 59
60 // this.objectNode_[apiMethod] are defined after the shadow object is appended
61 // to the shadow root.
62 BROWSER_TAG_API_METHODS.forEach(function(apiMethod) {
63 node[apiMethod] = this.objectNode_[apiMethod].bind(this.objectNode_);
64 }, this);
65
44 // Map attribute modifications on the <browser> tag to changes on the 66 // Map attribute modifications on the <browser> tag to changes on the
45 // underlying <object> node. 67 // underlying <object> node.
46 var handleMutation = this.handleMutation_.bind(this); 68 var handleMutation = this.handleMutation_.bind(this);
47 var observer = new WebKitMutationObserver(function(mutations) { 69 var observer = new WebKitMutationObserver(function(mutations) {
48 mutations.forEach(handleMutation); 70 mutations.forEach(handleMutation);
49 }); 71 });
50 observer.observe( 72 observer.observe(
51 this.node_, 73 this.node_,
52 {attributes: true, attributeFilter: BROWSER_TAG_ATTRIBUTES}); 74 {attributes: true, attributeFilter: BROWSER_TAG_ATTRIBUTES});
53 75
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 } 107 }
86 }; 108 };
87 109
88 /** 110 /**
89 * @private 111 * @private
90 */ 112 */
91 BrowserTag.prototype.copyAttribute_ = function(attributeName) { 113 BrowserTag.prototype.copyAttribute_ = function(attributeName) {
92 this.objectNode_.setAttribute( 114 this.objectNode_.setAttribute(
93 attributeName, this.node_.getAttribute(attributeName)); 115 attributeName, this.node_.getAttribute(attributeName));
94 }; 116 };
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/extensions/platform_apps/browser_tag/main.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698