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