Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugin-ext] Fix plugin api in electron packaging #2262

Merged
merged 1 commit into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/plugin-ext/src/api/rpc-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ class RPCMultiplexer {

public send(msg: string): void {
if (this.messagesToSend.length === 0) {
setImmediate(this.sendAccumulatedBound);
if (typeof setImmediate !== 'undefined') {
setImmediate(this.sendAccumulatedBound);
} else {
setTimeout(this.sendAccumulatedBound, 0);
}
}
this.messagesToSend.push(msg);
}
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ext/src/common/plugin-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface PluginPackage {
displayName: string;
description: string;
contributes: {};
packagePath: string;
}

export const PluginScanner = Symbol('PluginScanner');
Expand Down
15 changes: 14 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 @@ -43,7 +43,12 @@ rpc.set(MAIN_RPC_CONTEXT.HOSTED_PLUGIN_MANAGER_EXT, new HostedPluginManagerExtIm
ctx.importScripts('/context/' + contextPath);
},
loadPlugin(contextPath: string, plugin: Plugin): void {
ctx.importScripts('/hostedPlugin/' + getPluginId(plugin.model) + '/' + plugin.pluginPath);
if (isElectron()) {
ctx.importScripts(plugin.pluginPath);
} else {
ctx.importScripts('/hostedPlugin/' + getPluginId(plugin.model) + '/' + plugin.pluginPath);
}

if (plugin.lifecycle.frontendModuleName) {
if (!ctx[plugin.lifecycle.frontendModuleName]) {
console.error(`WebWorker: Cannot start plugin "${plugin.model.name}". Frontend plugin not found: "${plugin.lifecycle.frontendModuleName}"`);
Expand All @@ -62,3 +67,11 @@ rpc.set(MAIN_RPC_CONTEXT.HOSTED_PLUGIN_MANAGER_EXT, new HostedPluginManagerExtIm
});
}
}));

function isElectron() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't have utility function somewhere else (or it's due to worker API) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we don't, and its very specific for WebWorker

if (typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('Electron') >= 0) {
return true;
}

return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import { HostedPluginManager, ElectronNodeHostedPluginRunner } from '../node/hosted-plugin-manager';
import { interfaces } from 'inversify';
import { bindCommonHostedBackend } from '../node/plugin-ext-hosted-backend-module';
import { PluginScanner } from '../../common/plugin-protocol';
import { TheiaPluginScannerElectron } from './scanner-theia-electron';

export function bindElectronBackend(bind: interfaces.Bind): void {
bind(HostedPluginManager).to(ElectronNodeHostedPluginRunner);
bind(PluginScanner).to(TheiaPluginScannerElectron).inSingletonScope();
bindCommonHostedBackend(bind);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/********************************************************************************
* Copyright (C) 2018 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { TheiaPluginScanner } from "../node/scanners/scanner-theia";
import { injectable } from "inversify";
import { PluginPackage, PluginModel } from "../../common/plugin-protocol";
@injectable()
export class TheiaPluginScannerElectron extends TheiaPluginScanner {
getModel(plugin: PluginPackage): PluginModel {
const result = super.getModel(plugin);
if (result.entryPoint.frontend) {
result.entryPoint.frontend = plugin.packagePath + result.entryPoint.frontend;
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function bindCommonHostedBackend(bind: interfaces.Bind): void {
bind(HostedPluginReader).toSelf().inSingletonScope();
bind(HostedPluginServer).to(HostedPluginServerImpl).inSingletonScope();
bind(HostedPluginSupport).toSelf().inSingletonScope();
bind(PluginScanner).to(TheiaPluginScanner).inSingletonScope();
bind(MetadataScanner).toSelf().inSingletonScope();

bind(BackendApplicationContribution).toDynamicValue(ctx => ctx.container.get(HostedPluginReader)).inSingletonScope();
Expand All @@ -50,6 +49,7 @@ export function bindCommonHostedBackend(bind: interfaces.Bind): void {

export function bindHostedBackend(bind: interfaces.Bind): void {
bind(HostedPluginManager).to(NodeHostedPluginRunner).inSingletonScope();
bind(PluginScanner).to(TheiaPluginScanner).inSingletonScope();
bindContributionProvider(bind, Symbol.for(HostedPluginUriPostProcessorSymbolName));
bindCommonHostedBackend(bind);
}
1 change: 1 addition & 0 deletions packages/plugin-ext/src/hosted/node/plugin-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class HostedPluginReader implements BackendApplicationContribution {
}

const plugin: PluginPackage = require(packageJsonPath);
plugin.packagePath = path;
const pluginMetadata = this.scanner.getPluginMetadata(plugin);
if (pluginMetadata.model.entryPoint.backend) {
pluginMetadata.model.entryPoint.backend = resolve(path, pluginMetadata.model.entryPoint.backend);
Expand Down
8 changes: 7 additions & 1 deletion packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export function createAPI(rpc: RPCProtocol): typeof theia {
// tslint:disable-next-line:no-any
export function startPlugin(plugin: Plugin, pluginMain: any, plugins: Map<string, () => void>): void {
if (typeof pluginMain[plugin.lifecycle.startMethod] === 'function') {
pluginMain[plugin.lifecycle.startMethod].apply(global, []);
pluginMain[plugin.lifecycle.startMethod].apply(getGlobal(), []);
} else {
console.log('there is no doStart method on plugin');
}
Expand All @@ -288,3 +288,9 @@ export function startPlugin(plugin: Plugin, pluginMain: any, plugins: Map<string
plugins.set(pluginId, pluginMain[plugin.lifecycle.stopMethod]);
}
}

// for electron
function getGlobal() {
// tslint:disable-next-line:no-null-keyword
return typeof self === "undefined" ? typeof global === "undefined" ? null : global : self;
}