1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_RENDERER_NET_NET_ERROR_HELPER_CORE_H_
6 #define CHROME_RENDERER_NET_NET_ERROR_HELPER_CORE_H_
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/timer/timer.h"
14 #include "chrome/common/localized_error.h"
15 #include "chrome/common/net/net_error_info.h"
26 // Class that contains the logic for how the NetErrorHelper. This allows for
27 // testing the logic without a RenderView or WebFrame, which are difficult to
28 // mock, and for testing races which are impossible to reliably reproduce
29 // with real RenderViews or WebFrames.
30 class NetErrorHelperCore
{
49 // The Delegate handles all interaction with the RenderView, WebFrame, and
50 // the network, as well as the generation of error pages.
53 // Generates an error page's HTML for the given error.
54 virtual void GenerateLocalizedErrorPage(
55 const blink::WebURLError
& error
,
57 scoped_ptr
<LocalizedError::ErrorPageParams
> params
,
58 bool* reload_button_shown
,
59 bool* load_stale_button_shown
,
60 std::string
* html
) const = 0;
62 // Loads the given HTML in the main frame for use as an error page.
63 virtual void LoadErrorPageInMainFrame(const std::string
& html
,
64 const GURL
& failed_url
) = 0;
66 // Create extra Javascript bindings in the error page.
67 virtual void EnablePageHelperFunctions() = 0;
69 // Updates the currently displayed error page with a new error code. The
70 // currently displayed error page must have finished loading, and must have
71 // been generated by a call to GenerateLocalizedErrorPage.
72 virtual void UpdateErrorPage(const blink::WebURLError
& error
,
73 bool is_failed_post
) = 0;
75 // Fetches an error page and calls into OnErrorPageFetched when done. Any
76 // previous fetch must either be canceled or finished before calling. Can't
77 // be called synchronously after a previous fetch completes.
78 virtual void FetchNavigationCorrections(
79 const GURL
& navigation_correction_url
,
80 const std::string
& navigation_correction_request_body
) = 0;
82 // Cancels fetching navigation corrections. Does nothing if no fetch is
84 virtual void CancelFetchNavigationCorrections() = 0;
86 // Starts a reload of the page in the observed frame.
87 virtual void ReloadPage() = 0;
89 // Load the original page from cache.
90 virtual void LoadPageFromCache(const GURL
& page_url
) = 0;
93 virtual ~Delegate() {}
96 explicit NetErrorHelperCore(Delegate
* delegate
);
97 ~NetErrorHelperCore();
99 // Examines |frame| and |error| to see if this is an error worthy of a DNS
100 // probe. If it is, initializes |error_strings| based on |error|,
101 // |is_failed_post|, and |locale| with suitable strings and returns true.
102 // If not, returns false, in which case the caller should look up error
103 // strings directly using LocalizedError::GetNavigationErrorStrings.
105 // Updates the NetErrorHelper with the assumption the page will be loaded
107 void GetErrorHTML(FrameType frame_type
,
108 const blink::WebURLError
& error
,
110 std::string
* error_html
);
112 // These methods handle tracking the actual state of the page.
113 void OnStartLoad(FrameType frame_type
, PageType page_type
);
114 void OnCommitLoad(FrameType frame_type
);
115 void OnFinishLoad(FrameType frame_type
);
118 void CancelPendingFetches();
120 // Called when an error page have has been retrieved over the network. |html|
121 // must be an empty string on error.
122 void OnNavigationCorrectionsFetched(const std::string
& corrections
,
123 const std::string
& accept_languages
,
126 // Notifies |this| that network error information from the browser process
127 // has been received.
128 void OnNetErrorInfo(chrome_common_net::DnsProbeStatus status
);
130 void OnSetNavigationCorrectionInfo(const GURL
& navigation_correction_url
,
131 const std::string
& language
,
132 const std::string
& country_code
,
133 const std::string
& api_key
,
134 const GURL
& search_url
);
135 // Notifies |this| that the network's online status changed.
136 // Handler for NetworkStateChanged notification from the browser process. If
137 // the network state changes to online, this method is responsible for
138 // starting the auto-reload process.
140 // Warning: if there are many tabs sitting at an error page, this handler will
141 // be run at the same time for each of their top-level renderframes, which can
142 // cause many requests to be started at the same time. There's no current
143 // protection against this kind of "reload storm".
145 // TODO(rdsmith): prevent the reload storm.
146 void NetworkStateChanged(bool online
);
148 void set_auto_reload_enabled(bool auto_reload_enabled
) {
149 auto_reload_enabled_
= auto_reload_enabled
;
152 int auto_reload_count() const { return auto_reload_count_
; }
154 bool ShouldSuppressErrorPage(FrameType frame_type
, const GURL
& url
);
156 void set_timer_for_testing(scoped_ptr
<base::Timer
> timer
) {
157 auto_reload_timer_
.reset(timer
.release());
160 // Execute the effect of pressing the specified button.
161 // Note that the visual effects of the 'MORE' button are taken
162 // care of in JavaScript.
163 void ExecuteButtonPress(Button button
);
166 struct ErrorPageInfo
;
168 // Updates the currently displayed error page with a new error based on the
169 // most recently received DNS probe result. The page must have finished
170 // loading before this is called.
171 void UpdateErrorPage();
173 void GenerateLocalErrorPage(
174 FrameType frame_type
,
175 const blink::WebURLError
& error
,
177 scoped_ptr
<LocalizedError::ErrorPageParams
> params
,
178 std::string
* error_html
);
180 blink::WebURLError
GetUpdatedError(const blink::WebURLError
& error
) const;
183 bool MaybeStartAutoReloadTimer();
184 void StartAutoReloadTimer();
185 void AutoReloadTimerFired();
187 static bool IsReloadableError(const ErrorPageInfo
& info
);
191 // The last DnsProbeStatus received from the browser.
192 chrome_common_net::DnsProbeStatus last_probe_status_
;
194 // Information for the provisional / "pre-provisional" error page. NULL when
195 // there's no page pending, or the pending page is not an error page.
196 scoped_ptr
<ErrorPageInfo
> pending_error_page_info_
;
198 // Information for the committed error page. NULL when the committed page is
199 // not an error page.
200 scoped_ptr
<ErrorPageInfo
> committed_error_page_info_
;
202 GURL navigation_correction_url_
;
203 std::string language_
;
204 std::string country_code_
;
205 std::string api_key_
;
208 bool auto_reload_enabled_
;
209 scoped_ptr
<base::Timer
> auto_reload_timer_
;
211 // Is the browser online?
214 int auto_reload_count_
;
215 bool can_auto_reload_page_
;
217 // This value is set only when a navigation has been initiated from
218 // the error page. It is used to detect when such navigations result
220 Button navigation_from_button_
;
223 #endif // CHROME_RENDERER_NET_NET_ERROR_HELPER_CORE_H_