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

Autoplay refactoring #10178

Merged
merged 1 commit into from
Jul 31, 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
74 changes: 48 additions & 26 deletions app/browser/reducers/autoplayReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,26 @@
const appConstants = require('../../../js/constants/appConstants')
const {makeImmutable} = require('../../common/state/immutableUtil')
const {ipcMain, webContents} = require('electron')
const AppStore = require('../../../js/stores/appStore')
const siteSettings = require('../../../js/state/siteSettings')
const settings = require('../../../js/constants/settings')
const appActions = require('../../../js/actions/appActions')
const {getOrigin} = require('../../../js/state/siteUtil')
const locale = require('../../locale')
const messages = require('../../../js/constants/messages')
const getSetting = require('../../../js/settings').getSetting
const {autoplayOption} = require('../../common/constants/settingsEnums')

const showAutoplayMessageBox = (tabId) => {
let notificationCallbacks = []

const showAutoplayMessageBox = (state, tabId) => {
const tab = webContents.fromTabID(tabId)
if (!tab || tab.isDestroyed()) {
return
}
const location = tab.getURL()
const origin = getOrigin(location)
if (getSetting(settings.AUTOPLAY_MEDIA) === autoplayOption.ALWAYS_ALLOW) {
appActions.changeSiteSetting(origin, 'autoplay', true)
return
}
const originSettings = siteSettings.getSiteSettingsForURL(AppStore.getState().get('siteSettings'), origin)
const originSettings = siteSettings.getSiteSettingsForURL(state.get('siteSettings'), origin)
if (originSettings && originSettings.get('autoplay') === false) {
return
}

const message = locale.translation('allowAutoplay', {origin})

appActions.showNotification({
Expand All @@ -46,33 +41,60 @@ const showAutoplayMessageBox = (tabId) => {
}
})

ipcMain.once(messages.NOTIFICATION_RESPONSE, (e, msg, buttonIndex, persist) => {
if (msg === message) {
appActions.hideNotification(message)
if (buttonIndex === 0) {
appActions.changeSiteSetting(origin, 'autoplay', true)
if (tab && !tab.isDestroyed()) {
tab.reload()
tab.on('destroyed', function temporaryAllow (e) {
if (!persist) {
appActions.removeSiteSetting(origin, 'autoplay')
if (!notificationCallbacks[tabId]) {
notificationCallbacks[tabId] = (e, msg, buttonIndex, persist) => {
if (msg === message) {
appActions.hideNotification(message)
if (buttonIndex === 0) {
appActions.changeSiteSetting(origin, 'autoplay', true)
if (tab && !tab.isDestroyed()) {
tab.reload()
const temporaryAllow = (e) => {
tab.removeListener('media-started-playing', temporaryAllow)
if (!persist) {
appActions.removeSiteSetting(origin, 'autoplay')
}
}
})
tab.on('media-started-playing', temporaryAllow)
}
} else {
if (persist) {
appActions.changeSiteSetting(origin, 'autoplay', false)
}
}
} else {
if (persist) {
appActions.changeSiteSetting(origin, 'autoplay', false)
if (notificationCallbacks[tabId]) {
ipcMain.removeListener(messages.NOTIFICATION_RESPONSE, notificationCallbacks[tabId])
delete notificationCallbacks[tabId]
}
}
}
})
ipcMain.on(messages.NOTIFICATION_RESPONSE, notificationCallbacks[tabId])
}
}

const hideAutoplayMessageBox = (tabId) => {
const tab = webContents.fromTabID(tabId)
if (!tab || tab.isDestroyed()) {
return
}
const location = tab.getURL()
const origin = getOrigin(location)
const message = locale.translation('allowAutoplay', {origin})
appActions.hideNotification(message)
if (notificationCallbacks[tabId]) {
ipcMain.removeListener(messages.NOTIFICATION_RESPONSE, notificationCallbacks[tabId])
delete notificationCallbacks[tabId]
}
}

const autoplayReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
switch (action.get('actionType')) {
case appConstants.APP_AUTOPLAY_BLOCKED:
showAutoplayMessageBox(action.get('tabId'))
showAutoplayMessageBox(state, action.get('tabId'))
break
case appConstants.APP_AUTOPLAY_DISMISSED:
hideAutoplayMessageBox(action.get('tabId'))
break
}
return state
Expand Down
1 change: 1 addition & 0 deletions app/renderer/components/frame/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ class Frame extends React.Component {
if (this.frame.isEmpty()) {
return
}
appActions.autoplayDismissed(this.props.tabId)
windowActions.setAudioPlaybackActive(this.frame, true)
})
this.webview.addEventListener('media-paused', ({title}) => {
Expand Down
11 changes: 11 additions & 0 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,17 @@ const appActions = {
})
},

/**
* Notifies autoplay notification can be dismissed
* @param {number} tabId - Tab id of current frame
*/
autoplayDismissed: function (tabId) {
dispatch({
actionType: appConstants.APP_AUTOPLAY_DISMISSED,
tabId
})
},

/**
* Handle 'save-password' event from muon
*/
Expand Down
1 change: 1 addition & 0 deletions js/constants/appConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const appConstants = {
APP_ON_GO_BACK_LONG: _,
APP_ON_GO_FORWARD_LONG: _,
APP_AUTOPLAY_BLOCKED: _,
APP_AUTOPLAY_DISMISSED: _,
APP_SAVE_PASSWORD: _,
APP_UPDATE_PASSWORD: _,
APP_ADD_PASSWORD: _, /** @param {Object} passwordDetail */
Expand Down
3 changes: 2 additions & 1 deletion js/state/contentSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const urlParse = require('../../app/common/urlParse')
const siteSettings = require('./siteSettings')
const {registerUserPrefs} = require('./userPrefs')
const {getSetting} = require('../settings')
const {autoplayOption} = require('../../app/common/constants/settingsEnums')
const {getFlashResourceId} = require('../flash')
const net = require('net')

Expand Down Expand Up @@ -70,7 +71,7 @@ const getDefaultUserPrefContentSettings = (braveryDefaults, appSettings, appConf
braveryDefaults = makeImmutable(braveryDefaults)
return Immutable.fromJS({
autoplay: [{
setting: 'block',
setting: getSetting(settings.AUTOPLAY_MEDIA) === autoplayOption.ALWAYS_ALLOW ? 'allow' : 'block',
primaryPattern: '*'
}],
cookies: getDefault3rdPartyStorageSettings(braveryDefaults, appSettings, appConfig),
Expand Down
Loading