Skip to content
This repository has been archived by the owner on Jun 4, 2023. It is now read-only.

Commit

Permalink
feat: add a way to uninstall extensions (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
sentialx authored Feb 17, 2021
1 parent d34d277 commit dfa8a80
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/node-fetch": "^2.5.8",
"@types/react": "17.0.2",
"@types/react-dom": "17.0.1",
"@types/rimraf": "^3.0.0",
"@types/styled-components": "5.1.7",
"@typescript-eslint/eslint-plugin": "^4.15.1",
"@typescript-eslint/parser": "^4.15.1",
Expand Down
9 changes: 9 additions & 0 deletions src/common/rpc/extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { RendererToMainChannel } from '@wexond/rpc-electron';

export interface ExtensionMainService {
uninstall(id: string): void;
}

export const extensionMainChannel = new RendererToMainChannel<ExtensionMainService>(
'ExtensionMainService',
);
3 changes: 3 additions & 0 deletions src/main/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { runAutoUpdaterService } from './services';
import { DialogsService } from './services/dialogs-service';
import { requestAuth } from './dialogs/auth';
import { NetworkServiceHandler } from './network/network-service-handler';
import { ExtensionServiceHandler } from './extension-service-handler';

export class Application {
public static instance = new Application();
Expand Down Expand Up @@ -88,6 +89,8 @@ export class Application {
private async onReady() {
await app.whenReady();

new ExtensionServiceHandler();

NetworkServiceHandler.get();

checkFiles();
Expand Down
17 changes: 17 additions & 0 deletions src/main/extension-service-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { RpcMainEvent, RpcMainHandler } from '@wexond/rpc-electron';
import {
extensionMainChannel,
ExtensionMainService,
} from '~/common/rpc/extensions';
import { Application } from './application';

export class ExtensionServiceHandler
implements RpcMainHandler<ExtensionMainService> {
constructor() {
extensionMainChannel.getReceiver().handler = this;
}

uninstall(e: RpcMainEvent, id: string): void {
Application.instance.sessions.uninstallExtension(id);
}
}
18 changes: 15 additions & 3 deletions src/main/sessions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { pathExists } from '~/utils/files';
import { extractZip } from '~/utils/zip';
import { extensions, _setFallbackSession } from 'electron-extensions';
import { requestPermission } from './dialogs/permissions';
import * as rimraf from 'rimraf';
import { promisify } from 'util';

const rf = promisify(rimraf);

// TODO: sessions should be separate. This structure actually doesn't make sense.
export class SessionsService {
Expand All @@ -29,9 +33,6 @@ export class SessionsService {
this.clearCache('incognito');

if (process.env.ENABLE_EXTENSIONS) {
// TODO: remove this after fix for e.sender.session
_setFallbackSession(this.view);

extensions.initializeSession(
this.view,
`${app.getAppPath()}/build/extensions-preload.bundle.js`,
Expand Down Expand Up @@ -331,6 +332,17 @@ export class SessionsService {
this.extensionsLoaded = true;
}

async uninstallExtension(id: string) {
if (!process.env.ENABLE_EXTENSIONS) return;

const extension = this.view.getExtension(id);
if (!extension) return;

await this.view.removeExtension(id);

await rf(extension.path);
}

public onCreateTab = async (details: chrome.tabs.CreateProperties) => {
const view = Application.instance.windows.list
.find((x) => x.win.id === details.windowId)
Expand Down
6 changes: 6 additions & 0 deletions src/renderer/views/app/components/BrowserAction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ const onContextMenu = (data: IBrowserAction) => (
) => {
const { target } = e;
const menu = remote.Menu.buildFromTemplate([
{
label: 'Uninstall',
click: () => {
store.extensions.uninstallExtension(data.extensionId);
},
},
{
label: 'Inspect popup',
click: () => {
Expand Down
15 changes: 14 additions & 1 deletion src/renderer/views/app/store/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint @typescript-eslint/camelcase: 0 */

import { makeObservable, observable } from 'mobx';
import { action, makeObservable, observable } from 'mobx';
import { join } from 'path';

import { IBrowserAction } from '../models';
import { promises } from 'fs';
import { ipcRenderer } from 'electron';
import store from '.';
import { extensionMainChannel } from '~/common/rpc/extensions';

export class ExtensionsStore {
public browserActions: IBrowserAction[] = [];
Expand All @@ -20,6 +21,7 @@ export class ExtensionsStore {
browserActions: observable,
defaultBrowserActions: observable,
currentlyToggledPopup: observable,
uninstallExtension: action,
});

this.load();
Expand Down Expand Up @@ -87,4 +89,15 @@ export class ExtensionsStore {

await Promise.all(extensions.map((x) => this.loadExtension(x)));
}

uninstallExtension(id: string) {
this.browserActions = this.browserActions.filter(
(x) => x.extensionId !== id,
);
this.defaultBrowserActions = this.defaultBrowserActions.filter(
(x) => x.extensionId !== id,
);

extensionMainChannel.getInvoker().uninstall(id);
}
}
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,14 @@
dependencies:
"@types/node" "*"

"@types/glob@*":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.3.tgz#e6ba80f36b7daad2c685acd9266382e68985c183"
integrity sha512-SEYeGAIQIQX8NN6LDKprLjbrd5dARM5EXsd8GI/A5l0apYI1fGMWgPHSe4ZKL4eozlAyI+doUE9XbYS4xCkQ1w==
dependencies:
"@types/minimatch" "*"
"@types/node" "*"

"@types/glob@^7.1.1":
version "7.1.2"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.2.tgz#06ca26521353a545d94a0adc74f38a59d232c987"
Expand Down Expand Up @@ -716,6 +724,14 @@
"@types/node" "*"
safe-buffer "*"

"@types/rimraf@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/rimraf/-/rimraf-3.0.0.tgz#b9d03f090ece263671898d57bb7bb007023ac19f"
integrity sha512-7WhJ0MdpFgYQPXlF4Dx+DhgvlPCfz/x5mHaeDQAKhcenvQP1KCpLQ18JklAqeGMYSAT2PxLpzd0g2/HE7fj7hQ==
dependencies:
"@types/glob" "*"
"@types/node" "*"

"@types/semver@^7.3.1":
version "7.3.4"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.4.tgz#43d7168fec6fa0988bb1a513a697b29296721afb"
Expand Down

0 comments on commit dfa8a80

Please sign in to comment.