This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
/
GeckoWebViewProvider.kt
752 lines (663 loc) · 31.1 KB
/
GeckoWebViewProvider.kt
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* 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/. */
package org.mozilla.focus.web
import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.os.Parcelable
import android.preference.PreferenceManager
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.webkit.WebSettings
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import mozilla.components.browser.errorpages.ErrorPages
import mozilla.components.browser.errorpages.ErrorType
import mozilla.components.browser.session.Session
import mozilla.components.lib.crash.handler.CrashHandlerService
import mozilla.components.support.ktx.android.util.Base64
import org.json.JSONException
import org.mozilla.focus.R
import org.mozilla.focus.browser.LocalizedContent
import org.mozilla.focus.ext.savedWebViewState
import org.mozilla.focus.gecko.GeckoViewPrompt
import org.mozilla.focus.gecko.NestedGeckoView
import org.mozilla.focus.telemetry.TelemetryWrapper
import org.mozilla.focus.utils.AppConstants
import org.mozilla.focus.utils.IntentUtils
import org.mozilla.focus.utils.MobileMetricsPingStorage
import org.mozilla.focus.utils.Settings
import org.mozilla.focus.utils.UrlUtils
import org.mozilla.focus.webview.SystemWebView
import org.mozilla.geckoview.AllowOrDeny
import org.mozilla.geckoview.GeckoResult
import org.mozilla.geckoview.GeckoRuntime
import org.mozilla.geckoview.GeckoRuntimeSettings
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.GeckoSession.NavigationDelegate
import org.mozilla.geckoview.GeckoSessionSettings
import org.mozilla.geckoview.SessionFinder
import kotlin.coroutines.CoroutineContext
/**
* WebViewProvider implementation for creating a Gecko based implementation of IWebView.
*/
@Suppress("TooManyFunctions")
class GeckoWebViewProvider : IWebViewProvider {
override fun preload(context: Context) {
sendTelemetryEventOnSwitchToGecko(context)
createGeckoRuntime(context)
}
private fun sendTelemetryEventOnSwitchToGecko(context: Context) {
val settings = Settings.getInstance(context)
if (!settings.shouldShowFirstrun() && settings.isFirstGeckoRun()) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit().putBoolean(PREF_FIRST_GECKO_RUN, false).apply()
Log.d(javaClass.simpleName, "Sending change to Gecko ping")
TelemetryWrapper.changeToGeckoEngineEvent()
}
}
override fun create(context: Context, attributeSet: AttributeSet?): View {
return GeckoWebView(context, attributeSet)
}
override fun performCleanup(context: Context) {
// Nothing: a WebKit work-around.
}
override fun performNewBrowserSessionCleanup() {
// Nothing: a WebKit work-around.
}
private fun createGeckoRuntime(context: Context) {
if (geckoRuntime == null) {
val runtimeSettingsBuilder = GeckoRuntimeSettings.Builder()
runtimeSettingsBuilder.useContentProcessHint(true)
runtimeSettingsBuilder.blockMalware(Settings.getInstance(context).shouldUseSafeBrowsing())
runtimeSettingsBuilder.blockPhishing(Settings.getInstance(context).shouldUseSafeBrowsing())
runtimeSettingsBuilder.crashHandler(CrashHandlerService::class.java)
geckoRuntime =
GeckoRuntime.create(context.applicationContext, runtimeSettingsBuilder.build())
}
}
override fun requestMobileSite(context: Context, webSettings: WebSettings) {
}
override fun requestDesktopSite(webSettings: WebSettings) {
}
override fun applyAppSettings(
context: Context,
webSettings: WebSettings,
systemWebView: SystemWebView
) {
}
override fun disableBlocking(webSettings: WebSettings, systemWebView: SystemWebView) {
}
override fun getUABrowserString(existingUAString: String, focusToken: String): String {
return ""
}
@Suppress("LargeClass", "TooManyFunctions")
class GeckoWebView(context: Context, attrs: AttributeSet?) :
NestedGeckoView(context, attrs),
IWebView,
SharedPreferences.OnSharedPreferenceChangeListener,
CoroutineScope {
private var callback: IWebView.Callback? = null
private var findListener: IFindListener? = null
private var currentUrl: String = ABOUT_BLANK
private var canGoBack: Boolean = false
private var canGoForward: Boolean = false
private var isSecure: Boolean = false
private var geckoSession: GeckoSession
private var webViewTitle: String? = null
private var isLoadingInternalUrl = false
private lateinit var finder: SessionFinder
private var restored = false
private var job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
init {
PreferenceManager.getDefaultSharedPreferences(context)
.registerOnSharedPreferenceChangeListener(this)
geckoSession = createGeckoSession()
applySettingsAndSetDelegates()
setSession(geckoSession, geckoRuntime)
}
private fun applySettingsAndSetDelegates() {
applyAppSettings()
updateBlocking()
geckoSession.contentDelegate = createContentDelegate()
geckoSession.progressDelegate = createProgressDelegate()
geckoSession.navigationDelegate = createNavigationDelegate()
geckoSession.trackingProtectionDelegate = createTrackingProtectionDelegate()
geckoSession.promptDelegate = createPromptDelegate()
finder = geckoSession.finder
finder.displayFlags = GeckoSession.FINDER_DISPLAY_HIGHLIGHT_ALL
}
private fun createGeckoSession(): GeckoSession {
val settings = GeckoSessionSettings()
settings.setBoolean(GeckoSessionSettings.USE_MULTIPROCESS, true)
settings.setBoolean(GeckoSessionSettings.USE_PRIVATE_MODE, true)
settings.setBoolean(GeckoSessionSettings.SUSPEND_MEDIA_WHEN_INACTIVE, true)
return GeckoSession(settings)
}
override fun setCallback(callback: IWebView.Callback?) {
this.callback = callback
}
override fun onPause() {
job.cancel()
}
override fun goBack() {
geckoSession.goBack()
}
override fun goForward() {
geckoSession.goForward()
}
override fun reload() {
geckoSession.reload()
}
override fun destroy() {
}
override fun onResume() {
if (job.isCancelled) {
job = Job()
}
storeTelemetrySnapshots()
}
override fun stopLoading() {
geckoSession.stop()
callback?.onPageFinished(isSecure)
}
override fun getUrl(): String? {
return currentUrl
}
override fun loadUrl(url: String) {
currentUrl = url
geckoSession.loadUri(currentUrl)
}
override fun cleanup() {
if (geckoSession.isOpen) {
geckoSession.close()
}
}
override fun setBlockingEnabled(enabled: Boolean) {
geckoSession.settings.setBoolean(GeckoSessionSettings.USE_TRACKING_PROTECTION, enabled)
if (enabled) {
updateBlocking()
applyAppSettings()
} else {
geckoRuntime!!.settings.javaScriptEnabled = true
geckoRuntime!!.settings.webFontsEnabled = true
geckoRuntime!!.settings.cookieBehavior = GeckoRuntimeSettings.COOKIE_ACCEPT_ALL
}
callback?.onBlockingStateChanged(enabled)
}
override fun setRequestDesktop(shouldRequestDesktop: Boolean) {
geckoSession.settings.setInt(
GeckoSessionSettings.USER_AGENT_MODE,
if (shouldRequestDesktop) GeckoSessionSettings.USER_AGENT_MODE_DESKTOP
else GeckoSessionSettings.USER_AGENT_MODE_MOBILE
)
callback?.onRequestDesktopStateChanged(shouldRequestDesktop)
}
@Suppress("ComplexMethod")
override fun onSharedPreferenceChanged(
sharedPreferences: SharedPreferences,
key: String
) {
when (key) {
context.getString(R.string.pref_key_privacy_block_social),
context.getString(R.string.pref_key_privacy_block_ads),
context.getString(R.string.pref_key_privacy_block_analytics),
context.getString(R.string.pref_key_privacy_block_other) -> updateBlocking()
context.getString(R.string.pref_key_performance_block_javascript)
-> geckoRuntime!!.settings.javaScriptEnabled =
!Settings.getInstance(context).shouldBlockJavaScript()
context.getString(R.string.pref_key_performance_block_webfonts)
-> geckoRuntime!!.settings.webFontsEnabled =
!Settings.getInstance(context).shouldBlockWebFonts()
context.getString(R.string.pref_key_remote_debugging) ->
geckoRuntime!!.settings.remoteDebuggingEnabled =
Settings.getInstance(context).shouldEnableRemoteDebugging()
context.getString(R.string.pref_key_performance_enable_cookies) -> {
updateCookieSettings()
}
context.getString(R.string.pref_key_safe_browsing) -> {
val shouldUseSafeBrowsing =
Settings.getInstance(context).shouldUseSafeBrowsing()
geckoRuntime!!.settings.blockMalware = shouldUseSafeBrowsing
geckoRuntime!!.settings.blockPhishing = shouldUseSafeBrowsing
}
else -> return
}
reload()
}
private fun applyAppSettings() {
geckoRuntime!!.settings.javaScriptEnabled =
!Settings.getInstance(context).shouldBlockJavaScript()
geckoRuntime!!.settings.webFontsEnabled =
!Settings.getInstance(context).shouldBlockWebFonts()
geckoRuntime!!.settings.remoteDebuggingEnabled = false
updateCookieSettings()
}
private fun updateCookieSettings() {
geckoRuntime!!.settings.cookieBehavior =
when (Settings.getInstance(context).shouldBlockCookiesValue()) {
context.getString(
R.string.preference_privacy_should_block_cookies_yes_option
) ->
GeckoRuntimeSettings.COOKIE_ACCEPT_NONE
context.getString(
R.string.preference_privacy_should_block_cookies_third_party_tracker_cookies_option
) ->
GeckoRuntimeSettings.COOKIE_ACCEPT_NON_TRACKERS
context.getString(
R.string.preference_privacy_should_block_cookies_third_party_only_option
) ->
GeckoRuntimeSettings.COOKIE_ACCEPT_FIRST_PARTY
else -> GeckoRuntimeSettings.COOKIE_ACCEPT_ALL
}
}
private fun updateBlocking() {
val settings = Settings.getInstance(context)
var categories = 0
if (settings.shouldBlockSocialTrackers()) {
categories += GeckoSession.TrackingProtectionDelegate.CATEGORY_SOCIAL
}
if (settings.shouldBlockAdTrackers()) {
categories += GeckoSession.TrackingProtectionDelegate.CATEGORY_AD
}
if (settings.shouldBlockAnalyticTrackers()) {
categories += GeckoSession.TrackingProtectionDelegate.CATEGORY_ANALYTIC
}
if (settings.shouldBlockOtherTrackers()) {
categories += GeckoSession.TrackingProtectionDelegate.CATEGORY_CONTENT
}
geckoRuntime!!.settings.trackingProtectionCategories = categories
}
@Suppress("ComplexMethod", "ReturnCount")
private fun createContentDelegate(): GeckoSession.ContentDelegate {
return object : GeckoSession.ContentDelegate {
override fun onTitleChange(session: GeckoSession, title: String) {
webViewTitle = title
callback?.onTitleChanged(title)
}
override fun onFullScreen(session: GeckoSession, fullScreen: Boolean) {
if (fullScreen) {
callback?.onEnterFullScreen({ geckoSession.exitFullScreen() }, null)
} else {
callback?.onExitFullScreen()
}
}
override fun onContextMenu(
session: GeckoSession,
screenX: Int,
screenY: Int,
uri: String?,
elementType: Int,
elementSrc: String?
) {
if (elementSrc != null && uri != null &&
elementType == GeckoSession.ContentDelegate.ELEMENT_TYPE_IMAGE
) {
callback?.onLongPress(IWebView.HitTarget(true, uri, true, elementSrc))
} else if (elementSrc != null && elementType == GeckoSession.ContentDelegate.ELEMENT_TYPE_IMAGE) {
callback?.onLongPress(IWebView.HitTarget(false, null, true, elementSrc))
} else if (uri != null) {
callback?.onLongPress(IWebView.HitTarget(true, uri, false, null))
}
}
override fun onExternalResponse(
session: GeckoSession,
response: GeckoSession.WebResponseInfo
) {
if (!AppConstants.supportsDownloadingFiles()) {
return
}
val scheme = Uri.parse(response.uri).scheme
if (scheme == null || scheme != "http" && scheme != "https") {
// We are ignoring everything that is not http or https. This is a limitation of
// Android's download manager. There's no reason to show a download dialog for
// something we can't download anyways.
Log.w(TAG, "Ignoring download from non http(s) URL: " + response.uri)
return
}
val download = Download(
response.uri, USER_AGENT,
response.filename, response.contentType, response.contentLength,
Environment.DIRECTORY_DOWNLOADS, response.filename
)
callback?.onDownloadStart(download)
}
override fun onCrash(session: GeckoSession) {
geckoSession.close()
geckoSession = createGeckoSession()
applySettingsAndSetDelegates()
geckoSession.open(geckoRuntime!!)
setSession(geckoSession)
currentUrl = ABOUT_BLANK
geckoSession.loadUri(currentUrl)
}
override fun onFocusRequest(geckoSession: GeckoSession) {}
override fun onCloseRequest(geckoSession: GeckoSession) {
// Ignore this callback
}
}
}
@Suppress("ComplexMethod")
private fun createProgressDelegate(): GeckoSession.ProgressDelegate {
return object : GeckoSession.ProgressDelegate {
override fun onProgressChange(session: GeckoSession?, progress: Int) {
if (progress == PROGRESS_100) {
if (UrlUtils.isLocalizedContent(url)) {
// When the url is a localized content, then the page is secure
isSecure = true
}
callback?.onPageFinished(isSecure)
} else {
callback?.onProgress(progress)
}
}
override fun onPageStart(session: GeckoSession, url: String) {
callback?.onPageStarted(url)
callback?.resetBlockedTrackers()
isSecure = false
}
override fun onPageStop(session: GeckoSession, success: Boolean) {
if (success) {
if (UrlUtils.isLocalizedContent(url)) {
// When the url is a localized content, then the page is secure
isSecure = true
}
callback?.onPageFinished(isSecure)
}
}
override fun onSecurityChange(
session: GeckoSession,
securityInfo: GeckoSession.ProgressDelegate.SecurityInformation
) {
isSecure = securityInfo.isSecure
if (UrlUtils.isLocalizedContent(url)) {
// When the url is a localized content, then the page is secure
isSecure = true
}
callback?.onSecurityChanged(
isSecure,
securityInfo.host,
securityInfo.issuerOrganization
)
}
}
}
@Suppress("ComplexMethod")
private fun createNavigationDelegate(): GeckoSession.NavigationDelegate {
return object : GeckoSession.NavigationDelegate {
override fun onLoadRequest(
session: GeckoSession,
request: NavigationDelegate.LoadRequest
): GeckoResult<AllowOrDeny>? {
val uri = Uri.parse(request.uri)
val complete = when {
request.target == GeckoSession.NavigationDelegate.TARGET_WINDOW_NEW -> {
geckoSession.loadUri(request.uri)
AllowOrDeny.DENY
}
LocalizedContent.handleInternalContent(
request.uri,
this@GeckoWebView,
context
) -> {
AllowOrDeny.DENY
}
!UrlUtils.isSupportedProtocol(uri.scheme) && callback != null &&
IntentUtils.handleExternalUri(
context,
this@GeckoWebView,
request.uri
) -> {
AllowOrDeny.DENY
}
else -> {
callback?.onRequest(request.isUserTriggered)
AllowOrDeny.ALLOW
}
}
return GeckoResult.fromValue(complete)
}
override fun onLoadError(
session: GeckoSession?,
uri: String?,
category: Int,
error: Int
): GeckoResult<String> {
ErrorPages.createErrorPage(
context,
geckoErrorToErrorType(error),
uri
).apply {
return GeckoResult.fromValue(Base64.encodeToUriString(this))
}
}
override fun onNewSession(
session: GeckoSession,
uri: String
): GeckoResult<GeckoSession>? {
// Prevent new sessions to be created from onLoadRequest
throw IllegalStateException()
}
override fun onLocationChange(session: GeckoSession, url: String) {
var desiredUrl = url
// Save internal data: urls we should override to present focus:about, focus:rights
if (isLoadingInternalUrl) {
if (currentUrl == LocalizedContent.URL_ABOUT) {
internalAboutData = desiredUrl
} else if (currentUrl == LocalizedContent.URL_RIGHTS) {
internalRightsData = desiredUrl
}
isLoadingInternalUrl = false
desiredUrl = currentUrl
}
// Check for internal data: urls to instead present focus:rights, focus:about
if (!TextUtils.isEmpty(internalAboutData) && internalAboutData == desiredUrl) {
desiredUrl = LocalizedContent.URL_ABOUT
} else if (!TextUtils.isEmpty(internalRightsData) && internalRightsData == desiredUrl) {
desiredUrl = LocalizedContent.URL_RIGHTS
}
currentUrl = desiredUrl
callback?.onURLChanged(desiredUrl)
}
override fun onCanGoBack(session: GeckoSession, canGoBack: Boolean) {
this@GeckoWebView.canGoBack = canGoBack
}
override fun onCanGoForward(session: GeckoSession, canGoForward: Boolean) {
this@GeckoWebView.canGoForward = canGoForward
}
}
}
private fun createTrackingProtectionDelegate(): GeckoSession.TrackingProtectionDelegate {
return GeckoSession.TrackingProtectionDelegate { _, _, _ ->
callback?.countBlockedTracker()
}
}
private fun createPromptDelegate(): GeckoSession.PromptDelegate {
return GeckoViewPrompt(context as Activity)
}
override fun canGoForward(): Boolean {
return canGoForward
}
override fun canGoBack(): Boolean {
return canGoBack
}
override fun restoreWebViewState(session: Session) {
val stateData = session.savedWebViewState!!
val savedSession = stateData.getParcelable<GeckoSession>(GECKO_SESSION)!!
if (geckoSession != savedSession && !restored) {
// Tab changed, we need to close the default session and restore our saved session
geckoSession.close()
geckoSession = savedSession
canGoBack = stateData.getBoolean(CAN_GO_BACK, false)
canGoForward = stateData.getBoolean(CAN_GO_FORWARD, false)
isSecure = stateData.getBoolean(IS_SECURE, false)
webViewTitle = stateData.getString(WEBVIEW_TITLE, null)
currentUrl = stateData.getString(CURRENT_URL, ABOUT_BLANK)
applySettingsAndSetDelegates()
if (!geckoSession.isOpen) {
geckoSession.open(geckoRuntime!!)
}
setSession(geckoSession)
} else {
// App was backgrounded and restored;
// GV restored the GeckoSession itself, but we need to restore our variables
canGoBack = stateData.getBoolean(CAN_GO_BACK, false)
canGoForward = stateData.getBoolean(CAN_GO_FORWARD, false)
isSecure = stateData.getBoolean(IS_SECURE, false)
webViewTitle = stateData.getString(WEBVIEW_TITLE, null)
currentUrl = stateData.getString(CURRENT_URL, ABOUT_BLANK)
applySettingsAndSetDelegates()
restored = false
}
}
override fun onRestoreInstanceState(state: Parcelable?) {
restored = true
super.onRestoreInstanceState(state)
}
override fun saveWebViewState(session: Session) {
val sessionBundle = Bundle()
sessionBundle.putParcelable(GECKO_SESSION, geckoSession)
sessionBundle.putBoolean(CAN_GO_BACK, canGoBack)
sessionBundle.putBoolean(CAN_GO_FORWARD, canGoForward)
sessionBundle.putBoolean(IS_SECURE, isSecure)
sessionBundle.putString(WEBVIEW_TITLE, webViewTitle)
sessionBundle.putString(CURRENT_URL, currentUrl)
session.savedWebViewState = sessionBundle
}
override fun getTitle(): String? {
return webViewTitle
}
override fun exitFullscreen() {
geckoSession.exitFullScreen()
}
override fun findAllAsync(find: String) {
finder.find(find, 0).then({ result ->
if (result != null) {
findListener?.onFindResultReceived(result.current, result.total, true)
}
GeckoResult<Void>()
}, { _ ->
GeckoResult<Void>()
})
}
override fun findNext(forward: Boolean) {
finder.find(null, if (forward) 0 else GeckoSession.FINDER_FIND_BACKWARDS)
.then({ result ->
if (result != null) {
findListener?.onFindResultReceived(result.current, result.total, true)
}
GeckoResult<Void>()
}, { _ ->
GeckoResult<Void>()
})
}
override fun clearMatches() {
finder.clear()
}
override fun setFindListener(findListener: IFindListener) {
this.findListener = findListener
}
override fun loadData(
baseURL: String,
data: String,
mimeType: String,
encoding: String,
historyURL: String
) {
isLoadingInternalUrl = historyURL == LocalizedContent.URL_RIGHTS || historyURL ==
LocalizedContent.URL_ABOUT
geckoSession.loadData(data.toByteArray(Charsets.UTF_8), mimeType)
currentUrl = historyURL
}
private fun storeTelemetrySnapshots() {
val storage = MobileMetricsPingStorage(context)
if (!storage.shouldStoreMetrics()) return
geckoRuntime!!.telemetry.getSnapshots(true).then({ value ->
launch(IO) {
try {
value?.toJSONObject()?.also {
storage.save(it)
}
} catch (e: JSONException) {
Log.e("getSnapshots failed", e.message)
}
}
GeckoResult<Void>()
}, {
GeckoResult<Void>()
})
}
override fun onDetachedFromWindow() {
PreferenceManager.getDefaultSharedPreferences(context)
.unregisterOnSharedPreferenceChangeListener(this)
releaseSession()
super.onDetachedFromWindow()
}
companion object {
private const val TAG = "GeckoWebView"
}
}
companion object {
@Volatile
private var geckoRuntime: GeckoRuntime? = null
private var internalAboutData: String? = null
private var internalRightsData: String? = null
private const val USER_AGENT =
"Mozilla/5.0 (Android 8.1.0; Mobile; rv:60.0) Gecko/60.0 Firefox/60.0"
const val PREF_FIRST_GECKO_RUN: String = "first_gecko_run"
const val PROGRESS_100 = 100
const val CAN_GO_BACK = "canGoBack"
const val CAN_GO_FORWARD = "canGoForward"
const val GECKO_SESSION = "geckoSession"
const val IS_SECURE = "isSecure"
const val WEBVIEW_TITLE = "webViewTitle"
const val CURRENT_URL = "currentUrl"
const val ABOUT_BLANK = "about:blank"
/**
* Provides an ErrorType corresponding to the error code provided.
*/
@Suppress("ComplexMethod")
internal fun geckoErrorToErrorType(@GeckoSession.NavigationDelegate.LoadError errorCode: Int) =
when (errorCode) {
NavigationDelegate.ERROR_UNKNOWN -> ErrorType.UNKNOWN
NavigationDelegate.ERROR_SECURITY_SSL -> ErrorType.ERROR_SECURITY_SSL
NavigationDelegate.ERROR_SECURITY_BAD_CERT -> ErrorType.ERROR_SECURITY_BAD_CERT
NavigationDelegate.ERROR_NET_INTERRUPT -> ErrorType.ERROR_NET_INTERRUPT
NavigationDelegate.ERROR_NET_TIMEOUT -> ErrorType.ERROR_NET_TIMEOUT
NavigationDelegate.ERROR_CONNECTION_REFUSED -> ErrorType.ERROR_CONNECTION_REFUSED
NavigationDelegate.ERROR_UNKNOWN_SOCKET_TYPE -> ErrorType.ERROR_UNKNOWN_SOCKET_TYPE
NavigationDelegate.ERROR_REDIRECT_LOOP -> ErrorType.ERROR_REDIRECT_LOOP
NavigationDelegate.ERROR_OFFLINE -> ErrorType.ERROR_OFFLINE
NavigationDelegate.ERROR_PORT_BLOCKED -> ErrorType.ERROR_PORT_BLOCKED
NavigationDelegate.ERROR_NET_RESET -> ErrorType.ERROR_NET_RESET
NavigationDelegate.ERROR_UNSAFE_CONTENT_TYPE -> ErrorType.ERROR_UNSAFE_CONTENT_TYPE
NavigationDelegate.ERROR_CORRUPTED_CONTENT -> ErrorType.ERROR_CORRUPTED_CONTENT
NavigationDelegate.ERROR_CONTENT_CRASHED -> ErrorType.ERROR_CONTENT_CRASHED
NavigationDelegate.ERROR_INVALID_CONTENT_ENCODING -> ErrorType.ERROR_INVALID_CONTENT_ENCODING
NavigationDelegate.ERROR_UNKNOWN_HOST -> ErrorType.ERROR_UNKNOWN_HOST
NavigationDelegate.ERROR_MALFORMED_URI -> ErrorType.ERROR_MALFORMED_URI
NavigationDelegate.ERROR_UNKNOWN_PROTOCOL -> ErrorType.ERROR_UNKNOWN_PROTOCOL
NavigationDelegate.ERROR_FILE_NOT_FOUND -> ErrorType.ERROR_FILE_NOT_FOUND
NavigationDelegate.ERROR_FILE_ACCESS_DENIED -> ErrorType.ERROR_FILE_ACCESS_DENIED
NavigationDelegate.ERROR_PROXY_CONNECTION_REFUSED -> ErrorType.ERROR_PROXY_CONNECTION_REFUSED
NavigationDelegate.ERROR_UNKNOWN_PROXY_HOST -> ErrorType.ERROR_UNKNOWN_PROXY_HOST
NavigationDelegate.ERROR_SAFEBROWSING_MALWARE_URI -> ErrorType.ERROR_SAFEBROWSING_MALWARE_URI
NavigationDelegate.ERROR_SAFEBROWSING_UNWANTED_URI -> ErrorType.ERROR_SAFEBROWSING_UNWANTED_URI
NavigationDelegate.ERROR_SAFEBROWSING_HARMFUL_URI -> ErrorType.ERROR_SAFEBROWSING_HARMFUL_URI
NavigationDelegate.ERROR_SAFEBROWSING_PHISHING_URI -> ErrorType.ERROR_SAFEBROWSING_PHISHING_URI
else -> ErrorType.UNKNOWN
}
}
}