blob: 5030359d2e832b766edb833dc2444b6970227b70 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "url/gurl.h"
namespace web {
// Defines the states of a NavigationItem that failed to load. This is used to
// coordinate the display of error page such that back/forward/reload navigation
// to a loaded error page will load the original URL. This is achieved in four
// steps:
// 1) A NavigationItem is set to gkDisplayingError when it first failed to load
// and a web error is displayed. If the failure occurred during provisional
// navigation, a placeholder entry is inserted into WKBackForwardList for
// this item.
// 2) Upon navigation to this item, use |loadHTMLString:| to modify the URL of
// the placeholder entry to the original URL and change the item state to
// kNavigatingToFailedNavigationItem.
// 3) Upon completion of the previous navigation, force a reload in WKWebView to
// reload the original URL and change the item state to
// kRetryFailedNavigationItem.
// 4) Upon completion of the reload, if successful, the item state is changed to
// kNoNavigationError.
// Full state transition diagram:
enum class ErrorRetryState {
// This is a new navigation request.
// This navigation item loaded without error.
// This navigation item failed to load and is in the process of loading a
// placeholder.
// This navigation item failed to load and was stored in the WKBackForwardList
// and is now being reloaded.
// This navigation item has an entry in WKBackForwardList. Ready to present
// error in native view.
// This navigation item failed to load and a web error is displayed.
// This navigation item is reactivated due to back/forward navigation and
// needs to try reloading.
// This navigation item is ready to be reloaded in web view.
// Test only, used to ignore a placeholder navigation used to support
// LoadHtml.
// Commands for CRWWebController to execute the state transition.
enum class ErrorRetryCommand {
// WebView should load placeholder request.
// WebView should load error view.
// WebView should reload.
// WebView should rewrite its URL (assumed to be a placeholder URL) to the
// navigation item's URL to prepare for reload.
// WebView should rewrite its URL to a placeholder URL to prepare for loading
// the error view.
// WebView doesn't need to do anything.
// A state machine that manages error and retry state of a navigation item. This
// is used to adapt WKWebView, which does not add provisional navigation failure
// to the back-forward list, to the Chrome convention, where such navigations
// are added to the back-forward list.
class ErrorRetryStateMachine {
explicit ErrorRetryStateMachine(const ErrorRetryStateMachine& machine);
// Sets the original request URL to be tracked by this state machine. It is
// used for invariant detection.
void SetURL(const GURL& url);
// Returns the current error retry state.
ErrorRetryState state() const;
// Transitions the state machine to kNoNavigationError.
void SetNoNavigationError();
// Transitions the state machine to kDisplayingError.
void SetDisplayingWebError();
// Transitions the state machine to kRetryPlaceholderNavigation.
void SetRetryPlaceholderNavigation();
// Only used for testing. Sets state to kIgnorePlaceholderNavigation, which
// will ignore the placeholder navigation used to support LoadHtml in tests.
void SetIgnorePlaceholderNavigation();
// Runs state transitions upon a failed provisional navigation.
ErrorRetryCommand DidFailProvisionalNavigation(const GURL& web_view_url,
const GURL& error_url);
// Runs state transitions upon a failure after the navigation is committed.
ErrorRetryCommand DidFailNavigation(const GURL& web_view_url);
// Runs state transitions upon a successful navigation.
ErrorRetryCommand DidFinishNavigation(const GURL& web_view_url);
ErrorRetryCommand BackForwardOrReloadFailed();
ErrorRetryState state_;
GURL url_;
} // namespace web