| 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 // Some content types have been removed and no longer correspond to a real |
| 11 // content setting. Instead, these always return a fixed dummy value, and issue |
| 12 // a warning when accessed. This maps the content type name to the dummy value. |
| 13 var DEPRECATED_CONTENT_TYPES = { |
| 14 __proto__: null, |
| 15 |
| 16 fullscreen: 'allow', |
| 17 mouselock: 'allow', |
| 18 }; |
| 19 |
| 10 function extendSchema(schema) { | 20 function extendSchema(schema) { |
| 11 var extendedSchema = $Array.slice(schema); | 21 var extendedSchema = $Array.slice(schema); |
| 12 $Array.unshift(extendedSchema, {'type': 'string'}); | 22 $Array.unshift(extendedSchema, {'type': 'string'}); |
| 13 return extendedSchema; | 23 return extendedSchema; |
| 14 } | 24 } |
| 15 | 25 |
| 16 function ContentSetting(contentType, settingSchema, schema) { | 26 function ContentSetting(contentType, settingSchema, schema) { |
| 17 var getFunctionParameters = function(name) { | 27 var getFunctionParameters = function(name) { |
| 18 var f = $Array.filter( | 28 var f = $Array.filter( |
| 19 schema.functions, function(f) { return f.name === name; })[0]; | 29 schema.functions, function(f) { return f.name === name; })[0]; |
| 20 return f.parameters; | 30 return f.parameters; |
| 21 }; | 31 }; |
| 22 this.get = function(details, callback) { | 32 this.get = function(details, callback) { |
| 23 var getSchema = getFunctionParameters('get'); | 33 var getSchema = getFunctionParameters('get'); |
| 24 validate([details, callback], getSchema); | 34 validate([details, callback], getSchema); |
| 35 |
| 36 var dummySetting = DEPRECATED_CONTENT_TYPES[contentType]; |
| 37 if (dummySetting !== undefined) { |
| 38 console.warn('contentSettings.' + contentType + ' is deprecated; it will ' |
| 39 + 'always return \'' + dummySetting + '\'.'); |
| 40 $Function.apply(callback, undefined, [{setting: dummySetting}]); |
| 41 return; |
| 42 } |
| 43 |
| 25 return sendRequest('contentSettings.get', | 44 return sendRequest('contentSettings.get', |
| 26 [contentType, details, callback], | 45 [contentType, details, callback], |
| 27 extendSchema(getSchema)); | 46 extendSchema(getSchema)); |
| 28 }; | 47 }; |
| 48 |
| 29 this.set = function(details, callback) { | 49 this.set = function(details, callback) { |
| 30 // The set schema included in the Schema object is generic, since it varies | 50 // The set schema included in the Schema object is generic, since it varies |
| 31 // per-setting. However, this is only ever for a single setting, so we can | 51 // per-setting. However, this is only ever for a single setting, so we can |
| 32 // enforce the types more thoroughly. | 52 // enforce the types more thoroughly. |
| 33 var rawSetSchema = getFunctionParameters('set'); | 53 var rawSetSchema = getFunctionParameters('set'); |
| 34 var rawSettingParam = rawSetSchema[0]; | 54 var rawSettingParam = rawSetSchema[0]; |
| 35 var props = $Object.assign({}, rawSettingParam.properties); | 55 var props = $Object.assign({}, rawSettingParam.properties); |
| 36 props.setting = settingSchema; | 56 props.setting = settingSchema; |
| 37 var modSettingParam = { | 57 var modSettingParam = { |
| 38 name: rawSettingParam.name, | 58 name: rawSettingParam.name, |
| 39 type: rawSettingParam.type, | 59 type: rawSettingParam.type, |
| 40 properties: props, | 60 properties: props, |
| 41 }; | 61 }; |
| 42 var modSetSchema = $Array.slice(rawSetSchema); | 62 var modSetSchema = $Array.slice(rawSetSchema); |
| 43 modSetSchema[0] = modSettingParam; | 63 modSetSchema[0] = modSettingParam; |
| 44 validate([details, callback], rawSetSchema); | 64 validate([details, callback], rawSetSchema); |
| 65 |
| 66 if ($Object.hasOwnProperty(DEPRECATED_CONTENT_TYPES, contentType)) { |
| 67 console.warn('contentSettings.' + contentType + ' is deprecated; setting ' |
| 68 + 'it has no effect.'); |
| 69 $Function.apply(callback, undefined, []); |
| 70 return; |
| 71 } |
| 72 |
| 45 return sendRequest('contentSettings.set', | 73 return sendRequest('contentSettings.set', |
| 46 [contentType, details, callback], | 74 [contentType, details, callback], |
| 47 extendSchema(modSetSchema)); | 75 extendSchema(modSetSchema)); |
| 48 }; | 76 }; |
| 77 |
| 49 this.clear = function(details, callback) { | 78 this.clear = function(details, callback) { |
| 50 var clearSchema = getFunctionParameters('clear'); | 79 var clearSchema = getFunctionParameters('clear'); |
| 51 validate([details, callback], clearSchema); | 80 validate([details, callback], clearSchema); |
| 81 |
| 82 if ($Object.hasOwnProperty(DEPRECATED_CONTENT_TYPES, contentType)) { |
| 83 console.warn('contentSettings.' + contentType + ' is deprecated; ' |
| 84 + 'clearing it has no effect.'); |
| 85 $Function.apply(callback, undefined, []); |
| 86 return; |
| 87 } |
| 88 |
| 52 return sendRequest('contentSettings.clear', | 89 return sendRequest('contentSettings.clear', |
| 53 [contentType, details, callback], | 90 [contentType, details, callback], |
| 54 extendSchema(clearSchema)); | 91 extendSchema(clearSchema)); |
| 55 }; | 92 }; |
| 93 |
| 56 this.getResourceIdentifiers = function(callback) { | 94 this.getResourceIdentifiers = function(callback) { |
| 57 var schema = getFunctionParameters('getResourceIdentifiers'); | 95 var schema = getFunctionParameters('getResourceIdentifiers'); |
| 58 validate([callback], schema); | 96 validate([callback], schema); |
| 97 |
| 98 if ($Object.hasOwnProperty(DEPRECATED_CONTENT_TYPES, contentType)) { |
| 99 $Function.apply(callback, undefined, []); |
| 100 return; |
| 101 } |
| 102 |
| 59 return sendRequest( | 103 return sendRequest( |
| 60 'contentSettings.getResourceIdentifiers', | 104 'contentSettings.getResourceIdentifiers', |
| 61 [contentType, callback], | 105 [contentType, callback], |
| 62 extendSchema(schema)); | 106 extendSchema(schema)); |
| 63 }; | 107 }; |
| 64 } | 108 } |
| 65 | 109 |
| 66 exports.$set('ContentSetting', ContentSetting); | 110 exports.$set('ContentSetting', ContentSetting); |
| OLD | NEW |