Skip to content

Commit

Permalink
♻️ add cookie ipc service
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondYuan committed May 5, 2020
1 parent d08d344 commit f9d439f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/common/backend/services/youdao/service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as browser from '@web-clipper/chrome-promise';
import { CompleteStatus, UnauthorizedError } from './../interface';
import { DocumentService, Repository, CreateDocumentRequest } from '../../index';
import axios, { AxiosInstance } from 'axios';
import { stringify } from 'qs';
import { generateUuid } from '@web-clipper/shared/lib/uuid';
import localeService from '@/common/locales';
import Container from 'typedi';
import { ICookieService } from '@/service/common/cookie';

interface YouDaoRepository {
fileEntry: {
Expand Down Expand Up @@ -152,7 +153,7 @@ export default class YoudaoDocumentService implements DocumentService {
};

private getCSTK = async () => {
const cookie = await browser.cookies.get({
const cookie = await Container.get(ICookieService).get({
url: 'https://note.youdao.com',
name: 'YNOTE_CSTK',
});
Expand Down
1 change: 1 addition & 0 deletions src/main/background.main.common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'regenerator-runtime/runtime';
import 'reflect-metadata';
import '@/service/tab/browser/background/tabService';
import '@/service/cookie/background/cookieService';
import '@/service/trackService';
4 changes: 4 additions & 0 deletions src/main/background.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { ITabService } from '@/service/common/tab';
import { IChannelServer } from '@/service/common/ipc';
import { BackgroundIPCServer } from '@/service/ipc/browser/background-main/ipcService';
import { TabChannel } from '@/service/tab/common/tabIpc';
import { ICookieService } from '@/service/common/cookie';
import { CookieChannel } from '@/service/cookie/common/cookieIpc';

const backgroundIPCServer: IChannelServer = new BackgroundIPCServer();

Expand All @@ -29,6 +31,8 @@ backgroundIPCServer.registerChannel(
new WebRequestChannel(Container.get(IWebRequestService))
);

backgroundIPCServer.registerChannel('cookies', new CookieChannel(Container.get(ICookieService)));

const contentScriptIPCClient = new PopupContentScriptIPCClient(Container.get(ITabService));
const contentScriptChannel = contentScriptIPCClient.getChannel('contentScript');
Container.set(IContentScriptService, new ContentScriptChannelClient(contentScriptChannel));
Expand Down
5 changes: 5 additions & 0 deletions src/main/tool.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import '@/service/extension/browser/extensionService';
import '@/service/extension/browser/extensionContainer';
import { TabChannelClient } from '@/service/tab/common/tabIpc';
import app from '@/pages/app';
import { CookieChannelClient } from '@/service/cookie/common/cookieIpc';
import { ICookieService } from '@/service/common/cookie';

const ipcClient = new PopupIpcClient();

Expand All @@ -32,4 +34,7 @@ Container.set(IContentScriptService, new ContentScriptChannelClient(contentScrip
const webRequestChannel = ipcClient.getChannel('webRequest');
Container.set(IWebRequestService, new WebRequestChannelClient(webRequestChannel));

const cookieChannel = ipcClient.getChannel('cookies');
Container.set(ICookieService, new CookieChannelClient(cookieChannel));

app();
1 change: 1 addition & 0 deletions src/service/common/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Token } from 'typedi';

export interface ICookieService {
get(details: chrome.cookies.Details): Promise<chrome.cookies.Cookie | null>;
getAll(details: chrome.cookies.GetAllDetails): Promise<chrome.cookies.Cookie[]>;
getAllCookieStores(): Promise<chrome.cookies.CookieStore[]>;
}
Expand Down
25 changes: 25 additions & 0 deletions src/service/cookie/background/cookieService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Service } from 'typedi';
import { ICookieService } from '@/service/common/cookie';

class ChromeCookieService implements ICookieService {
get(detail: chrome.cookies.Details): Promise<chrome.cookies.Cookie | null> {
return new Promise<chrome.cookies.Cookie | null>(r => {
chrome.cookies.get(detail, r);
});
}

getAll(detail: chrome.cookies.GetAllDetails): Promise<chrome.cookies.Cookie[]> {
return new Promise<chrome.cookies.Cookie[]>(r => {
chrome.cookies.getAll(detail, r);
});
}
getAllCookieStores(): Promise<chrome.cookies.CookieStore[]> {
return new Promise<chrome.cookies.CookieStore[]>(r => {
chrome.cookies.getAllCookieStores(cookieStores => {
r(cookieStores);
});
});
}
}

Service(ICookieService)(ChromeCookieService);
40 changes: 40 additions & 0 deletions src/service/cookie/common/cookieIpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { IServerChannel, IChannel } from '@/service/common/ipc';
import { ICookieService } from '@/service/common/cookie';

export class CookieChannel implements IServerChannel {
constructor(private service: ICookieService) {}

call = async (
_context: chrome.runtime.Port['sender'],
command: string,
arg: any
): Promise<any> => {
switch (command) {
case 'get':
return this.service.get(arg);
case 'getAll':
return this.service.getAll(arg);
case 'getAllCookieStores':
return this.service.getAllCookieStores();
default: {
throw new Error(`Call not found: ${command}`);
}
}
};
}

export class CookieChannelClient implements ICookieService {
constructor(private channel: IChannel) {}

get = async (detail: chrome.cookies.Details): Promise<chrome.cookies.Cookie | null> => {
return this.channel.call('get', detail);
};

getAll = async (detail: chrome.cookies.GetAllDetails): Promise<chrome.cookies.Cookie[]> => {
return this.channel.call('getAll', detail);
};

getAllCookieStores = async (): Promise<chrome.cookies.CookieStore[]> => {
return this.channel.call('getAllCookieStores');
};
}

0 comments on commit f9d439f

Please sign in to comment.