Move some navigation operations to background

jh-changes
Shin'ya Ueoka 6 years ago
parent 07897df636
commit 5a7db96feb
  1. 29
      src/background/clients/NavigateClient.ts
  2. 10
      src/background/controllers/OperationController.ts
  3. 40
      src/background/usecases/NavigateUseCase.ts
  4. 10
      src/content/Application.ts
  5. 12
      src/content/controllers/KeymapController.ts
  6. 31
      src/content/controllers/NavigateController.ts
  7. 31
      src/shared/messages.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,
});
}
}

@ -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);
}

@ -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');
}
}

@ -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);
}
});

@ -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;

@ -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();
}
}

@ -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);