@interopio/browser-platform 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser.platform.d.ts +455 -0
- package/changelog.md +69 -0
- package/dist/browser.platform.es.js +46074 -0
- package/dist/browser.platform.es.js.map +1 -0
- package/dist/browser.platform.umd.js +46082 -0
- package/dist/browser.platform.umd.js.map +1 -0
- package/license.md +21 -0
- package/package.json +78 -0
- package/readme.md +9 -0
- package/rollup.config.js +45 -0
- package/src/common/constants.ts +46 -0
- package/src/common/defaultConfig.ts +56 -0
- package/src/common/gateway.d.ts +32 -0
- package/src/common/types.ts +222 -0
- package/src/connection/gateway.ts +132 -0
- package/src/connection/no-gateway.ts +1 -0
- package/src/connection/portsBridge.ts +441 -0
- package/src/connection/preferred.ts +247 -0
- package/src/controllers/domains.ts +156 -0
- package/src/controllers/glue.ts +604 -0
- package/src/controllers/interception.ts +53 -0
- package/src/controllers/license.ts +50 -0
- package/src/controllers/main.ts +256 -0
- package/src/controllers/plugins.ts +121 -0
- package/src/controllers/serviceWorker.ts +193 -0
- package/src/controllers/state.ts +84 -0
- package/src/controllers/system.ts +99 -0
- package/src/controllers/transactions.ts +80 -0
- package/src/factory.ts +40 -0
- package/src/fallbacks/browser.ts +47 -0
- package/src/fallbacks/dekstop.ts +21 -0
- package/src/index.ts +18 -0
- package/src/libs/applications/appStore/directory.ts +235 -0
- package/src/libs/applications/appStore/remoteWatcher.ts +112 -0
- package/src/libs/applications/controller.ts +469 -0
- package/src/libs/applications/decoders.ts +98 -0
- package/src/libs/applications/defaults.ts +6 -0
- package/src/libs/applications/types.ts +98 -0
- package/src/libs/channels/controller.ts +109 -0
- package/src/libs/channels/decoders.ts +19 -0
- package/src/libs/extension/controller.ts +120 -0
- package/src/libs/extension/decoders.ts +18 -0
- package/src/libs/extension/types.ts +11 -0
- package/src/libs/intents/constants.ts +9 -0
- package/src/libs/intents/controller.ts +500 -0
- package/src/libs/intents/decoders.ts +96 -0
- package/src/libs/intents/resolverHelper.ts +264 -0
- package/src/libs/intents/types.ts +119 -0
- package/src/libs/layouts/constants.ts +3 -0
- package/src/libs/layouts/controller.ts +476 -0
- package/src/libs/layouts/decoders.ts +132 -0
- package/src/libs/layouts/global/builder.ts +400 -0
- package/src/libs/layouts/global/resetter.ts +116 -0
- package/src/libs/layouts/global/restorer.ts +217 -0
- package/src/libs/layouts/global/validator.ts +169 -0
- package/src/libs/layouts/idbStore.ts +63 -0
- package/src/libs/layouts/types.ts +133 -0
- package/src/libs/manager/controller.ts +175 -0
- package/src/libs/manager/decoders.ts +6 -0
- package/src/libs/manager/defaults.ts +5 -0
- package/src/libs/manager/identity.ts +92 -0
- package/src/libs/manager/session.ts +195 -0
- package/src/libs/manager/types.ts +51 -0
- package/src/libs/notifications/controller.ts +447 -0
- package/src/libs/notifications/decoders.ts +166 -0
- package/src/libs/notifications/types.ts +49 -0
- package/src/libs/notifications/utils.ts +7 -0
- package/src/libs/search/controller.ts +232 -0
- package/src/libs/search/decoders.ts +6 -0
- package/src/libs/search/repositories/applications.ts +62 -0
- package/src/libs/search/repositories/layouts.ts +48 -0
- package/src/libs/search/repositories/workspaces.ts +47 -0
- package/src/libs/search/types.ts +9 -0
- package/src/libs/themes/constants.ts +9 -0
- package/src/libs/themes/controller.ts +153 -0
- package/src/libs/themes/decoders.ts +28 -0
- package/src/libs/themes/types.ts +28 -0
- package/src/libs/windows/controller.ts +498 -0
- package/src/libs/windows/decoders.ts +64 -0
- package/src/libs/windows/types.ts +59 -0
- package/src/libs/workspaces/controller.ts +922 -0
- package/src/libs/workspaces/decoders.ts +868 -0
- package/src/libs/workspaces/defaultConfig.ts +11 -0
- package/src/libs/workspaces/frames.ts +239 -0
- package/src/libs/workspaces/hibernationWatcher.ts +231 -0
- package/src/libs/workspaces/types.ts +506 -0
- package/src/platform.ts +156 -0
- package/src/shared/decoders.ts +479 -0
- package/src/shared/fetchTimeout.ts +25 -0
- package/src/shared/ioc.ts +580 -0
- package/src/shared/logger.ts +19 -0
- package/src/shared/promisePlus.ts +62 -0
- package/src/shared/sequelizer.ts +48 -0
- package/src/shared/utils.ts +86 -0
- package/src/storage/local.ts +111 -0
- package/src/storage/session.ts +436 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { IOConnectBrowser } from "@interopio/browser";
|
|
2
|
+
import { IOConnectBrowserPlatform } from "../../../../browser.platform";
|
|
3
|
+
import { allApplicationDefinitionsDecoder } from "../../../shared/decoders";
|
|
4
|
+
import { fetchTimeout } from "../../../shared/fetchTimeout";
|
|
5
|
+
import { defaultRemoteWatcherHeaders, defaultRemoteWatcherRequestTimeoutMS } from "../defaults";
|
|
6
|
+
import logger from "../../../shared/logger";
|
|
7
|
+
import { IOConnectCore } from "@interopio/core";
|
|
8
|
+
|
|
9
|
+
export class RemoteWatcher {
|
|
10
|
+
private url!: string;
|
|
11
|
+
private request!: Request;
|
|
12
|
+
private handleApps!: (apps: Array<IOConnectBrowser.AppManager.Definition | IOConnectBrowserPlatform.Applications.FDC3Definition>) => Promise<void>;
|
|
13
|
+
private requestTimeout!: number;
|
|
14
|
+
private pollingInterval: number | undefined;
|
|
15
|
+
private running?: boolean;
|
|
16
|
+
|
|
17
|
+
public start(config: IOConnectBrowserPlatform.RemoteStore, handleApps: (apps: Array<IOConnectBrowser.AppManager.Definition | IOConnectBrowserPlatform.Applications.FDC3Definition>) => Promise<void>): void {
|
|
18
|
+
this.url = config.url;
|
|
19
|
+
this.handleApps = handleApps;
|
|
20
|
+
this.requestTimeout = config.requestTimeout || defaultRemoteWatcherRequestTimeoutMS;
|
|
21
|
+
this.pollingInterval = config.pollingInterval;
|
|
22
|
+
|
|
23
|
+
this.setRequest(config.customHeaders);
|
|
24
|
+
|
|
25
|
+
this.logger?.trace(`Remote watcher configured with timeout: ${this.requestTimeout} and interval: ${this.pollingInterval}`);
|
|
26
|
+
|
|
27
|
+
this.running = true;
|
|
28
|
+
|
|
29
|
+
this.poll();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public stop(): void {
|
|
33
|
+
this.running = false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private async poll(): Promise<void> {
|
|
37
|
+
|
|
38
|
+
if (!this.running) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const response = await fetchTimeout(this.request, this.requestTimeout);
|
|
44
|
+
|
|
45
|
+
if (!this.running) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const responseJson: { applications: Array<IOConnectBrowser.AppManager.Definition | IOConnectBrowserPlatform.Applications.FDC3Definition> } = await response.json();
|
|
50
|
+
|
|
51
|
+
if (!responseJson || !Array.isArray(responseJson.applications)) {
|
|
52
|
+
throw new Error("The remote response was either empty or did not contain an applications collection");
|
|
53
|
+
}
|
|
54
|
+
this.logger?.trace("There is a valid response from the app store, processing definitions...");
|
|
55
|
+
const validatedApps = responseJson.applications.reduce<Array<IOConnectBrowser.AppManager.Definition | IOConnectBrowserPlatform.Applications.FDC3Definition>>((soFar, app) => {
|
|
56
|
+
|
|
57
|
+
const result = allApplicationDefinitionsDecoder.run(app);
|
|
58
|
+
|
|
59
|
+
if (result.ok) {
|
|
60
|
+
soFar.push(app);
|
|
61
|
+
} else {
|
|
62
|
+
this.logger?.warn(`Removing applications definition with name: ${app.name} from the remote response, because of validation error: ${JSON.stringify(result.error)}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return soFar;
|
|
66
|
+
}, []);
|
|
67
|
+
|
|
68
|
+
await this.handleApps(validatedApps);
|
|
69
|
+
|
|
70
|
+
} catch (error: any) {
|
|
71
|
+
|
|
72
|
+
const stringError = typeof error === "string" ? error : JSON.stringify(error.message);
|
|
73
|
+
this.logger?.warn(stringError);
|
|
74
|
+
|
|
75
|
+
} finally {
|
|
76
|
+
if (this.pollingInterval) {
|
|
77
|
+
|
|
78
|
+
await this.waitInterval();
|
|
79
|
+
this.poll();
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private setRequest(customHeaders: { [key: string]: string } = {}): void {
|
|
86
|
+
const requestHeaders = new Headers();
|
|
87
|
+
|
|
88
|
+
for (const key in defaultRemoteWatcherHeaders) {
|
|
89
|
+
requestHeaders.append(key, defaultRemoteWatcherHeaders[key]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
for (const key in customHeaders) {
|
|
93
|
+
this.logger?.trace("Custom headers detected and set");
|
|
94
|
+
requestHeaders.append(key, customHeaders[key]);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
this.request = new Request(this.url, {
|
|
98
|
+
method: "GET",
|
|
99
|
+
headers: requestHeaders,
|
|
100
|
+
mode: "cors",
|
|
101
|
+
cache: "default"
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private waitInterval(): Promise<void> {
|
|
106
|
+
return new Promise((resolve) => setTimeout(resolve, this.pollingInterval));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private get logger(): IOConnectCore.Logger.API | undefined {
|
|
110
|
+
return logger.get("applications.remote.directory");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { IOConnectBrowser } from "@interopio/browser";
|
|
3
|
+
import { IOConnectCore } from "@interopio/core";
|
|
4
|
+
import { generate } from "shortid";
|
|
5
|
+
import { ApplicationStartConfig, BridgeOperation, InternalApplicationsConfig, InternalPlatformConfig, LibController, OperationCheckConfig, OperationCheckResult, SessionWindowData } from "../../common/types";
|
|
6
|
+
import { GlueController } from "../../controllers/glue";
|
|
7
|
+
import { SessionStorageController } from "../../storage/session";
|
|
8
|
+
import { WindowsStateController } from "../../controllers/state";
|
|
9
|
+
import { IoC } from "../../shared/ioc";
|
|
10
|
+
import { PromiseWrap } from "../../shared/promisePlus";
|
|
11
|
+
import { getRelativeBounds } from "../../shared/utils";
|
|
12
|
+
import { appHelloDecoder, appHelloSuccessDecoder, applicationStartConfigDecoder, appManagerOperationTypesDecoder, appRemoveConfigDecoder, appsExportOperationDecoder, appsImportOperationDecoder, appsRemoteRegistrationDecoder, basicInstanceDataDecoder, instanceDataDecoder } from "./decoders";
|
|
13
|
+
import { AppsImportOperation, AppHello, AppHelloSuccess, ApplicationData, AppManagerOperationTypes, BasicInstanceData, InstanceData, InstanceLock, InstanceProcessInfo, AppsExportOperation, AppRemoveConfig, AppsRemoteRegistration, AppDirectoryStateChange } from "./types";
|
|
14
|
+
import logger from "../../shared/logger";
|
|
15
|
+
import { workspaceWindowDataDecoder } from "../workspaces/decoders";
|
|
16
|
+
import { simpleWindowDecoder } from "../windows/decoders";
|
|
17
|
+
import { WorkspaceWindowData } from "../workspaces/types";
|
|
18
|
+
import { SimpleWindowCommand, WindowTitleConfig } from "../windows/types";
|
|
19
|
+
import { AppDirectory } from "./appStore/directory";
|
|
20
|
+
import { defaultNoAppWindowComponentAppName } from "../../common/constants";
|
|
21
|
+
import { operationCheckConfigDecoder, operationCheckResultDecoder } from "../../shared/decoders";
|
|
22
|
+
|
|
23
|
+
export class ApplicationsController implements LibController {
|
|
24
|
+
private config!: InternalApplicationsConfig;
|
|
25
|
+
private applicationStartTimeoutMs = 15000;
|
|
26
|
+
private started = false;
|
|
27
|
+
private defaultBounds!: IOConnectBrowser.Windows.Bounds;
|
|
28
|
+
|
|
29
|
+
private locks: { [key: string]: InstanceLock } = {};
|
|
30
|
+
|
|
31
|
+
private operations: { [key in AppManagerOperationTypes]: BridgeOperation } = {
|
|
32
|
+
appHello: { name: "appHello", dataDecoder: appHelloDecoder, resultDecoder: appHelloSuccessDecoder, execute: this.handleAppHello.bind(this) },
|
|
33
|
+
applicationStart: { name: "applicationStart", dataDecoder: applicationStartConfigDecoder, resultDecoder: instanceDataDecoder, execute: this.handleApplicationStart.bind(this) },
|
|
34
|
+
instanceStop: { name: "instanceStop", dataDecoder: basicInstanceDataDecoder, execute: this.handleInstanceStop.bind(this) },
|
|
35
|
+
registerWorkspaceApp: { name: "registerWorkspaceApp", dataDecoder: workspaceWindowDataDecoder, execute: this.registerWorkspaceApp.bind(this) },
|
|
36
|
+
unregisterWorkspaceApp: { name: "unregisterWorkspaceApp", dataDecoder: simpleWindowDecoder, execute: this.unregisterWorkspaceApp.bind(this) },
|
|
37
|
+
import: { name: "import", dataDecoder: appsImportOperationDecoder, execute: this.handleImport.bind(this) },
|
|
38
|
+
remove: { name: "remove", dataDecoder: appRemoveConfigDecoder, execute: this.handleRemove.bind(this) },
|
|
39
|
+
export: { name: "export", resultDecoder: appsExportOperationDecoder, execute: this.handleExport.bind(this) },
|
|
40
|
+
clear: { name: "clear", execute: this.handleClear.bind(this) },
|
|
41
|
+
registerRemoteApps: { name: "registerRemoteApps", dataDecoder: appsRemoteRegistrationDecoder, execute: this.handleRegisterRemoteApps.bind(this) },
|
|
42
|
+
operationCheck: { name: "operationCheck", dataDecoder: operationCheckConfigDecoder, resultDecoder: operationCheckResultDecoder, execute: this.handleOperationCheck.bind(this) }
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
constructor(
|
|
46
|
+
private readonly glueController: GlueController,
|
|
47
|
+
private readonly sessionStorage: SessionStorageController,
|
|
48
|
+
private readonly stateController: WindowsStateController,
|
|
49
|
+
private readonly appDirectory: AppDirectory,
|
|
50
|
+
private readonly ioc: IoC
|
|
51
|
+
) {}
|
|
52
|
+
|
|
53
|
+
private get logger(): IOConnectCore.Logger.API | undefined {
|
|
54
|
+
return logger.get("applications.controller");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public handlePlatformShutdown(): void {
|
|
58
|
+
this.locks = {};
|
|
59
|
+
this.started = false;
|
|
60
|
+
this.appDirectory.stop();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public async start(config: InternalPlatformConfig): Promise<void> {
|
|
64
|
+
this.defaultBounds = config.windows.defaultWindowOpenBounds;
|
|
65
|
+
|
|
66
|
+
this.logger?.trace("initializing applications");
|
|
67
|
+
|
|
68
|
+
this.config = config.applications;
|
|
69
|
+
|
|
70
|
+
await this.appDirectory.start({
|
|
71
|
+
config: config.applications,
|
|
72
|
+
appsStateChange: (data: AppDirectoryStateChange) => this.emitStreamData("appDirectoryStateChange", data),
|
|
73
|
+
sequelizer: this.ioc.createSequelizer()
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
this.started = true;
|
|
77
|
+
this.stateController.onWindowDisappeared(this.processInstanceClosed.bind(this));
|
|
78
|
+
|
|
79
|
+
this.logger?.trace("initialization is completed");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public async handleControl(args: any): Promise<any> {
|
|
83
|
+
if (!this.started) {
|
|
84
|
+
new Error("Cannot handle this windows control message, because the controller has not been started");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const applicationData = args.data;
|
|
88
|
+
|
|
89
|
+
const commandId = args.commandId;
|
|
90
|
+
|
|
91
|
+
const operationValidation = appManagerOperationTypesDecoder.run(args.operation);
|
|
92
|
+
|
|
93
|
+
if (!operationValidation.ok) {
|
|
94
|
+
throw new Error(`This appManager request cannot be completed, because the operation name did not pass validation: ${JSON.stringify(operationValidation.error)}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const operationName: AppManagerOperationTypes = operationValidation.result;
|
|
98
|
+
|
|
99
|
+
const incomingValidation = this.operations[operationName].dataDecoder?.run(applicationData);
|
|
100
|
+
|
|
101
|
+
if (incomingValidation && !incomingValidation.ok) {
|
|
102
|
+
throw new Error(`AppManager request for ${operationName} rejected, because the provided arguments did not pass the validation: ${JSON.stringify(incomingValidation.error)}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
this.logger?.debug(`[${commandId}] ${operationName} command is valid with data: ${JSON.stringify(applicationData)}`);
|
|
106
|
+
|
|
107
|
+
const result = await this.operations[operationName].execute(applicationData, commandId);
|
|
108
|
+
|
|
109
|
+
const resultValidation = this.operations[operationName].resultDecoder?.run(result);
|
|
110
|
+
|
|
111
|
+
if (resultValidation && !resultValidation.ok) {
|
|
112
|
+
throw new Error(`AppManager request for ${operationName} could not be completed, because the operation result did not pass the validation: ${JSON.stringify(resultValidation.error)}`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
this.logger?.trace(`[${commandId}] ${operationName} command was executed successfully`);
|
|
116
|
+
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public handleClientUnloaded(windowId: string, win: Window): void {
|
|
121
|
+
this.logger?.trace(`handling unloading of ${windowId}`);
|
|
122
|
+
|
|
123
|
+
if (!windowId) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!win || win.closed) {
|
|
128
|
+
this.logger?.trace(`${windowId} detected as closed, processing instance closed`);
|
|
129
|
+
return this.processInstanceClosed(windowId);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
this.logger?.trace(`${windowId} detected as not closed, skipping instance closed procedure`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public async unregisterWorkspaceApp(config: SimpleWindowCommand): Promise<void> {
|
|
136
|
+
this.processInstanceClosed(config.windowId);
|
|
137
|
+
this.ioc.windowsController.cleanUpWindow(config.windowId);
|
|
138
|
+
this.ioc.portsBridge.removeGwClient(config.windowId);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public async handleApplicationStart(config: ApplicationStartConfig, commandId: string): Promise<InstanceData> {
|
|
142
|
+
|
|
143
|
+
this.logger?.trace(`[${commandId}] handling application start command for application: ${config.name}`);
|
|
144
|
+
|
|
145
|
+
const appDefinition = (await this.appDirectory.getAll()).find((app) => app.name === config.name);
|
|
146
|
+
|
|
147
|
+
if (!appDefinition) {
|
|
148
|
+
throw new Error(`Cannot start an instance of application: ${config.name}, because it is not found.`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const id = config.id ?? `g42-${generate()}`;
|
|
152
|
+
|
|
153
|
+
const instance: InstanceData = {
|
|
154
|
+
id,
|
|
155
|
+
applicationName: config.name
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const openBounds = await this.getStartingBounds(appDefinition.createOptions, config, commandId);
|
|
159
|
+
|
|
160
|
+
const options = config.forceChromeTab ?
|
|
161
|
+
undefined :
|
|
162
|
+
`left=${openBounds.left},top=${openBounds.top},width=${openBounds.width},height=${openBounds.height}`;
|
|
163
|
+
|
|
164
|
+
this.logger?.trace(`[${commandId}] open arguments are valid, opening to bounds: ${options}`);
|
|
165
|
+
|
|
166
|
+
const childWindow = window.open(appDefinition.createOptions.url, instance.id, options);
|
|
167
|
+
|
|
168
|
+
if (!childWindow) {
|
|
169
|
+
throw new Error(`Cannot an instance with url: ${appDefinition.createOptions.url} for application: ${config.name}. The most likely reason is that the user has not approved popups or has a blocker.`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.sessionStorage.saveBridgeInstanceData({ windowId: instance.id, appName: instance.applicationName });
|
|
173
|
+
|
|
174
|
+
const processConfig: InstanceProcessInfo = {
|
|
175
|
+
data: instance,
|
|
176
|
+
context: config.context
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
await this.processNewInstance(processConfig);
|
|
180
|
+
|
|
181
|
+
this.logger?.trace(`[${commandId}] the new window has been opened successfully with id: ${instance.id}, checking for AGM ready and notifying windows`);
|
|
182
|
+
|
|
183
|
+
if (config.waitForAGMReady) {
|
|
184
|
+
this.logger?.trace(`[${commandId}] wait for AGM is set, configuring the lock`);
|
|
185
|
+
this.setLock(instance.id);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
await this.notifyWindows(appDefinition.createOptions.url, instance, openBounds, config.context, childWindow, config.layoutComponentId);
|
|
189
|
+
|
|
190
|
+
if (this.locks[instance.id]) {
|
|
191
|
+
try {
|
|
192
|
+
await PromiseWrap(() => this.locks[instance.id]?.keyOne, this.applicationStartTimeoutMs);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
delete this.locks[instance.id];
|
|
195
|
+
throw new Error(`Application start for ${config.name} timed out waiting for client to initialize Glue`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
this.logger?.trace(`[${commandId}] the windows controller has been successfully notified`);
|
|
200
|
+
|
|
201
|
+
this.logger?.trace(`[${commandId}] the new instance with id ${instance.id} has been saved, announced and context set, lifting key two and responding to caller`);
|
|
202
|
+
|
|
203
|
+
this.locks[instance.id]?.openKeyTwo();
|
|
204
|
+
|
|
205
|
+
return instance;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private processInstanceClosed(selfWindowId?: string): void {
|
|
209
|
+
if (!selfWindowId) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const instanceData = this.sessionStorage.getInstanceData(selfWindowId);
|
|
214
|
+
|
|
215
|
+
if (instanceData) {
|
|
216
|
+
delete this.locks[instanceData.id];
|
|
217
|
+
this.glueController.clearContext(selfWindowId, "instance").catch(() => {});
|
|
218
|
+
this.sessionStorage.removeInstance(instanceData.id);
|
|
219
|
+
this.emitStreamData("instanceStopped", instanceData);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private async notifyWindows(url: string, instance: InstanceData, initialBounds: IOConnectBrowser.Windows.Bounds, context?: any, child?: Window, layoutComponentId?: string): Promise<void> {
|
|
225
|
+
const windowData: SessionWindowData = {
|
|
226
|
+
windowId: instance.id,
|
|
227
|
+
name: `${instance.applicationName}_${instance.id}`,
|
|
228
|
+
initialUrl: url,
|
|
229
|
+
initialContext: context,
|
|
230
|
+
initialBounds,
|
|
231
|
+
layoutComponentId
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
await this.ioc.windowsController.processNewWindow(windowData, context, child);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private async handleOperationCheck(config: OperationCheckConfig): Promise<OperationCheckResult> {
|
|
238
|
+
const operations = Object.keys(this.operations);
|
|
239
|
+
|
|
240
|
+
const isSupported = operations.some((operation) => operation.toLowerCase() === config.operation.toLowerCase());
|
|
241
|
+
|
|
242
|
+
return { isSupported };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private async handleAppHello(helloMsg: AppHello, commandId: string): Promise<AppHelloSuccess> {
|
|
246
|
+
this.logger?.trace(`[${commandId}] handling hello message for id: ${helloMsg.windowId}`);
|
|
247
|
+
|
|
248
|
+
if (helloMsg.windowId && this.locks[helloMsg.windowId]) {
|
|
249
|
+
this.logger?.trace(`[${commandId}] found an app lock, unlocking key one and waiting for the second one`);
|
|
250
|
+
|
|
251
|
+
this.locks[helloMsg.windowId].openKeyOne();
|
|
252
|
+
|
|
253
|
+
await this.locks[helloMsg.windowId].keyTwo;
|
|
254
|
+
|
|
255
|
+
delete this.locks[helloMsg.windowId];
|
|
256
|
+
|
|
257
|
+
this.logger?.trace(`[${commandId}] the lock is lifted, proceeding`);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const allInstances = this.sessionStorage.getAllInstancesData();
|
|
261
|
+
|
|
262
|
+
const allAppsFull = (await this.appDirectory.getAll()).map<ApplicationData>((app) => {
|
|
263
|
+
|
|
264
|
+
const appInstances = allInstances.filter((inst) => inst.applicationName === app.name);
|
|
265
|
+
|
|
266
|
+
return Object.assign({}, app, { instances: appInstances });
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
if (helloMsg.windowId) {
|
|
270
|
+
this.logger?.trace(`[${commandId}] there is a valid windowId, removing ${helloMsg.windowId} from the state controller`);
|
|
271
|
+
this.stateController.remove(helloMsg.windowId);
|
|
272
|
+
|
|
273
|
+
const foundApp = allAppsFull.find((app) => app.instances.some((inst) => inst.id === helloMsg.windowId));
|
|
274
|
+
|
|
275
|
+
if (foundApp && foundApp.title) {
|
|
276
|
+
|
|
277
|
+
const windowId: string = helloMsg.windowId;
|
|
278
|
+
const title: string = foundApp.title;
|
|
279
|
+
|
|
280
|
+
PromiseWrap<void>(() => this.glueController.callWindow<WindowTitleConfig, void>("windows", this.ioc.windowsController.setTitleOperation, { windowId, title }, { windowId }), 20000)
|
|
281
|
+
.catch((err) => this.logger?.trace(`[${commandId}] error while setting the application instance title: ${err.message}`));
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const helloSuccessMessage = { apps: allAppsFull };
|
|
286
|
+
|
|
287
|
+
this.logger?.trace(`[${commandId}] compiled a list of all active applications and instances and returning it to the caller`);
|
|
288
|
+
|
|
289
|
+
return helloSuccessMessage;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
public async handleInstanceStop(inst: BasicInstanceData, commandId: string): Promise<void> {
|
|
293
|
+
this.logger?.trace(`[${commandId}] handling stop command for instance: ${inst.id}`);
|
|
294
|
+
|
|
295
|
+
const workspaceClient = this.sessionStorage.getWorkspaceClientById(inst.id);
|
|
296
|
+
|
|
297
|
+
if (workspaceClient) {
|
|
298
|
+
this.logger?.trace(`[${commandId}] this instance is detected as a workspace window, closing via the workspaces controller`);
|
|
299
|
+
|
|
300
|
+
await this.ioc.workspacesController.closeItem({ itemId: inst.id }, commandId);
|
|
301
|
+
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const instanceData = this.sessionStorage.getInstanceData(inst.id);
|
|
306
|
+
|
|
307
|
+
if (!instanceData) {
|
|
308
|
+
throw new Error(`Cannot close instance: ${inst.id}, because it is not known by the platform`);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const windowData = this.sessionStorage.getWindowDataById(inst.id);
|
|
312
|
+
|
|
313
|
+
if (!windowData) {
|
|
314
|
+
throw new Error(`Cannot close instance: ${inst.id}, because it's window is not known by the platform`);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
window.open(undefined, windowData.windowId)?.close();
|
|
318
|
+
|
|
319
|
+
this.processInstanceClosed(inst.id);
|
|
320
|
+
|
|
321
|
+
this.ioc.windowsController.cleanUpWindow(inst.id);
|
|
322
|
+
|
|
323
|
+
this.logger?.trace(`[${commandId}] instance ${inst.id} has been closed, removed from store, announced stopped and notified windows, responding to caller`);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
public async handleRegisterRemoteApps(config: AppsRemoteRegistration, commandId: string): Promise<void> {
|
|
327
|
+
this.logger?.trace(`[${commandId}] handling remote bypass command`);
|
|
328
|
+
|
|
329
|
+
if (this.config.remote) {
|
|
330
|
+
throw new Error(`[${commandId}] cannot accept remote apps from the protocol, because there is an active remote configuration.`);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
await this.appDirectory.processAppDefinitions(config.definitions, { mode: "replace", type: "remote" });
|
|
334
|
+
|
|
335
|
+
this.logger?.trace(`[${commandId}] remote bypass command completed`);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
public async handleImport(config: AppsImportOperation, commandId: string): Promise<void> {
|
|
340
|
+
this.logger?.trace(`[${commandId}] handling import command`);
|
|
341
|
+
|
|
342
|
+
await this.appDirectory.processAppDefinitions(config.definitions, { type: "inmemory", mode: config.mode });
|
|
343
|
+
|
|
344
|
+
this.logger?.trace(`[${commandId}] import command completed`);
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
public async handleRemove(config: AppRemoveConfig, commandId: string): Promise<void> {
|
|
349
|
+
this.logger?.trace(`[${commandId}] handling remove command for ${config.name}`);
|
|
350
|
+
|
|
351
|
+
const removed = await this.appDirectory.removeInMemory(config.name);
|
|
352
|
+
|
|
353
|
+
if (removed) {
|
|
354
|
+
this.logger?.trace(`definition ${removed.name} removed successfully`);
|
|
355
|
+
this.emitStreamData("appDirectoryStateChange", { appsRemoved: [removed], appsAdded: [], appsChanged: [] });
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
public async handleExport(_: any, commandId: string): Promise<AppsExportOperation> {
|
|
360
|
+
this.logger?.trace(`[${commandId}] handling export command`);
|
|
361
|
+
|
|
362
|
+
const definitions = await this.appDirectory.exportInMemory();
|
|
363
|
+
|
|
364
|
+
this.logger?.trace(`[${commandId}] export command successful`);
|
|
365
|
+
|
|
366
|
+
return { definitions };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
public async handleClear(_: any, commandId: string): Promise<void> {
|
|
370
|
+
this.logger?.trace(`[${commandId}] handling clear command`);
|
|
371
|
+
|
|
372
|
+
await this.appDirectory.processAppDefinitions([], { type: "inmemory", mode: "replace" });
|
|
373
|
+
|
|
374
|
+
this.logger?.trace(`[${commandId}] all in-memory apps are cleared`);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
private setLock(id: string): void {
|
|
378
|
+
|
|
379
|
+
const lock: any = {};
|
|
380
|
+
|
|
381
|
+
const keyOne = new Promise<void>((resolve) => {
|
|
382
|
+
lock.openKeyOne = resolve;
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
const keyTwo = new Promise<void>((resolve) => {
|
|
386
|
+
lock.openKeyTwo = resolve;
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
lock.keyOne = keyOne;
|
|
390
|
+
lock.keyTwo = keyTwo;
|
|
391
|
+
|
|
392
|
+
this.locks[id] = lock;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
private async registerWorkspaceApp(data: WorkspaceWindowData, commandId: string): Promise<void> {
|
|
396
|
+
if (!data.appName) {
|
|
397
|
+
throw new Error(`Cannot register application with config: ${JSON.stringify(data)}, because no app name was found`);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const allAppDefinitions = await this.appDirectory.getAll();
|
|
401
|
+
|
|
402
|
+
if (data.appName === defaultNoAppWindowComponentAppName) {
|
|
403
|
+
return await this.ioc.windowsController.registerWorkspaceWindow(data, commandId);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (!allAppDefinitions.some((app) => app.name === data.appName)) {
|
|
407
|
+
throw new Error(`Cannot register application with config: ${JSON.stringify(data)}, because no app with this name name was found`);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
this.sessionStorage.saveBridgeInstanceData({ windowId: data.windowId, appName: data.appName });
|
|
411
|
+
|
|
412
|
+
this.logger?.trace(`[${commandId}] processing valid workspace application registration with id ${data.windowId}, app name ${data.appName} and frame ${data.frameId}`);
|
|
413
|
+
|
|
414
|
+
if (data.context) {
|
|
415
|
+
await this.glueController.setStartContext(data.windowId, data.context, "instance");
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const instanceData: InstanceData = { id: data.windowId, applicationName: data.appName };
|
|
419
|
+
|
|
420
|
+
this.sessionStorage.saveInstanceData(instanceData);
|
|
421
|
+
|
|
422
|
+
this.emitStreamData("instanceStarted", instanceData);
|
|
423
|
+
|
|
424
|
+
this.logger?.trace(`[${commandId}] instance registration is completed and announced, calling windows registration`);
|
|
425
|
+
|
|
426
|
+
await this.ioc.windowsController.registerWorkspaceWindow(data, commandId);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
private async processNewInstance(config: InstanceProcessInfo): Promise<void> {
|
|
430
|
+
if (config.context) {
|
|
431
|
+
await this.glueController.setStartContext(config.data.id, config.context, "instance");
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
this.sessionStorage.saveInstanceData(config.data);
|
|
435
|
+
|
|
436
|
+
this.emitStreamData("instanceStarted", config.data);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
private emitStreamData(operation: "appDirectoryStateChange" | "instanceStarted" | "instanceStopped", data: any): void {
|
|
440
|
+
this.logger?.trace(`sending notification of event: ${operation} with data: ${JSON.stringify(data)}`);
|
|
441
|
+
this.glueController.pushSystemMessage("appManager", operation, data);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
private async getStartingBounds(appDefOptions: IOConnectBrowser.AppManager.DefinitionDetails, openOptions: ApplicationStartConfig, commandId: string): Promise<IOConnectBrowser.Windows.Bounds> {
|
|
445
|
+
|
|
446
|
+
const openBounds: IOConnectBrowser.Windows.Bounds = {
|
|
447
|
+
top: openOptions.top || appDefOptions.top || this.defaultBounds.top,
|
|
448
|
+
left: openOptions.left || appDefOptions.left || this.defaultBounds.left,
|
|
449
|
+
width: openOptions.width || appDefOptions.width || this.defaultBounds.width,
|
|
450
|
+
height: openOptions.height || appDefOptions.height || this.defaultBounds.height
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
if (!openOptions.relativeTo) {
|
|
454
|
+
return openBounds;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
try {
|
|
458
|
+
const relativeWindowBounds = await this.ioc.windowsController.getWindowBounds(openOptions.relativeTo, commandId);
|
|
459
|
+
|
|
460
|
+
const relativeDir = openOptions.relativeDirection ?? "right";
|
|
461
|
+
|
|
462
|
+
const newBounds = getRelativeBounds(openBounds, relativeWindowBounds, relativeDir);
|
|
463
|
+
|
|
464
|
+
return newBounds;
|
|
465
|
+
} catch (error) {
|
|
466
|
+
return openBounds;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Decoder, object, array, optional, anyJson, number, oneOf, constant, boolean } from "decoder-validate";
|
|
2
|
+
import { allApplicationDefinitionsDecoder, applicationDetailsDecoder, glueCoreAppDefinitionDecoder, nonEmptyStringDecoder, nonNegativeNumberDecoder } from "../../shared/decoders";
|
|
3
|
+
import { ApplicationData, BaseApplicationData, AppHelloSuccess, InstanceData, AppHello, BasicInstanceData, AppManagerOperationTypes, AppsImportOperation as AppsImportOperation, AppRemoveConfig, AppsExportOperation, AppsRemoteRegistration } from "./types";
|
|
4
|
+
import { ApplicationStartConfig } from "../../common/types";
|
|
5
|
+
|
|
6
|
+
export const appManagerOperationTypesDecoder: Decoder<AppManagerOperationTypes> = oneOf<"appHello" | "applicationStart" | "instanceStop" | "registerWorkspaceApp" | "unregisterWorkspaceApp" | "export" | "import" | "remove" | "clear" | "registerRemoteApps" | "operationCheck">(
|
|
7
|
+
constant("appHello"),
|
|
8
|
+
constant("applicationStart"),
|
|
9
|
+
constant("instanceStop"),
|
|
10
|
+
constant("registerWorkspaceApp"),
|
|
11
|
+
constant("unregisterWorkspaceApp"),
|
|
12
|
+
constant("export"),
|
|
13
|
+
constant("import"),
|
|
14
|
+
constant("remove"),
|
|
15
|
+
constant("clear"),
|
|
16
|
+
constant("registerRemoteApps"),
|
|
17
|
+
constant("operationCheck")
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export const basicInstanceDataDecoder: Decoder<BasicInstanceData> = object({
|
|
21
|
+
id: nonEmptyStringDecoder
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export const instanceDataDecoder: Decoder<InstanceData> = object({
|
|
25
|
+
id: nonEmptyStringDecoder,
|
|
26
|
+
applicationName: nonEmptyStringDecoder
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export const applicationDataDecoder: Decoder<ApplicationData> = object({
|
|
30
|
+
name: nonEmptyStringDecoder,
|
|
31
|
+
type: nonEmptyStringDecoder.where((s) => s === "window", "Expected a value of window"),
|
|
32
|
+
createOptions: applicationDetailsDecoder,
|
|
33
|
+
instances: array(instanceDataDecoder),
|
|
34
|
+
userProperties: optional(anyJson()),
|
|
35
|
+
title: optional(nonEmptyStringDecoder),
|
|
36
|
+
version: optional(nonEmptyStringDecoder),
|
|
37
|
+
icon: optional(nonEmptyStringDecoder),
|
|
38
|
+
caption: optional(nonEmptyStringDecoder)
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export const baseApplicationDataDecoder: Decoder<BaseApplicationData> = object({
|
|
42
|
+
name: nonEmptyStringDecoder,
|
|
43
|
+
type: nonEmptyStringDecoder.where((s) => s === "window", "Expected a value of window"),
|
|
44
|
+
createOptions: applicationDetailsDecoder,
|
|
45
|
+
userProperties: optional(anyJson()),
|
|
46
|
+
title: optional(nonEmptyStringDecoder),
|
|
47
|
+
version: optional(nonEmptyStringDecoder),
|
|
48
|
+
icon: optional(nonEmptyStringDecoder),
|
|
49
|
+
caption: optional(nonEmptyStringDecoder)
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export const appHelloSuccessDecoder: Decoder<AppHelloSuccess> = object({
|
|
53
|
+
apps: array(applicationDataDecoder)
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
export const appHelloDecoder: Decoder<AppHello> = object({
|
|
57
|
+
windowId: optional(nonEmptyStringDecoder)
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export const applicationStartConfigDecoder: Decoder<ApplicationStartConfig> = object({
|
|
61
|
+
name: nonEmptyStringDecoder,
|
|
62
|
+
id: optional(nonEmptyStringDecoder),
|
|
63
|
+
context: optional(anyJson()),
|
|
64
|
+
top: optional(number()),
|
|
65
|
+
left: optional(number()),
|
|
66
|
+
width: optional(nonNegativeNumberDecoder),
|
|
67
|
+
height: optional(nonNegativeNumberDecoder),
|
|
68
|
+
relativeTo: optional(nonEmptyStringDecoder),
|
|
69
|
+
relativeDirection: optional(oneOf<"top" | "left" | "right" | "bottom">(
|
|
70
|
+
constant("top"),
|
|
71
|
+
constant("left"),
|
|
72
|
+
constant("right"),
|
|
73
|
+
constant("bottom")
|
|
74
|
+
)),
|
|
75
|
+
waitForAGMReady: optional(boolean()),
|
|
76
|
+
forceChromeTab: optional(boolean()),
|
|
77
|
+
layoutComponentId: optional(nonEmptyStringDecoder)
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
export const appsImportOperationDecoder: Decoder<AppsImportOperation> = object({
|
|
81
|
+
definitions: array(allApplicationDefinitionsDecoder),
|
|
82
|
+
mode: oneOf<"replace" | "merge">(
|
|
83
|
+
constant("replace"),
|
|
84
|
+
constant("merge")
|
|
85
|
+
)
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
export const appRemoveConfigDecoder: Decoder<AppRemoveConfig> = object({
|
|
89
|
+
name: nonEmptyStringDecoder
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
export const appsExportOperationDecoder: Decoder<AppsExportOperation> = object({
|
|
93
|
+
definitions: array(glueCoreAppDefinitionDecoder)
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
export const appsRemoteRegistrationDecoder: Decoder<AppsRemoteRegistration> = object({
|
|
97
|
+
definitions: array(allApplicationDefinitionsDecoder)
|
|
98
|
+
});
|