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) { |
(...skipping 10 matching lines...) Expand all Loading... |
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'; |
41 BROWSER_TAG_ATTRIBUTES.forEach(this.copyAttribute_, this); | 55 BROWSER_TAG_ATTRIBUTES.forEach(this.copyAttribute_, this); |
| 56 |
42 shadowRoot.appendChild(this.objectNode_); | 57 shadowRoot.appendChild(this.objectNode_); |
43 | 58 |
| 59 // this.objectNode_[apiMethod] are defined after the shadow object is appended |
| 60 // to the shadow root. |
| 61 BROWSER_TAG_API_METHODS.forEach(function(apiMethod) { |
| 62 node[apiMethod] = this.objectNode_[apiMethod].bind(this.objectNode_); |
| 63 }, this); |
| 64 |
44 // Map attribute modifications on the <browser> tag to changes on the | 65 // Map attribute modifications on the <browser> tag to changes on the |
45 // underlying <object> node. | 66 // underlying <object> node. |
46 var handleMutation = this.handleMutation_.bind(this); | 67 var handleMutation = this.handleMutation_.bind(this); |
47 var observer = new WebKitMutationObserver(function(mutations) { | 68 var observer = new WebKitMutationObserver(function(mutations) { |
48 mutations.forEach(handleMutation); | 69 mutations.forEach(handleMutation); |
49 }); | 70 }); |
50 observer.observe( | 71 observer.observe( |
51 this.node_, | 72 this.node_, |
52 {attributes: true, attributeFilter: BROWSER_TAG_ATTRIBUTES}); | 73 {attributes: true, attributeFilter: BROWSER_TAG_ATTRIBUTES}); |
53 | 74 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 } | 106 } |
86 }; | 107 }; |
87 | 108 |
88 /** | 109 /** |
89 * @private | 110 * @private |
90 */ | 111 */ |
91 BrowserTag.prototype.copyAttribute_ = function(attributeName) { | 112 BrowserTag.prototype.copyAttribute_ = function(attributeName) { |
92 this.objectNode_.setAttribute( | 113 this.objectNode_.setAttribute( |
93 attributeName, this.node_.getAttribute(attributeName)); | 114 attributeName, this.node_.getAttribute(attributeName)); |
94 }; | 115 }; |
OLD | NEW |