-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow users to recover from WebView renderer failures #644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
90496d3
94a28b2
d65e3f6
3864cfa
c91f8da
ef50eda
334d55d
b32a596
71445cb
1c46cb4
0435a69
9ba5c83
31f0f6e
ad312aa
9a9d78a
fccad7b
0f29296
254ba38
ffad15d
b87e4f5
75183a8
41807ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Copyright (c) 2019 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.app.browser | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Test | ||
|
|
||
| class EmptyNavigationStateTest { | ||
|
|
||
| @Test | ||
| fun whenEmptyNavigationStateFromNavigationStateThenBrowserPropertiesAreTheSame() { | ||
| val previousState = buildState("originalUrl", "currentUrl", "titlle") | ||
| val emptyNavigationState = EmptyNavigationState(previousState) | ||
|
|
||
| assertEquals(emptyNavigationState.currentUrl, previousState.currentUrl) | ||
| assertEquals(emptyNavigationState.originalUrl, previousState.originalUrl) | ||
| assertEquals(emptyNavigationState.title, previousState.title) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenEmptyNavigationStateFromNavigationStateThenNavigationPropertiesAreCleared() { | ||
| val emptyNavigationState = EmptyNavigationState(buildState("originalUrl", "currentUrl", "titlle")) | ||
|
|
||
| assertEquals(emptyNavigationState.stepsToPreviousPage, 0) | ||
| assertFalse(emptyNavigationState.canGoBack) | ||
| assertFalse(emptyNavigationState.canGoForward) | ||
| assertFalse(emptyNavigationState.hasNavigationHistory) | ||
| } | ||
|
|
||
| private fun buildState(originalUrl: String?, currentUrl: String?, title: String? = null): WebNavigationState { | ||
| return TestNavigationState( | ||
| originalUrl = originalUrl, | ||
| currentUrl = currentUrl, | ||
| title = title, | ||
| stepsToPreviousPage = 1, | ||
| canGoBack = true, | ||
| canGoForward = true, | ||
| hasNavigationHistory = true | ||
| ) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright (c) 2019 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.app.browser | ||
|
|
||
| data class TestNavigationState( | ||
| override val originalUrl: String?, | ||
| override val currentUrl: String?, | ||
| override val title: String?, | ||
| override val stepsToPreviousPage: Int, | ||
| override val canGoBack: Boolean, | ||
| override val canGoForward: Boolean, | ||
| override val hasNavigationHistory: Boolean | ||
| ) : WebNavigationState |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,6 +203,11 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
|
|
||
| private var webView: WebView? = null | ||
|
|
||
| private val errorSnackbar: Snackbar by lazy { | ||
| Snackbar.make(browserLayout, R.string.crashedWebViewErrorMessage, Snackbar.LENGTH_INDEFINITE) | ||
| .setBehavior(NonDismissibleBehavior()) | ||
| } | ||
|
|
||
| private val findInPageTextWatcher = object : TextChangedWatcher() { | ||
| override fun afterTextChanged(editable: Editable) { | ||
| viewModel.userFindingInPage(findInPageInput.text.toString()) | ||
|
|
@@ -309,7 +314,7 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
| popupMenu.apply { | ||
| onMenuItemClicked(view.forwardPopupMenuItem) { viewModel.onUserPressedForward() } | ||
| onMenuItemClicked(view.backPopupMenuItem) { activity?.onBackPressed() } | ||
| onMenuItemClicked(view.refreshPopupMenuItem) { refresh() } | ||
| onMenuItemClicked(view.refreshPopupMenuItem) { viewModel.onRefreshRequested() } | ||
| onMenuItemClicked(view.newTabPopupMenuItem) { viewModel.userRequestedOpeningNewTab() } | ||
| onMenuItemClicked(view.bookmarksPopupMenuItem) { browserActivity?.launchBookmarks() } | ||
| onMenuItemClicked(view.addBookmarksPopupMenuItem) { launch { viewModel.onBookmarkAddRequested() } } | ||
|
|
@@ -374,6 +379,8 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
| } | ||
|
|
||
| private fun showHome() { | ||
| errorSnackbar.dismiss() | ||
| newTabLayout.show() | ||
| showKeyboardImmediately() | ||
| appBarLayout.setExpanded(true) | ||
| webView?.onPause() | ||
|
|
@@ -383,6 +390,7 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
| } | ||
|
|
||
| private fun showBrowser() { | ||
| newTabLayout.gone() | ||
| webView?.show() | ||
| webView?.onResume() | ||
| omnibarScrolling.enableOmnibarScrolling(toolbarContainer) | ||
|
|
@@ -398,13 +406,17 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
| webView?.loadUrl(url) | ||
| } | ||
|
|
||
| fun onRefreshRequested() { | ||
| viewModel.onRefreshRequested() | ||
| } | ||
|
|
||
| fun refresh() { | ||
| webView?.reload() | ||
| } | ||
|
|
||
| private fun processCommand(it: Command?) { | ||
| when (it) { | ||
| Command.Refresh -> refresh() | ||
| is Command.Refresh -> refresh() | ||
| is Command.OpenInNewTab -> { | ||
| browserActivity?.openInNewTab(it.query) | ||
| } | ||
|
|
@@ -419,10 +431,10 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
| is Command.NavigateBack -> { | ||
| webView?.goBackOrForward(-it.steps) | ||
| } | ||
| Command.NavigateForward -> { | ||
| is Command.NavigateForward -> { | ||
| webView?.goForward() | ||
| } | ||
| Command.ResetHistory -> { | ||
| is Command.ResetHistory -> { | ||
| resetWebView() | ||
| } | ||
| is Command.DialNumber -> { | ||
|
|
@@ -439,10 +451,10 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
| val intent = Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:${it.telephoneNumber}")) | ||
| openExternalDialog(intent) | ||
| } | ||
| Command.ShowKeyboard -> { | ||
| is Command.ShowKeyboard -> { | ||
| showKeyboard() | ||
| } | ||
| Command.HideKeyboard -> { | ||
| is Command.HideKeyboard -> { | ||
| hideKeyboard() | ||
| } | ||
| is Command.BrokenSiteFeedback -> { | ||
|
|
@@ -458,7 +470,7 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
| } | ||
| is Command.DownloadImage -> requestImageDownload(it.url) | ||
| is Command.FindInPageCommand -> webView?.findAllAsync(it.searchTerm) | ||
| Command.DismissFindInPage -> webView?.findAllAsync(null) | ||
| is Command.DismissFindInPage -> webView?.findAllAsync(null) | ||
| is Command.ShareLink -> launchSharePageChooser(it.url) | ||
| is Command.CopyLink -> { | ||
| clipboardManager.primaryClip = ClipData.newPlainText(null, it.url) | ||
|
|
@@ -481,6 +493,14 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
| is Command.SaveCredentials -> saveBasicAuthCredentials(it.request, it.credentials) | ||
| is Command.GenerateWebViewPreviewImage -> generateWebViewPreviewImage() | ||
| is Command.LaunchTabSwitcher -> launchTabSwitcher() | ||
| is Command.ShowErrorWithAction -> showErrorSnackbar(it) | ||
| } | ||
| } | ||
|
|
||
| private fun showErrorSnackbar(command: Command.ShowErrorWithAction) { | ||
| //Snackbar is global and it should appear only the foreground fragment | ||
| if (!errorSnackbar.view.isAttachedToWindow && isVisible) { | ||
| errorSnackbar.setAction(R.string.crashedWebViewErrorAction) { command.action() }.show() | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1124,13 +1144,23 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
| } | ||
|
|
||
| fun renderGlobalViewState(viewState: GlobalLayoutViewState) { | ||
| if (lastSeenGlobalViewState is GlobalLayoutViewState.Invalidated && | ||
| viewState is GlobalLayoutViewState.Browser) { | ||
| throw IllegalStateException("Invalid state transition") | ||
| } | ||
|
|
||
| renderIfChanged(viewState, lastSeenGlobalViewState) { | ||
| lastSeenGlobalViewState = viewState | ||
|
|
||
| if (viewState.isNewTabState) { | ||
| browserLayout.hide() | ||
| } else { | ||
| browserLayout.show() | ||
| when (viewState) { | ||
| is GlobalLayoutViewState.Browser -> { | ||
| if (viewState.isNewTabState) { | ||
| browserLayout.hide() | ||
| } else { | ||
| browserLayout.show() | ||
| } | ||
| } | ||
| is GlobalLayoutViewState.Invalidated -> destroyWebView() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this valid? It eventually calls
I recommend creating an invalidate method that removes and nulls out the WebView rather than reusing this method that interacts with the Webview instance when we don't know what state it is in.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I had the same doubt because "WebView can't be used" is not clear enough. While reading again the docs, their sample is using exactly that method call, so I would say that it's safe and correct in order to clean up WebView internal references. |
||
| } | ||
| } | ||
| } | ||
|
|
@@ -1174,6 +1204,8 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope { | |
| newTabPopupMenuItem.isEnabled = browserShowing | ||
| addBookmarksPopupMenuItem?.isEnabled = viewState.canAddBookmarks | ||
| sharePageMenuItem?.isEnabled = viewState.canSharePage | ||
| brokenSitePopupMenuItem?.isEnabled = viewState.canReportSite | ||
| requestDesktopSiteCheckMenuItem?.isEnabled = viewState.canChangeBrowsingMode | ||
|
|
||
| addToHome?.let { | ||
| it.visibility = if (viewState.addToHomeVisible) VISIBLE else GONE | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.