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