target-configuration-command.js (3005B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 /** 8 * The TargetConfigurationCommand should be used to populate the DevTools server 9 * with settings read from the client side but which impact the server. 10 * For instance, "disable cache" is a feature toggled via DevTools UI (client), 11 * but which should be communicated to the targets (server). 12 * 13 * See the TargetConfigurationActor for a list of supported configuration options. 14 */ 15 class TargetConfigurationCommand { 16 constructor({ commands, watcherFront }) { 17 this._commands = commands; 18 this._watcherFront = watcherFront; 19 } 20 21 /** 22 * Return a promise that resolves to the related target configuration actor's front. 23 * 24 * @return {Promise<TargetConfigurationFront>} 25 */ 26 async getFront() { 27 const front = await this._watcherFront.getTargetConfigurationActor(); 28 29 if (!this._configuration) { 30 // Retrieve initial data from the front 31 this._configuration = front.initialConfiguration; 32 } 33 34 return front; 35 } 36 37 _hasTargetWatcherSupport() { 38 return this._commands.targetCommand.hasTargetWatcherSupport(); 39 } 40 41 /** 42 * Retrieve the current map of configuration options pushed to the server. 43 */ 44 get configuration() { 45 return this._configuration || {}; 46 } 47 48 async updateConfiguration(configuration) { 49 if (this._hasTargetWatcherSupport()) { 50 const front = await this.getFront(); 51 const updatedConfiguration = 52 await front.updateConfiguration(configuration); 53 // Update the client-side copy of the DevTools configuration 54 this._configuration = updatedConfiguration; 55 } else { 56 await this._commands.targetCommand.targetFront.reconfigure({ 57 options: configuration, 58 }); 59 } 60 } 61 62 async isJavascriptEnabled() { 63 // If we don't have target watcher support, we can't get this value, so just 64 // fall back to true. Only content tab targets can update javascriptEnabled 65 // and all should have watcher support. 66 if (!this._hasTargetWatcherSupport()) { 67 return true; 68 } 69 70 const front = await this.getFront(); 71 return front.isJavascriptEnabled(); 72 } 73 74 /** 75 * Reports if the given configuration key is supported by the server. 76 * If the debugged context doesn't support the watcher actor, 77 * we won't be using the target configuration actor and report all keys 78 * as not supported. 79 * 80 * @param {object} configurationKey 81 * Name of the configuration you would like to set. 82 * @return {Promise<boolean>} True, if this configuration can be set via this API. 83 */ 84 async supports(configurationKey) { 85 if (!this._hasTargetWatcherSupport()) { 86 return false; 87 } 88 const front = await this.getFront(); 89 return !!front.traits.supportedOptions[configurationKey]; 90 } 91 } 92 93 module.exports = TargetConfigurationCommand;