-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
WebBrowser.tsx
145 lines (125 loc) · 3.9 KB
/
WebBrowser.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React, { Component } from 'react'
import { connect } from 'react-redux'
import cx from 'classnames'
import styles from './WebBrowser.css'
import { WEBVIEW_PARTITION } from 'constants/http'
import { WebControls } from 'renderer/components/browser/Controls'
import { sendMediaRequest } from 'renderer/lobby/actions/mediaPlayer'
import { absoluteUrl } from 'utils/appUrl'
import { IReactReduxProps } from 'types/redux-thunk'
const { ipcRenderer, remote } = chrome
const DEFAULT_URL = absoluteUrl('./browser/resources/homescreen.html')
interface IProps {
className?: string
initialUrl?: string
onClose?: () => void
devTools?: boolean
}
type PrivateProps = IProps & IReactReduxProps
export class _WebBrowser extends Component<PrivateProps> {
private webview?: Electron.WebviewTag | null
private webContents!: Electron.webContents
private controls?: WebControls | null
private hasSetupControls?: boolean
private get initialUrl() {
return this.props.initialUrl || DEFAULT_URL
}
componentDidMount(): void {
ipcRenderer.on('command', this.dispatchCommand)
}
componentWillUnmount(): void {
ipcRenderer.removeListener('command', this.dispatchCommand)
;(window as any).WEBBROWSER = null
}
private dispatchCommand = (sender: Electron.WebContents, cmd: string) => {
if (!this.webview) {
return
}
switch (cmd) {
case 'window:close':
if (this.props.onClose) {
this.props.onClose()
}
break
case 'window:focus-url':
if (this.controls) {
this.controls.focusURL()
}
break
case 'window:history-prev':
if (this.webContents.canGoBack()) {
this.webContents.goBack()
}
break
case 'window:history-next':
if (this.webContents.canGoForward()) {
this.webContents.goForward()
}
break
}
}
private setupControls() {
if (!this.hasSetupControls && this.controls && this.webview && this.webContents) {
this.controls.setWebview(this.webview, this.webContents)
this.hasSetupControls = true
}
}
private setupWebview = (webview: Electron.WebviewTag | null): void => {
this.webview = webview
if (this.webview) {
// this.webview.addEventListener('new-window', e => {
// TODO: security???
// this.webview!.loadURL(e.url);
// });
const wv = this.webview as any
wv.addEventListener('did-attach', (e: any) => {
;(remote as any).getWebContents(e.tabId, (webContents: Electron.WebContents) => {
this.webContents = webContents
this.setupControls()
if (process.env.NODE_ENV === 'development') {
const win = window as any
win.WEBBROWSER = webContents
}
})
})
} else {
this.webContents = undefined as any
}
}
render(): JSX.Element {
return (
<div className={cx(styles.container, this.props.className)}>
<WebControls
ref={el => {
this.controls = el
this.setupControls()
}}
initialUrl={this.initialUrl}
onClose={this.props.onClose}
onRequestUrl={url => {
this.props.dispatch(sendMediaRequest(url, 'browser'))
if (this.props.onClose) {
this.props.onClose()
}
}}
devTools={this.props.devTools}
/>
{this.renderContent()}
</div>
)
}
private renderContent() {
return React.createElement('webview', {
is: 'is',
ref: this.setupWebview,
src: this.initialUrl,
class: styles.content,
/* Some website embeds are disabled without an HTTP referrer */
httpreferrer: 'http://mediaplayer.samuelmaddock.com/',
plugins: 'true',
partition: WEBVIEW_PARTITION,
allowtransparency: true
})
}
}
export const WebBrowser = connect()(_WebBrowser) as React.ComponentClass<IProps>