From a797f18b78e45b536d257052198432bc585deec6 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 6 Mar 2019 18:42:05 -0500 Subject: [PATCH] make clipboardRead optional permission --- src/browser/browserApi.ts | 28 +++++++++++++++++++++++++ src/manifest.json | 4 +++- src/popup/settings/options.component.ts | 24 +++++++++++++++++---- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index d1cd121522..a7255e0ef0 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -295,4 +295,32 @@ export class BrowserApi { return chrome.runtime.reload(); } } + + static hasPermission(permissionName: string): Promise { + return new Promise((resolve) => { + if (BrowserApi.isSafariApi) { + resolve(true); + } else { + chrome.permissions.contains({ + permissions: [permissionName], + }, (granted: boolean) => { + resolve(granted); + }); + } + }); + } + + static requestPermission(permissionName: string): Promise { + return new Promise((resolve) => { + if (BrowserApi.isSafariApi) { + resolve(true); + } else { + chrome.permissions.request({ + permissions: [permissionName], + }, (granted: boolean) => { + resolve(granted); + }); + } + }); + } } diff --git a/src/manifest.json b/src/manifest.json index 3e58fd4357..e4e6e89ab4 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -71,7 +71,6 @@ "contextMenus", "storage", "unlimitedStorage", - "clipboardRead", "clipboardWrite", "idle", "http://*/*", @@ -79,6 +78,9 @@ "webRequest", "webRequestBlocking" ], + "optional_permissions": [ + "clipboardRead" + ], "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", "commands": { "autofill_login": { diff --git a/src/popup/settings/options.component.ts b/src/popup/settings/options.component.ts index 072d04bc35..81cba9f086 100644 --- a/src/popup/settings/options.component.ts +++ b/src/popup/settings/options.component.ts @@ -16,6 +16,8 @@ import { TotpService } from 'jslib/abstractions/totp.service'; import { ConstantsService } from 'jslib/services/constants.service'; +import { BrowserApi } from '../../browser/browserApi'; + @Component({ selector: 'app-options', templateUrl: 'options.component.html', @@ -35,6 +37,7 @@ export class OptionsComponent implements OnInit { themeOptions: any[]; defaultUriMatch = UriMatchType.Domain; uriMatchOptions: any[]; + clearClipboardOriginalValue: number; clearClipboard: number; clearClipboardOptions: any[]; @@ -94,6 +97,7 @@ export class OptionsComponent implements OnInit { this.defaultUriMatch = defaultUriMatch == null ? UriMatchType.Domain : defaultUriMatch; this.clearClipboard = await this.storageService.get(ConstantsService.clearClipboardKey); + this.clearClipboardOriginalValue = this.clearClipboard; } async updateAddLoginNotification() { @@ -155,10 +159,22 @@ export class OptionsComponent implements OnInit { } async saveClearClipboard() { - await this.storageService.save(ConstantsService.clearClipboardKey, this.clearClipboard); - this.analytics.eventTrack.next({ - action: 'Set Clear Clipboard ' + (this.clearClipboard == null ? 'Disabled' : this.clearClipboard), - }); + if (!this.showClearClipboard) { + return; + } + let hasClipboardPermission = await BrowserApi.hasPermission('clipboardRead'); + if (!hasClipboardPermission) { + hasClipboardPermission = await BrowserApi.requestPermission('clipboardRead'); + } + if (hasClipboardPermission) { + this.clearClipboardOriginalValue = this.clearClipboard; + await this.storageService.save(ConstantsService.clearClipboardKey, this.clearClipboard); + this.analytics.eventTrack.next({ + action: 'Set Clear Clipboard ' + (this.clearClipboard == null ? 'Disabled' : this.clearClipboard), + }); + } else { + this.clearClipboard = this.clearClipboardOriginalValue; + } } private callAnalytics(name: string, enabled: boolean) {