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

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

Issue 378783002: Initial implementation of the <extensionoptions> GuestView tag (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove WIP test Created 6 years, 5 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/extension_options.js
diff --git a/chrome/renderer/resources/extensions/extension_options.js b/chrome/renderer/resources/extensions/extension_options.js
new file mode 100644
index 0000000000000000000000000000000000000000..8417674162fd78dd0240075177f76c5f690e2169
--- /dev/null
+++ b/chrome/renderer/resources/extensions/extension_options.js
@@ -0,0 +1,144 @@
+// Copyright 2014 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.
+
+var DocumentNatives = requireNative('document_natives');
+var GuestViewInternal =
+ require('binding').Binding.create('guestViewInternal').generate();
+var IdGenerator = requireNative('id_generator');
+
+function ExtensionOptionsInternal(extensionoptionsNode) {
+ privates(extensionoptionsNode).internal = this;
+ this.extensionoptionsNode = extensionoptionsNode;
+
+ if (this.parseExtensionAttribute()) {
Devlin 2014/07/16 20:55:07 no brackets.
ericzeng 2014/07/16 22:27:38 Done.
+ this.init();
+ }
+};
+
+ExtensionOptionsInternal.prototype.attachWindow = function(instanceId) {
+ this.instanceId = instanceId;
+ var params = {
+ 'instanceId': this.viewInstanceId,
+ }
+ return this.browserPluginNode['-internal-attach'](instanceId, params);
+};
+
+ExtensionOptionsInternal.prototype.createBrowserPluginNode = function() {
+ var browserPluginNode = new ExtensionOptionsInternal.BrowserPlugin();
+ privates(browserPluginNode).internal = this;
+ return browserPluginNode;
+};
+
+ExtensionOptionsInternal.prototype.createGuest = function() {
+ var params = {
+ 'extensionId': this.extensionId,
+ };
+ var self = this;
+ GuestViewInternal.createGuest(
+ 'extensionoptions',
+ params,
+ function(instanceId) {
+ self.instanceId = instanceId;
+ self.attachWindow(instanceId);
+ });
+};
+
+ExtensionOptionsInternal.prototype.handleExtensionOptionsAttributeMutation =
+ function(name, oldValue, newValue) {
+ if (name != 'extension')
+ return;
+ // We treat null attribute (attribute removed) and the empty string as
+ // one case.
+ oldValue = oldValue || '';
+ newValue = newValue || '';
+
+ if (oldValue === newValue)
+ return;
+ this.extensionId = newValue;
+
+ // Create new guest view if one hasn't been created for this element.
+ if (!this.instanceId && this.parseExtensionAttribute()) {
+ this.init();
+ }
+ // TODO(ericzeng): Implement navigation to another guest view if we want
+ // that functionality.
+};
+
+ExtensionOptionsInternal.prototype.init = function() {
+ this.browserPluginNode = this.createBrowserPluginNode();
+ var shadowRoot = this.extensionoptionsNode.createShadowRoot();
+ shadowRoot.appendChild(this.browserPluginNode);
+ this.viewInstanceId = IdGenerator.GetNextId();
+ this.createGuest();
+};
+
+ExtensionOptionsInternal.prototype.parseExtensionAttribute = function() {
+ if (this.extensionoptionsNode.hasAttribute('extension')) {
+ var extensionId = this.extensionoptionsNode.getAttribute('extension');
+ // Only allow extensions to embed their own options page.
+ if (chrome.runtime.id == extensionId) {
+ this.extensionId = extensionId;
+ return true;
+ }
+ }
+ return false;
+};
+
+function registerBrowserPluginElement() {
+ var proto = Object.create(HTMLObjectElement.prototype);
+
+ proto.createdCallback = function() {
+ this.setAttribute('type', 'application/browser-plugin');
+ this.style.width = '100%';
+ this.style.height = '100%';
+ };
+
+ proto.attachedCallback = function() {
+ // Load the plugin immediately.
+ var unused = this.nonExistentAttribute;
+ };
+
+ ExtensionOptionsInternal.BrowserPlugin =
+ DocumentNatives.RegisterElement('extensionoptionsplugin',
+ {extends: 'object', prototype: proto});
+ delete proto.createdCallback;
+ delete proto.attachedCallback;
+ delete proto.detachedCallback;
+ delete proto.attributeChangedCallback;
+}
+
+function registerExtensionOptionsElement() {
+ var proto = Object.create(HTMLElement.prototype);
+
+ proto.createdCallback = function() {
+ new ExtensionOptionsInternal(this);
+ };
+
+ proto.attributeChangedCallback = function(name, oldValue, newValue) {
+ var internal = privates(this).internal;
+ if (!internal)
+ return;
+ internal.handleExtensionOptionsAttributeMutation(name, oldValue, newValue);
+ };
+
+ window.ExtensionOptions =
+ DocumentNatives.RegisterElement('extensionoptions', {prototype: proto});
Devlin 2014/07/16 20:55:07 this indentation is off by one.
ericzeng 2014/07/16 22:27:38 Done.
+
+ // Delete the callbacks so developers cannot call them and produce unexpected
+ // behavior.
+ delete proto.createdCallback;
+ delete proto.attachedCallback;
+ delete proto.detachedCallback;
+ delete proto.attributeChangedCallback;
+}
+
+var useCapture = true;
+window.addEventListener('readystatechange', function listener(event) {
+ if (document.readyState == 'loading')
+ return;
+
+ registerBrowserPluginElement();
+ registerExtensionOptionsElement();
+ window.removeEventListener(event.type, listener, useCapture);
+}, useCapture);

Powered by Google App Engine
This is Rietveld 408576698