Index: chrome/renderer/resources/extension_process_bindings.js |
diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js |
index d6e08fe4cba5d686171258dc6986a7f963dadc15..5ffbef20afca58bf63dbc92ce5cf6137cd684fce 100644 |
--- a/chrome/renderer/resources/extension_process_bindings.js |
+++ b/chrome/renderer/resources/extension_process_bindings.js |
@@ -38,8 +38,15 @@ var chrome = chrome || {}; |
if (!chrome) |
chrome = {}; |
+ function forEach(dict, f) { |
+ for (key in dict) { |
+ if (dict.hasOwnProperty(key)) |
+ f(key, dict[key]); |
+ } |
+ } |
+ |
// Validate arguments. |
- chromeHidden.validationTypes = []; |
+ chromeHidden.validationTypes = {}; |
chromeHidden.validate = function(args, schemas) { |
if (args.length > schemas.length) |
throw new Error("Too many arguments."); |
@@ -47,7 +54,9 @@ var chrome = chrome || {}; |
for (var i = 0; i < schemas.length; i++) { |
if (i in args && args[i] !== null && args[i] !== undefined) { |
var validator = new chromeHidden.JSONSchemaValidator(); |
- validator.addTypes(chromeHidden.validationTypes); |
+ forEach(chromeHidden.validationTypes, function(id, type) { |
+ validator.addTypes(type); |
+ }); |
validator.validate(args[i], schemas[i]); |
if (validator.errors.length == 0) |
continue; |
@@ -416,6 +425,37 @@ var chrome = chrome || {}; |
}; |
} |
+ var customInitializers = {}; |
+ |
+ function setupPreferences() { |
+ var parameters = {}; |
+ chromeHidden.validationTypes['Preference'].functions.forEach(function(f) { |
+ parameters[f.name] = f.parameters; |
+ }); |
+ |
+ customInitializers['Preference'] = function(prefKey, valueSchema) { |
+ var getSchema = parameters.get; |
+ var extendedGetSchema = getSchema.slice(); |
Aaron Boodman
2011/02/23 01:17:26
Can you find a way to generalize this such that ev
Bernhard Bauer
2011/02/23 17:33:27
Done. I pulled out a function |extendSchema|.
|
+ extendedGetSchema.unshift({'type': 'string'}); |
+ this.get = function(details, callback) { |
+ chromeHidden.validate([details, callback], getSchema); |
+ return sendRequest('experimental.preferences.get', |
+ [prefKey, details, callback], |
+ extendedGetSchema); |
+ }; |
+ var setSchema = parameters.set.slice(); |
+ setSchema[0].properties.value = valueSchema; |
+ var extendedSetSchema = setSchema.slice(); |
+ extendedSetSchema.unshift({'type': 'string'}); |
+ this.set = function(details, callback) { |
+ chromeHidden.validate([details, callback], setSchema); |
+ return sendRequest('experimental.preferences.set', |
+ [prefKey, details, callback], |
+ extendedSetSchema); |
+ }; |
+ }; |
+ } |
+ |
chromeHidden.onLoad.addListener(function (extensionId) { |
if (!extensionId) { |
return; |
@@ -427,6 +467,8 @@ var chrome = chrome || {}; |
// is implemented by adding a "handleRequest" function to the object. |
var apiFunctions = {}; |
+ var customObjects = {}; |
+ |
// 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. |
@@ -446,7 +488,21 @@ var chrome = chrome || {}; |
// Add types to global validationTypes |
if (apiDef.types) { |
apiDef.types.forEach(function(t) { |
- chromeHidden.validationTypes.push(t); |
+ chromeHidden.validationTypes[t.id] = t; |
+ if (t.type == 'object' && t.customBindings) { |
Aaron Boodman
2011/02/23 01:17:26
The customBindings flag seems unnecessary since yo
Bernhard Bauer
2011/02/23 17:33:27
Done.
|
+ function CustomBindingsObject(args) { |
Aaron Boodman
2011/02/23 01:17:26
This object doesn't seem to do a lot right now. I
Bernhard Bauer
2011/02/23 17:33:27
Done. I'm still using CustomBindingsObject as a ba
|
+ this.initArgs = args; |
+ } |
+ customObjects[t.id] = CustomBindingsObject; |
+ |
+ t.functions.forEach(function(f) { |
+ CustomBindingsObject.prototype[f.name] = function() { |
+ customInitializers[t.customBindings].apply(this, this.initArgs); |
+ delete this.initArgs; |
+ this[f.name].apply(this, arguments); |
+ }; |
+ }); |
+ } |
}); |
} |
@@ -514,24 +570,23 @@ var chrome = chrome || {}; |
// Parse any values defined for properties. |
if (apiDef.properties) { |
- for (var prop in apiDef.properties) { |
- if (!apiDef.properties.hasOwnProperty(prop)) |
- continue; |
- |
- var property = apiDef.properties[prop]; |
+ forEach(apiDef.properties, function(prop, property) { |
if (property.value) { |
var value = property.value; |
if (property.type === 'integer') { |
value = parseInt(value); |
} else if (property.type === 'boolean') { |
value = value === "true"; |
+ } else if (property["$ref"]) { |
+ var proto = customObjects[property["$ref"]]; |
Aaron Boodman
2011/02/23 01:17:26
naming nit: I think this should really be 'ctor' o
Bernhard Bauer
2011/02/23 17:33:27
Done. I think constructor only has a special meani
|
+ value = new proto(value); |
} else if (property.type !== 'string') { |
throw "NOT IMPLEMENTED (extension_api.json error): Cannot " + |
"parse values for type \"" + property.type + "\""; |
} |
module[prop] = value; |
} |
- } |
+ }); |
} |
// getTabContentses is retained for backwards compatibility |
@@ -788,6 +843,7 @@ var chrome = chrome || {}; |
setupHiddenContextMenuEvent(extensionId); |
setupOmniboxEvents(); |
setupTtsEvents(); |
+ setupPreferences(); |
}); |
if (!chrome.experimental) |