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 bindings for the experimental.storage API. | |
6 | |
7 (function() { | |
8 | |
9 native function GetChromeHidden(); | |
10 | |
11 var chromeHidden = GetChromeHidden(); | |
12 | |
13 chromeHidden.registerCustomType('StorageNamespace', | |
14 function(typesAPI) { | |
15 var sendRequest = typesAPI.sendRequest; | |
16 | |
17 function extendSchema(schema) { | |
18 var extendedSchema = schema.slice(); | |
19 extendedSchema.unshift({'type': 'string'}); | |
20 return extendedSchema; | |
21 } | |
22 | |
23 function StorageNamespace(namespace, schema) { | |
24 // Binds an API function for a namespace to its browser-side call, e.g. | |
25 // experimental.storage.sync.get('foo') -> (binds to) -> | |
26 // experimental.storage.get('sync', 'foo'). | |
27 // | |
28 // TODO(kalman): Put as a method on CustomBindingsObject and re-use (or | |
29 // even generate) for other APIs that need to do this. Same for other | |
30 // callers of registerCustomType(). | |
31 function bindApiFunction(functionName) { | |
32 this[functionName] = function() { | |
33 var schema = this.parameters[functionName]; | |
34 chromeHidden.validate(arguments, schema); | |
35 return sendRequest( | |
36 'experimental.storage.' + functionName, | |
37 [namespace].concat(Array.prototype.slice.call(arguments)), | |
38 extendSchema(schema)); | |
39 }; | |
40 } | |
41 ['get', 'set', 'remove', 'clear'].forEach(bindApiFunction.bind(this)); | |
42 } | |
43 | |
44 return StorageNamespace; | |
45 }); | |
46 | |
47 })(); | |
OLD | NEW |