Skip to content

Commit

Permalink
[workspace] Better localstorage keys eclipse-theia#904
Browse files Browse the repository at this point in the history
Adds the current `window.location.pathname` in the key used to store data into the local storage.

This fixes an issue where multiple theia behind a proxy would share the same local storage domain.

Signed-off-by: Paul Maréchal <[email protected]>
  • Loading branch information
paul-marechal committed Apr 23, 2018
1 parent da4e14a commit 5aef2fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
37 changes: 22 additions & 15 deletions packages/workspace/src/browser/workspace-storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import { StorageService } from '@theia/core/lib/browser/storage-service';
import { WorkspaceService } from './workspace-service';
import { inject, injectable } from 'inversify';
import { inject, injectable, postConstruct } from 'inversify';
import { ILogger } from '@theia/core/lib/common';
import { LocalStorageService } from '@theia/core/lib/browser/storage-service';
import URI from '@theia/core/lib/common/uri';
import { Deferred } from '@theia/core/lib/common/promise-util';

/*
* Prefixes any stored data with the current workspace path.
Expand All @@ -18,31 +20,36 @@ import { LocalStorageService } from '@theia/core/lib/browser/storage-service';
export class WorkspaceStorageService implements StorageService {

private prefix: string;
private initialized: Promise<void>;
protected initialized: Deferred<void>;
protected storageService: StorageService;

constructor( @inject(WorkspaceService) protected workspaceService: WorkspaceService,
@inject(ILogger) protected logger: ILogger) {
this.initialized = this.workspaceService.root.then(stat => {
if (stat) {
this.prefix = stat.uri;
} else {
this.prefix = '_global_';
}
});
@inject(WorkspaceService)
protected workspaceService: WorkspaceService;

@inject(ILogger)
protected logger: ILogger;

constructor() {
this.storageService = new LocalStorageService(this.logger);
this.initialized = new Deferred<void>();
}

@postConstruct()
protected async init(): Promise<void> {
const statFile = await this.workspaceService.root;
const workspace = statFile ? new URI(statFile.uri).path : '_global_';
this.prefix = `${window.location.pathname}:${workspace}`;
this.initialized.resolve();
}

async setData<T>(key: string, data: T): Promise<void> {
if (!this.prefix) {
await this.initialized;
}
if (!this.prefix) { await this.initialized.promise; }
const fullKey = this.prefixWorkspaceURI(key);
return this.storageService.setData(fullKey, data);
}

async getData<T>(key: string, defaultValue?: T): Promise<T | undefined> {
await this.initialized;
if (!this.prefix) { await this.initialized.promise; }
const fullKey = this.prefixWorkspaceURI(key);
return this.storageService.getData(fullKey, defaultValue);
}
Expand Down
22 changes: 11 additions & 11 deletions packages/workspace/src/node/default-workspace-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as yargs from 'yargs';
import * as fs from 'fs-extra';
import * as os from 'os';

import { injectable, inject } from "inversify";
import { injectable, inject, postConstruct } from "inversify";
import { FileUri } from '@theia/core/lib/node';
import { CliContribution } from '@theia/core/lib/node/cli';
import { Deferred } from '@theia/core/lib/common/promise-util';
Expand Down Expand Up @@ -50,18 +50,18 @@ export class DefaultWorkspaceServer implements WorkspaceServer {

protected root: Promise<string | undefined>;

constructor(
@inject(WorkspaceCliContribution) protected readonly cliParams: WorkspaceCliContribution
) {
@inject(WorkspaceCliContribution)
protected readonly cliParams: WorkspaceCliContribution;

@postConstruct()
protected async init(): Promise<void> {
this.root = this.getRootURIFromCli();
this.root.then(async root => {
if (!root) {
const data = await this.readFromUserHome();
if (data && data.recentRoots) {
this.root = Promise.resolve(data.recentRoots[0]);
}
if (!await this.root) {
const data = await this.readFromUserHome();
if (data && data.recentRoots) {
this.root = Promise.resolve(data.recentRoots[0]);
}
});
}
}

getRoot(): Promise<string | undefined> {
Expand Down

0 comments on commit 5aef2fa

Please sign in to comment.