7
7
const appConstants = require ( '../../../js/constants/appConstants' )
8
8
const { makeImmutable} = require ( '../../common/state/immutableUtil' )
9
9
const { ipcMain, webContents} = require ( 'electron' )
10
- const AppStore = require ( '../../../js/stores/appStore' )
11
10
const siteSettings = require ( '../../../js/state/siteSettings' )
12
- const settings = require ( '../../../js/constants/settings' )
13
11
const appActions = require ( '../../../js/actions/appActions' )
14
12
const { getOrigin} = require ( '../../../js/state/siteUtil' )
15
13
const locale = require ( '../../locale' )
16
14
const messages = require ( '../../../js/constants/messages' )
17
- const getSetting = require ( '../../../js/settings' ) . getSetting
18
- const { autoplayOption} = require ( '../../common/constants/settingsEnums' )
19
15
20
- const showAutoplayMessageBox = ( tabId ) => {
16
+ let notificationCallbacks = [ ]
17
+
18
+ const showAutoplayMessageBox = ( state , tabId ) => {
21
19
const tab = webContents . fromTabID ( tabId )
22
20
if ( ! tab || tab . isDestroyed ( ) ) {
23
21
return
24
22
}
25
23
const location = tab . getURL ( )
26
24
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 )
32
26
if ( originSettings && originSettings . get ( 'autoplay' ) === false ) {
33
27
return
34
28
}
29
+
35
30
const message = locale . translation ( 'allowAutoplay' , { origin} )
36
31
37
32
appActions . showNotification ( {
@@ -46,33 +41,60 @@ const showAutoplayMessageBox = (tabId) => {
46
41
}
47
42
} )
48
43
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
+ }
59
57
}
60
- } )
58
+ tab . on ( 'media-started-playing' , temporaryAllow )
59
+ }
60
+ } else {
61
+ if ( persist ) {
62
+ appActions . changeSiteSetting ( origin , 'autoplay' , false )
63
+ }
61
64
}
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 ]
65
68
}
66
69
}
67
70
}
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
+ }
69
88
}
70
89
71
90
const autoplayReducer = ( state , action , immutableAction ) => {
72
91
action = immutableAction || makeImmutable ( action )
73
92
switch ( action . get ( 'actionType' ) ) {
74
93
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' ) )
76
98
break
77
99
}
78
100
return state
0 commit comments