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 // Sends an HTTP request used to track which link on the page was clicked to
87 // the navigation correction service.
88 virtual void SendTrackingRequest(
89 const GURL& tracking_url,
90 const std::string& tracking_request_body) = 0;
92 // Starts a reload of the page in the observed frame.
93 virtual void ReloadPage() = 0;
95 // Load the original page from cache.
96 virtual void LoadPageFromCache(const GURL& page_url) = 0;
99 virtual ~Delegate() {}
102 struct NavigationCorrectionParams {
103 NavigationCorrectionParams();
104 ~NavigationCorrectionParams();
106 // URL used both for getting the suggestions and tracking clicks.
109 std::string language;
110 std::string country_code;
115 explicit NetErrorHelperCore(Delegate* delegate);
116 ~NetErrorHelperCore();
118 // Examines |frame| and |error| to see if this is an error worthy of a DNS
119 // probe. If it is, initializes |error_strings| based on |error|,
120 // |is_failed_post|, and |locale| with suitable strings and returns true.
121 // If not, returns false, in which case the caller should look up error
122 // strings directly using LocalizedError::GetNavigationErrorStrings.
124 // Updates the NetErrorHelper with the assumption the page will be loaded
126 void GetErrorHTML(FrameType frame_type,
127 const blink::WebURLError& error,
129 std::string* error_html);
131 // These methods handle tracking the actual state of the page.
132 void OnStartLoad(FrameType frame_type, PageType page_type);
133 void OnCommitLoad(FrameType frame_type);
134 void OnFinishLoad(FrameType frame_type);
137 void CancelPendingFetches();
139 // Called when an error page have has been retrieved over the network. |html|
140 // must be an empty string on error.
141 void OnNavigationCorrectionsFetched(const std::string& corrections,
142 const std::string& accept_languages,
145 // Notifies |this| that network error information from the browser process
146 // has been received.
147 void OnNetErrorInfo(chrome_common_net::DnsProbeStatus status);
149 void OnSetNavigationCorrectionInfo(const GURL& navigation_correction_url,
150 const std::string& language,
151 const std::string& country_code,
152 const std::string& api_key,
153 const GURL& search_url);
154 // Notifies |this| that the network's online status changed.
155 // Handler for NetworkStateChanged notification from the browser process. If
156 // the network state changes to online, this method is responsible for
157 // starting the auto-reload process.
159 // Warning: if there are many tabs sitting at an error page, this handler will
160 // be run at the same time for each of their top-level renderframes, which can
161 // cause many requests to be started at the same time. There's no current
162 // protection against this kind of "reload storm".
164 // TODO(rdsmith): prevent the reload storm.
165 void NetworkStateChanged(bool online);
167 void set_auto_reload_enabled(bool auto_reload_enabled) {
168 auto_reload_enabled_ = auto_reload_enabled;
171 int auto_reload_count() const { return auto_reload_count_; }
173 bool ShouldSuppressErrorPage(FrameType frame_type, const GURL& url);
175 void set_timer_for_testing(scoped_ptr<base::Timer> timer) {
176 auto_reload_timer_.reset(timer.release());
179 // Execute the effect of pressing the specified button.
180 // Note that the visual effects of the 'MORE' button are taken
181 // care of in JavaScript.
182 void ExecuteButtonPress(Button button);
184 // Reports to the correction service that the link with the given tracking
185 // ID was clicked. Only pages generated with information from the service
186 // have links with tracking IDs. Duplicate requests from the same page with
187 // the same tracking ID are ignored.
188 void TrackClick(int tracking_id);
191 struct ErrorPageInfo;
193 // Gets HTML for a main frame error page. Depending on
194 // |pending_error_page_info|, may use the navigation correction service, or
195 // show a DNS probe error page. May modify |pending_error_page_info|.
196 void GetErrorHtmlForMainFrame(ErrorPageInfo* pending_error_page_info,
197 std::string* error_html);
199 // Updates the currently displayed error page with a new error based on the
200 // most recently received DNS probe result. The page must have finished
201 // loading before this is called.
202 void UpdateErrorPage();
204 blink::WebURLError GetUpdatedError(const blink::WebURLError& error) const;
207 bool MaybeStartAutoReloadTimer();
208 void StartAutoReloadTimer();
209 void AutoReloadTimerFired();
211 static bool IsReloadableError(const ErrorPageInfo& info);
215 // The last DnsProbeStatus received from the browser.
216 chrome_common_net::DnsProbeStatus last_probe_status_;
218 // Information for the provisional / "pre-provisional" error page. NULL when
219 // there's no page pending, or the pending page is not an error page.
220 scoped_ptr<ErrorPageInfo> pending_error_page_info_;
222 // Information for the committed error page. NULL when the committed page is
223 // not an error page.
224 scoped_ptr<ErrorPageInfo> committed_error_page_info_;
226 NavigationCorrectionParams navigation_correction_params_;
228 bool auto_reload_enabled_;
229 scoped_ptr<base::Timer> auto_reload_timer_;
231 // Is the browser online?
234 int auto_reload_count_;
235 bool can_auto_reload_page_;
237 // This value is set only when a navigation has been initiated from
238 // the error page. It is used to detect when such navigations result
240 Button navigation_from_button_;
243 #endif // CHROME_RENDERER_NET_NET_ERROR_HELPER_CORE_H_