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

Commit a0b5d9d

Browse files
committed
Autoplay refactoring
fixes #9143 fixes #9008 fixes #9171 Auditors: @bsclifton, @bridiver, @cezaraugusto Test Plan: Covered by automatic test Specific STR for #9171: 1. Turn on autoplay to `always ask` and make sure there is no any allow autoplay permissions 2. Go to https://youtu.be/g_6yBZKj-eo and don't click on notification bar 3. Open another tab and go to https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_video_autoplay and still don't click on notification bar 4. Go back the youtube tab you will find out the notification bar is non clickable 5. Go to the w3schools tab and it will also be non clickable
1 parent 18acc2a commit a0b5d9d

File tree

8 files changed

+750
-227
lines changed

8 files changed

+750
-227
lines changed

app/browser/reducers/autoplayReducer.js

+48-26
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,26 @@
77
const appConstants = require('../../../js/constants/appConstants')
88
const {makeImmutable} = require('../../common/state/immutableUtil')
99
const {ipcMain, webContents} = require('electron')
10-
const AppStore = require('../../../js/stores/appStore')
1110
const siteSettings = require('../../../js/state/siteSettings')
12-
const settings = require('../../../js/constants/settings')
1311
const appActions = require('../../../js/actions/appActions')
1412
const {getOrigin} = require('../../../js/state/siteUtil')
1513
const locale = require('../../locale')
1614
const messages = require('../../../js/constants/messages')
17-
const getSetting = require('../../../js/settings').getSetting
18-
const {autoplayOption} = require('../../common/constants/settingsEnums')
1915

20-
const showAutoplayMessageBox = (tabId) => {
16+
let notificationCallbacks = []
17+
18+
const showAutoplayMessageBox = (state, tabId) => {
2119
const tab = webContents.fromTabID(tabId)
2220
if (!tab || tab.isDestroyed()) {
2321
return
2422
}
2523
const location = tab.getURL()
2624
const origin = getOrigin(location)
27-
if (getSetting(settings.AUTOPLAY_MEDIA) === autoplayOption.ALWAYS_ALLOW) {
28-
appActions.changeSiteSetting(origin, 'autoplay', true)
29-
return
30-
}
31-
const originSettings = siteSettings.getSiteSettingsForURL(AppStore.getState().get('siteSettings'), origin)
25+
const originSettings = siteSettings.getSiteSettingsForURL(state.get('siteSettings'), origin)
3226
if (originSettings && originSettings.get('autoplay') === false) {
3327
return
3428
}
29+
3530
const message = locale.translation('allowAutoplay', {origin})
3631

3732
appActions.showNotification({
@@ -46,33 +41,60 @@ const showAutoplayMessageBox = (tabId) => {
4641
}
4742
})
4843

49-
ipcMain.once(messages.NOTIFICATION_RESPONSE, (e, msg, buttonIndex, persist) => {
50-
if (msg === message) {
51-
appActions.hideNotification(message)
52-
if (buttonIndex === 0) {
53-
appActions.changeSiteSetting(origin, 'autoplay', true)
54-
if (tab && !tab.isDestroyed()) {
55-
tab.reload()
56-
tab.on('destroyed', function temporaryAllow (e) {
57-
if (!persist) {
58-
appActions.removeSiteSetting(origin, 'autoplay')
44+
if (!notificationCallbacks[tabId]) {
45+
notificationCallbacks[tabId] = (e, msg, buttonIndex, persist) => {
46+
if (msg === message) {
47+
appActions.hideNotification(message)
48+
if (buttonIndex === 0) {
49+
appActions.changeSiteSetting(origin, 'autoplay', true)
50+
if (tab && !tab.isDestroyed()) {
51+
tab.reload()
52+
const temporaryAllow = (e) => {
53+
tab.removeListener('media-started-playing', temporaryAllow)
54+
if (!persist) {
55+
appActions.removeSiteSetting(origin, 'autoplay')
56+
}
5957
}
60-
})
58+
tab.on('media-started-playing', temporaryAllow)
59+
}
60+
} else {
61+
if (persist) {
62+
appActions.changeSiteSetting(origin, 'autoplay', false)
63+
}
6164
}
62-
} else {
63-
if (persist) {
64-
appActions.changeSiteSetting(origin, 'autoplay', false)
65+
if (notificationCallbacks[tabId]) {
66+
ipcMain.removeListener(messages.NOTIFICATION_RESPONSE, notificationCallbacks[tabId])
67+
delete notificationCallbacks[tabId]
6568
}
6669
}
6770
}
68-
})
71+
ipcMain.on(messages.NOTIFICATION_RESPONSE, notificationCallbacks[tabId])
72+
}
73+
}
74+
75+
const hideAutoplayMessageBox = (tabId) => {
76+
const tab = webContents.fromTabID(tabId)
77+
if (!tab || tab.isDestroyed()) {
78+
return
79+
}
80+
const location = tab.getURL()
81+
const origin = getOrigin(location)
82+
const message = locale.translation('allowAutoplay', {origin})
83+
appActions.hideNotification(message)
84+
if (notificationCallbacks[tabId]) {
85+
ipcMain.removeListener(messages.NOTIFICATION_RESPONSE, notificationCallbacks[tabId])
86+
delete notificationCallbacks[tabId]
87+
}
6988
}
7089

7190
const autoplayReducer = (state, action, immutableAction) => {
7291
action = immutableAction || makeImmutable(action)
7392
switch (action.get('actionType')) {
7493
case appConstants.APP_AUTOPLAY_BLOCKED:
75-
showAutoplayMessageBox(action.get('tabId'))
94+
showAutoplayMessageBox(state, action.get('tabId'))
95+
break
96+
case appConstants.APP_AUTOPLAY_DISMISSED:
97+
hideAutoplayMessageBox(action.get('tabId'))
7698
break
7799
}
78100
return state

app/renderer/components/frame/frame.js

+1
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ class Frame extends React.Component {
779779
if (this.frame.isEmpty()) {
780780
return
781781
}
782+
appActions.autoplayDismissed(this.props.tabId)
782783
windowActions.setAudioPlaybackActive(this.frame, true)
783784
})
784785
this.webview.addEventListener('media-paused', ({title}) => {

js/actions/appActions.js

+11
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,17 @@ const appActions = {
12921292
})
12931293
},
12941294

1295+
/**
1296+
* Notifies autoplay notification can be dismissed
1297+
* @param {number} tabId - Tab id of current frame
1298+
*/
1299+
autoplayDismissed: function (tabId) {
1300+
dispatch({
1301+
actionType: appConstants.APP_AUTOPLAY_DISMISSED,
1302+
tabId
1303+
})
1304+
},
1305+
12951306
/**
12961307
* Handle 'save-password' event from muon
12971308
*/

js/constants/appConstants.js

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ const appConstants = {
125125
APP_ON_GO_BACK_LONG: _,
126126
APP_ON_GO_FORWARD_LONG: _,
127127
APP_AUTOPLAY_BLOCKED: _,
128+
APP_AUTOPLAY_DISMISSED: _,
128129
APP_SAVE_PASSWORD: _,
129130
APP_UPDATE_PASSWORD: _,
130131
APP_ADD_PASSWORD: _, /** @param {Object} passwordDetail */

js/state/contentSettings.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const urlParse = require('../../app/common/urlParse')
1717
const siteSettings = require('./siteSettings')
1818
const {registerUserPrefs} = require('./userPrefs')
1919
const {getSetting} = require('../settings')
20+
const {autoplayOption} = require('../../app/common/constants/settingsEnums')
2021
const {getFlashResourceId} = require('../flash')
2122
const net = require('net')
2223

@@ -70,7 +71,7 @@ const getDefaultUserPrefContentSettings = (braveryDefaults, appSettings, appConf
7071
braveryDefaults = makeImmutable(braveryDefaults)
7172
return Immutable.fromJS({
7273
autoplay: [{
73-
setting: 'block',
74+
setting: getSetting(settings.AUTOPLAY_MEDIA) === autoplayOption.ALWAYS_ALLOW ? 'allow' : 'block',
7475
primaryPattern: '*'
7576
}],
7677
cookies: getDefault3rdPartyStorageSettings(braveryDefaults, appSettings, appConfig),

0 commit comments

Comments
 (0)