| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 var DocumentNatives = requireNative('document_natives'); | 5 var DocumentNatives = requireNative('document_natives'); |
| 6 var ExtensionOptionsEvents = | 6 var ExtensionOptionsEvents = |
| 7 require('extensionOptionsEvents').ExtensionOptionsEvents; | 7 require('extensionOptionsEvents').ExtensionOptionsEvents; |
| 8 var GuestViewInternal = | 8 var GuestViewInternal = |
| 9 require('binding').Binding.create('guestViewInternal').generate(); | 9 require('binding').Binding.create('guestViewInternal').generate(); |
| 10 var IdGenerator = requireNative('id_generator'); | 10 var IdGenerator = requireNative('id_generator'); |
| 11 var utils = require('utils'); | 11 var utils = require('utils'); |
| 12 | 12 |
| 13 // Mapping of the autosize attribute names to default values | 13 // Mapping of the autosize attribute names to default values |
| 14 var AUTO_SIZE_ATTRIBUTES = { | 14 var AUTO_SIZE_ATTRIBUTES = { |
| 15 'autosize': 'on', | 15 'autosize': 'on', |
| 16 'maxheight': 600, | 16 'maxheight': 600, |
| 17 'maxwidth': 800, | 17 'maxwidth': 800, |
| 18 'minheight': 32, | 18 'minheight': 32, |
| 19 'minwidth': 80 | 19 'minwidth': 80 |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 function ExtensionOptionsInternal(extensionoptionsNode) { | 22 function ExtensionOptionsInternal(extensionoptionsNode) { |
| 23 privates(extensionoptionsNode).internal = this; | 23 privates(extensionoptionsNode).internal = this; |
| 24 this.extensionoptionsNode = extensionoptionsNode; | 24 this.extensionoptionsNode = extensionoptionsNode; |
| 25 this.viewInstanceId = IdGenerator.GetNextId(); | 25 this.viewInstanceId = IdGenerator.GetNextId(); |
| 26 | 26 |
| 27 // on* Event handlers. | 27 // on* Event handlers. |
| 28 this.eventHandlers = {}; | 28 this.eventHandlers = {}; |
| 29 |
| 30 // setupEventProperty is normally called in extension_options_events.js to |
| 31 // register events, but the createfailed event is registered here because |
| 32 // the event is fired from here instead of through |
| 33 // extension_options_events.js. |
| 34 this.setupEventProperty('createfailed'); |
| 35 |
| 29 new ExtensionOptionsEvents(this, this.viewInstanceId); | 36 new ExtensionOptionsEvents(this, this.viewInstanceId); |
| 30 | 37 |
| 31 this.setupNodeProperties(); | 38 this.setupNodeProperties(); |
| 32 | 39 |
| 33 if (this.parseExtensionAttribute()) | 40 if (this.parseExtensionAttribute()) |
| 34 this.init(); | 41 this.init(); |
| 35 }; | 42 }; |
| 36 | 43 |
| 37 ExtensionOptionsInternal.prototype.attachWindow = function(instanceId) { | 44 ExtensionOptionsInternal.prototype.attachWindow = function(instanceId) { |
| 38 this.instanceId = instanceId; | 45 this.instanceId = instanceId; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 55 | 62 |
| 56 ExtensionOptionsInternal.prototype.createGuest = function() { | 63 ExtensionOptionsInternal.prototype.createGuest = function() { |
| 57 var params = { | 64 var params = { |
| 58 'extensionId': this.extensionId, | 65 'extensionId': this.extensionId, |
| 59 }; | 66 }; |
| 60 GuestViewInternal.createGuest( | 67 GuestViewInternal.createGuest( |
| 61 'extensionoptions', | 68 'extensionoptions', |
| 62 params, | 69 params, |
| 63 function(instanceId) { | 70 function(instanceId) { |
| 64 if (instanceId == 0) { | 71 if (instanceId == 0) { |
| 72 // Fire a createfailed event here rather than in ExtensionOptionsGuest |
| 73 // because the guest will not be created, and cannot fire an event. |
| 65 this.initCalled = false; | 74 this.initCalled = false; |
| 75 var createFailedEvent = new Event('createfailed', { bubbles: true }); |
| 76 this.dispatchEvent(createFailedEvent); |
| 66 } else { | 77 } else { |
| 67 this.attachWindow(instanceId); | 78 this.attachWindow(instanceId); |
| 68 GuestViewInternal.setAutoSize(this.instanceId, { | 79 GuestViewInternal.setAutoSize(this.instanceId, { |
| 69 'enableAutoSize': | 80 'enableAutoSize': |
| 70 this.extensionoptionsNode.hasAttribute('autosize'), | 81 this.extensionoptionsNode.hasAttribute('autosize'), |
| 71 'min': { | 82 'min': { |
| 72 'width': parseInt(this.minwidth || 0), | 83 'width': parseInt(this.minwidth || 0), |
| 73 'height': parseInt(this.minheight || 0) | 84 'height': parseInt(this.minheight || 0) |
| 74 }, | 85 }, |
| 75 'max': { | 86 'max': { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 this.createGuest(); | 146 this.createGuest(); |
| 136 }; | 147 }; |
| 137 | 148 |
| 138 ExtensionOptionsInternal.prototype.onSizeChanged = function(width, height) { | 149 ExtensionOptionsInternal.prototype.onSizeChanged = function(width, height) { |
| 139 this.browserPluginNode.style.width = width + 'px'; | 150 this.browserPluginNode.style.width = width + 'px'; |
| 140 this.browserPluginNode.style.height = height + 'px'; | 151 this.browserPluginNode.style.height = height + 'px'; |
| 141 }; | 152 }; |
| 142 | 153 |
| 143 ExtensionOptionsInternal.prototype.parseExtensionAttribute = function() { | 154 ExtensionOptionsInternal.prototype.parseExtensionAttribute = function() { |
| 144 if (this.extensionoptionsNode.hasAttribute('extension')) { | 155 if (this.extensionoptionsNode.hasAttribute('extension')) { |
| 145 var extensionId = this.extensionoptionsNode.getAttribute('extension'); | 156 this.extensionId = this.extensionoptionsNode.getAttribute('extension'); |
| 146 // Only allow extensions to embed their own options page (if it has one). | 157 return true; |
| 147 if (chrome.runtime.id == extensionId && | |
| 148 chrome.runtime.getManifest().hasOwnProperty('options_page')) { | |
| 149 this.extensionId = extensionId; | |
| 150 return true; | |
| 151 } | |
| 152 } | 158 } |
| 153 return false; | 159 return false; |
| 154 }; | 160 }; |
| 155 | 161 |
| 156 // Adds an 'on<event>' property on the view, which can be used to set/unset | 162 // Adds an 'on<event>' property on the view, which can be used to set/unset |
| 157 // an event handler. | 163 // an event handler. |
| 158 ExtensionOptionsInternal.prototype.setupEventProperty = function(eventName) { | 164 ExtensionOptionsInternal.prototype.setupEventProperty = function(eventName) { |
| 159 var propertyName = 'on' + eventName.toLowerCase(); | 165 var propertyName = 'on' + eventName.toLowerCase(); |
| 160 var self = this; | |
| 161 var extensionoptionsNode = this.extensionoptionsNode; | 166 var extensionoptionsNode = this.extensionoptionsNode; |
| 162 Object.defineProperty(extensionoptionsNode, propertyName, { | 167 Object.defineProperty(extensionoptionsNode, propertyName, { |
| 163 get: function() { | 168 get: function() { |
| 164 return self.eventHandlers[propertyName]; | 169 return this.eventHandlers[propertyName]; |
| 165 }, | 170 }.bind(this), |
| 166 set: function(value) { | 171 set: function(value) { |
| 167 if (self.eventHandlers[propertyName]) | 172 if (this.eventHandlers[propertyName]) |
| 168 extensionoptionsNode.removeEventListener( | 173 extensionoptionsNode.removeEventListener( |
| 169 eventName, self.eventHandlers[propertyName]); | 174 eventName, this.eventHandlers[propertyName]); |
| 170 self.eventHandlers[propertyName] = value; | 175 this.eventHandlers[propertyName] = value; |
| 171 if (value) | 176 if (value) |
| 172 extensionoptionsNode.addEventListener(eventName, value); | 177 extensionoptionsNode.addEventListener(eventName, value); |
| 173 }, | 178 }.bind(this), |
| 174 enumerable: true | 179 enumerable: true |
| 175 }); | 180 }); |
| 176 }; | 181 }; |
| 177 | 182 |
| 178 ExtensionOptionsInternal.prototype.setupNodeProperties = function() { | 183 ExtensionOptionsInternal.prototype.setupNodeProperties = function() { |
| 179 utils.forEach(AUTO_SIZE_ATTRIBUTES, function(attributeName) { | 184 utils.forEach(AUTO_SIZE_ATTRIBUTES, function(attributeName) { |
| 180 // Get the size constraints from the <extensionoptions> tag, or use the | 185 // Get the size constraints from the <extensionoptions> tag, or use the |
| 181 // defaults if not specified | 186 // defaults if not specified |
| 182 if (this.extensionoptionsNode.hasAttribute(attributeName)) { | 187 if (this.extensionoptionsNode.hasAttribute(attributeName)) { |
| 183 this[attributeName] = | 188 this[attributeName] = |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 | 276 |
| 272 var useCapture = true; | 277 var useCapture = true; |
| 273 window.addEventListener('readystatechange', function listener(event) { | 278 window.addEventListener('readystatechange', function listener(event) { |
| 274 if (document.readyState == 'loading') | 279 if (document.readyState == 'loading') |
| 275 return; | 280 return; |
| 276 | 281 |
| 277 registerBrowserPluginElement(); | 282 registerBrowserPluginElement(); |
| 278 registerExtensionOptionsElement(); | 283 registerExtensionOptionsElement(); |
| 279 window.removeEventListener(event.type, listener, useCapture); | 284 window.removeEventListener(event.type, listener, useCapture); |
| 280 }, useCapture); | 285 }, useCapture); |
| OLD | NEW |