1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import { inject, injectable, postConstruct } from 'inversify';
|
18 | import { ILogger } from '../common/logger';
|
19 | import { MessageService } from '../common/message-service';
|
20 | import { WindowService } from './window/window-service';
|
21 | import { environment } from '@theia/application-package/lib/environment';
|
22 |
|
23 | export const StorageService = Symbol('IStorageService');
|
24 |
|
25 |
|
26 |
|
27 | export interface StorageService {
|
28 |
|
29 | |
30 |
|
31 |
|
32 | setData<T>(key: string, data: T): Promise<void>;
|
33 |
|
34 | |
35 |
|
36 |
|
37 | getData<T>(key: string, defaultValue: T): Promise<T>;
|
38 | getData<T>(key: string): Promise<T | undefined>;
|
39 | }
|
40 |
|
41 | interface LocalStorage {
|
42 |
|
43 | [key: string]: any;
|
44 | }
|
45 |
|
46 | @injectable()
|
47 | export class LocalStorageService implements StorageService {
|
48 | private storage: LocalStorage;
|
49 |
|
50 | @inject(ILogger) protected logger: ILogger;
|
51 | @inject(MessageService) protected readonly messageService: MessageService;
|
52 | @inject(WindowService) protected readonly windowService: WindowService;
|
53 |
|
54 | @postConstruct()
|
55 | protected init(): void {
|
56 | if (typeof window !== 'undefined' && window.localStorage) {
|
57 | this.storage = window.localStorage;
|
58 | this.testLocalStorage();
|
59 | } else {
|
60 | this.logger.warn(log => log("The browser doesn't support localStorage. state will not be persisted across sessions."));
|
61 | this.storage = {};
|
62 | }
|
63 | }
|
64 |
|
65 | setData<T>(key: string, data?: T): Promise<void> {
|
66 | if (data !== undefined) {
|
67 | try {
|
68 | this.storage[this.prefix(key)] = JSON.stringify(data);
|
69 | } catch (e) {
|
70 | this.showDiskQuotaExceededMessage();
|
71 | }
|
72 | } else {
|
73 | delete this.storage[this.prefix(key)];
|
74 | }
|
75 | return Promise.resolve();
|
76 | }
|
77 |
|
78 | getData<T>(key: string, defaultValue?: T): Promise<T | undefined> {
|
79 | const result = this.storage[this.prefix(key)];
|
80 | if (result === undefined) {
|
81 | return Promise.resolve(defaultValue);
|
82 | }
|
83 | return Promise.resolve(JSON.parse(result));
|
84 | }
|
85 |
|
86 | protected prefix(key: string): string {
|
87 | if (environment.electron.is()) {
|
88 | return `theia:${key}`;
|
89 | }
|
90 | const pathname = typeof window === 'undefined' ? '' : window.location.pathname;
|
91 | return `theia:${pathname}:${key}`;
|
92 | }
|
93 |
|
94 | private async showDiskQuotaExceededMessage(): Promise<void> {
|
95 | const READ_INSTRUCTIONS_ACTION = 'Read Instructions';
|
96 | const CLEAR_STORAGE_ACTION = 'Clear Local Storage';
|
97 | const ERROR_MESSAGE = `Your preferred browser's local storage is almost full.
|
98 | To be able to save your current workspace layout or data, you may need to free up some space.
|
99 | You can refer to Theia's documentation page for instructions on how to manually clean
|
100 | your browser's local storage or choose to clear all.`;
|
101 | this.messageService.warn(ERROR_MESSAGE, READ_INSTRUCTIONS_ACTION, CLEAR_STORAGE_ACTION).then(async selected => {
|
102 | if (READ_INSTRUCTIONS_ACTION === selected) {
|
103 | this.windowService.openNewWindow('https://github.com/eclipse-theia/theia/wiki/Cleaning-Local-Storage', { external: true });
|
104 | } else if (CLEAR_STORAGE_ACTION === selected) {
|
105 | this.clearStorage();
|
106 | }
|
107 | });
|
108 | }
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 | private testLocalStorage(): void {
|
115 | const keyTest = this.prefix('Test');
|
116 | try {
|
117 | this.storage[keyTest] = JSON.stringify(new Array(60000));
|
118 | } catch (error) {
|
119 | this.showDiskQuotaExceededMessage();
|
120 | } finally {
|
121 | this.storage.removeItem(keyTest);
|
122 | }
|
123 | }
|
124 |
|
125 | private clearStorage(): void {
|
126 | this.storage.clear();
|
127 | }
|
128 |
|
129 | }
|