From 5a7db96febd4e3cb2aa519610da75e0bccd69a7f Mon Sep 17 00:00:00 2001
From: Shin'ya Ueoka <ueokande@i-beam.org>
Date: Thu, 23 May 2019 21:30:07 +0900
Subject: Move some navigation operations to background

---
 src/background/clients/NavigateClient.ts          | 29 ++++++++++++++++
 src/background/controllers/OperationController.ts | 10 ++++++
 src/background/usecases/NavigateUseCase.ts        | 40 +++++++++++++++++++++++
 src/content/Application.ts                        | 10 ++++++
 src/content/controllers/KeymapController.ts       | 12 -------
 src/content/controllers/NavigateController.ts     | 31 ++++++++++++++++++
 src/shared/messages.ts                            | 31 +++++++++++++++++-
 7 files changed, 150 insertions(+), 13 deletions(-)
 create mode 100644 src/background/clients/NavigateClient.ts
 create mode 100644 src/background/usecases/NavigateUseCase.ts
 create mode 100644 src/content/controllers/NavigateController.ts

(limited to 'src')

diff --git a/src/background/clients/NavigateClient.ts b/src/background/clients/NavigateClient.ts
new file mode 100644
index 0000000..bdd94ec
--- /dev/null
+++ b/src/background/clients/NavigateClient.ts
@@ -0,0 +1,29 @@
+import { injectable } from 'tsyringe';
+import * as messages from '../../shared/messages';
+
+@injectable()
+export default class NavigateClient {
+  async historyNext(tabId: number): Promise<void> {
+    await browser.tabs.sendMessage(tabId, {
+      type: messages.NAVIGATE_HISTORY_NEXT,
+    });
+  }
+
+  async historyPrev(tabId: number): Promise<void> {
+    await browser.tabs.sendMessage(tabId, {
+      type: messages.NAVIGATE_HISTORY_PREV,
+    });
+  }
+
+  async linkNext(tabId: number): Promise<void> {
+    await browser.tabs.sendMessage(tabId, {
+      type: messages.NAVIGATE_LINK_NEXT,
+    });
+  }
+
+  async linkPrev(tabId: number): Promise<void> {
+    await browser.tabs.sendMessage(tabId, {
+      type: messages.NAVIGATE_LINK_PREV,
+    });
+  }
+}
diff --git a/src/background/controllers/OperationController.ts b/src/background/controllers/OperationController.ts
index de6f8cb..cb5c043 100644
--- a/src/background/controllers/OperationController.ts
+++ b/src/background/controllers/OperationController.ts
@@ -5,6 +5,7 @@ import ConsoleUseCase from '../usecases/ConsoleUseCase';
 import TabUseCase from '../usecases/TabUseCase';
 import TabSelectUseCase from '../usecases/TabSelectUseCase';
 import ZoomUseCase from '../usecases/ZoomUseCase';
+import NavigateUseCase from '../usecases/NavigateUseCase';
 
 @injectable()
 export default class OperationController {
@@ -14,6 +15,7 @@ export default class OperationController {
     private tabUseCase: TabUseCase,
     private tabSelectUseCase: TabSelectUseCase,
     private zoomUseCase: ZoomUseCase,
+    private navigateUseCase: NavigateUseCase,
   ) {
   }
 
@@ -74,6 +76,14 @@ export default class OperationController {
       return this.findUseCase.findStart();
     case operations.CANCEL:
       return this.consoleUseCase.hideConsole();
+    case operations.NAVIGATE_HISTORY_PREV:
+      return this.navigateUseCase.openHistoryPrev();
+    case operations.NAVIGATE_HISTORY_NEXT:
+      return this.navigateUseCase.openHistoryNext();
+    case operations.NAVIGATE_LINK_PREV:
+      return this.navigateUseCase.openLinkPrev();
+    case operations.NAVIGATE_LINK_NEXT:
+      return this.navigateUseCase.openLinkNext();
     }
     throw new Error('unknown operation: ' + operation.type);
   }
diff --git a/src/background/usecases/NavigateUseCase.ts b/src/background/usecases/NavigateUseCase.ts
new file mode 100644
index 0000000..76e1c8e
--- /dev/null
+++ b/src/background/usecases/NavigateUseCase.ts
@@ -0,0 +1,40 @@
+import { injectable } from 'tsyringe';
+import NavigateClient from '../clients/NavigateClient';
+import TabPresenter from '../presenters/TabPresenter';
+
+@injectable()
+export default class NavigateUseCase {
+  constructor(
+    private tabPresenter: TabPresenter,
+    private navigateClient: NavigateClient,
+  ) {
+  }
+
+  async openHistoryNext(): Promise<void> {
+    let tab = await this.tabPresenter.getCurrent();
+    await this.navigateClient.historyNext(tab.id!!);
+  }
+
+  async openHistoryPrev(): Promise<void> {
+    let tab = await this.tabPresenter.getCurrent();
+    await this.navigateClient.historyPrev(tab.id!!);
+  }
+
+  async openLinkNext(): Promise<void> {
+    let tab = await this.tabPresenter.getCurrent();
+    await this.navigateClient.linkNext(tab.id!!);
+  }
+
+  async openLinkPrev(): Promise<void> {
+    let tab = await this.tabPresenter.getCurrent();
+    await this.navigateClient.linkPrev(tab.id!!);
+  }
+
+  openParent(): Promise<void> {
+    throw new Error('not implemented');
+  }
+
+  openRoot(): Promise<void> {
+    throw new Error('not implemented');
+  }
+}
diff --git a/src/content/Application.ts b/src/content/Application.ts
index 470bf53..1677655 100644
--- a/src/content/Application.ts
+++ b/src/content/Application.ts
@@ -12,6 +12,7 @@ import MarkKeyController from './controllers/MarkKeyController';
 import AddonEnabledController from './controllers/AddonEnabledController';
 import SettingController from './controllers/SettingController';
 import ConsoleFrameController from './controllers/ConsoleFrameController';
+import NavigateController from './controllers/NavigateController';
 import * as messages from '../shared/messages';
 
 type Message = messages.Message;
@@ -33,6 +34,7 @@ export default class Application {
     private addonEnabledController: AddonEnabledController,
     private settingController: SettingController,
     private consoleFrameController: ConsoleFrameController,
+    private navigateController: NavigateController,
   ) {
   }
 
@@ -98,6 +100,14 @@ export default class Application {
         return this.settingController.reloadSettings(msg);
       case messages.ADDON_TOGGLE_ENABLED:
         return this.addonEnabledUseCase.toggle();
+      case messages.NAVIGATE_HISTORY_NEXT:
+        return this.navigateController.openHistoryNext(msg);
+      case messages.NAVIGATE_HISTORY_PREV:
+        return this.navigateController.openHistoryPrev(msg);
+      case messages.NAVIGATE_LINK_NEXT:
+        return this.navigateController.openLinkNext(msg);
+      case messages.NAVIGATE_LINK_PREV:
+        return this.navigateController.openLinkPrev(msg);
       }
     });
 
diff --git a/src/content/controllers/KeymapController.ts b/src/content/controllers/KeymapController.ts
index 1835546..4be8f9d 100644
--- a/src/content/controllers/KeymapController.ts
+++ b/src/content/controllers/KeymapController.ts
@@ -84,18 +84,6 @@ export default class KeymapController {
     case operations.MARK_JUMP_PREFIX:
       this.markKeyUseCase.enableJumpMode();
       break;
-    case operations.NAVIGATE_HISTORY_PREV:
-      this.navigateUseCase.openHistoryPrev();
-      break;
-    case operations.NAVIGATE_HISTORY_NEXT:
-      this.navigateUseCase.openHistoryNext();
-      break;
-    case operations.NAVIGATE_LINK_PREV:
-      this.navigateUseCase.openLinkPrev();
-      break;
-    case operations.NAVIGATE_LINK_NEXT:
-      this.navigateUseCase.openLinkNext();
-      break;
     case operations.NAVIGATE_PARENT:
       this.navigateUseCase.openParent();
       break;
diff --git a/src/content/controllers/NavigateController.ts b/src/content/controllers/NavigateController.ts
new file mode 100644
index 0000000..3f2df7a
--- /dev/null
+++ b/src/content/controllers/NavigateController.ts
@@ -0,0 +1,31 @@
+import { injectable } from 'tsyringe';
+import { Message } from '../../shared/messages';
+import NavigateUseCase from '../usecases/NavigateUseCase';
+
+@injectable()
+export default class NavigateController {
+  constructor(
+    private navigateUseCase: NavigateUseCase,
+  ) {
+  }
+
+  openHistoryNext(_m: Message): Promise<void> {
+    this.navigateUseCase.openHistoryNext();
+    return Promise.resolve();
+  }
+
+  openHistoryPrev(_m: Message): Promise<void> {
+    this.navigateUseCase.openHistoryPrev();
+    return Promise.resolve();
+  }
+
+  openLinkNext(_m: Message): Promise<void> {
+    this.navigateUseCase.openLinkNext();
+    return Promise.resolve();
+  }
+
+  openLinkPrev(_m: Message): Promise<void> {
+    this.navigateUseCase.openLinkPrev();
+    return Promise.resolve();
+  }
+}
diff --git a/src/shared/messages.ts b/src/shared/messages.ts
index fbd3478..36a23d8 100644
--- a/src/shared/messages.ts
+++ b/src/shared/messages.ts
@@ -42,6 +42,11 @@ export const SETTINGS_QUERY = 'settings.query';
 
 export const CONSOLE_FRAME_MESSAGE = 'console.frame.message';
 
+export const NAVIGATE_HISTORY_NEXT = 'navigate.history.next';
+export const NAVIGATE_HISTORY_PREV = 'navigate.history.prev';
+export const NAVIGATE_LINK_NEXT = 'navigate.link.next';
+export const NAVIGATE_LINK_PREV = 'navigate.link.prev';
+
 export interface BackgroundOperationMessage {
   type: typeof BACKGROUND_OPERATION;
   operation: operations.Operation;
@@ -204,6 +209,22 @@ export interface ConsoleFrameMessageMessage {
   message: any;
 }
 
+export interface NavigateHistoryNextMessage {
+  type: typeof NAVIGATE_HISTORY_NEXT;
+}
+
+export interface NavigateHistoryPrevMessage {
+  type: typeof NAVIGATE_HISTORY_PREV;
+}
+
+export interface NavigateLinkNext {
+  type: typeof NAVIGATE_LINK_NEXT;
+}
+
+export interface NavigateLinkPrev {
+  type: typeof NAVIGATE_LINK_PREV;
+}
+
 export type Message =
   BackgroundOperationMessage |
   ConsoleUnfocusMessage |
@@ -236,7 +257,11 @@ export type Message =
   OpenUrlMessage |
   SettingsChangedMessage |
   SettingsQueryMessage |
-  ConsoleFrameMessageMessage;
+  ConsoleFrameMessageMessage |
+  NavigateHistoryNextMessage |
+  NavigateHistoryPrevMessage |
+  NavigateLinkNext |
+  NavigateLinkPrev;
 
 // eslint-disable-next-line complexity
 export const valueOf = (o: any): Message => {
@@ -272,6 +297,10 @@ export const valueOf = (o: any): Message => {
   case SETTINGS_CHANGED:
   case SETTINGS_QUERY:
   case CONSOLE_FRAME_MESSAGE:
+  case NAVIGATE_HISTORY_NEXT:
+  case NAVIGATE_HISTORY_PREV:
+  case NAVIGATE_LINK_NEXT:
+  case NAVIGATE_LINK_PREV:
     return o;
   }
   throw new Error('unknown operation type: ' + o.type);
-- 
cgit v1.2.3