forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeWebViewLayout.kt
More file actions
242 lines (205 loc) · 8.04 KB
/
SafeWebViewLayout.kt
File metadata and controls
242 lines (205 loc) · 8.04 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright (c) 2025 Brayan Oliveira <69634269+brayandso@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki.workarounds
import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import android.webkit.CookieManager
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.widget.FrameLayout
import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
import androidx.fragment.app.findFragment
import com.ichi2.anki.BuildConfig
import com.ichi2.anki.runCatchingWithReport
import timber.log.Timber
open class SafeWebViewLayout :
FrameLayout,
OnRenderProcessGoneListener {
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private var webView: WebView = createWebView()
var scrollBars: Int = webView.scrollBarStyle
set(value) {
webView.scrollBarStyle = value
field = value
}
protected open fun createWebView() = WebView(context)
init {
addView(webView, webViewLayoutParams)
}
val settings: WebSettings get() = webView.settings
@Suppress("DEPRECATION")
val scale get() = webView.scale
@MainThread
fun setWebViewClient(webViewClient: SafeWebViewClient) {
webViewClient.setOnRenderProcessGoneListener(this)
webView.webViewClient = webViewClient
}
@MainThread
fun setWebChromeClient(webChromeClient: WebChromeClient) {
webView.webChromeClient = webChromeClient
}
@MainThread
fun evaluateJavascript(
script: String,
resultCallback: ((String) -> Unit)? = null,
) = webView.evaluateJavascript(script) { callback ->
resultCallback?.invoke(callback)
}
@MainThread
fun addJavascriptInterface(
javascriptInterface: Any,
name: String,
) = webView.addJavascriptInterface(javascriptInterface, name)
@MainThread
fun loadUrl(url: String) = webView.loadUrl(url)
@MainThread
fun loadDataWithBaseURL(
baseUrl: String?,
data: String,
mimeType: String?,
encoding: String?,
historyUrl: String?,
) = webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl)
fun setAcceptThirdPartyCookies(accept: Boolean) = CookieManager.getInstance().setAcceptThirdPartyCookies(webView, accept)
@MainThread
fun goBack() = webView.goBack()
@MainThread
fun pageUp() = webView.pageUp(false)
@MainThread
fun pageDown() = webView.pageDown(false)
@MainThread
fun reload() = webView.reload()
@MainThread
fun focusOnWebView() = webView.requestFocus()
@MainThread
fun destroy() = webView.destroy()
@MainThread
fun scrollVerticallyBy(y: Int) {
if (webView.canScrollVertically(y)) {
webView.scrollBy(0, y)
}
}
@MainThread
fun createPrintDocumentAdapter(documentName: String) = webView.createPrintDocumentAdapter(documentName)
override fun setOnScrollChangeListener(l: OnScrollChangeListener?) = webView.setOnScrollChangeListener(l)
override fun onRenderProcessGone(webView: WebView) {
removeView(webView)
webView.destroy()
this.webView = createWebView()
addView(this.webView, webViewLayoutParams)
val fragment = findFragment<Fragment>()
(fragment as? OnWebViewRecreatedListener)?.onWebViewRecreated(this.webView)
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
val fragment =
try {
findFragment<Fragment>()
} catch (e: IllegalStateException) {
// findFragment throws if the View is not attached to a Fragment.
// This can happen in scenarios like Android Studio previews
// or if the view is added directly to an Activity.
if (BuildConfig.DEBUG && !isInEditMode) {
throw IllegalStateException(
"SafeWebViewLayout must be used within a Fragment",
e,
)
} else {
Timber.w(e, "SafeWebViewLayout not attached to a Fragment")
}
return
}
if (fragment !is OnWebViewRecreatedListener) {
if (BuildConfig.DEBUG && !isInEditMode) {
throw IllegalStateException(
"Fragment '${fragment::class.simpleName}' must implement OnWebViewRecreatedListener",
)
} else {
Timber.w("Fragment does not implement OnWebViewRecreatedListener. WebView recreation may not be handled")
}
}
}
/**
* Destroys the internal state of the wrapped [webView]
*
* No other methods may be called on this WebView after destroy
*/
@MainThread
fun safeDestroy() {
destroyWebView(webView, this)
}
companion object {
private val webViewLayoutParams =
ViewGroup.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
)
/**
* Clean up and destroy the [webView] to prevent memory leaks.
*
* Stops any loading, clears callbacks, and removes the view from its [parent].
*/
fun destroyWebView(
webView: WebView?,
parent: ViewGroup? = webView?.parent as? ViewGroup,
) {
Timber.d("Destroying WebView")
runCatchingWithReport("safeDestroy", onlyIfSilent = true) {
webView?.apply {
stopLoading()
loadUrl("about:blank")
webChromeClient = null
// remove listeners this class exposes
setOnScrollChangeListener(null)
}
// remove WebView from parent view
parent?.removeView(webView) ?: Timber.w("WebView parent is null")
}
// attempt to run destroy() even if the above fails
runCatchingWithReport("safeDestroy", onlyIfSilent = true) {
webView?.destroy()
}
}
}
}
/**
* Listener for [SafeWebViewLayout.onRenderProcessGone], called after the internal [WebView] is
* replaced due to a render process crash or the system killing it to free memory.
*
* Any [Fragment] containing a [SafeWebViewLayout] **must** implement this interface. In debug builds,
* a missing implementation will throw at [SafeWebViewLayout.onAttachedToWindow()]
*
* @see SafeWebViewLayout.onRenderProcessGone
*/
fun interface OnWebViewRecreatedListener {
/**
* Reconfigures a [WebView] after [SafeWebViewLayout.onRenderProcessGone] has replaced
* the old instance. Implementations must reapply all clients, settings and content
* that were configured on the original [WebView], as [SafeWebViewLayout] only handles
* the structural replacement and cannot reapply app level configuration.
*
* To manually trigger this path, call `webViewLayout.loadUrl("chrome://crash")`.
* Automated testing requires an instrumented test; a unit test is not sufficient.
*
* @param webView the new [WebView], already attached to [SafeWebViewLayout]
*/
fun onWebViewRecreated(webView: WebView)
}