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