diff --git a/app/browser/reducers/autoplayReducer.js b/app/browser/reducers/autoplayReducer.js new file mode 100644 index 00000000000..4e326e1526b --- /dev/null +++ b/app/browser/reducers/autoplayReducer.js @@ -0,0 +1,74 @@ +/* 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/. */ + +'use strict' + +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 appActions = require('../../../js/actions/appActions') +const {getOrigin} = require('../../../js/state/siteUtil') +const locale = require('../../locale') +const messages = require('../../../js/constants/messages') +const urlParse = require('../../common/urlParse') + +const showAutoplayMessageBox = (location, tabId) => { + const origin = getOrigin(location) + const originSettings = siteSettings.getSiteSettingsForURL(AppStore.getState().get('siteSettings'), origin) + if (originSettings && originSettings.get('noAutoplay') === true) { + return + } + const message = locale.translation('allowAutoplay', {origin}) + + appActions.showNotification({ + buttons: [ + {text: locale.translation('yes')}, + {text: locale.translation('no')} + ], + message, + frameOrigin: origin, + options: { + persist: true + } + }) + + ipcMain.once(messages.NOTIFICATION_RESPONSE, (e, msg, buttonIndex, persist) => { + if (msg === message) { + appActions.hideNotification(message) + let ruleKey = origin + const parsedUrl = urlParse(location) + if ((parsedUrl.protocol === 'https:' || parsedUrl.protocol === 'http:')) { + ruleKey = `https?://${parsedUrl.host}` + } + if (buttonIndex === 0) { + appActions.changeSiteSetting(ruleKey, 'noAutoplay', false) + + if (tabId) { + const tab = webContents.fromTabID(tabId) + if (tab && !tab.isDestroyed()) { + return tab.reload() + } + } + } else { + if (persist) { + appActions.changeSiteSetting(ruleKey, 'noAutoplay', true) + } + } + } + }) +} + +const autoplayReducer = (state, action, immutableAction) => { + action = immutableAction || makeImmutable(action) + switch (action.get('actionType')) { + case appConstants.APP_AUTOPLAY_BLOCKED: + showAutoplayMessageBox(action.get('location'), action.get('tabId')) + break + } + return state +} + +module.exports = autoplayReducer diff --git a/app/extensions/brave/locales/en-US/app.properties b/app/extensions/brave/locales/en-US/app.properties index 4bcb699d0f5..37df438e513 100644 --- a/app/extensions/brave/locales/en-US/app.properties +++ b/app/extensions/brave/locales/en-US/app.properties @@ -260,3 +260,4 @@ copied=Copied! connectionError=Server connection failed. Please make sure you are connected to the Internet. unknownError=Oops, something went wrong. browserActionButton.title={{name}} +allowAutoplay=Allow {{origin}} to autoplay media? diff --git a/app/extensions/brave/locales/en-US/bravery.properties b/app/extensions/brave/locales/en-US/bravery.properties index da711ef83dd..b95e1a314fb 100644 --- a/app/extensions/brave/locales/en-US/bravery.properties +++ b/app/extensions/brave/locales/en-US/bravery.properties @@ -26,4 +26,4 @@ httpReroutes={[plural(httpsUpgradeCount)]} httpReroutes[one]=HTTPS Upgrade httpReroutes[other]=HTTPS Upgrades editBraveryGlobalSettings=Edit default shield settingsā€¦ -allowAutoplay=Allow Autoplay Media +noAutoplay=Block Autoplay diff --git a/app/locale.js b/app/locale.js index 964a9d3f4d5..eaa5766bd18 100644 --- a/app/locale.js +++ b/app/locale.js @@ -251,7 +251,8 @@ var rendererIdentifiers = function () { 'closeFirefoxWarningOk', 'importSuccessOk', 'connectionError', - 'unknownError' + 'unknownError', + 'allowAutoplay' ] } diff --git a/docs/appActions.md b/docs/appActions.md index 007e5695e61..292525d89d1 100644 --- a/docs/appActions.md +++ b/docs/appActions.md @@ -1025,6 +1025,18 @@ because ESC was pressed. +### autoplayBlocked(location, tabId) + +Notifies autoplay has been blocked + +**Parameters** + +**location**: `string`, Location of current frame + +**tabId**: `number`, Tab id of current frame + + + * * * diff --git a/js/about/preferences.js b/js/about/preferences.js index 6de3009a341..7329b641269 100644 --- a/js/about/preferences.js +++ b/js/about/preferences.js @@ -49,7 +49,7 @@ const adInsertion = appConfig.resourceNames.AD_INSERTION const trackingProtection = appConfig.resourceNames.TRACKING_PROTECTION const httpsEverywhere = appConfig.resourceNames.HTTPS_EVERYWHERE const safeBrowsing = appConfig.resourceNames.SAFE_BROWSING -const autoplay = appConfig.resourceNames.AUTOPLAY +const noAutoplay = appConfig.resourceNames.NOAUTOPLAY const noScript = appConfig.resourceNames.NOSCRIPT const flash = appConfig.resourceNames.FLASH @@ -88,7 +88,8 @@ const braveryPermissionNames = { 'safeBrowsing': ['boolean'], 'httpsEverywhere': ['boolean'], 'fingerprintingProtection': ['boolean'], - 'noScript': ['boolean', 'number'] + 'noScript': ['boolean', 'number'], + 'noAutoplay': ['boolean'] } class GeneralTab extends ImmutableComponent { @@ -497,7 +498,7 @@ class ShieldsTab extends ImmutableComponent { this.onChangeAdControl = this.onChangeAdControl.bind(this) this.onToggleHTTPSE = this.onToggleSetting.bind(this, httpsEverywhere) this.onToggleSafeBrowsing = this.onToggleSetting.bind(this, safeBrowsing) - this.onToggleAutoplay = this.onToggleSetting.bind(this, autoplay) + this.onToggleNoAutoplay = this.onToggleSetting.bind(this, noAutoplay) this.onToggleNoScript = this.onToggleSetting.bind(this, noScript) } onChangeAdControl (e) { @@ -548,7 +549,7 @@ class ShieldsTab extends ImmutableComponent { - +