提交 b7b21844 编写于 作者: B Benjamin Pasero

api - fold app uri creation into asExternalUri (fix #82884)

上级 d1866531
......@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { env, extensions, ExtensionKind, UIKind } from 'vscode';
import { env, extensions, ExtensionKind, UIKind, Uri } from 'vscode';
suite('env-namespace', () => {
......@@ -45,8 +45,29 @@ suite('env-namespace', () => {
}
});
test('env.uiKind', function () {
test('env.uiKind', async function () {
const uri = Uri.parse(`${env.uriScheme}:://vscode.vscode-api-tests/path?key=value&other=false`);
const result = await env.asExternalUri(uri);
const kind = env.uiKind;
assert.equal(kind, UIKind.Desktop);
if (result.scheme === 'http' || result.scheme === 'https') {
assert.equal(kind, UIKind.Web);
} else {
assert.equal(kind, UIKind.Desktop);
}
});
test('env.asExternalUri - with env.uriScheme', async function () {
const uri = Uri.parse(`${env.uriScheme}:://vscode.vscode-api-tests/path?key=value&other=false`);
const result = await env.asExternalUri(uri);
assert.ok(result);
if (env.uiKind === UIKind.Desktop) {
assert.equal(uri.scheme, result.scheme);
assert.equal(uri.authority, result.authority);
assert.equal(uri.path, result.path);
} else {
assert.ok(result.scheme === 'http' || result.scheme === 'https');
}
});
});
......@@ -23,7 +23,8 @@ if (isWeb) {
assign(product, {
version: '1.39.0-dev',
nameLong: 'Visual Studio Code Web Dev',
nameShort: 'VSCode Web Dev'
nameShort: 'VSCode Web Dev',
urlProtocol: 'code-oss'
});
}
}
......
......@@ -6433,22 +6433,51 @@ declare module 'vscode' {
export function openExternal(target: Uri): Thenable<boolean>;
/**
* Resolves a uri to form that is accessible externally. Currently only supports `https:`, `http:` and
* `vscode.env.uriScheme` uris.
*
* #### `http:` or `https:` scheme
*
* Resolves an *external* uri, such as a `http:` or `https:` link, from where the extension is running to a
* uri to the same resource on the client machine.
*
* This is a no-op if the extension is running on the client machine. Currently only supports
* `https:` and `http:` uris.
* This is a no-op if the extension is running on the client machine.
*
* If the extension is running remotely, this function automatically establishes a port forwarding tunnel
* from the local machine to `target` on the remote and returns a local uri to the tunnel. The lifetime of
* the port fowarding tunnel is managed by VS Code and the tunnel can be closed by the user.
*
* Extensions should not cache the result of `asExternalUri` as the resolved uri may become invalid due to
* a system or user action — for example, in remote cases, a user may close a port forwardng tunnel
* that was opened by `asExternalUri`.
* *Note* that uris passed through `openExternal` are automatically resolved and you should not call `asExternalUri` on them.
*
* #### `vscode.env.uriScheme`
*
* Creates a uri that - if opened in a browser (e.g. via `openExternal`) - will result in a registered [UriHandler](#UriHandler)
* to trigger.
*
* Extensions should not make any assumptions about the resulting uri and should not alter it in anyway.
* Rather, extensions can e.g. use this uri in an authentication flow, by adding the uri as callback query
* argument to the server to authenticate to.
*
* *Note* that if the server decides to add additional query parameters to the uri (e.g. a token or secret), it
* will appear in the uri that is passed to the [UriHandler](#UriHandler).
*
* **Example** of an authentication flow:
* ```typescript
* vscode.window.registerUriHandler({
* handleUri(uri: vscode.Uri): vscode.ProviderResult<void> {
* if (uri.path === '/did-authenticate') {
* console.log(uri.toString());
* }
* }
* });
*
* const callableUri = await vscode.env.asExternalUri(vscode.Uri.parse(`${env.uriScheme}:://my.extension/did-authenticate`));
* await vscode.env.openExternal(callableUri);
* ```
*
* *Note* that uris passed through `openExternal` are automatically resolved and you should not call `asExternalUri`
* on them.
* *Note* that extensions should not cache the result of `asExternalUri` as the resolved uri may become invalid due to
* a system or user action — for example, in remote cases, a user may close a port forwarding tunnel that was opened by
* `asExternalUri`.
*
* @return A uri that can be used on the client machine.
*/
......
......@@ -855,33 +855,7 @@ declare module 'vscode' {
export namespace env {
/**
* Creates a Uri that - if opened in a browser - will result in a
* registered [UriHandler](#UriHandler) to fire. The handler's
* Uri will be configured with the path, query and fragment of
* [AppUriOptions](#AppUriOptions) if provided, otherwise it will be empty.
*
* Extensions should not make any assumptions about the resulting
* Uri and should not alter it in anyway. Rather, extensions can e.g.
* use this Uri in an authentication flow, by adding the Uri as
* callback query argument to the server to authenticate to.
*
* Note: If the server decides to add additional query parameters to the Uri
* (e.g. a token or secret), it will appear in the Uri that is passed
* to the [UriHandler](#UriHandler).
*
* **Example** of an authentication flow:
* ```typescript
* vscode.window.registerUriHandler({
* handleUri(uri: vscode.Uri): vscode.ProviderResult<void> {
* if (uri.path === '/did-authenticate') {
* console.log(uri.toString());
* }
* }
* });
*
* const callableUri = await vscode.env.createAppUri({ payload: { path: '/did-authenticate' } });
* await vscode.env.openExternal(callableUri);
* ```
* @deprecated use `vscode.env.asExternalUri` instead.
*/
export function createAppUri(options?: AppUriOptions): Thenable<Uri>;
}
......
......@@ -68,7 +68,11 @@ export class MainThreadUrls implements MainThreadUrlsShape {
return Promise.resolve(undefined);
}
async $createAppUri(extensionId: ExtensionIdentifier, options?: { payload?: Partial<UriComponents> }): Promise<URI> {
async $createAppUri(uri: UriComponents): Promise<URI> {
return this.urlService.create(uri);
}
async $proposedCreateAppUri(extensionId: ExtensionIdentifier, options?: { payload?: Partial<UriComponents> }): Promise<URI> {
const payload: Partial<UriComponents> = options && options.payload ? options.payload : Object.create(null);
// we define the authority to be the extension ID to ensure
......
......@@ -228,7 +228,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
get uriScheme() { return initData.environment.appUriScheme; },
createAppUri(options?) {
checkProposedApiEnabled(extension);
return extHostUrls.createAppUri(extension.identifier, options);
return extHostUrls.proposedCreateAppUri(extension.identifier, options);
},
get logLevel() {
checkProposedApiEnabled(extension);
......@@ -248,6 +248,10 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
return extHostWindow.openUri(uri, { allowTunneling: !!initData.remote.isRemote });
},
asExternalUri(uri: URI) {
if (uri.scheme === initData.environment.appUriScheme) {
return extHostUrls.createAppUri(uri);
}
return extHostWindow.asExternalUri(uri, { allowTunneling: !!initData.remote.isRemote });
},
get remoteName() {
......
......@@ -594,7 +594,8 @@ export interface ExtHostWebviewsShape {
export interface MainThreadUrlsShape extends IDisposable {
$registerUriHandler(handle: number, extensionId: ExtensionIdentifier): Promise<void>;
$unregisterUriHandler(handle: number): Promise<void>;
$createAppUri(extensionId: ExtensionIdentifier, options?: { payload?: Partial<UriComponents> }): Promise<UriComponents>;
$createAppUri(uri: UriComponents): Promise<UriComponents>;
$proposedCreateAppUri(extensionId: ExtensionIdentifier, options?: { payload?: Partial<UriComponents> }): Promise<UriComponents>;
}
export interface ExtHostUrlsShape {
......
......@@ -56,7 +56,11 @@ export class ExtHostUrls implements ExtHostUrlsShape {
return Promise.resolve(undefined);
}
async createAppUri(extensionId: ExtensionIdentifier, options?: vscode.AppUriOptions): Promise<vscode.Uri> {
return URI.revive(await this._proxy.$createAppUri(extensionId, options));
async createAppUri(uri: URI): Promise<vscode.Uri> {
return URI.revive(await this._proxy.$createAppUri(uri));
}
async proposedCreateAppUri(extensionId: ExtensionIdentifier, options?: vscode.AppUriOptions): Promise<vscode.Uri> {
return URI.revive(await this._proxy.$proposedCreateAppUri(extensionId, options));
}
}
......@@ -162,10 +162,7 @@ class BrowserMain extends Disposable {
// Product
const productService = {
_serviceBrand: undefined,
...{
...product, // dev or built time config
...{ urlProtocol: '' } // web related overrides from us
}
...product
};
serviceCollection.set(IProductService, productService);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册