mirror of
https://github.com/coder/code-server.git
synced 2026-05-08 13:27:25 +02:00
Merge commit 'be3e8236086165e5e45a5a10783823874b3f3ebd' as 'lib/vscode'
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IURLService } from 'vs/platform/url/common/url';
|
||||
import { URI, UriComponents } from 'vs/base/common/uri';
|
||||
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
|
||||
import { AbstractURLService } from 'vs/platform/url/common/urlService';
|
||||
import { Event } from 'vs/base/common/event';
|
||||
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
|
||||
|
||||
export interface IURLCallbackProvider {
|
||||
|
||||
/**
|
||||
* Indicates that a Uri has been opened outside of VSCode. The Uri
|
||||
* will be forwarded to all installed Uri handlers in the system.
|
||||
*/
|
||||
readonly onCallback: Event<URI>;
|
||||
|
||||
/**
|
||||
* Creates a Uri that - if opened in a browser - must result in
|
||||
* the `onCallback` to fire.
|
||||
*
|
||||
* The optional `Partial<UriComponents>` must be properly restored for
|
||||
* the Uri passed to the `onCallback` handler.
|
||||
*
|
||||
* For example: if a Uri is to be created with `scheme:"vscode"`,
|
||||
* `authority:"foo"` and `path:"bar"` the `onCallback` should fire
|
||||
* with a Uri `vscode://foo/bar`.
|
||||
*
|
||||
* If there are additional `query` values in the Uri, they should
|
||||
* be added to the list of provided `query` arguments from the
|
||||
* `Partial<UriComponents>`.
|
||||
*/
|
||||
create(options?: Partial<UriComponents>): URI;
|
||||
}
|
||||
|
||||
export class BrowserURLService extends AbstractURLService {
|
||||
|
||||
declare readonly _serviceBrand: undefined;
|
||||
|
||||
private provider: IURLCallbackProvider | undefined;
|
||||
|
||||
constructor(
|
||||
@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService
|
||||
) {
|
||||
super();
|
||||
|
||||
this.provider = environmentService.options?.urlCallbackProvider;
|
||||
|
||||
this.registerListeners();
|
||||
}
|
||||
|
||||
private registerListeners(): void {
|
||||
if (this.provider) {
|
||||
this._register(this.provider.onCallback(uri => this.open(uri, { trusted: true })));
|
||||
}
|
||||
}
|
||||
|
||||
create(options?: Partial<UriComponents>): URI {
|
||||
if (this.provider) {
|
||||
return this.provider.create(options);
|
||||
}
|
||||
|
||||
return URI.parse('unsupported://');
|
||||
}
|
||||
}
|
||||
|
||||
registerSingleton(IURLService, BrowserURLService, true);
|
||||
@@ -0,0 +1,76 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IURLService, IURLHandler, IOpenURLOptions } from 'vs/platform/url/common/url';
|
||||
import { URI, UriComponents } from 'vs/base/common/uri';
|
||||
import { IMainProcessService } from 'vs/platform/ipc/electron-sandbox/mainProcessService';
|
||||
import { URLHandlerChannel } from 'vs/platform/url/common/urlIpc';
|
||||
import { IOpenerService, IOpener, matchesScheme } from 'vs/platform/opener/common/opener';
|
||||
import { IProductService } from 'vs/platform/product/common/productService';
|
||||
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
|
||||
import { createChannelSender } from 'vs/base/parts/ipc/common/ipc';
|
||||
import { INativeHostService } from 'vs/platform/native/electron-sandbox/native';
|
||||
import { NativeURLService } from 'vs/platform/url/common/urlService';
|
||||
|
||||
export interface IRelayOpenURLOptions extends IOpenURLOptions {
|
||||
openToSide?: boolean;
|
||||
openExternal?: boolean;
|
||||
}
|
||||
|
||||
export class RelayURLService extends NativeURLService implements IURLHandler, IOpener {
|
||||
|
||||
private urlService: IURLService;
|
||||
|
||||
constructor(
|
||||
@IMainProcessService mainProcessService: IMainProcessService,
|
||||
@IOpenerService openerService: IOpenerService,
|
||||
@INativeHostService private readonly nativeHostService: INativeHostService,
|
||||
@IProductService private readonly productService: IProductService
|
||||
) {
|
||||
super();
|
||||
|
||||
this.urlService = createChannelSender<IURLService>(mainProcessService.getChannel('url'));
|
||||
|
||||
mainProcessService.registerChannel('urlHandler', new URLHandlerChannel(this));
|
||||
openerService.registerOpener(this);
|
||||
}
|
||||
|
||||
create(options?: Partial<UriComponents>): URI {
|
||||
const uri = super.create(options);
|
||||
|
||||
let query = uri.query;
|
||||
if (!query) {
|
||||
query = `windowId=${encodeURIComponent(this.nativeHostService.windowId)}`;
|
||||
} else {
|
||||
query += `&windowId=${encodeURIComponent(this.nativeHostService.windowId)}`;
|
||||
}
|
||||
|
||||
return uri.with({ query });
|
||||
}
|
||||
|
||||
async open(resource: URI | string, options?: IRelayOpenURLOptions): Promise<boolean> {
|
||||
|
||||
if (!matchesScheme(resource, this.productService.urlProtocol)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof resource === 'string') {
|
||||
resource = URI.parse(resource);
|
||||
}
|
||||
return await this.urlService.open(resource, options);
|
||||
}
|
||||
|
||||
async handleURL(uri: URI, options?: IOpenURLOptions): Promise<boolean> {
|
||||
const result = await super.open(uri, options);
|
||||
|
||||
if (result) {
|
||||
await this.nativeHostService.focusWindow({ force: true /* Application may not be active */ });
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
registerSingleton(IURLService, RelayURLService);
|
||||
Reference in New Issue
Block a user