forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 6
/
webview.ts
383 lines (313 loc) · 11.3 KB
/
webview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Dimension } from 'vs/base/browser/dom';
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
import { equals } from 'vs/base/common/arrays';
import { Event } from 'vs/base/common/event';
import { IDisposable } from 'vs/base/common/lifecycle';
import { isEqual } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { generateUuid } from 'vs/base/common/uuid';
import { IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { IWebviewPortMapping } from 'vs/platform/webview/common/webviewPortMapping';
import { Memento, MementoObject } from 'vs/workbench/common/memento';
/**
* Set when the find widget in a webview in a webview is visible.
*/
export const KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE = new RawContextKey<boolean>('webviewFindWidgetVisible', false);
/**
* Set when the find widget in a webview is focused.
*/
export const KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_FOCUSED = new RawContextKey<boolean>('webviewFindWidgetFocused', false);
/**
* Set when the find widget in a webview is enabled in a webview
*/
export const KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_ENABLED = new RawContextKey<boolean>('webviewFindWidgetEnabled', false);
export const IWebviewService = createDecorator<IWebviewService>('webviewService');
export interface IWebviewService {
readonly _serviceBrand: undefined;
/**
* The currently focused webview.
*/
readonly activeWebview: IWebview | undefined;
/**
* All webviews.
*/
readonly webviews: Iterable<IWebview>;
/**
* Fired when the currently focused webview changes.
*/
readonly onDidChangeActiveWebview: Event<IWebview | undefined>;
/**
* Create a basic webview dom element.
*/
createWebviewElement(initInfo: WebviewInitInfo): IWebviewElement;
/**
* Create a lazily created webview element that is overlaid on top of another element.
*
* Allows us to avoid re-parenting the webview (which destroys its contents) when
* moving webview around the workbench.
*/
createWebviewOverlay(initInfo: WebviewInitInfo): IOverlayWebview;
}
export interface WebviewInitInfo {
readonly providedViewType?: string;
readonly origin?: string;
readonly title: string | undefined;
readonly options: WebviewOptions;
readonly contentOptions: WebviewContentOptions;
readonly extension: WebviewExtensionDescription | undefined;
}
export const enum WebviewContentPurpose {
NotebookRenderer = 'notebookRenderer',
CustomEditor = 'customEditor',
WebviewView = 'webviewView',
}
export type WebviewStyles = { readonly [key: string]: string | number };
export interface WebviewOptions {
/**
* The purpose of the webview; this is (currently) only used for filtering in js-debug
*/
readonly purpose?: WebviewContentPurpose;
readonly customClasses?: string;
readonly enableFindWidget?: boolean;
readonly tryRestoreScrollPosition?: boolean;
readonly retainContextWhenHidden?: boolean;
transformCssVariables?(styles: WebviewStyles): WebviewStyles;
}
export interface WebviewContentOptions {
/**
* Should the webview allow `acquireVsCodeApi` to be called multiple times? Defaults to false.
*/
readonly allowMultipleAPIAcquire?: boolean;
/**
* Should scripts be enabled in the webview? Defaults to false.
*/
readonly allowScripts?: boolean;
/**
* Should forms be enabled in the webview? Defaults to the value of {@link allowScripts}.
*/
readonly allowForms?: boolean;
/**
* Set of root paths from which the webview can load local resources.
*/
readonly localResourceRoots?: readonly URI[];
/**
* Set of localhost port mappings to apply inside the webview.
*/
readonly portMapping?: readonly IWebviewPortMapping[];
/**
* Are command uris enabled in the webview? Defaults to false.
*
* TODO: This is only supported by mainThreadWebviews and should be removed from here.
*/
readonly enableCommandUris?: boolean | readonly string[];
}
/**
* Check if two {@link WebviewContentOptions} are equal.
*/
export function areWebviewContentOptionsEqual(a: WebviewContentOptions, b: WebviewContentOptions): boolean {
return (
a.allowMultipleAPIAcquire === b.allowMultipleAPIAcquire
&& a.allowScripts === b.allowScripts
&& a.allowForms === b.allowForms
&& equals(a.localResourceRoots, b.localResourceRoots, isEqual)
&& equals(a.portMapping, b.portMapping, (a, b) => a.extensionHostPort === b.extensionHostPort && a.webviewPort === b.webviewPort)
&& areEnableCommandUrisEqual(a, b)
);
}
function areEnableCommandUrisEqual(a: WebviewContentOptions, b: WebviewContentOptions): boolean {
if (a.enableCommandUris === b.enableCommandUris) {
return true;
}
if (Array.isArray(a.enableCommandUris) && Array.isArray(b.enableCommandUris)) {
return equals(a.enableCommandUris, b.enableCommandUris);
}
return false;
}
export interface WebviewExtensionDescription {
readonly location?: URI;
readonly id: ExtensionIdentifier;
}
export interface WebviewMessageReceivedEvent {
readonly message: any;
readonly transfer?: readonly ArrayBuffer[];
}
export interface IWebview extends IDisposable {
/**
* The original view type of the webview.
*/
readonly providedViewType?: string;
/**
* The origin this webview itself is loaded from. May not be unique.
*/
readonly origin: string;
/**
* Set html content of the webview.
*/
setHtml(html: string): void;
/**
* Set the title of the webview. This is set on the webview's iframe element.
*/
setTitle(title: string): void;
/**
* Control what content is allowed/blocked inside the webview.
*/
contentOptions: WebviewContentOptions;
/**
* List of roots from which local resources can be loaded.
*
* Requests for local resources not in this list are blocked.
*/
localResourcesRoot: readonly URI[];
/**
* The extension that created/owns this webview.
*/
extension: WebviewExtensionDescription | undefined;
initialScrollProgress: number;
state: string | undefined;
readonly isFocused: boolean;
readonly onDidFocus: Event<void>;
readonly onDidBlur: Event<void>;
/**
* Fired when the webview is disposed of.
*/
readonly onDidDispose: Event<void>;
readonly onDidClickLink: Event<string>;
readonly onDidScroll: Event<{ readonly scrollYPercentage: number }>;
readonly onDidWheel: Event<IMouseWheelEvent>;
readonly onDidUpdateState: Event<string | undefined>;
readonly onDidReload: Event<void>;
/**
* Fired when the webview cannot be loaded or is now in a non-functional state.
*/
readonly onFatalError: Event<{ readonly message: string }>;
readonly onMissingCsp: Event<ExtensionIdentifier>;
readonly onMessage: Event<WebviewMessageReceivedEvent>;
postMessage(message: any, transfer?: readonly ArrayBuffer[]): Promise<boolean>;
focus(): void;
reload(): void;
showFind(animated?: boolean): void;
hideFind(animated?: boolean): void;
runFindAction(previous: boolean): void;
selectAll(): void;
copy(): void;
paste(): void;
cut(): void;
undo(): void;
redo(): void;
windowDidDragStart(): void;
windowDidDragEnd(): void;
setContextKeyService(scopedContextKeyService: IContextKeyService): void;
}
/**
* Basic webview rendered directly in the dom
*/
export interface IWebviewElement extends IWebview {
/**
* Append the webview to a HTML element.
*
* Note that the webview content will be destroyed if any part of the parent hierarchy
* changes. You can avoid this by using a {@link IOverlayWebview} instead.
*
* @param parent Element to append the webview to.
*/
mountTo(parent: HTMLElement): void;
}
/**
* Lazily created {@link IWebview} that is absolutely positioned over another element.
*
* Absolute positioning lets us avoid having the webview be re-parented, which would destroy the
* webview's content.
*
* Note that the underlying webview owned by a `WebviewOverlay` can be dynamically created
* and destroyed depending on who has {@link IOverlayWebview.claim claimed} or {@link IOverlayWebview.release released} it.
*/
export interface IOverlayWebview extends IWebview {
/**
* The HTML element that holds the webview.
*/
readonly container: HTMLElement;
origin: string;
options: WebviewOptions;
/**
* Take ownership of the webview.
*
* This will create the underlying webview element.
*
* @param claimant Identifier for the object claiming the webview.
* This must match the `claimant` passed to {@link IOverlayWebview.release}.
*/
claim(claimant: any, scopedContextKeyService: IContextKeyService | undefined): void;
/**
* Release ownership of the webview.
*
* If the {@link claimant} is still the current owner of the webview, this will
* cause the underlying webview element to be destoryed.
*
* @param claimant Identifier for the object releasing its claim on the webview.
* This must match the `claimant` passed to {@link IOverlayWebview.claim}.
*/
release(claimant: any): void;
/**
* Absolutely position the webview on top of another element in the DOM.
*
* @param element Element to position the webview on top of. This element should
* be an placeholder for the webview since the webview will entirely cover it.
* @param dimension Optional explicit dimensions to use for sizing the webview.
* @param clippingContainer Optional container to clip the webview to. This should generally be a parent of `element`.
*/
layoutWebviewOverElement(element: HTMLElement, dimension?: Dimension, clippingContainer?: HTMLElement): void;
}
/**
* Stores the unique origins for a webview.
*
* These are randomly generated
*/
export class WebviewOriginStore {
private readonly _memento: Memento;
private readonly _state: MementoObject;
constructor(
rootStorageKey: string,
@IStorageService storageService: IStorageService,
) {
this._memento = new Memento(rootStorageKey, storageService);
this._state = this._memento.getMemento(StorageScope.APPLICATION, StorageTarget.MACHINE);
}
public getOrigin(viewType: string, additionalKey: string | undefined): string {
const key = this._getKey(viewType, additionalKey);
const existing = this._state[key];
if (existing && typeof existing === 'string') {
return existing;
}
const newOrigin = generateUuid();
this._state[key] = newOrigin;
this._memento.saveMemento();
return newOrigin;
}
private _getKey(viewType: string, additionalKey: string | undefined): string {
return JSON.stringify({ viewType, key: additionalKey });
}
}
/**
* Stores the unique origins for a webview.
*
* These are randomly generated, but keyed on extension and webview viewType.
*/
export class ExtensionKeyedWebviewOriginStore {
private readonly _store: WebviewOriginStore;
constructor(
rootStorageKey: string,
@IStorageService storageService: IStorageService,
) {
this._store = new WebviewOriginStore(rootStorageKey, storageService);
}
public getOrigin(viewType: string, extId: ExtensionIdentifier): string {
return this._store.getOrigin(viewType, extId.value);
}
}