tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mock-managed-config.js (2596B)


      1 'use strict'
      2 
      3 import{ManagedConfigurationObserverRemote, ManagedConfigurationService, ManagedConfigurationServiceReceiver} from '/gen/third_party/blink/public/mojom/device/device.mojom.m.js';
      4 
      5 
      6 self.ManagedConfigTest = (() => {
      7  // Class that mocks ManagedConfigurationService interface defined in
      8  // https://source.chromium.org/chromium/chromium/src/third_party/blink/public/mojom/device/device.mojom
      9  class MockManagedConfig {
     10    constructor() {
     11      this.receiver_ = new ManagedConfigurationServiceReceiver(this);
     12      this.interceptor_ = new MojoInterfaceInterceptor(
     13          ManagedConfigurationService.$interfaceName);
     14      this.interceptor_.oninterfacerequest = e =>
     15          this.receiver_.$.bindHandle(e.handle);
     16      this.interceptor_.start();
     17      this.subscription_ = null;
     18      this.reset();
     19    }
     20 
     21    reset() {
     22      this.configuration_ = null;
     23      this.onObserverAdd_ = null;
     24    }
     25 
     26    async getManagedConfiguration(keys) {
     27      if (this.configuration_ === null) {
     28        return {};
     29      }
     30 
     31      return {
     32        configurations: Object.keys(this.configuration_)
     33                            .filter(key => keys.includes(key))
     34                            .reduce(
     35                                (obj, key) => {
     36                                  obj[key] =
     37                                      JSON.stringify(this.configuration_[key]);
     38                                  return obj;
     39                                },
     40                                {})
     41      };
     42    }
     43 
     44    subscribeToManagedConfiguration(remote) {
     45      this.subscription_ = remote;
     46      if (this.onObserverAdd_ !== null) {
     47        this.onObserverAdd_();
     48      }
     49    }
     50 
     51    setManagedConfig(value) {
     52      this.configuration_ = value;
     53      if (this.subscription_ !== null) {
     54        this.subscription_.onConfigurationChanged();
     55      }
     56    }
     57  }
     58 
     59  let testInternal = {
     60    initialized: false,
     61    mockManagedConfig: null
     62  }
     63 
     64  class ManagedConfigTestChromium {
     65    constructor() {
     66      Object.freeze(this);  // Make it immutable.
     67    }
     68 
     69    initialize() {
     70      if (testInternal.mockManagedConfig !== null) {
     71        testInternal.mockManagedConfig.reset();
     72        return;
     73      }
     74 
     75      testInternal.mockManagedConfig = new MockManagedConfig;
     76      testInternal.initialized = true;
     77    }
     78 
     79    setManagedConfig(config) {
     80      testInternal.mockManagedConfig.setManagedConfig(config);
     81    }
     82 
     83    async nextObserverAdded() {
     84      await new Promise(resolve => {
     85        testInternal.mockManagedConfig.onObserverAdd_ = resolve;
     86      });
     87    }
     88  }
     89 
     90  return ManagedConfigTestChromium;
     91 })();