Skip to content

Commit

Permalink
fix #6305: initialize ext storage proxy earlier
Browse files Browse the repository at this point in the history
Otherwise the main side can send events before the plugin manager is initialized leading to exceptions.

Signed-off-by: Anton Kosyakov <[email protected]>
  • Loading branch information
akosyakov committed Oct 4, 2019
1 parent 0bb4c49 commit e3ff100
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
4 changes: 3 additions & 1 deletion packages/plugin-ext/src/hosted/browser/worker/worker-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { WorkspaceExtImpl } from '../../../plugin/workspace';
import { MessageRegistryExt } from '../../../plugin/message-registry';
import { WorkerEnvExtImpl } from './worker-env-ext';
import { ClipboardExt } from '../../../plugin/clipboard-ext';
import { KeyValueStorageProxy } from '../../../plugin/plugin-storage';

// tslint:disable-next-line:no-any
const ctx = self as any;
Expand All @@ -51,6 +52,7 @@ function initialize(contextPath: string, pluginMetadata: PluginMetadata): void {
ctx.importScripts('/context/' + contextPath);
}
const envExt = new WorkerEnvExtImpl(rpc);
const storageProxy = new KeyValueStorageProxy(rpc);
const editorsAndDocuments = new EditorsAndDocumentsExtImpl(rpc);
const messageRegistryExt = new MessageRegistryExt(rpc);
const workspaceExt = new WorkspaceExtImpl(rpc, editorsAndDocuments, messageRegistryExt);
Expand Down Expand Up @@ -129,7 +131,7 @@ const pluginManager = new PluginManagerExtImpl({
}
}
}
}, envExt, preferenceRegistryExt, rpc);
}, envExt, storageProxy, preferenceRegistryExt, rpc);

const apiFactory = createAPIFactory(
rpc,
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { MessageRegistryExt } from '../../plugin/message-registry';
import { EnvNodeExtImpl } from '../../plugin/node/env-node-ext';
import { ClipboardExt } from '../../plugin/clipboard-ext';
import { loadManifest } from './plugin-manifest-loader';
import { KeyValueStorageProxy } from '../../plugin/plugin-storage';

/**
* Handle the RPC calls.
Expand All @@ -44,17 +45,19 @@ export class PluginHostRPC {

initialize(): void {
const envExt = new EnvNodeExtImpl(this.rpc);
const storageProxy = new KeyValueStorageProxy(this.rpc);
const debugExt = new DebugExtImpl(this.rpc);
const editorsAndDocumentsExt = new EditorsAndDocumentsExtImpl(this.rpc);
const messageRegistryExt = new MessageRegistryExt(this.rpc);
const workspaceExt = new WorkspaceExtImpl(this.rpc, editorsAndDocumentsExt, messageRegistryExt);
const preferenceRegistryExt = new PreferenceRegistryExtImpl(this.rpc, workspaceExt);
const clipboardExt = new ClipboardExt(this.rpc);
this.pluginManager = this.createPluginManager(envExt, preferenceRegistryExt, this.rpc);
this.pluginManager = this.createPluginManager(envExt, storageProxy, preferenceRegistryExt, this.rpc);
this.rpc.set(MAIN_RPC_CONTEXT.HOSTED_PLUGIN_MANAGER_EXT, this.pluginManager);
this.rpc.set(MAIN_RPC_CONTEXT.EDITORS_AND_DOCUMENTS_EXT, editorsAndDocumentsExt);
this.rpc.set(MAIN_RPC_CONTEXT.WORKSPACE_EXT, workspaceExt);
this.rpc.set(MAIN_RPC_CONTEXT.PREFERENCE_REGISTRY_EXT, preferenceRegistryExt);
this.rpc.set(MAIN_RPC_CONTEXT.STORAGE_EXT, storageProxy);

this.apiFactory = createAPIFactory(
this.rpc,
Expand Down Expand Up @@ -82,7 +85,7 @@ export class PluginHostRPC {
}

// tslint:disable-next-line:no-any
createPluginManager(envExt: EnvExtImpl, preferencesManager: PreferenceRegistryExtImpl, rpc: any): PluginManagerExtImpl {
createPluginManager(envExt: EnvExtImpl, storageProxy: KeyValueStorageProxy, preferencesManager: PreferenceRegistryExtImpl, rpc: any): PluginManagerExtImpl {
const { extensionTestsPath } = process.env;
const self = this;
const pluginManager = new PluginManagerExtImpl({
Expand Down Expand Up @@ -213,7 +216,7 @@ export class PluginHostRPC {
`Path ${extensionTestsPath} does not point to a valid extension test runner.`
);
} : undefined
}, envExt, preferencesManager, rpc);
}, envExt, storageProxy, preferencesManager, rpc);
return pluginManager;
}
}
10 changes: 2 additions & 8 deletions packages/plugin-ext/src/plugin/plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import {
PLUGIN_RPC_CONTEXT,
MAIN_RPC_CONTEXT,
MainMessageType,
MessageRegistryMain,
PluginManagerExt,
Expand Down Expand Up @@ -81,7 +80,6 @@ export class PluginManagerExtImpl implements PluginManagerExt, PluginManager {
private readonly activatedPlugins = new Map<string, ActivatedPlugin>();
private readonly pluginActivationPromises = new Map<string, Deferred<void>>();
private readonly pluginContextsMap = new Map<string, theia.PluginContext>();
private storageProxy: KeyValueStorageProxy;

private onDidChangeEmitter = new Emitter<void>();
private messageRegistryProxy: MessageRegistryMain;
Expand All @@ -92,6 +90,7 @@ export class PluginManagerExtImpl implements PluginManagerExt, PluginManager {
constructor(
private readonly host: PluginHost,
private readonly envExt: EnvExtImpl,
private readonly storageProxy: KeyValueStorageProxy,
private readonly preferencesManager: PreferenceRegistryExtImpl,
private readonly rpc: RPCProtocol
) {
Expand Down Expand Up @@ -138,12 +137,7 @@ export class PluginManagerExtImpl implements PluginManagerExt, PluginManager {
}

async $init(params: PluginManagerInitializeParams): Promise<void> {
this.storageProxy = this.rpc.set(
MAIN_RPC_CONTEXT.STORAGE_EXT,
new KeyValueStorageProxy(this.rpc.getProxy(PLUGIN_RPC_CONTEXT.STORAGE_MAIN),
params.globalState,
params.workspaceState)
);
this.storageProxy.init(params.globalState, params.workspaceState);

this.envExt.setQueryParameters(params.env.queryParams);
this.envExt.setLanguage(params.env.language);
Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-ext/src/plugin/plugin-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import * as theia from '@theia/plugin';
import { Event, Emitter } from '@theia/core/lib/common/event';
import { StorageMain, StorageExt } from '../common/plugin-api-rpc';
import { KeysToAnyValues, KeysToKeysToAnyValue } from '../common/types';
import { RPCProtocol } from '../common/rpc-protocol';
import { PLUGIN_RPC_CONTEXT } from '../common/plugin-api-rpc';

export class Memento implements theia.Memento {

Expand Down Expand Up @@ -68,13 +70,11 @@ export class KeyValueStorageProxy implements StorageExt {
private globalDataCache: KeysToKeysToAnyValue;
private workspaceDataCache: KeysToKeysToAnyValue;

constructor(
proxy: StorageMain,
initGlobalData: KeysToKeysToAnyValue,
initWorkspaceData: KeysToKeysToAnyValue
) {
this.proxy = proxy;
constructor(rpc: RPCProtocol) {
this.proxy = rpc.getProxy<StorageMain>(PLUGIN_RPC_CONTEXT.STORAGE_MAIN);
}

init(initGlobalData: KeysToKeysToAnyValue, initWorkspaceData: KeysToKeysToAnyValue): void {
this.globalDataCache = initGlobalData;
this.workspaceDataCache = initWorkspaceData;
}
Expand Down

0 comments on commit e3ff100

Please sign in to comment.