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

Commit

Permalink
handle an array of permission types
Browse files Browse the repository at this point in the history
fix #10864
requires muon 4.3.19+
  • Loading branch information
bridiver committed Sep 11, 2017
1 parent c7a4e13 commit cd968b7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 72 deletions.
142 changes: 73 additions & 69 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,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 @@ -419,34 +419,7 @@ function registerPermissionHandler (session, partition) {
}
}

if (!permissions[permission]) {
console.warn('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 @@ -474,50 +447,81 @@ 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
}
let response = []
for (let i = 0; i < permissionTypes.length; i++) {
const permission = permissionTypes[i]
if (!permissions[permission]) {
console.warn('WARNING: got unregistered permission request', permission)
response.push(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)
// The Torrent Viewer extension is always allowed to show fullscreen media
if (permission === 'fullscreen' &&
origin.startsWith('chrome-extension://' + config.torrentExtensionId)) {
response.push(true)
}

// If this is a duplicate, clear the previous callback and use the new one
if (permissionCallbacks[message]) {
permissionCallbacks[message](0, false)
}
// Always allow fullscreen if setting is ON
const alwaysAllowFullscreen = module.exports.alwaysAllowFullscreen() === fullscreenOption.ALWAYS_ALLOW
if (permission === 'fullscreen' && alwaysAllowFullscreen) {
response.push(true)
}

appActions.showNotification({
buttons: [
{text: locale.translation('deny')},
{text: locale.translation('allow')}
],
frameOrigin: getOrigin(mainFrameUrl),
options: {
persist: !!origin
},
message
})
// 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))) {
response.push(true)
}

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)
}

// 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)

// If this is a duplicate, clear the previous callback and use the new one
if (permissionCallbacks[message]) {
permissionCallbacks[message](0, false, i)
}

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
})

permissionCallbacks[message] = (buttonIndex, persist, index) => {
// 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
9 changes: 6 additions & 3 deletions app/renderer/components/main/notificationItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ class NotificationItem extends React.Component {

clickHandler (buttonIndex) {
if (this.props.nonce) {
ipc.emit(
ipc.send(

This comment has been minimized.

Copy link
@bbondy

bbondy Sep 21, 2017

Member

This obviously needs a refactor but this was going to same process before because that's where the listener is. This changes it to go to the browser process now which doesn't listen for response with a nonce. I'll revert this change back to fix #11054

messages.NOTIFICATION_RESPONSE + this.props.nonce,
{},
this.props.message,
buttonIndex,
this.checkbox ? this.checkbox.checked : false
this.checkbox ? this.checkbox.checked : false,
this.props.index
)
} else {
ipc.send(
messages.NOTIFICATION_RESPONSE,
this.props.message,
buttonIndex,
this.checkbox ? this.checkbox.checked : false
this.checkbox ? this.checkbox.checked : false,
this.props.index
)
}
}
Expand Down Expand Up @@ -74,6 +76,7 @@ class NotificationItem extends React.Component {
props.advancedLink = notification.getIn(['options', 'advancedLink'])
props.persist = notification.getIn(['options', 'persist'])
props.nonce = notification.getIn(['options', 'nonce'])
props.index = notification.getIn(['options', 'index'])

return props
}
Expand Down

0 comments on commit cd968b7

Please sign in to comment.