[email protected] | 8f857ef8 | 2014-06-04 23:46:16 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 7c04d6b | 2013-03-22 17:05:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 7c04d6b | 2013-03-22 17:05:57 | [diff] [blame] | 5 | var normalizeArgumentsAndValidate = |
| 6 | require('schemaUtils').normalizeArgumentsAndValidate |
| 7 | var sendRequest = require('sendRequest').sendRequest; |
| 8 | |
| 9 | function extendSchema(schema) { |
[email protected] | 31bbfd7 | 2013-06-22 02:35:54 | [diff] [blame] | 10 | var extendedSchema = $Array.slice(schema); |
[email protected] | 7c04d6b | 2013-03-22 17:05:57 | [diff] [blame] | 11 | extendedSchema.unshift({'type': 'string'}); |
| 12 | return extendedSchema; |
| 13 | } |
| 14 | |
| 15 | function StorageArea(namespace, schema) { |
| 16 | // Binds an API function for a namespace to its browser-side call, e.g. |
| 17 | // storage.sync.get('foo') -> (binds to) -> |
| 18 | // storage.get('sync', 'foo'). |
| 19 | // |
| 20 | // TODO(kalman): Put as a method on CustombindingObject and re-use (or |
| 21 | // even generate) for other APIs that need to do this. Same for other |
| 22 | // callers of registerCustomType(). |
| 23 | var self = this; |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 24 | function bindApiFunction(functionName) { |
[email protected] | 7c04d6b | 2013-03-22 17:05:57 | [diff] [blame] | 25 | self[functionName] = function() { |
| 26 | var funSchema = this.functionSchemas[functionName]; |
[email protected] | 31bbfd7 | 2013-06-22 02:35:54 | [diff] [blame] | 27 | var args = $Array.slice(arguments); |
[email protected] | 7c04d6b | 2013-03-22 17:05:57 | [diff] [blame] | 28 | args = normalizeArgumentsAndValidate(args, funSchema); |
| 29 | return sendRequest( |
| 30 | 'storage.' + functionName, |
[email protected] | 31bbfd7 | 2013-06-22 02:35:54 | [diff] [blame] | 31 | $Array.concat([namespace], args), |
[email protected] | 7c04d6b | 2013-03-22 17:05:57 | [diff] [blame] | 32 | extendSchema(funSchema.definition.parameters), |
| 33 | {preserveNullInObjects: true}); |
| 34 | }; |
| 35 | } |
| 36 | var apiFunctions = ['get', 'set', 'remove', 'clear', 'getBytesInUse']; |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 37 | $Array.forEach(apiFunctions, bindApiFunction); |
[email protected] | 7c04d6b | 2013-03-22 17:05:57 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | exports.StorageArea = StorageArea; |