diff options
author | koz@chromium.org <koz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-18 06:35:38 +0000 |
---|---|---|
committer | koz@chromium.org <koz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-18 06:35:38 +0000 |
commit | bc514050e8a6b0692ac52007c2fa0eccfce55789 (patch) | |
tree | b5a8c089fc4b2f8c5f5af81b2730c3c43c48019a /chrome/renderer | |
parent | 6f63e82590b9459a56dad76c1db888400c308eb3 (diff) | |
download | chromium_src-bc514050e8a6b0692ac52007c2fa0eccfce55789.zip chromium_src-bc514050e8a6b0692ac52007c2fa0eccfce55789.tar.gz chromium_src-bc514050e8a6b0692ac52007c2fa0eccfce55789.tar.bz2 |
Split logic for defining custom types in schema_generated_bindings.js into separate files.
BUG=80310
TEST=
Review URL: http://codereview.chromium.org/9179006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@118070 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
6 files changed, 177 insertions, 120 deletions
diff --git a/chrome/renderer/extensions/custom_bindings_util.cc b/chrome/renderer/extensions/custom_bindings_util.cc index b4ff5a5..d4b8018 100644 --- a/chrome/renderer/extensions/custom_bindings_util.cc +++ b/chrome/renderer/extensions/custom_bindings_util.cc @@ -33,11 +33,14 @@ std::vector<v8::Extension*> GetAll(ExtensionDispatcher* extension_dispatcher) { // Must match kResourceIDs. static const char* kJavascriptFiles[] = { "extensions/browser_action_custom_bindings.js", + "extensions/content_settings_custom_bindings.js", "extensions/devtools_custom_bindings.js", "extensions/experimental.input.ime_custom_bindings.js", + "extensions/experimental.storage_custom_bindings.js", "extensions/omnibox_custom_bindings.js", "extensions/page_action_custom_bindings.js", "extensions/tts_engine_custom_bindings.js", + "extensions/types_custom_bindings.js", "extensions/windows_custom_bindings.js", }; static const size_t kJavascriptFilesSize = arraysize(kJavascriptFiles); @@ -45,11 +48,14 @@ std::vector<v8::Extension*> GetAll(ExtensionDispatcher* extension_dispatcher) { // Must match kJavascriptFiles. static const int kResourceIDs[] = { IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS, + IDR_CONTENT_SETTINGS_CUSTOM_BINDINGS_JS, IDR_DEVTOOLS_CUSTOM_BINDINGS_JS, IDR_EXPERIMENTAL_INPUT_IME_CUSTOM_BINDINGS_JS, + IDR_EXPERIMENTAL_STORAGE_CUSTOM_BINDINGS_JS, IDR_OMNIBOX_CUSTOM_BINDINGS_JS, IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS, IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS, + IDR_TYPES_CUSTOM_BINDINGS_JS, IDR_WINDOWS_CUSTOM_BINDINGS_JS, }; static const size_t kResourceIDsSize = arraysize(kResourceIDs); diff --git a/chrome/renderer/renderer_resources.grd b/chrome/renderer/renderer_resources.grd index 20d99b9..0275f04 100644 --- a/chrome/renderer/renderer_resources.grd +++ b/chrome/renderer/renderer_resources.grd @@ -30,20 +30,23 @@ without changes to the corresponding grd file. fb9 --> <!-- Custom bindings for extension APIs. --> <include name="IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS" file="resources\extensions\browser_action_custom_bindings.js" type="BINDATA" /> <include name="IDR_CHROME_PRIVATE_CUSTOM_BINDINGS_JS" file="resources\extensions\chrome_private_custom_bindings.js" type="BINDATA" /> + <include name="IDR_CONTENT_SETTINGS_CUSTOM_BINDINGS_JS" file="resources\extensions\content_settings_custom_bindings.js" type="BINDATA" /> <include name="IDR_CONTEXT_MENUS_CUSTOM_BINDINGS_JS" file="resources\extensions\context_menus_custom_bindings.js" type="BINDATA" /> <include name="IDR_DEVTOOLS_CUSTOM_BINDINGS_JS" file="resources\extensions\devtools_custom_bindings.js" type="BINDATA" /> <include name="IDR_EXPERIMENTAL_INPUT_IME_CUSTOM_BINDINGS_JS" file="resources\extensions\experimental.input.ime_custom_bindings.js" type="BINDATA" /> <include name="IDR_EXPERIMENTAL_SOCKET_CUSTOM_BINDINGS_JS" file="resources\extensions\experimental.socket_custom_bindings.js" type="BINDATA" /> + <include name="IDR_EXPERIMENTAL_STORAGE_CUSTOM_BINDINGS_JS" file="resources\extensions\experimental.storage_custom_bindings.js" type="BINDATA" /> <include name="IDR_EXTENSION_CUSTOM_BINDINGS_JS" file="resources\extensions\extension_custom_bindings.js" type="BINDATA" /> <include name="IDR_FILE_BROWSER_HANDLER_CUSTOM_BINDINGS_JS" file="resources\extensions\file_browser_handler_custom_bindings.js" type="BINDATA" /> <include name="IDR_FILE_BROWSER_PRIVATE_CUSTOM_BINDINGS_JS" file="resources\extensions\file_browser_private_custom_bindings.js" type="BINDATA" /> <include name="IDR_OMNIBOX_CUSTOM_BINDINGS_JS" file="resources\extensions\omnibox_custom_bindings.js" type="BINDATA" /> - <include name="IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS" file="resources\extensions\page_action_custom_bindings.js" type="BINDATA" /> <include name="IDR_PAGE_ACTIONS_CUSTOM_BINDINGS_JS" file="resources\extensions\page_actions_custom_bindings.js" type="BINDATA" /> + <include name="IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS" file="resources\extensions\page_action_custom_bindings.js" type="BINDATA" /> <include name="IDR_PAGE_CAPTURE_CUSTOM_BINDINGS_JS" file="resources\extensions\page_capture_custom_bindings.js" type="BINDATA" /> <include name="IDR_TABS_CUSTOM_BINDINGS_JS" file="resources\extensions\tabs_custom_bindings.js" type="BINDATA" /> <include name="IDR_TTS_CUSTOM_BINDINGS_JS" file="resources\extensions\tts_custom_bindings.js" type="BINDATA" /> <include name="IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS" file="resources\extensions\tts_engine_custom_bindings.js" type="BINDATA" /> + <include name="IDR_TYPES_CUSTOM_BINDINGS_JS" file="resources\extensions\types_custom_bindings.js" type="BINDATA" /> <include name="IDR_WEB_REQUEST_CUSTOM_BINDINGS_JS" file="resources\extensions\web_request_custom_bindings.js" type="BINDATA" /> <include name="IDR_WINDOWS_CUSTOM_BINDINGS_JS" file="resources\extensions\windows_custom_bindings.js" type="BINDATA" /> </includes> diff --git a/chrome/renderer/resources/extensions/content_settings_custom_bindings.js b/chrome/renderer/resources/extensions/content_settings_custom_bindings.js new file mode 100644 index 0000000..14bc91c --- /dev/null +++ b/chrome/renderer/resources/extensions/content_settings_custom_bindings.js @@ -0,0 +1,58 @@ +// Copyright (c) 2012 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. + +// Custom bindings for the contentSettings API. + +(function() { + +native function GetChromeHidden(); + +var chromeHidden = GetChromeHidden(); + +chromeHidden.registerCustomType('ContentSetting', function(typesAPI) { + var sendRequest = typesAPI.sendRequest; + + function extendSchema(schema) { + var extendedSchema = schema.slice(); + extendedSchema.unshift({'type': 'string'}); + return extendedSchema; + } + + function ContentSetting(contentType, settingSchema) { + this.get = function(details, callback) { + var getSchema = this.parameters.get; + chromeHidden.validate([details, callback], getSchema); + return sendRequest('contentSettings.get', + [contentType, details, callback], + extendSchema(getSchema)); + }; + this.set = function(details, callback) { + var setSchema = this.parameters.set.slice(); + setSchema[0].properties.setting = settingSchema; + chromeHidden.validate([details, callback], setSchema); + return sendRequest('contentSettings.set', + [contentType, details, callback], + extendSchema(setSchema)); + }; + this.clear = function(details, callback) { + var clearSchema = this.parameters.clear; + chromeHidden.validate([details, callback], clearSchema); + return sendRequest('contentSettings.clear', + [contentType, details, callback], + extendSchema(clearSchema)); + }; + this.getResourceIdentifiers = function(callback) { + var schema = this.parameters.getResourceIdentifiers; + chromeHidden.validate([callback], schema); + return sendRequest( + 'contentSettings.getResourceIdentifiers', + [contentType, callback], + extendSchema(schema)); + }; + } + + return ContentSetting; +}); + +})(); diff --git a/chrome/renderer/resources/extensions/experimental.storage_custom_bindings.js b/chrome/renderer/resources/extensions/experimental.storage_custom_bindings.js new file mode 100644 index 0000000..b751ec0 --- /dev/null +++ b/chrome/renderer/resources/extensions/experimental.storage_custom_bindings.js @@ -0,0 +1,47 @@ +// Copyright (c) 2012 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. + +// Custom bindings for the experimental.storage API. + +(function() { + +native function GetChromeHidden(); + +var chromeHidden = GetChromeHidden(); + +chromeHidden.registerCustomType('StorageNamespace', + function(typesAPI) { + var sendRequest = typesAPI.sendRequest; + + function extendSchema(schema) { + var extendedSchema = schema.slice(); + extendedSchema.unshift({'type': 'string'}); + return extendedSchema; + } + + function StorageNamespace(namespace, schema) { + // Binds an API function for a namespace to its browser-side call, e.g. + // experimental.storage.sync.get('foo') -> (binds to) -> + // experimental.storage.get('sync', 'foo'). + // + // TODO(kalman): Put as a method on CustomBindingsObject and re-use (or + // even generate) for other APIs that need to do this. Same for other + // callers of registerCustomType(). + function bindApiFunction(functionName) { + this[functionName] = function() { + var schema = this.parameters[functionName]; + chromeHidden.validate(arguments, schema); + return sendRequest( + 'experimental.storage.' + functionName, + [namespace].concat(Array.prototype.slice.call(arguments)), + extendSchema(schema)); + }; + } + ['get', 'set', 'remove', 'clear'].forEach(bindApiFunction.bind(this)); + } + + return StorageNamespace; +}); + +})(); diff --git a/chrome/renderer/resources/extensions/schema_generated_bindings.js b/chrome/renderer/resources/extensions/schema_generated_bindings.js index ae7ab64..4134762 100644 --- a/chrome/renderer/resources/extensions/schema_generated_bindings.js +++ b/chrome/renderer/resources/extensions/schema_generated_bindings.js @@ -311,8 +311,6 @@ var chrome = chrome || {}; customHooks[namespace] = fn; }; - // --- Setup additional api's not currently handled in common/extensions/api - function CustomBindingsObject() { } CustomBindingsObject.prototype.setSchema = function(schema) { @@ -325,105 +323,16 @@ var chrome = chrome || {}; }); }; - function extendSchema(schema) { - var extendedSchema = schema.slice(); - extendedSchema.unshift({'type': 'string'}); - return extendedSchema; - } - + // Registers a custom type referenced via "$ref" fields in the API schema + // JSON. var customTypes = {}; - - function setupChromeSetting() { - function ChromeSetting(prefKey, valueSchema) { - this.get = function(details, callback) { - var getSchema = this.parameters.get; - chromeHidden.validate([details, callback], getSchema); - return sendRequest('types.ChromeSetting.get', - [prefKey, details, callback], - extendSchema(getSchema)); - }; - this.set = function(details, callback) { - var setSchema = this.parameters.set.slice(); - setSchema[0].properties.value = valueSchema; - chromeHidden.validate([details, callback], setSchema); - return sendRequest('types.ChromeSetting.set', - [prefKey, details, callback], - extendSchema(setSchema)); - }; - this.clear = function(details, callback) { - var clearSchema = this.parameters.clear; - chromeHidden.validate([details, callback], clearSchema); - return sendRequest('types.ChromeSetting.clear', - [prefKey, details, callback], - extendSchema(clearSchema)); - }; - this.onChange = new chrome.Event('types.ChromeSetting.' + prefKey + - '.onChange'); - }; - ChromeSetting.prototype = new CustomBindingsObject(); - customTypes['ChromeSetting'] = ChromeSetting; - } - - function setupContentSetting() { - function ContentSetting(contentType, settingSchema) { - this.get = function(details, callback) { - var getSchema = this.parameters.get; - chromeHidden.validate([details, callback], getSchema); - return sendRequest('contentSettings.get', - [contentType, details, callback], - extendSchema(getSchema)); - }; - this.set = function(details, callback) { - var setSchema = this.parameters.set.slice(); - setSchema[0].properties.setting = settingSchema; - chromeHidden.validate([details, callback], setSchema); - return sendRequest('contentSettings.set', - [contentType, details, callback], - extendSchema(setSchema)); - }; - this.clear = function(details, callback) { - var clearSchema = this.parameters.clear; - chromeHidden.validate([details, callback], clearSchema); - return sendRequest('contentSettings.clear', - [contentType, details, callback], - extendSchema(clearSchema)); - }; - this.getResourceIdentifiers = function(callback) { - var schema = this.parameters.getResourceIdentifiers; - chromeHidden.validate([callback], schema); - return sendRequest( - 'contentSettings.getResourceIdentifiers', - [contentType, callback], - extendSchema(schema)); - }; - } - ContentSetting.prototype = new CustomBindingsObject(); - customTypes['ContentSetting'] = ContentSetting; - } - - function setupStorageNamespace() { - function StorageNamespace(namespace, schema) { - // Binds an API function for a namespace to its browser-side call, e.g. - // experimental.storage.sync.get('foo') -> (binds to) -> - // experimental.storage.get('sync', 'foo'). - // - // TODO(kalman): Put as a method on CustomBindingsObject and re-use (or - // even generate) for other APIs that need to do this. - function bindApiFunction(functionName) { - this[functionName] = function() { - var schema = this.parameters[functionName]; - chromeHidden.validate(arguments, schema); - return sendRequest( - 'experimental.storage.' + functionName, - [namespace].concat(Array.prototype.slice.call(arguments)), - extendSchema(schema)); - }; - } - ['get', 'set', 'remove', 'clear'].forEach(bindApiFunction.bind(this)); - } - StorageNamespace.prototype = new CustomBindingsObject(); - customTypes['StorageNamespace'] = StorageNamespace; - } + chromeHidden.registerCustomType = function(typeName, customTypeFactory) { + var customType = customTypeFactory({ + sendRequest: sendRequest, + }); + customType.prototype = new CustomBindingsObject(); + customTypes[typeName] = customType; + }; // Get the platform from navigator.appVersion. function getPlatform() { @@ -447,24 +356,6 @@ var chrome = chrome || {}; isIncognitoProcess) { var apiDefinitions = GetExtensionAPIDefinition(); - // Setup custom classes so we can use them to construct $ref'd objects from - // the API definition. - apiDefinitions.forEach(function(apiDef) { - switch (apiDef.namespace) { - case "types": - setupChromeSetting(); - break; - - case "contentSettings": - setupContentSetting(); - break; - - case "experimental.storage": - setupStorageNamespace(); - break; - } - }); - // Read api definitions and setup api functions in the chrome namespace. // TODO(rafaelw): Consider defining a json schema for an api definition // and validating either here, in a unit_test or both. @@ -497,7 +388,7 @@ var chrome = chrome || {}; } // Adds a getter that throws an access denied error to object |module| - // for property |name| described by |schemaNode| if necessary. + // for property |name|. // // Returns true if the getter was necessary (access is disallowed), or // false otherwise. diff --git a/chrome/renderer/resources/extensions/types_custom_bindings.js b/chrome/renderer/resources/extensions/types_custom_bindings.js new file mode 100644 index 0000000..62dbe0a --- /dev/null +++ b/chrome/renderer/resources/extensions/types_custom_bindings.js @@ -0,0 +1,52 @@ +// Copyright (c) 2012 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. + +// Custom bindings for the types API. + +(function() { + +native function GetChromeHidden(); + +var chromeHidden = GetChromeHidden(); + +chromeHidden.registerCustomType('ChromeSetting', function(typesAPI) { + var sendRequest = typesAPI.sendRequest; + + function extendSchema(schema) { + var extendedSchema = schema.slice(); + extendedSchema.unshift({'type': 'string'}); + return extendedSchema; + } + + function ChromeSetting(prefKey, valueSchema) { + this.get = function(details, callback) { + var getSchema = this.parameters.get; + chromeHidden.validate([details, callback], getSchema); + return sendRequest('types.ChromeSetting.get', + [prefKey, details, callback], + extendSchema(getSchema)); + }; + this.set = function(details, callback) { + var setSchema = this.parameters.set.slice(); + setSchema[0].properties.value = valueSchema; + chromeHidden.validate([details, callback], setSchema); + return sendRequest('types.ChromeSetting.set', + [prefKey, details, callback], + extendSchema(setSchema)); + }; + this.clear = function(details, callback) { + var clearSchema = this.parameters.clear; + chromeHidden.validate([details, callback], clearSchema); + return sendRequest('types.ChromeSetting.clear', + [prefKey, details, callback], + extendSchema(clearSchema)); + }; + this.onChange = new chrome.Event('types.ChromeSetting.' + prefKey + + '.onChange'); + }; + + return ChromeSetting; +}); + +})(); |