-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Artem Zatsarynnyi <azatsary@redhat.com>
- Loading branch information
1 parent
1808d78
commit 7ab689f
Showing
13 changed files
with
293 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 RedHat and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { injectable, inject } from 'inversify'; | ||
import { isFirefox } from './browser'; | ||
import { ClipboardService } from './clipboard-service'; | ||
import { ILogger } from '../common/logger'; | ||
import { MessageService } from '../common/message-service'; | ||
|
||
export interface NavigatorClipboard { | ||
readText(): Promise<string>; | ||
writeText(value: string): Promise<void>; | ||
} | ||
export interface PermissionStatus { | ||
state: 'granted' | 'prompt' | 'denied' | ||
} | ||
export interface NavigatorPermissions { | ||
query(options: { name: string }): Promise<PermissionStatus> | ||
} | ||
|
||
@injectable() | ||
export class BrowserClipboardService implements ClipboardService { | ||
|
||
@inject(MessageService) | ||
protected readonly messageService: MessageService; | ||
|
||
@inject(ILogger) | ||
protected readonly logger: ILogger; | ||
|
||
async readText(): Promise<string> { | ||
let permission; | ||
try { | ||
permission = await this.queryPermission('clipboard-read'); | ||
} catch (e) { | ||
this.logger.error('Failed checking a clipboard-read permission.', e); | ||
// in FireFox, Clipboard API isn't gated with the permissions | ||
try { | ||
return await this.getClipboardAPI().readText(); | ||
} catch (e) { | ||
this.logger.error('Failed reading clipboard content.', e); | ||
if (isFirefox) { | ||
this.messageService.warn(`Clipboard API is not available. | ||
It can be enabled by 'dom.events.testing.asyncClipboard' preference on 'about:config' page. Then reload Theia. | ||
Note, it will allow FireFox getting full access to the system clipboard.`); | ||
} | ||
throw new Error('Failed reading clipboard content.'); | ||
} | ||
} | ||
if (permission.state === 'denied') { | ||
// most likely, the user intentionally denied the access | ||
this.messageService.error("Access to the clipboard is denied. Check your browser's permission."); | ||
throw new Error('Access to the clipboard is denied.'); | ||
} | ||
return this.getClipboardAPI().readText(); | ||
} | ||
|
||
async writeText(value: string): Promise<void> { | ||
let permission; | ||
try { | ||
permission = await this.queryPermission('clipboard-write'); | ||
} catch (e) { | ||
this.logger.error('Failed checking a clipboard-write permission.', e); | ||
// in FireFox, Clipboard API isn't gated with the permissions | ||
try { | ||
await this.getClipboardAPI().writeText(value); | ||
return; | ||
} catch (e) { | ||
this.logger.error('Failed writing to the clipboard.', e); | ||
if (isFirefox) { | ||
this.messageService.warn(`Clipboard API is not available. | ||
It can be enabled by 'dom.events.testing.asyncClipboard' preference on 'about:config' page. Then reload Theia. | ||
Note, it will allow FireFox getting full access to the system clipboard.`); | ||
} | ||
throw new Error('Failed writing the the clipboard.'); | ||
} | ||
} | ||
if (permission.state === 'denied') { | ||
// most likely, the user intentionally denied the access | ||
this.messageService.error("Access to the clipboard is denied. Check your browser's permission."); | ||
throw new Error('Access to the clipboard is denied.'); | ||
} | ||
return this.getClipboardAPI().writeText(value); | ||
} | ||
|
||
protected async queryPermission(name: string): Promise<PermissionStatus> { | ||
if ('permissions' in navigator) { | ||
return (<NavigatorPermissions>navigator['permissions']).query({ name: name }); | ||
} | ||
throw new Error('Permissions API unavailable'); | ||
} | ||
|
||
protected getClipboardAPI(): NavigatorClipboard { | ||
if ('clipboard' in navigator) { | ||
return (<NavigatorClipboard>navigator['clipboard']); | ||
} | ||
throw new Error('Async Clipboard API unavailable'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 RedHat and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { MaybePromise } from '../common/types'; | ||
|
||
export const ClipboardService = Symbol('ClipboardService'); | ||
export interface ClipboardService { | ||
readText(): MaybePromise<string>; | ||
writeText(value: string): MaybePromise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/core/src/electron-browser/electron-clipboard-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 RedHat and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { clipboard } from 'electron'; | ||
import { injectable } from 'inversify'; | ||
import { ClipboardService } from '../browser/clipboard-service'; | ||
|
||
@injectable() | ||
export class ElectronClipboardService implements ClipboardService { | ||
|
||
readText(): string { | ||
return clipboard.readText(); | ||
} | ||
|
||
writeText(value: string): void { | ||
clipboard.writeText(value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 RedHat and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { interfaces } from 'inversify'; | ||
import { ClipboardMain } from '../../common'; | ||
import { ClipboardService } from '@theia/core/lib/browser/clipboard-service'; | ||
|
||
export class ClipboardMainImpl implements ClipboardMain { | ||
|
||
protected readonly clipboardService: ClipboardService; | ||
|
||
constructor(container: interfaces.Container) { | ||
this.clipboardService = container.get(ClipboardService); | ||
} | ||
|
||
async $readText(): Promise<string> { | ||
const result = await this.clipboardService.readText(); | ||
return result; | ||
} | ||
|
||
async $writeText(value: string): Promise<void> { | ||
await this.clipboardService.writeText(value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 RedHat and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import * as theia from '@theia/plugin'; | ||
import { RPCProtocol } from '../common/rpc-protocol'; | ||
import { PLUGIN_RPC_CONTEXT, ClipboardMain } from '../common'; | ||
|
||
export class ClipboardExt implements theia.Clipboard { | ||
|
||
protected readonly proxy: ClipboardMain; | ||
|
||
constructor(rpc: RPCProtocol) { | ||
this.proxy = rpc.getProxy(PLUGIN_RPC_CONTEXT.CLIPBOARD_MAIN); | ||
} | ||
|
||
readText(): Promise<string> { | ||
return this.proxy.$readText(); | ||
} | ||
|
||
writeText(value: string): Promise<void> { | ||
return this.proxy.$writeText(value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.