Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/clipboard/browser/clipboardService.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { localize } from '../../../../nls.js';
7
import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
8
import { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.js';
9
import { BrowserClipboardService as BaseBrowserClipboardService } from '../../../../platform/clipboard/browser/clipboardService.js';
10
import { INotificationService, Severity } from '../../../../platform/notification/common/notification.js';
11
import { IOpenerService } from '../../../../platform/opener/common/opener.js';
12
import { Event } from '../../../../base/common/event.js';
13
import { DisposableStore } from '../../../../base/common/lifecycle.js';
14
import { IWorkbenchEnvironmentService } from '../../environment/common/environmentService.js';
15
import { ILogService } from '../../../../platform/log/common/log.js';
16
import { ILayoutService } from '../../../../platform/layout/browser/layoutService.js';
17
import { getActiveWindow } from '../../../../base/browser/dom.js';
18
19
export class BrowserClipboardService extends BaseBrowserClipboardService {
20
21
constructor(
22
@INotificationService private readonly notificationService: INotificationService,
23
@IOpenerService private readonly openerService: IOpenerService,
24
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
25
@ILogService logService: ILogService,
26
@ILayoutService layoutService: ILayoutService
27
) {
28
super(layoutService, logService);
29
}
30
31
override async writeText(text: string, type?: string): Promise<void> {
32
this.logService.trace('BrowserClipboardService#writeText called with type:', type, ' with text.length:', text.length);
33
if (!!this.environmentService.extensionTestsLocationURI && typeof type !== 'string') {
34
type = 'vscode-tests'; // force in-memory clipboard for tests to avoid permission issues
35
}
36
this.logService.trace('BrowserClipboardService#super.writeText');
37
return super.writeText(text, type);
38
}
39
40
override async readText(type?: string): Promise<string> {
41
this.logService.trace('BrowserClipboardService#readText called with type:', type);
42
if (!!this.environmentService.extensionTestsLocationURI && typeof type !== 'string') {
43
type = 'vscode-tests'; // force in-memory clipboard for tests to avoid permission issues
44
}
45
46
if (type) {
47
this.logService.trace('BrowserClipboardService#super.readText');
48
return super.readText(type);
49
}
50
51
try {
52
const readText = await getActiveWindow().navigator.clipboard.readText();
53
this.logService.trace('BrowserClipboardService#readText with readText.length:', readText.length);
54
return readText;
55
} catch (error) {
56
return new Promise<string>(resolve => {
57
58
// Inform user about permissions problem (https://github.com/microsoft/vscode/issues/112089)
59
const listener = new DisposableStore();
60
const handle = this.notificationService.prompt(
61
Severity.Error,
62
localize('clipboardError', "Unable to read from the browser's clipboard. Please make sure you have granted access for this website to read from the clipboard."),
63
[{
64
label: localize('retry', "Retry"),
65
run: async () => {
66
listener.dispose();
67
resolve(await this.readText(type));
68
}
69
}, {
70
label: localize('learnMore', "Learn More"),
71
run: () => this.openerService.open('https://go.microsoft.com/fwlink/?linkid=2151362')
72
}],
73
{
74
sticky: true
75
}
76
);
77
78
// Always resolve the promise once the notification closes
79
listener.add(Event.once(handle.onDidClose)(() => resolve('')));
80
});
81
}
82
}
83
}
84
85
registerSingleton(IClipboardService, BrowserClipboardService, InstantiationType.Delayed);
86
87