tor-browser

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

contacts_manager_mock.js (2847B)


      1 // Copyright 2018 The Chromium Authors
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 import {ContactsManager, ContactsManagerReceiver} from '/gen/third_party/blink/public/mojom/contacts/contacts_manager.mojom.m.js';
      6 
      7 self.WebContactsTest = (() => {
      8  class MockContacts {
      9    constructor() {
     10      this.receiver_ = new ContactsManagerReceiver(this);
     11 
     12      this.interceptor_ =
     13          new MojoInterfaceInterceptor(ContactsManager.$interfaceName);
     14      this.interceptor_.oninterfacerequest =
     15          e => this.receiver_.$.bindHandle(e.handle);
     16      this.interceptor_.start();
     17 
     18      this.selectedContacts_ = [];
     19    }
     20 
     21    formatAddress_(address) {
     22      // These are all required fields in the mojo definition.
     23      return {
     24        country: address.country || '',
     25        addressLine: address.addressLine || [],
     26        region: address.region || '',
     27        city: address.city || '',
     28        dependentLocality: address.dependentLocality || '',
     29        postalCode: address.postCode || '',
     30        sortingCode: address.sortingCode || '',
     31        organization: address.organization || '',
     32        recipient: address.recipient || '',
     33        phone: address.phone || '',
     34      };
     35    }
     36 
     37    async select(multiple, includeNames, includeEmails, includeTel, includeAddresses, includeIcons) {
     38      if (this.selectedContacts_ === null)
     39        return {contacts: null};
     40 
     41      const contactInfos = await Promise.all(this.selectedContacts_.map(async contact => {
     42        const contactInfo = {};
     43        if (includeNames)
     44          contactInfo.name = contact.name || [];
     45        if (includeEmails)
     46          contactInfo.email = contact.email || [];
     47        if (includeTel)
     48          contactInfo.tel = contact.tel || [];
     49        if (includeAddresses) {
     50          contactInfo.address = (contact.address || []).map(address => this.formatAddress_(address));
     51        }
     52        if (includeIcons) {
     53          contactInfo.icon = await Promise.all(
     54            (contact.icon || []).map(async blob => ({
     55              mimeType: blob.type,
     56              data: (await blob.text()).split('').map(s => s.charCodeAt(0)),
     57            })));
     58        }
     59        return contactInfo;
     60      }));
     61 
     62      if (!contactInfos.length) return {contacts: []};
     63      if (!multiple) return {contacts: [contactInfos[0]]};
     64      return {contacts: contactInfos};
     65    }
     66 
     67    setSelectedContacts(contacts) {
     68      this.selectedContacts_ = contacts;
     69    }
     70 
     71    reset() {
     72      this.receiver_.$.close();
     73      this.interceptor_.stop();
     74    }
     75  }
     76 
     77  const mockContacts = new MockContacts();
     78 
     79  class ContactsTestChromium {
     80    constructor() {
     81      Object.freeze(this); // Make it immutable.
     82    }
     83 
     84    setSelectedContacts(contacts) {
     85      mockContacts.setSelectedContacts(contacts);
     86    }
     87  }
     88 
     89  return ContactsTestChromium;
     90 })();