OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 var DocumentNatives = requireNative('document_natives'); |
| 6 var GuestViewInternal = |
| 7 require('binding').Binding.create('guestViewInternal').generate(); |
| 8 var IdGenerator = requireNative('id_generator'); |
| 9 |
| 10 function ExtensionOptionsInternal(extensionoptionsNode) { |
| 11 privates(extensionoptionsNode).internal = this; |
| 12 this.extensionoptionsNode = extensionoptionsNode; |
| 13 |
| 14 if (this.parseExtensionAttribute()) |
| 15 this.init(); |
| 16 }; |
| 17 |
| 18 ExtensionOptionsInternal.prototype.attachWindow = function(instanceId) { |
| 19 this.instanceId = instanceId; |
| 20 var params = { |
| 21 'instanceId': this.viewInstanceId, |
| 22 } |
| 23 return this.browserPluginNode['-internal-attach'](instanceId, params); |
| 24 }; |
| 25 |
| 26 ExtensionOptionsInternal.prototype.createBrowserPluginNode = function() { |
| 27 var browserPluginNode = new ExtensionOptionsInternal.BrowserPlugin(); |
| 28 privates(browserPluginNode).internal = this; |
| 29 return browserPluginNode; |
| 30 }; |
| 31 |
| 32 ExtensionOptionsInternal.prototype.createGuest = function() { |
| 33 var params = { |
| 34 'extensionId': this.extensionId, |
| 35 }; |
| 36 var self = this; |
| 37 GuestViewInternal.createGuest( |
| 38 'extensionoptions', |
| 39 params, |
| 40 function(instanceId) { |
| 41 self.instanceId = instanceId; |
| 42 self.attachWindow(instanceId); |
| 43 }); |
| 44 }; |
| 45 |
| 46 ExtensionOptionsInternal.prototype.handleExtensionOptionsAttributeMutation = |
| 47 function(name, oldValue, newValue) { |
| 48 if (name != 'extension') |
| 49 return; |
| 50 // We treat null attribute (attribute removed) and the empty string as |
| 51 // one case. |
| 52 oldValue = oldValue || ''; |
| 53 newValue = newValue || ''; |
| 54 |
| 55 if (oldValue === newValue) |
| 56 return; |
| 57 this.extensionId = newValue; |
| 58 |
| 59 // Create new guest view if one hasn't been created for this element. |
| 60 if (!this.instanceId && this.parseExtensionAttribute()) |
| 61 this.init(); |
| 62 // TODO(ericzeng): Implement navigation to another guest view if we want |
| 63 // that functionality. |
| 64 }; |
| 65 |
| 66 ExtensionOptionsInternal.prototype.init = function() { |
| 67 this.browserPluginNode = this.createBrowserPluginNode(); |
| 68 var shadowRoot = this.extensionoptionsNode.createShadowRoot(); |
| 69 shadowRoot.appendChild(this.browserPluginNode); |
| 70 this.viewInstanceId = IdGenerator.GetNextId(); |
| 71 this.createGuest(); |
| 72 }; |
| 73 |
| 74 ExtensionOptionsInternal.prototype.parseExtensionAttribute = function() { |
| 75 if (this.extensionoptionsNode.hasAttribute('extension')) { |
| 76 var extensionId = this.extensionoptionsNode.getAttribute('extension'); |
| 77 // Only allow extensions to embed their own options page (if it has one). |
| 78 if (chrome.runtime.id == extensionId && |
| 79 chrome.runtime.getManifest().hasOwnProperty('options_page')) { |
| 80 this.extensionId = extensionId; |
| 81 return true; |
| 82 } |
| 83 } |
| 84 return false; |
| 85 }; |
| 86 |
| 87 function registerBrowserPluginElement() { |
| 88 var proto = Object.create(HTMLObjectElement.prototype); |
| 89 |
| 90 proto.createdCallback = function() { |
| 91 this.setAttribute('type', 'application/browser-plugin'); |
| 92 this.style.width = '100%'; |
| 93 this.style.height = '100%'; |
| 94 }; |
| 95 |
| 96 proto.attachedCallback = function() { |
| 97 // Load the plugin immediately. |
| 98 var unused = this.nonExistentAttribute; |
| 99 }; |
| 100 |
| 101 ExtensionOptionsInternal.BrowserPlugin = |
| 102 DocumentNatives.RegisterElement('extensionoptionsplugin', |
| 103 {extends: 'object', prototype: proto}); |
| 104 delete proto.createdCallback; |
| 105 delete proto.attachedCallback; |
| 106 delete proto.detachedCallback; |
| 107 delete proto.attributeChangedCallback; |
| 108 } |
| 109 |
| 110 function registerExtensionOptionsElement() { |
| 111 var proto = Object.create(HTMLElement.prototype); |
| 112 |
| 113 proto.createdCallback = function() { |
| 114 new ExtensionOptionsInternal(this); |
| 115 }; |
| 116 |
| 117 proto.attributeChangedCallback = function(name, oldValue, newValue) { |
| 118 var internal = privates(this).internal; |
| 119 if (!internal) |
| 120 return; |
| 121 internal.handleExtensionOptionsAttributeMutation(name, oldValue, newValue); |
| 122 }; |
| 123 |
| 124 window.ExtensionOptions = |
| 125 DocumentNatives.RegisterElement('extensionoptions', {prototype: proto}); |
| 126 |
| 127 // Delete the callbacks so developers cannot call them and produce unexpected |
| 128 // behavior. |
| 129 delete proto.createdCallback; |
| 130 delete proto.attachedCallback; |
| 131 delete proto.detachedCallback; |
| 132 delete proto.attributeChangedCallback; |
| 133 } |
| 134 |
| 135 var useCapture = true; |
| 136 window.addEventListener('readystatechange', function listener(event) { |
| 137 if (document.readyState == 'loading') |
| 138 return; |
| 139 |
| 140 registerBrowserPluginElement(); |
| 141 registerExtensionOptionsElement(); |
| 142 window.removeEventListener(event.type, listener, useCapture); |
| 143 }, useCapture); |
OLD | NEW |