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

Side by Side 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: Address kalman's comments, add new test for privileged extension apis 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 unified diff | Download patch
OLDNEW
(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.
78 if (chrome.runtime.id == extensionId) {
79 this.extensionId = extensionId;
80 return true;
81 }
82 }
83 return false;
84 };
85
86 function registerBrowserPluginElement() {
87 var proto = Object.create(HTMLObjectElement.prototype);
88
89 proto.createdCallback = function() {
90 this.setAttribute('type', 'application/browser-plugin');
91 this.style.width = '100%';
92 this.style.height = '100%';
93 };
94
95 proto.attachedCallback = function() {
96 // Load the plugin immediately.
97 var unused = this.nonExistentAttribute;
98 };
99
100 ExtensionOptionsInternal.BrowserPlugin =
101 DocumentNatives.RegisterElement('extensionoptionsplugin',
102 {extends: 'object', prototype: proto});
103 delete proto.createdCallback;
104 delete proto.attachedCallback;
105 delete proto.detachedCallback;
106 delete proto.attributeChangedCallback;
107 }
108
109 function registerExtensionOptionsElement() {
110 var proto = Object.create(HTMLElement.prototype);
111
112 proto.createdCallback = function() {
113 new ExtensionOptionsInternal(this);
114 };
115
116 proto.attributeChangedCallback = function(name, oldValue, newValue) {
117 var internal = privates(this).internal;
118 if (!internal)
119 return;
120 internal.handleExtensionOptionsAttributeMutation(name, oldValue, newValue);
121 };
122
123 window.ExtensionOptions =
124 DocumentNatives.RegisterElement('extensionoptions', {prototype: proto});
125
126 // Delete the callbacks so developers cannot call them and produce unexpected
127 // behavior.
128 delete proto.createdCallback;
129 delete proto.attachedCallback;
130 delete proto.detachedCallback;
131 delete proto.attributeChangedCallback;
132 }
133
134 var useCapture = true;
135 window.addEventListener('readystatechange', function listener(event) {
136 if (document.readyState == 'loading')
137 return;
138
139 registerBrowserPluginElement();
140 registerExtensionOptionsElement();
141 window.removeEventListener(event.type, listener, useCapture);
142 }, useCapture);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698