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

block drag and drop data from Brave inside webviews #10589

Merged
merged 1 commit into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions app/extensions/brave/content/scripts/inputHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,34 @@ document.addEventListener('keydown', (e /*: Event*/) => {
break
}
})

function hasBraveDragData (dataTransfer) {
if (!dataTransfer || !dataTransfer.types) {
return false
}
for (let i = 0; i < dataTransfer.types.length; i++) {
let type = dataTransfer.types[i]
if (type && type.startsWith('application/x-brave-')) {
return true
}
}
return false
}

function blockDndData (e) {
if (hasBraveDragData(e.dataTransfer)) {
// Block drag data from the Brave UI
try {
e.dataTransfer.dropEffect = 'none'
} catch (e) {}
e.preventDefault()
e.stopPropagation()
return false
}
return true
}

window.addEventListener('dragover', blockDndData, true)
window.addEventListener('dragenter', blockDndData, true)
window.addEventListener('dragleave', blockDndData, true)
window.addEventListener('drop', blockDndData, true)
13 changes: 9 additions & 4 deletions app/renderer/components/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const {isSourceAboutUrl} = require('../../../../js/lib/appUrlUtil')
const {getCurrentWindowId, isMaximized, isFocused, isFullScreen} = require('../../currentWindow')
const platformUtil = require('../../../common/lib/platformUtil')
const urlUtil = require('../../../../js/lib/urlutil')
const {hasBraveDragData} = require('../../../../js/dndData')
const isDarwin = platformUtil.isDarwin()
const isWindows = platformUtil.isWindows()
const isLinux = platformUtil.isLinux()
Expand Down Expand Up @@ -445,8 +446,10 @@ class Main extends React.Component {

// disable dnd by default
window.addEventListener('dragover', function (event) {
// allow webviews to handle dnd
if (event.target.tagName === 'WEBVIEW') {
// allow webviews to handle dnd as long as the dragged object isn't from
// Brave itself
if (event.target.tagName === 'WEBVIEW' &&
!hasBraveDragData(event.dataTransfer)) {
return true
}
event.dataTransfer.dropEffect = 'none'
Expand All @@ -455,8 +458,10 @@ class Main extends React.Component {
}, true)

window.addEventListener('drop', function (event) {
// allow webviews to handle dnd
if (event.target.tagName === 'WEBVIEW') {
// allow webviews to handle dnd as long as the dragged object isn't from
// Brave itself
if (event.target.tagName === 'WEBVIEW' &&
!hasBraveDragData(event.dataTransfer)) {
return true
}
event.preventDefault()
Expand Down
19 changes: 17 additions & 2 deletions js/dndData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,33 @@

const Immutable = require('immutable')

const braveDragTypePrefix = 'application/x-brave-'

module.exports.hasDragData = (dataTransfer, dragType) => {
return dataTransfer.types.includes(`application/x-brave-${dragType}`)
return dataTransfer.types.includes(`${braveDragTypePrefix}${dragType}`)
}

module.exports.getDragData = (dataTransfer, dragType) => {
const data = dataTransfer.getData(`application/x-brave-${dragType}`)
const data = dataTransfer.getData(`${braveDragTypePrefix}${dragType}`)
if (!data) {
return undefined
}
return Immutable.fromJS(JSON.parse(data))
}

module.exports.hasBraveDragData = (dataTransfer) => {
if (!dataTransfer || !dataTransfer.types) {
return false
}
for (let i = 0; i < dataTransfer.types.length; i++) {
let type = dataTransfer.types[i]
if (type && type.startsWith(braveDragTypePrefix)) {
return true
}
}
return false
}

module.exports.setupDataTransferURL = (dataTransfer, location, title) => {
dataTransfer.setData('text/plain', location)
dataTransfer.setData('text/uri-list', location)
Expand Down
25 changes: 25 additions & 0 deletions test/unit/dndDataTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* 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/. */
/* global describe, it */

const dndData = require('../../js/dndData')
const assert = require('assert')

describe('dndData', function () {
describe('hasBraveDragData', function () {
it('returns false for empty transfer', () => {
assert.equal(dndData.hasBraveDragData(), false)
assert.equal(dndData.hasBraveDragData({types: []}), false)
assert.equal(dndData.hasBraveDragData({}), false)
})
it('returns false for regular transfer', () => {
assert.equal(dndData.hasBraveDragData({types: ['text/plain']}), false)
assert.equal(dndData.hasBraveDragData({types: ['text/plain', 'text/html']}), false)
})
it('returns true for brave transfer', () => {
assert.equal(dndData.hasBraveDragData({types: ['application/x-brave-tab']}), true)
assert.equal(dndData.hasBraveDragData({types: ['text/plain', 'text/uri-list', 'application/x-brave-bookmark']}), true)
})
})
})