-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement stubbed window#registerURIhandler
fixes #13169 contributed on behalf of STMicroelectronics Signed-off-by: Remi Schnekenburger <rschnekenburger@eclipsesource.com>
- Loading branch information
1 parent
aeee83b
commit 7ea3fc1
Showing
7 changed files
with
237 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 STMicroelectronics. | ||
// | ||
// 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-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { injectable } from 'inversify'; | ||
import { MaybePromise, URI } from '../common'; | ||
import { OpenHandler, OpenerOptions } from './opener-service'; | ||
|
||
export interface UriHandler { | ||
canHandleURI(uri: URI): boolean; | ||
handleUri(uri: URI): Promise<boolean>; | ||
} | ||
|
||
@injectable() | ||
export class ExtensionOpenHandler implements OpenHandler { | ||
|
||
readonly id = 'extensionsURIHandlers'; | ||
|
||
private providers = new Map<string, UriHandler>(); | ||
|
||
canHandle(uri: URI, options?: OpenerOptions | undefined): MaybePromise<number> { | ||
if (!uri.scheme.startsWith('theia')) { | ||
return 0; | ||
} | ||
|
||
const authority = uri.authority; | ||
const handler = this.providers.get(authority); | ||
if (handler?.canHandleURI(uri)) { | ||
return 500; | ||
} | ||
return 0; | ||
} | ||
|
||
open(uri: URI, options?: OpenerOptions | undefined): MaybePromise<object | undefined> { | ||
const authority = uri.authority; | ||
const provider = this.providers.get(authority); | ||
if (provider) { | ||
return provider.handleUri(uri); | ||
} | ||
return Promise.reject(`Impossible to handle ${uri}.`); | ||
} | ||
|
||
public registerHandler(extensionId: string, handler: UriHandler): void { | ||
this.providers.set(extensionId, handler); | ||
} | ||
|
||
public unregisterHandler(extensionId: string): void { | ||
this.providers.delete(extensionId); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 STMicroelectronics. | ||
// | ||
// 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-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { Disposable, URI } from '@theia/core'; | ||
import { ExtensionOpenHandler, UriHandler} from '@theia/core/lib/browser/extension-open-handler'; | ||
import { MAIN_RPC_CONTEXT, UriExt, UriMain } from '../../common'; | ||
import { RPCProtocol } from '../../common/rpc-protocol'; | ||
import { interfaces } from '@theia/core/shared/inversify'; | ||
|
||
export class UriMainImpl implements UriMain, Disposable { | ||
|
||
private readonly proxy: UriExt; | ||
private readonly handlers = new Map<number, string>(); | ||
private readonly extensionOpenHandler: ExtensionOpenHandler; | ||
|
||
constructor(rpc: RPCProtocol, container: interfaces.Container) { | ||
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.URI_EXT); | ||
this.extensionOpenHandler = container.get(ExtensionOpenHandler); | ||
} | ||
|
||
dispose(): void { | ||
this.handlers.clear(); | ||
} | ||
|
||
async $registerUriHandler(handle: number, extensionId: string, extensionDisplayName: string): Promise<void> { | ||
const extensionUrlHandler = new ExtensionUriHandler(this.proxy, handle, extensionId, extensionDisplayName); | ||
this.extensionOpenHandler.registerHandler(extensionId, extensionUrlHandler); | ||
this.handlers.set(handle, extensionId); | ||
|
||
return Promise.resolve(undefined); | ||
} | ||
|
||
async $unregisterUriHandler(handle: number): Promise<void> { | ||
const extensionId = this.handlers.get(handle); | ||
if (extensionId) { | ||
this.handlers.delete(handle); | ||
this.extensionOpenHandler.unregisterHandler(extensionId); | ||
} | ||
} | ||
|
||
} | ||
|
||
class ExtensionUriHandler implements UriHandler { | ||
|
||
constructor( | ||
private proxy: UriExt, | ||
private readonly handle: number, | ||
readonly extensionId: string, | ||
readonly extensionDisplayName: string | ||
) { } | ||
|
||
canHandleURI(uri: URI): boolean { | ||
return uri.authority === this.extensionId; | ||
} | ||
|
||
handleUri(uri: URI): Promise<boolean> { | ||
if (this.extensionId !== uri.authority) { | ||
return Promise.resolve(false); | ||
} | ||
|
||
return Promise.resolve(this.proxy.$handleExternalUri(this.handle, uri.toComponents())).then(() => true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 STMicroelectronics. | ||
// | ||
// 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-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import * as theia from '@theia/plugin'; | ||
import { | ||
UriExt, | ||
PLUGIN_RPC_CONTEXT, PluginInfo, UriMain | ||
} from '../common/plugin-api-rpc'; | ||
import { RPCProtocol } from '../common/rpc-protocol'; | ||
import { Disposable, URI } from './types-impl'; | ||
import { UriComponents } from '../common/uri-components'; | ||
|
||
export class UriExtImpl implements UriExt { | ||
|
||
private static handle = 0; | ||
private handles = new Set<string>(); | ||
private handlers = new Map<number, theia.UriHandler>(); | ||
|
||
private readonly proxy: UriMain; | ||
|
||
constructor(readonly rpc: RPCProtocol) { | ||
this.proxy = rpc.getProxy(PLUGIN_RPC_CONTEXT.URI_MAIN); | ||
console.log(this.proxy); | ||
} | ||
|
||
registerUriHandler(handler: theia.UriHandler, plugin: PluginInfo): theia.Disposable { | ||
const extensionId = plugin.id; | ||
if (this.handles.has(extensionId)) { | ||
throw new Error(`URI handler already registered for extension ${extensionId}`); | ||
} | ||
|
||
const handle = UriExtImpl.handle++; | ||
this.handles.add(extensionId); | ||
this.handlers.set(handle, handler); | ||
this.proxy.$registerUriHandler(handle, extensionId, plugin.displayName || plugin.name); | ||
|
||
return new Disposable(() => { | ||
this.proxy.$unregisterUriHandler(handle); | ||
this.handles.delete(extensionId); | ||
this.handlers.delete(handle); | ||
}); | ||
} | ||
|
||
$handleExternalUri(handle: number, uri: UriComponents): Promise<void> { | ||
const handler = this.handlers.get(handle); | ||
|
||
if (!handler) { | ||
return Promise.resolve(undefined); | ||
} | ||
try { | ||
handler.handleUri(URI.revive(uri)); | ||
} catch (err) { | ||
console.log(`error while handling external uri: ${uri}`); | ||
} | ||
|
||
return Promise.resolve(undefined); | ||
} | ||
|
||
} |