-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 Begin work on settings page for search engines
Sync with laptop
- Loading branch information
Showing
3 changed files
with
39 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const DEFAULT_SEARCH_ENGINE: (typeof SEARCH_ENGINE_IDS)[number] = | ||
'[email protected]' | ||
export const SEARCH_ENGINE_PREF = 'browser.search.engine.default' | ||
|
||
export const SEARCH_ENGINE_IDS = [ | ||
'[email protected]', | ||
'[email protected]', | ||
] as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,22 +3,24 @@ | |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import type { AddonSearchEngine } from 'resource://gre/modules/AddonSearchEngine.sys.mjs' | ||
import { Deferred } from '../Deferred' | ||
import { lazyESModuleGetters } from '../../../shared/TypedImportUtilities' | ||
import { Deferred } from '../../browser/lib/Deferred' | ||
import { lazyESModuleGetters } from '../TypedImportUtilities' | ||
import { | ||
SEARCH_ENGINE_IDS, | ||
SEARCH_ENGINE_PREF, | ||
DEFAULT_SEARCH_ENGINE, | ||
} from './constants' | ||
|
||
const lazy = lazyESModuleGetters({ | ||
AddonSearchEngine: 'resource://gre/modules/AddonSearchEngine.sys.mjs', | ||
SearchUtils: 'resource://gre/modules/SearchUtils.sys.mjs', | ||
}) | ||
|
||
const SEARCH_ENGINE_IDS = [ | ||
'[email protected]', | ||
'[email protected]', | ||
] as const | ||
|
||
class SearchService { | ||
private initPromise: Deferred<void> | undefined | ||
|
||
private searchEngines: Deferred<AddonSearchEngine[]> = new Deferred() | ||
private defaultEngine: Deferred<AddonSearchEngine> = new Deferred() | ||
|
||
constructor() {} | ||
|
||
|
@@ -45,7 +47,6 @@ class SearchService { | |
|
||
const locale = lazy.SearchUtils.DEFAULT_TAG | ||
const engines: AddonSearchEngine[] = [] | ||
|
||
for (const extensionID of SEARCH_ENGINE_IDS) { | ||
const engine = new (lazy.AddonSearchEngine as any)({ | ||
isAppProvided: true, | ||
|
@@ -55,15 +56,29 @@ class SearchService { | |
|
||
engines.push(engine) | ||
} | ||
|
||
this.searchEngines.resolve(engines) | ||
|
||
const defaultEngineId = Services.prefs.getStringPref( | ||
SEARCH_ENGINE_PREF, | ||
DEFAULT_SEARCH_ENGINE, | ||
) | ||
const defaultEngine = engines.find( | ||
(engine: any) => engine._extensionID === defaultEngineId, | ||
)! | ||
this.defaultEngine.resolve(defaultEngine) | ||
|
||
this.initPromise.resolve() | ||
} | ||
|
||
async getSearchEngines() { | ||
await this.init() | ||
return await this.searchEngines.promise | ||
} | ||
|
||
async getDefaultEngine() { | ||
await this.init() | ||
return await this.defaultEngine.promise | ||
} | ||
} | ||
|
||
export const searchService = new SearchService() |