| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Custom binding for the storage API. | 5 // Custom binding for the storage API. |
| 6 | 6 |
| 7 var binding = require('binding').Binding.create('storage'); | 7 var binding = require('binding').Binding.create('storage'); |
| 8 | 8 |
| 9 var forEach = require('utils').forEach; |
| 9 var normalizeArgumentsAndValidate = | 10 var normalizeArgumentsAndValidate = |
| 10 require('schemaUtils').normalizeArgumentsAndValidate | 11 require('schemaUtils').normalizeArgumentsAndValidate |
| 11 var sendRequest = require('sendRequest').sendRequest; | 12 var sendRequest = require('sendRequest').sendRequest; |
| 12 | 13 |
| 13 binding.registerCustomType('storage.StorageArea', function() { | 14 binding.registerCustomType('storage.StorageArea', function() { |
| 14 function extendSchema(schema) { | 15 function extendSchema(schema) { |
| 15 var extendedSchema = schema.slice(); | 16 var extendedSchema = schema.slice(); |
| 16 extendedSchema.unshift({'type': 'string'}); | 17 extendedSchema.unshift({'type': 'string'}); |
| 17 return extendedSchema; | 18 return extendedSchema; |
| 18 } | 19 } |
| 19 | 20 |
| 20 function StorageArea(namespace, schema) { | 21 function StorageArea(namespace, schema) { |
| 21 // Binds an API function for a namespace to its browser-side call, e.g. | 22 // Binds an API function for a namespace to its browser-side call, e.g. |
| 22 // storage.sync.get('foo') -> (binds to) -> | 23 // storage.sync.get('foo') -> (binds to) -> |
| 23 // storage.get('sync', 'foo'). | 24 // storage.get('sync', 'foo'). |
| 24 // | 25 // |
| 25 // TODO(kalman): Put as a method on CustombindingObject and re-use (or | 26 // TODO(kalman): Put as a method on CustombindingObject and re-use (or |
| 26 // even generate) for other APIs that need to do this. Same for other | 27 // even generate) for other APIs that need to do this. Same for other |
| 27 // callers of registerCustomType(). | 28 // callers of registerCustomType(). |
| 28 var self = this; | 29 var self = this; |
| 29 function bindApiFunction(functionName) { | 30 function bindApiFunction(i, functionName) { |
| 30 self[functionName] = function() { | 31 self[functionName] = function() { |
| 31 var funSchema = this.functionSchemas[functionName]; | 32 var funSchema = this.functionSchemas[functionName]; |
| 32 var args = Array.prototype.slice.call(arguments); | 33 var args = Array.prototype.slice.call(arguments); |
| 33 args = normalizeArgumentsAndValidate(args, funSchema); | 34 args = normalizeArgumentsAndValidate(args, funSchema); |
| 34 return sendRequest( | 35 return sendRequest( |
| 35 'storage.' + functionName, | 36 'storage.' + functionName, |
| 36 [namespace].concat(args), | 37 [namespace].concat(args), |
| 37 extendSchema(funSchema.definition.parameters), | 38 extendSchema(funSchema.definition.parameters), |
| 38 {preserveNullInObjects: true}); | 39 {preserveNullInObjects: true}); |
| 39 }; | 40 }; |
| 40 } | 41 } |
| 41 var apiFunctions = ['get', 'set', 'remove', 'clear', 'getBytesInUse']; | 42 var apiFunctions = ['get', 'set', 'remove', 'clear', 'getBytesInUse']; |
| 42 apiFunctions.forEach(bindApiFunction); | 43 forEach(apiFunctions, bindApiFunction); |
| 43 } | 44 } |
| 44 | 45 |
| 45 return StorageArea; | 46 return StorageArea; |
| 46 }); | 47 }); |
| 47 | 48 |
| 48 exports.binding = binding.generate(); | 49 exports.binding = binding.generate(); |
| OLD | NEW |