summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/parent/ext-search.js
blob: 4fe7a096f40106edb01af9522cba92f5bee73a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */

"use strict";

var { ExtensionError } = ExtensionUtils;

const dispositionMap = {
  CURRENT_TAB: "current",
  NEW_TAB: "tab",
  NEW_WINDOW: "window",
};

this.search = class extends ExtensionAPI {
  getAPI(context) {
    function getTarget({ tabId, disposition, defaultDisposition }) {
      let tab, where;
      if (disposition) {
        if (tabId) {
          throw new ExtensionError(`Cannot set both 'disposition' and 'tabId'`);
        }
        where = dispositionMap[disposition];
      } else if (tabId) {
        tab = tabTracker.getTab(tabId);
      } else {
        where = dispositionMap[defaultDisposition];
      }
      return { tab, where };
    }

    return {
      search: {
        async get() {
          await Services.search.promiseInitialized;
          let visibleEngines = await Services.search.getVisibleEngines();
          let defaultEngine = await Services.search.getDefault();
          return Promise.all(
            visibleEngines.map(async engine => {
              let favIconUrl = engine.getIconURL();
              // Convert moz-extension:-URLs to data:-URLs to make sure that
              // extensions can see icons from other extensions, even if they
              // are not web-accessible.
              // Also prevents leakage of extension UUIDs to other extensions..
              if (
                favIconUrl &&
                favIconUrl.startsWith("moz-extension:") &&
                !favIconUrl.startsWith(context.extension.baseURL)
              ) {
                favIconUrl = await ExtensionUtils.makeDataURI(favIconUrl);
              }

              return {
                name: engine.name,
                isDefault: engine.name === defaultEngine.name,
                alias: engine.alias || undefined,
                favIconUrl,
              };
            })
          );
        },

        async search(searchProperties) {
          await Services.search.promiseInitialized;
          let engine;

          if (searchProperties.engine) {
            engine = Services.search.getEngineByName(searchProperties.engine);
            if (!engine) {
              throw new ExtensionError(
                `${searchProperties.engine} was not found`
              );
            }
          }

          let { tab, where } = getTarget({
            tabId: searchProperties.tabId,
            disposition: searchProperties.disposition,
            defaultDisposition: "NEW_TAB",
          });

          await windowTracker.topWindow.BrowserSearch.loadSearchFromExtension({
            query: searchProperties.query,
            where,
            engine,
            tab,
            triggeringPrincipal: context.principal,
          });
        },

        async query(queryProperties) {
          await Services.search.promiseInitialized;

          let { tab, where } = getTarget({
            tabId: queryProperties.tabId,
            disposition: queryProperties.disposition,
            defaultDisposition: "CURRENT_TAB",
          });

          await windowTracker.topWindow.BrowserSearch.loadSearchFromExtension({
            query: queryProperties.text,
            where,
            tab,
            triggeringPrincipal: context.principal,
          });
        },
      },
    };
  }
};