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

Commit

Permalink
Merge pull request #10929 from NejcZdovc/issue-10864-18
Browse files Browse the repository at this point in the history
[0.18.x] handle an array of permission types
  • Loading branch information
bbondy authored Sep 13, 2017
2 parents 76fa610 + 29010cc commit 0c25ca2
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 74 deletions.
140 changes: 72 additions & 68 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ function registerPermissionHandler (session, partition) {
const isPrivate = module.exports.isPrivate(partition)
// Keep track of per-site permissions granted for this session.
let permissions = null
session.setPermissionRequestHandler((origin, mainFrameUrl, permission, cb) => {
session.setPermissionRequestHandler((origin, mainFrameUrl, permissionTypes, cb) => {
if (!permissions) {
permissions = {
media: {
Expand Down Expand Up @@ -395,34 +395,7 @@ function registerPermissionHandler (session, partition) {
}
}

if (!permissions[permission]) {
console.log('WARNING: got unregistered permission request', permission)
cb(false)
return
}

// The Torrent Viewer extension is always allowed to show fullscreen media
if (permission === 'fullscreen' &&
origin.startsWith('chrome-extension://' + config.torrentExtensionId)) {
cb(true)
return
}

// Always allow fullscreen if setting is ON
const alwaysAllowFullscreen = module.exports.alwaysAllowFullscreen() === fullscreenOption.ALWAYS_ALLOW
if (permission === 'fullscreen' && alwaysAllowFullscreen) {
cb(true)
return
}

// The Brave extension and PDFJS are always allowed to open files in an external app
if (permission === 'openExternal' && (
origin.startsWith('chrome-extension://' + config.PDFJSExtensionId) ||
origin.startsWith('chrome-extension://' + config.braveExtensionId))) {
cb(true)
return
}

// TODO(bridiver) - the permission handling should be converted to an action because we should never call `appStore.getState()`
// Check whether there is a persistent site setting for this host
const appState = appStore.getState()
let settings
Expand Down Expand Up @@ -450,48 +423,79 @@ function registerPermissionHandler (session, partition) {
tempSettings = siteSettings.getSiteSettingsForURL(appState.get('temporarySiteSettings'), origin)
}

const permissionName = permission + 'Permission'
let isAllowed
if (settings) {
isAllowed = settings.get(permissionName)
}
// Private tabs inherit settings from normal tabs, but not vice versa.
if (isPrivate && tempSettings) {
isAllowed = tempSettings.get(permissionName)
}
if (typeof isAllowed === 'boolean') {
cb(isAllowed)
return
}

const message = locale.translation('permissionMessage').replace(/{{\s*host\s*}}/, origin).replace(/{{\s*permission\s*}}/, permissions[permission].action)
let response = []
for (let i = 0; i < permissionTypes.length; i++) {
const permission = permissionTypes[i]
const alwaysAllowFullscreen = module.exports.alwaysAllowFullscreen() === fullscreenOption.ALWAYS_ALLOW
if (!permissions[permission]) {
console.warn('WARNING: got unregistered permission request', permission)
response.push(false)
} else if (permission === 'fullscreen' &&
// The Torrent Viewer extension is always allowed to show fullscreen media
origin.startsWith('chrome-extension://' + config.torrentExtensionId)) {
response.push(true)
} else if (permission === 'fullscreen' && alwaysAllowFullscreen) {
// Always allow fullscreen if setting is ON
response.push(true)
} else if (permission === 'openExternal' && (
// The Brave extension and PDFJS are always allowed to open files in an external app
origin.startsWith('chrome-extension://' + config.PDFJSExtensionId) ||
origin.startsWith('chrome-extension://' + config.braveExtensionId))) {
response.push(true)
} else {
const permissionName = permission + 'Permission'
let isAllowed
if (settings) {
isAllowed = settings.get(permissionName)
}
// Private tabs inherit settings from normal tabs, but not vice versa.
if (isPrivate && tempSettings) {
isAllowed = tempSettings.get(permissionName)
}
if (typeof isAllowed === 'boolean') {
response.push(isAllowed)
}
}

// If this is a duplicate, clear the previous callback and use the new one
if (permissionCallbacks[message]) {
permissionCallbacks[message](0, false)
}
// Display 'Brave Browser' if the origin is null; ex: when a mailto: link
// is opened in a new tab via right-click
const message = locale.translation('permissionMessage').replace(/{{\s*host\s*}}/, origin || 'Brave Browser').replace(/{{\s*permission\s*}}/, permissions[permission].action)

appActions.showNotification({
buttons: [
{text: locale.translation('deny')},
{text: locale.translation('allow')}
],
frameOrigin: getOrigin(mainFrameUrl),
options: {
persist: true
},
message
})
// If this is a duplicate, clear the previous callback and use the new one
if (permissionCallbacks[message]) {
permissionCallbacks[message](0, false)
}

permissionCallbacks[message] = (buttonIndex, persist) => {
permissionCallbacks[message] = null
// hide the notification if this was triggered automatically
appActions.hideNotification(message)
const result = !!(buttonIndex)
cb(result)
if (persist) {
// remember site setting for this host
appActions.changeSiteSetting(origin, permission + 'Permission', result, isPrivate)
appActions.showNotification({
buttons: [
{text: locale.translation('deny')},
{text: locale.translation('allow')}
],
frameOrigin: getOrigin(mainFrameUrl),
options: {
persist: !!origin,
index: i
},
message
})

// Use a closure here for the index instead of passing an index to the
// function because ipcMain.on(messages.NOTIFICATION_RESPONSE above
// calls into the callback without knowing an index.
const index = i
permissionCallbacks[message] = (buttonIndex, persist) => {
// hide the notification if this was triggered automatically
appActions.hideNotification(message)
const result = !!(buttonIndex)
response[index] = result
if (persist) {
// remember site setting for this host
appActions.changeSiteSetting(origin, permission + 'Permission', result, isPrivate)
}
if (response.length === permissionTypes.length) {
permissionCallbacks[message] = null
cb(response)
}
}
}
})
Expand Down
20 changes: 14 additions & 6 deletions app/renderer/components/main/notificationBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ const globalStyles = require('../styles/global')

class NotificationItem extends ImmutableComponent {
clickHandler (buttonIndex, e) {
const nonce = this.props.detail.get('options').get('nonce')
const nonce = this.props.detail.getIn(['options', 'nonce'])
const index = this.props.detail.getIn(['options', 'index'])
if (nonce) {
ipc.emit(messages.NOTIFICATION_RESPONSE + nonce, {},
this.props.detail.get('message'),
buttonIndex, this.checkbox ? this.checkbox.checked : false)
ipc.send(
messages.NOTIFICATION_RESPONSE + nonce, {},
this.props.detail.get('message'),
buttonIndex, this.checkbox ? this.checkbox.checked : false,
index
)
} else {
ipc.send(messages.NOTIFICATION_RESPONSE, this.props.detail.get('message'),
buttonIndex, this.checkbox ? this.checkbox.checked : false)
ipc.send(
messages.NOTIFICATION_RESPONSE,
this.props.detail.get('message'),
buttonIndex, this.checkbox ? this.checkbox.checked : false,
index
)
}
}

Expand Down

0 comments on commit 0c25ca2

Please sign in to comment.