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