blob: eca295548c368ad8389ca3d6238c23863cc36915 [file] [log] [blame]
Adrià Vilanova Martínezb523be92024-05-25 19:14:19 +02001import PartialOptionsWatcher from '../options/partialOptionsWatcher.js';
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +01002import MainWorldContentScriptBridgeServer from '../mainWorldContentScriptBridge/Server.js';
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +01003
4// Main World OptionsWatcher server (used in content scripts to be able to serve
5// the options to Main World (MW) scripts).
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +01006export default class MWOptionsWatcherServer extends
7 MainWorldContentScriptBridgeServer {
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +01008 constructor(CSTarget, MWTarget) {
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +01009 super(CSTarget, MWTarget);
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010010 this.optionsWatcher = null;
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010011 this.setUpHandler(this.handleMessage);
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010012 }
13
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010014 handleMessage(uuid, action, request) {
15 if (action === 'setUp') {
Adrià Vilanova Martínez8300cc42024-05-11 12:42:52 +020016 this.optionsWatcher = new PartialOptionsWatcher(request?.options);
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010017 return;
18 }
19
20 if (!this.optionsWatcher) {
21 console.warn(`[MWOptionsWatcherServer] Action '${
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010022 action}' called before setting up options watcher.`);
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010023 return;
24 }
25
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010026 switch (action) {
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010027 case 'getOption':
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010028 this.optionsWatcher.getOption(request?.option).then(value => {
29 this._respond(uuid, value);
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010030 });
31 return;
32
33 case 'getOptions':
34 var promises = [];
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010035 var options = request?.options ?? [];
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010036 for (const option of options) {
37 promises.push(this.optionsWatcher.getOption(option));
38 }
39 Promise.all(promises).then(responses => {
40 const response = {};
41 for (let i = 0; i < responses.length; i++) {
42 response[options[i]] = responses[i];
43 }
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010044 this._respond(uuid, response);
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010045 });
46 return;
47
48 case 'isEnabled':
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010049 this.optionsWatcher.isEnabled(request?.option).then(value => {
50 this._respond(uuid, value);
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010051 });
52 return;
53
54 case 'areEnabled':
55 var promises = [];
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010056 var options = request?.options ?? [];
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010057 for (const option of options) {
58 promises.push(this.optionsWatcher.isEnabled(option));
59 }
60 Promise.all(promises).then(responses => {
61 const response = {};
62 for (let i = 0; i < responses.length; i++) {
63 response[options[i]] = responses[i];
64 }
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010065 this._respond(uuid, response);
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010066 });
67 return;
68
69 default:
Adrià Vilanova Martínez2548e442023-03-05 18:45:30 +010070 console.error(
71 `[MWOptionsWatcherServer] Invalid action received (${action})`);
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010072 }
73 }
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010074}