From 68a8fccb5045d0fe4b2201906339497cf05d9cc0 Mon Sep 17 00:00:00 2001 From: glacambre Date: Wed, 20 Feb 2019 06:42:45 +0100 Subject: [PATCH] Mobe background/native_background.ts to lib/native.ts All functions in native_background.ts use browserBg in order to interact with the native messenger. This means that these functions can also be used in the content script. This means that there's no point in keeping these functions in the background/ folder and that there's no point in having a native_background message type. --- contributing.md | 2 +- src/background.ts | 2 +- src/background/config_rc.ts | 2 +- src/background/download_background.ts | 2 +- src/completions/FileSystem.ts | 6 ++---- src/completions/Preferences.ts | 4 ++-- src/content.ts | 2 +- src/content/scrolling.ts | 1 - src/excmds.ts | 6 ++---- src/lib/messaging.ts | 1 - src/{background/native_background.ts => lib/native.ts} | 4 ---- 11 files changed, 11 insertions(+), 21 deletions(-) rename src/{background/native_background.ts => lib/native.ts} (99%) diff --git a/contributing.md b/contributing.md index 6d10f996..6301e62d 100644 --- a/contributing.md +++ b/contributing.md @@ -58,7 +58,6 @@ Here's an example: you're writing the [`native()`](https://github.com/tridactyl/ - config_rc.ts: Functions related to loading and executing the tridactylrc. - controller_background.ts: Parses and executes ex commands. - download_background.ts: Utility functions related to downloading that have to live in the background because downloading APIs aren't available to other processes. -- native_background.ts: Wrappers around Firefox's native messaging API. Also has "higher-level" functions that interact with the native messenger (finding the user's favorite editor, reading/setting preferences...). - nearley_utils.ts: Remnant of Tridactyl's previous architecture, where keys were handled in the background script. ### src/content/ @@ -88,6 +87,7 @@ Here's an example: you're writing the [`native()`](https://github.com/tridactyl/ - logging.ts: Tridactyl's logging interfaces. - math.ts: Math stuff. - messaging.ts: Implementation of Tridactyl's messaging functions (attributeCaller, message, messageTab, messageOwnTab...). +- native.ts: Wrappers around Firefox's native messaging API. Also has "higher-level" functions that interact with the native messenger (finding the user's favorite editor, reading/setting preferences...). - requests.ts: CSP-clobbering code. - text_to_speech.ts: Various wrappers around Firefox's TTS APIs. - url_util.ts: Url incrementation, query-extraction, interpolation. diff --git a/src/background.ts b/src/background.ts index 401e5d73..66d47594 100644 --- a/src/background.ts +++ b/src/background.ts @@ -16,7 +16,7 @@ import * as download_background from "@src/background/download_background" import * as itertools from "@src/lib/itertools" import * as keyseq from "@src/lib/keyseq" import * as request from "@src/lib/requests" -import * as native from "@src/background/native_background" +import * as native from "@src/lib/native" import state from "@src/state" import * as webext from "@src/lib/webext" import { AutoContain } from "@src/lib/autocontainers" diff --git a/src/background/config_rc.ts b/src/background/config_rc.ts index e91264b8..d4599a39 100644 --- a/src/background/config_rc.ts +++ b/src/background/config_rc.ts @@ -1,5 +1,5 @@ import * as Controller from "@src/background/controller_background" -import * as Native from "@src/background/native_background" +import * as Native from "@src/lib/native" import Logger from "@src/lib/logging" const logger = new Logger("rc") diff --git a/src/background/download_background.ts b/src/background/download_background.ts index b1940fa6..fd02fc65 100644 --- a/src/background/download_background.ts +++ b/src/background/download_background.ts @@ -2,7 +2,7 @@ * Background download-related functions */ -import * as Native from "@src/background/native_background" +import * as Native from "@src/lib/native" import { getDownloadFilenameForUrl } from "@src/lib/url_util" /** Construct an object URL string from a given data URL diff --git a/src/completions/FileSystem.ts b/src/completions/FileSystem.ts index 74211f65..dfeedd55 100644 --- a/src/completions/FileSystem.ts +++ b/src/completions/FileSystem.ts @@ -1,5 +1,5 @@ import * as Completions from "@src/completions" -import * as Messaging from "@src/lib/messaging" +import * as Native from "@src/lib/native" import * as config from "@src/lib/config" class FileSystemCompletionOption extends Completions.CompletionOptionHTML @@ -53,9 +53,7 @@ export class FileSystemCompletionSource extends Completions.CompletionSourceFuse let req try { - req = await Messaging.message("native_background", "listDir", [ - path, - ]) + req = await Native.listDir(path) } catch (e) { // Failing silently because we can't nativegate (the user is typing stuff in the commandline) this.state = "hidden" diff --git a/src/completions/Preferences.ts b/src/completions/Preferences.ts index eef118a6..057b0188 100644 --- a/src/completions/Preferences.ts +++ b/src/completions/Preferences.ts @@ -1,5 +1,5 @@ import * as Completions from "@src/completions" -import * as Messaging from "@src/lib/messaging" +import * as Native from "@src/lib/native" class PreferenceCompletionOption extends Completions.CompletionOptionHTML implements Completions.CompletionOptionFuse { @@ -46,7 +46,7 @@ export class PreferenceCompletionSource extends Completions.CompletionSourceFuse return } this.lastExstr = exstr - let preferences = await Messaging.message("native_background", "getPrefs", []) + let preferences = await Native.getPrefs() this.options = Object.keys(preferences) .filter(key => key.startsWith(pref)) .map(key => new PreferenceCompletionOption(key, preferences[key])) diff --git a/src/content.ts b/src/content.ts index 25568028..03826b55 100644 --- a/src/content.ts +++ b/src/content.ts @@ -70,7 +70,7 @@ import * as webext from "@src/lib/webext" import Mark from "mark.js" import * as perf from "@src/perf" import * as keyseq from "@src/lib/keyseq" -import * as native from "@src/background/native_background" +import * as native from "@src/lib/native" import * as styling from "@src/content/styling" ;(window as any).tri = Object.assign(Object.create(null), { browserBg: webext.browserBg, diff --git a/src/content/scrolling.ts b/src/content/scrolling.ts index 8d96c556..03b13949 100644 --- a/src/content/scrolling.ts +++ b/src/content/scrolling.ts @@ -1,4 +1,3 @@ -import * as Native from "@src/background/native_background" import * as config from "@src/lib/config" type scrollingDirection = "scrollLeft" | "scrollTop" diff --git a/src/excmds.ts b/src/excmds.ts index aeb96446..bd9529cd 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -84,6 +84,7 @@ import Mark from "mark.js" import * as CSS from "css" import * as Perf from "@src/perf" import * as Metadata from "@src/.metadata.generated" +import * as Native from "@src/lib/native" /** @hidden **/ const TRI_VERSION = "REPLACE_ME_WITH_THE_VERSION_USING_SED" @@ -112,9 +113,6 @@ import * as rc from "@src/background/config_rc" import * as excmd_parser from "@src/parsers/exmode" import { mapstrToKeyseq } from "@src/lib/keyseq" -//#background_helper -import * as Native from "@src/background/native_background" - /** @hidden */ export const cmd_params = new Map>() // } @@ -251,7 +249,7 @@ export function removeTridactylEditorClass(selector: string) { * Opens your favourite editor (which is currently gVim) and fills the last used input with whatever you write into that file. * **Requires that the native messenger is installed, see [[native]] and [[installnative]]**. * - * Uses the `editorcmd` config option, default = `auto` looks through a list defined in native_background.ts try find a sensible combination. If it's a bit slow, or chooses the wrong editor, or gives up completely, set editorcmd to something you want. The command must stay in the foreground until the editor exits. + * Uses the `editorcmd` config option, default = `auto` looks through a list defined in lib/native.ts try find a sensible combination. If it's a bit slow, or chooses the wrong editor, or gives up completely, set editorcmd to something you want. The command must stay in the foreground until the editor exits. * * The editorcmd needs to accept a filename, stay in the foreground while it's edited, save the file and exit. By default the filename is added to the end of editorcmd, if you require control over the position of that argument, the first occurrence of %f in editorcmd is replaced with the filename: * ``` diff --git a/src/lib/messaging.ts b/src/lib/messaging.ts index c168bc26..a3610fa9 100644 --- a/src/lib/messaging.ts +++ b/src/lib/messaging.ts @@ -12,7 +12,6 @@ export type NonTabMessageType = | "commandline_background" | "controller_background" | "browser_proxy_background" - | "native_background" | "download_background" | "performance_background" export type MessageType = TabMessageType | NonTabMessageType diff --git a/src/background/native_background.ts b/src/lib/native.ts similarity index 99% rename from src/background/native_background.ts rename to src/lib/native.ts index a6be9075..dedad94d 100644 --- a/src/background/native_background.ts +++ b/src/lib/native.ts @@ -2,7 +2,6 @@ * Background functions for the native messenger interface */ -import * as Messaging from "@src/lib/messaging" import * as semverCompare from "semver-compare" import * as config from "@src/lib/config" import { browserBg } from "@src/lib/webext" @@ -619,6 +618,3 @@ export async function writePref(name: string, value: any) { write(file, text.replace(substr, `pref("${name}", ${value})`)) } } - -import * as SELF from "@src/background/native_background" -Messaging.addListener("native_background", Messaging.attributeCaller(SELF))