From 14f40b4c6bbb72234ce68101a736e51306335ee1 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Thu, 3 Nov 2016 17:32:33 +0100 Subject: [PATCH] ipc: remove vscode:setHeaders #10587 --- src/vs/code/electron-main/window.ts | 16 ++++++++++++++++ src/vs/code/electron-main/windows.ts | 14 -------------- src/vs/platform/environment/common/http.ts | 16 ++++++++++++++++ .../node/extensionGalleryService.ts | 16 ++++++---------- src/vs/workbench/electron-browser/integration.ts | 9 --------- 5 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 src/vs/platform/environment/common/http.ts diff --git a/src/vs/code/electron-main/window.ts b/src/vs/code/electron-main/window.ts index 2d90236b64f..47ecaf823a3 100644 --- a/src/vs/code/electron-main/window.ts +++ b/src/vs/code/electron-main/window.ts @@ -16,6 +16,7 @@ import { ILogService } from 'vs/code/electron-main/log'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; import { parseArgs, ParsedArgs } from 'vs/platform/environment/node/argv'; import product from 'vs/platform/product'; +import { getCommonHTTPHeaders } from 'vs/platform/environment/common/http'; export interface IWindowState { width?: number; @@ -180,6 +181,21 @@ export class VSCodeWindow { this._win = new BrowserWindow(options); this._id = this._win.id; + // TODO@joao: hook this up to some initialization routine + // this causes a race between setting the headers and doing + // a request that needs them. chances are low + getCommonHTTPHeaders().done(headers => { + if (!this._win) { + return; + } + + const urls = ['https://marketplace.visualstudio.com/*', 'https://*.vsassets.io/*']; + + this._win.webContents.session.webRequest.onBeforeSendHeaders({ urls }, (details, cb) => { + cb({ cancel: false, requestHeaders: objects.assign(details.requestHeaders, headers) }); + }); + }); + if (isFullscreenOrMaximized) { this.win.maximize(); diff --git a/src/vs/code/electron-main/windows.ts b/src/vs/code/electron-main/windows.ts index a70d672a4bd..27c94c57685 100644 --- a/src/vs/code/electron-main/windows.ts +++ b/src/vs/code/electron-main/windows.ts @@ -232,20 +232,6 @@ export class WindowsManager implements IWindowsMainService, IWindowEventService } }); - ipc.on('vscode:setHeaders', (event, windowId: number, urls: string[], headers: any) => { - this.logService.log('IPC#vscode:setHeaders'); - - const vscodeWindow = this.getWindowById(windowId); - - if (!vscodeWindow || !urls || !urls.length || !headers) { - return; - } - - vscodeWindow.win.webContents.session.webRequest.onBeforeSendHeaders({ urls }, (details, cb) => { - cb({ cancel: false, requestHeaders: assign(details.requestHeaders, headers) }); - }); - }); - ipc.on('vscode:broadcast', (event, windowId: number, target: string, broadcast: { channel: string; payload: any; }) => { if (broadcast.channel && !types.isUndefinedOrNull(broadcast.payload)) { this.logService.log('IPC#vscode:broadcast', target, broadcast.channel, broadcast.payload); diff --git a/src/vs/platform/environment/common/http.ts b/src/vs/platform/environment/common/http.ts new file mode 100644 index 00000000000..d1bf7af0181 --- /dev/null +++ b/src/vs/platform/environment/common/http.ts @@ -0,0 +1,16 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { TPromise } from 'vs/base/common/winjs.base'; +import { getMachineId } from 'vs/base/node/id'; +import pkg from 'vs/platform/package'; + +export function getCommonHTTPHeaders(): TPromise<{ [key: string]: string; }> { + return getMachineId().then(machineId => ({ + 'X-Market-Client-Id': `VSCode ${pkg.version}`, + 'User-Agent': `VSCode ${pkg.version}`, + 'X-Market-User-Id': machineId + })); +} \ No newline at end of file diff --git a/src/vs/platform/extensionManagement/node/extensionGalleryService.ts b/src/vs/platform/extensionManagement/node/extensionGalleryService.ts index b31ffe53d0d..a3bc9eab7de 100644 --- a/src/vs/platform/extensionManagement/node/extensionGalleryService.ts +++ b/src/vs/platform/extensionManagement/node/extensionGalleryService.ts @@ -23,7 +23,7 @@ import pkg from 'vs/platform/package'; import product from 'vs/platform/product'; import { isVersionValid } from 'vs/platform/extensions/node/extensionValidator'; import * as url from 'url'; -import { getMachineId } from 'vs/base/node/id'; +import { getCommonHTTPHeaders } from 'vs/platform/environment/common/http'; interface IRawGalleryExtensionFile { assetType: string; @@ -262,12 +262,8 @@ export class ExtensionGalleryService implements IExtensionGalleryService { private extensionsGalleryUrl: string; @memoize - private get commonHeaders(): TPromise<{ [key: string]: string; }> { - return getMachineId().then(machineId => ({ - 'X-Market-Client-Id': `VSCode ${pkg.version}`, - 'User-Agent': `VSCode ${pkg.version}`, - 'X-Market-User-Id': machineId - })); + private get commonHTTPHeaders(): TPromise<{ [key: string]: string; }> { + return getCommonHTTPHeaders(); } constructor( @@ -288,7 +284,7 @@ export class ExtensionGalleryService implements IExtensionGalleryService { } getRequestHeaders(): TPromise<{ [key: string]: string; }> { - return this.commonHeaders; + return this.commonHTTPHeaders; } query(options: IQueryOptions = {}): TPromise> { @@ -338,7 +334,7 @@ export class ExtensionGalleryService implements IExtensionGalleryService { } private queryGallery(query: Query): TPromise<{ galleryExtensions: IRawGalleryExtension[], total: number; }> { - return this.commonHeaders + return this.commonHTTPHeaders .then(headers => { const data = JSON.stringify(query.raw); @@ -478,7 +474,7 @@ export class ExtensionGalleryService implements IExtensionGalleryService { parsedUrl.search = undefined; parsedUrl.query['redirect'] = 'true'; - return this.commonHeaders.then(headers => { + return this.commonHTTPHeaders.then(headers => { headers = assign({}, headers, options.headers || {}); options = assign({}, options, { headers }); diff --git a/src/vs/workbench/electron-browser/integration.ts b/src/vs/workbench/electron-browser/integration.ts index ed76f6de9de..763fa81bdb4 100644 --- a/src/vs/workbench/electron-browser/integration.ts +++ b/src/vs/workbench/electron-browser/integration.ts @@ -34,7 +34,6 @@ import { IPath, IOpenFileRequest, IWindowConfiguration } from 'vs/workbench/elec import { IResourceInput } from 'vs/platform/editor/common/editor'; import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService'; -import { IExtensionGalleryService } from 'vs/platform/extensionManagement/common/extensionManagement'; import URI from 'vs/base/common/uri'; import { ipcRenderer as ipc, webFrame, remote } from 'electron'; @@ -69,7 +68,6 @@ export class ElectronIntegration { @IMessageService private messageService: IMessageService, @IContextMenuService private contextMenuService: IContextMenuService, @IWorkbenchEditorService private editorService: IWorkbenchEditorService, - @IExtensionGalleryService private extensionGalleryService: IExtensionGalleryService, @IUntitledEditorService private untitledEditorService: IUntitledEditorService ) { } @@ -194,13 +192,6 @@ export class ElectronIntegration { } } }); - - // Extra request headers - this.extensionGalleryService.getRequestHeaders().done(headers => { - const urls = ['https://marketplace.visualstudio.com/*', 'https://*.vsassets.io/*']; - - ipc.send('vscode:setHeaders', this.windowService.getWindowId(), urls, headers); - }); } private resolveKeybindings(actionIds: string[]): TPromise<{ id: string; binding: number; }[]> { -- GitLab