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 var Event = require('event_bindings').Event; | 5 var Event = require('event_bindings').Event; |
6 var sendRequest = require('sendRequest').sendRequest; | 6 var sendRequest = require('sendRequest').sendRequest; |
7 var validate = require('schemaUtils').validate; | 7 var validate = require('schemaUtils').validate; |
8 | 8 |
9 function extendSchema(schema) { | 9 function extendSchema(schema) { |
10 var extendedSchema = $Array.slice(schema); | 10 var extendedSchema = $Array.slice(schema); |
11 extendedSchema.unshift({'type': 'string'}); | 11 $Array.unshift(extendedSchema, {'type': 'string'}); |
12 return extendedSchema; | 12 return extendedSchema; |
13 } | 13 } |
14 | 14 |
| 15 // TODO(devlin): Maybe find a way to combine this and ContentSetting. |
15 function ChromeSetting(prefKey, valueSchema) { | 16 function ChromeSetting(prefKey, valueSchema) { |
16 this.get = function(details, callback) { | 17 this.get = function(details, callback) { |
17 var getSchema = this.functionSchemas.get.definition.parameters; | 18 var getSchema = this.functionSchemas.get.definition.parameters; |
18 validate([details, callback], getSchema); | 19 validate([details, callback], getSchema); |
19 return sendRequest('types.ChromeSetting.get', | 20 return sendRequest('types.ChromeSetting.get', |
20 [prefKey, details, callback], | 21 [prefKey, details, callback], |
21 extendSchema(getSchema)); | 22 extendSchema(getSchema)); |
22 }; | 23 }; |
23 this.set = function(details, callback) { | 24 this.set = function(details, callback) { |
24 var setSchema = $Array.slice( | 25 // The set schema included in the Schema object is generic, since it varies |
25 this.functionSchemas.set.definition.parameters); | 26 // per-setting. However, this is only ever for a single setting, so we can |
26 setSchema[0].properties.value = valueSchema; | 27 // enforce the types more thoroughly. |
27 validate([details, callback], setSchema); | 28 var rawSetSchema = this.functionSchemas.set.definition.parameters; |
| 29 var rawSettingParam = rawSetSchema[0]; |
| 30 var props = $Object.assign({}, rawSettingParam.properties); |
| 31 props.value = valueSchema; |
| 32 var modSettingParam = { |
| 33 name: rawSettingParam.name, |
| 34 type: rawSettingParam.type, |
| 35 properties: props, |
| 36 }; |
| 37 var modSetSchema = $Array.slice(rawSetSchema); |
| 38 modSetSchema[0] = modSettingParam; |
| 39 validate([details, callback], modSetSchema); |
28 return sendRequest('types.ChromeSetting.set', | 40 return sendRequest('types.ChromeSetting.set', |
29 [prefKey, details, callback], | 41 [prefKey, details, callback], |
30 extendSchema(setSchema)); | 42 extendSchema(modSetSchema)); |
31 }; | 43 }; |
32 this.clear = function(details, callback) { | 44 this.clear = function(details, callback) { |
33 var clearSchema = this.functionSchemas.clear.definition.parameters; | 45 var clearSchema = this.functionSchemas.clear.definition.parameters; |
34 validate([details, callback], clearSchema); | 46 validate([details, callback], clearSchema); |
35 return sendRequest('types.ChromeSetting.clear', | 47 return sendRequest('types.ChromeSetting.clear', |
36 [prefKey, details, callback], | 48 [prefKey, details, callback], |
37 extendSchema(clearSchema)); | 49 extendSchema(clearSchema)); |
38 }; | 50 }; |
39 this.onChange = new Event('types.ChromeSetting.' + prefKey + '.onChange'); | 51 this.onChange = new Event('types.ChromeSetting.' + prefKey + '.onChange'); |
40 }; | 52 }; |
41 | 53 |
42 exports.$set('ChromeSetting', ChromeSetting); | 54 exports.$set('ChromeSetting', ChromeSetting); |
OLD | NEW |