Skip to content

Commit 0609129

Browse files
Revert "fix: delayed webview resource loading due to sw controller changes (#249114)"
This reverts commit 64f6c3d.
1 parent 3b5d7d0 commit 0609129

3 files changed

Lines changed: 38 additions & 72 deletions

File tree

code/src/vs/workbench/contrib/webview/browser/pre/index.html

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
const onElectron = searchParams.get('platform') === 'electron';
3737
const disableServiceWorker = searchParams.has('disableServiceWorker');
3838
const expectedWorkerVersion = parseInt(searchParams.get('swVersion'));
39-
/** @type {MessageChannel | undefined} */
40-
let outerIframeMessageChannel;
4139

4240
/**
4341
* Use polling to track focus of main webview and iframes within the webview
@@ -238,7 +236,7 @@
238236
return reject(new Error('Service Workers are not enabled. Webviews will not work. Try disabling private/incognito mode.'));
239237
}
240238

241-
const swPath = encodeURI(`service-worker.js?v=${expectedWorkerVersion}&vscode-resource-base-authority=${searchParams.get('vscode-resource-base-authority')}&remoteAuthority=${searchParams.get('remoteAuthority') ?? ''}`);
239+
const swPath = encodeURI(`service-worker.js?v=${expectedWorkerVersion}&vscode-resource-base-authority=${searchParams.get('vscode-resource-base-authority')}&id=${ID}&remoteAuthority=${searchParams.get('remoteAuthority') ?? ''}`);
242240
navigator.serviceWorker.register(swPath, { type: 'module' })
243241
.then(async registration => {
244242
/**
@@ -267,8 +265,7 @@
267265
navigator.serviceWorker.addEventListener('message', versionHandler);
268266

269267
const postVersionMessage = (/** @type {ServiceWorker} */ controller) => {
270-
outerIframeMessageChannel = new MessageChannel();
271-
controller.postMessage({ channel: 'version' }, [outerIframeMessageChannel.port2]);
268+
controller.postMessage({ channel: 'version' });
272269
};
273270

274271
// At this point, either the service worker is ready and
@@ -1212,17 +1209,6 @@
12121209

12131210
unloadMonitor.onIframeLoaded(newFrame);
12141211
}
1215-
1216-
if (!disableServiceWorker && outerIframeMessageChannel) {
1217-
outerIframeMessageChannel.port1.onmessage = event => {
1218-
switch (event.data.channel) {
1219-
case 'load-resource':
1220-
case 'load-localhost':
1221-
hostMessaging.postMessage(event.data.channel, event.data);
1222-
return;
1223-
}
1224-
};
1225-
}
12261212
});
12271213

12281214
// propagate vscode-context-menu-visible class

code/src/vs/workbench/contrib/webview/browser/pre/service-worker.ts

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const searchParams = new URL(location.toString()).searchParams;
1616

1717
const remoteAuthority = searchParams.get('remoteAuthority');
1818

19-
let outerIframeMessagePort: MessagePort | undefined;
19+
const ID = searchParams.get('id');
2020

2121
/**
2222
* Origin used for resources
@@ -103,7 +103,6 @@ sw.addEventListener('message', async (event: ExtendableMessageEvent) => {
103103
const source = event.source as Client;
104104
switch (event.data.channel) {
105105
case 'version': {
106-
outerIframeMessagePort = event.ports[0];
107106
sw.clients.get(source.id).then(client => {
108107
if (client) {
109108
client.postMessage({
@@ -203,24 +202,21 @@ async function processResourceRequest(
203202
event: FetchEvent,
204203
requestUrlComponents: ResourceRequestUrlComponents
205204
): Promise<Response> {
206-
let client = await sw.clients.get(event.clientId);
205+
const client = await sw.clients.get(event.clientId);
206+
let webviewId: string | null | undefined;
207207
if (!client) {
208-
client = await getWorkerClientForId(event.clientId);
209-
if (!client) {
208+
const workerClient = await getWorkerClientForId(event.clientId);
209+
if (!workerClient) {
210210
console.error('Could not find inner client for request');
211211
return notFound();
212+
} else {
213+
webviewId = getWebviewIdForClient(workerClient);
212214
}
215+
} else {
216+
webviewId = getWebviewIdForClient(client);
213217
}
214218

215-
const webviewId = getWebviewIdForClient(client);
216-
217-
// Refs https://github.com/microsoft/vscode/issues/244143
218-
// With PlzDedicatedWorker, worker subresources and blob wokers
219-
// will use clients different from the window client.
220-
// Since we cannot different a worker main resource from a worker subresource
221-
// we will use message channel to the outer iframe provided at the time
222-
// of service worker controller version initialization.
223-
if (!webviewId && client.type !== 'worker' && client.type !== 'sharedworker') {
219+
if (!webviewId) {
224220
console.error('Could not resolve webview id');
225221
return notFound();
226222
}
@@ -325,6 +321,12 @@ async function processResourceRequest(
325321
return response.clone();
326322
};
327323

324+
const parentClients = await getOuterIframeClient(webviewId);
325+
if (!parentClients.length) {
326+
console.log('Could not find parent client for request');
327+
return notFound();
328+
}
329+
328330
let cached: Response | undefined;
329331
if (shouldTryCaching) {
330332
const cache = await caches.open(resourceCacheName);
@@ -333,26 +335,8 @@ async function processResourceRequest(
333335

334336
const { requestId, promise } = resourceRequestStore.create();
335337

336-
if (webviewId) {
337-
const parentClients = await getOuterIframeClient(webviewId);
338-
if (!parentClients.length) {
339-
console.log('Could not find parent client for request');
340-
return notFound();
341-
}
342-
343-
for (const parentClient of parentClients) {
344-
parentClient.postMessage({
345-
channel: 'load-resource',
346-
id: requestId,
347-
scheme: requestUrlComponents.scheme,
348-
authority: requestUrlComponents.authority,
349-
path: requestUrlComponents.path,
350-
query: requestUrlComponents.query,
351-
ifNoneMatch: cached?.headers.get('ETag'),
352-
});
353-
}
354-
} else if (client.type === 'worker' || client.type === 'sharedworker') {
355-
outerIframeMessagePort?.postMessage({
338+
for (const parentClient of parentClients) {
339+
parentClient.postMessage({
356340
channel: 'load-resource',
357341
id: requestId,
358342
scheme: requestUrlComponents.scheme,
@@ -377,13 +361,7 @@ async function processLocalhostRequest(
377361
return fetch(event.request);
378362
}
379363
const webviewId = getWebviewIdForClient(client);
380-
// Refs https://github.com/microsoft/vscode/issues/244143
381-
// With PlzDedicatedWorker, worker subresources and blob wokers
382-
// will use clients different from the window client.
383-
// Since we cannot different a worker main resource from a worker subresource
384-
// we will use message channel to the outer iframe provided at the time
385-
// of service worker controller version initialization.
386-
if (!webviewId && client.type !== 'worker' && client.type !== 'sharedworker') {
364+
if (!webviewId) {
387365
console.error('Could not resolve webview id');
388366
return fetch(event.request);
389367
}
@@ -407,22 +385,15 @@ async function processLocalhostRequest(
407385
});
408386
};
409387

388+
const parentClients = await getOuterIframeClient(webviewId);
389+
if (!parentClients.length) {
390+
console.log('Could not find parent client for request');
391+
return notFound();
392+
}
393+
410394
const { requestId, promise } = localhostRequestStore.create();
411-
if (webviewId) {
412-
const parentClients = await getOuterIframeClient(webviewId);
413-
if (!parentClients.length) {
414-
console.log('Could not find parent client for request');
415-
return notFound();
416-
}
417-
for (const parentClient of parentClients) {
418-
parentClient.postMessage({
419-
channel: 'load-localhost',
420-
origin: origin,
421-
id: requestId,
422-
});
423-
}
424-
} else if (client.type === 'worker' || client.type === 'sharedworker') {
425-
outerIframeMessagePort?.postMessage({
395+
for (const parentClient of parentClients) {
396+
parentClient.postMessage({
426397
channel: 'load-localhost',
427398
origin: origin,
428399
id: requestId,
@@ -433,6 +404,15 @@ async function processLocalhostRequest(
433404
}
434405

435406
function getWebviewIdForClient(client: Client): string | null {
407+
// Refs https://github.com/microsoft/vscode/issues/244143
408+
// With PlzDedicatedWorker, worker subresources and blob wokers
409+
// will use clients different from the window client.
410+
// Since we cannot different a worker main resource from a worker subresource
411+
// we will use the global webview ID passed in at the time of
412+
// service worker registration.
413+
if (client.type === 'worker' || client.type === 'sharedworker') {
414+
return ID;
415+
}
436416
const requesterClientUrl = new URL(client.url);
437417
return requesterClientUrl.searchParams.get('id');
438418
}

src/vs/workbench/contrib/webview/browser/pre/index-no-csp.html

Whitespace-only changes.

0 commit comments

Comments
 (0)