Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Fix clipboard no avail in renderer proces
Browse files Browse the repository at this point in the history
Auditors: @bsclifton

Fix #6302
  • Loading branch information
bbondy authored and cezaraugusto committed Dec 20, 2016
1 parent 970a9e8 commit 44ba341
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 16 deletions.
19 changes: 19 additions & 0 deletions app/browser/reducers/clipboardReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict'

const appConstants = require('../../../js/constants/appConstants')
const {clipboard} = require('electron')

const clipboardReducer = (state, action) => {
switch (action.actionType) {
case appConstants.APP_CLIPBOARD_TEXT_UPDATED:
clipboard.writeText(action.text)
break
}
return state
}

module.exports = clipboardReducer
10 changes: 10 additions & 0 deletions docs/appActions.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,16 @@ Dispatches a message when a download is being redownloaded



### clipboardTextCopied(text)

Dispatches a message when text is updated to the clipboard

**Parameters**

**text**: `string`, clipboard text which is copied




* * *

Expand Down
11 changes: 11 additions & 0 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,17 @@ const appActions = {
actionType: appConstants.APP_DOWNLOAD_REDOWNLOADED,
downloadId
})
},

/**
* Dispatches a message when text is updated to the clipboard
* @param {string} text - clipboard text which is copied
*/
clipboardTextCopied: function (text) {
AppDispatcher.dispatch({
actionType: appConstants.APP_CLIPBOARD_TEXT_UPDATED,
text
})
}
}

Expand Down
3 changes: 1 addition & 2 deletions js/components/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const UrlUtil = require('../lib/urlutil')
const messages = require('../constants/messages')
const contextMenus = require('../contextMenus')
const ipc = require('electron').ipcRenderer
const clipboard = require('electron').clipboard
const FullScreenWarning = require('./fullScreenWarning')
const debounce = require('../lib/debounce')
const getSetting = require('../settings').getSetting
Expand Down Expand Up @@ -514,7 +513,7 @@ class Frame extends ImmutableComponent {
case 'copy':
let selection = window.getSelection()
if (selection && selection.toString()) {
clipboard.writeText(selection.toString())
appActions.clipboardTextCopied(selection.toString())
} else {
this.webview.copy()
}
Expand Down
3 changes: 2 additions & 1 deletion js/constants/appConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ const appConstants = {
APP_DOWNLOAD_REDOWNLOADED: _,
APP_ALLOW_FLASH_ONCE: _,
APP_ALLOW_FLASH_ALWAYS: _,
APP_SHUTTING_DOWN: _
APP_SHUTTING_DOWN: _,
APP_CLIPBOARD_TEXT_UPDATED: _
}

module.exports = mapValuesByKeys(appConstants)
4 changes: 2 additions & 2 deletions js/contextMenus.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ const copyAddressMenuItem = (label, location) => {
label: locale.translation(label),
click: (item, focusedWindow) => {
if (focusedWindow && location) {
clipboard.writeText(location)
appActions.clipboardTextCopied(location)
}
}
}
Expand All @@ -858,7 +858,7 @@ const copyEmailAddressMenuItem = (location) => {
return {
label: locale.translation('copyEmailAddress'),
click: () => {
clipboard.writeText(location.substring('mailto:'.length, location.length))
appActions.clipboardTextCopied(location.substring('mailto:'.length, location.length))
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions js/stores/appStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const nativeImage = require('../../app/nativeImage')
const Filtering = require('../../app/filtering')
const basicAuth = require('../../app/browser/basicAuth')
const windows = require('../../app/browser/windows')
const downloadsReducer = require('../../app/browser/reducers/downloadsReducer')

// state helpers
const basicAuthState = require('../../app/common/state/basicAuthState')
Expand All @@ -43,8 +42,6 @@ const aboutNewTabState = require('../../app/common/state/aboutNewTabState')
const aboutHistoryState = require('../../app/common/state/aboutHistoryState')
const windowState = require('../../app/common/state/windowState')

const flashReducer = require('../../app/browser/reducers/flashReducer')
const tabsReducer = require('../../app/browser/reducers/tabsReducer')
const webtorrent = require('../../app/browser/webtorrent')

const isDarwin = process.platform === 'darwin'
Expand Down Expand Up @@ -353,16 +350,21 @@ function handleChangeSettingAction (settingKey, settingValue) {
}
}

const reducers = [
require('../../app/browser/reducers/downloadsReducer'),
require('../../app/browser/reducers/flashReducer'),
require('../../app/browser/reducers/tabsReducer'),
require('../../app/browser/reducers/clipboardReducer')
]

const handleAppAction = (action) => {
if (shuttingDown) {
return
}

const ledger = require('../../app/ledger')

appState = downloadsReducer(appState, action)
appState = flashReducer(appState, action)
appState = tabsReducer(appState, action)
appState = reducers.reduce((appState, reducer) => reducer(appState, action), appState)

switch (action.actionType) {
case appConstants.APP_SET_STATE:
Expand Down
41 changes: 41 additions & 0 deletions test/unit/app/browser/reducers/clipboardReducerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* global describe, it, before, after */
const mockery = require('mockery')
const sinon = require('sinon')
const Immutable = require('immutable')
const assert = require('assert')
const fakeElectron = require('../../../lib/fakeElectron')

const appConstants = require('../../../../../js/constants/appConstants')
require('../../../braveUnit')

describe('clipboardReducer', function () {
let clipboardReducer
before(function () {
mockery.enable({
warnOnReplace: false,
warnOnUnregistered: false,
useCleanCache: true
})
mockery.registerMock('electron', fakeElectron)
clipboardReducer = require('../../../../../app/browser/reducers/clipboardReducer')
})

after(function () {
mockery.disable()
})

describe('APP_CLIPBOARD_TEXT_UPDATED', function () {
before(function () {
this.spy = sinon.spy(fakeElectron.clipboard, 'writeText')
// Make sure clipboardReducer doesn't update state when text is updated
this.text = 'Mn = 2n − 1'
this.newState = clipboardReducer(Immutable.Map(), {actionType: appConstants.APP_CLIPBOARD_TEXT_UPDATED, text: this.text})
})
it('Does not modify state', function () {
assert(this.newState.isEmpty())
})
it('Copies text into electron.clipboard', function () {
assert(this.spy.withArgs(this.text).calledOnce)
})
})
})
6 changes: 1 addition & 5 deletions test/unit/app/browser/reducers/downloadsReducerTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global describe, it, before, after, beforeEach */
/* global describe, it, before, after */
const mockery = require('mockery')
const sinon = require('sinon')
const Immutable = require('immutable')
Expand Down Expand Up @@ -42,9 +42,6 @@ describe('downloadsReducer', function () {
downloadsReducer = require('../../../../../app/browser/reducers/downloadsReducer')
})

beforeEach(function () {
})

after(function () {
mockery.disable()
})
Expand Down Expand Up @@ -120,7 +117,6 @@ describe('downloadsReducer', function () {
describe('APP_DOWNLOAD_COPIED_TO_CLIPBOARD', function () {
it('copies the download URL to the clipboard', function () {
const spy = sinon.spy(fakeElectron.clipboard, 'writeText')
spy.withArgs(downloadUrl)
const oldState = oneDownloadWithState(IN_PROGRESS)
downloadsReducer(oldState, {actionType: appConstants.APP_DOWNLOAD_COPIED_TO_CLIPBOARD, downloadId: downloadId(oldState)})
assert(spy.withArgs(downloadUrl).calledOnce)
Expand Down

0 comments on commit 44ba341

Please sign in to comment.