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