Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / renderer / net / net_error_helper_core.h
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.
4
5 #ifndef CHROME_RENDERER_NET_NET_ERROR_HELPER_CORE_H_
6 #define CHROME_RENDERER_NET_NET_ERROR_HELPER_CORE_H_
7
8 #include <string>
9
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"
16 #include "url/gurl.h"
17
18 namespace base {
19 class ListValue;
20 }
21
22 namespace blink {
23 struct WebURLError;
24 }
25
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 {
31  public:
32   enum FrameType {
33     MAIN_FRAME,
34     SUB_FRAME,
35   };
36
37   enum PageType {
38     NON_ERROR_PAGE,
39     ERROR_PAGE,
40   };
41
42   enum Button {
43     NO_BUTTON,
44     RELOAD_BUTTON,
45     LOAD_STALE_BUTTON,
46     MORE_BUTTON,
47   };
48
49   // The Delegate handles all interaction with the RenderView, WebFrame, and
50   // the network, as well as the generation of error pages.
51   class Delegate {
52    public:
53     // Generates an error page's HTML for the given error.
54     virtual void GenerateLocalizedErrorPage(
55         const blink::WebURLError& error,
56         bool is_failed_post,
57         scoped_ptr<LocalizedError::ErrorPageParams> params,
58         bool* reload_button_shown,
59         bool* load_stale_button_shown,
60         std::string* html) const = 0;
61
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;
65
66     // Create extra Javascript bindings in the error page.
67     virtual void EnablePageHelperFunctions() = 0;
68
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;
74
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;
81
82     // Cancels fetching navigation corrections.  Does nothing if no fetch is
83     // ongoing.
84     virtual void CancelFetchNavigationCorrections() = 0;
85
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;
91
92     // Starts a reload of the page in the observed frame.
93     virtual void ReloadPage() = 0;
94
95     // Load the original page from cache.
96     virtual void LoadPageFromCache(const GURL& page_url) = 0;
97
98    protected:
99     virtual ~Delegate() {}
100   };
101
102   struct NavigationCorrectionParams {
103     NavigationCorrectionParams();
104     ~NavigationCorrectionParams();
105
106     // URL used both for getting the suggestions and tracking clicks.
107     GURL url;
108
109     std::string language;
110     std::string country_code;
111     std::string api_key;
112     GURL search_url;
113   };
114
115   explicit NetErrorHelperCore(Delegate* delegate);
116   ~NetErrorHelperCore();
117
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.
123   //
124   // Updates the NetErrorHelper with the assumption the page will be loaded
125   // immediately.
126   void GetErrorHTML(FrameType frame_type,
127                     const blink::WebURLError& error,
128                     bool is_failed_post,
129                     std::string* error_html);
130
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);
135   void OnStop();
136
137   void CancelPendingFetches();
138
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,
143                                       bool is_rtl);
144
145   // Notifies |this| that network error information from the browser process
146   // has been received.
147   void OnNetErrorInfo(chrome_common_net::DnsProbeStatus status);
148
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.
158   //
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".
163   //
164   // TODO(rdsmith): prevent the reload storm.
165   void NetworkStateChanged(bool online);
166
167   void set_auto_reload_enabled(bool auto_reload_enabled) {
168     auto_reload_enabled_ = auto_reload_enabled;
169   }
170
171   int auto_reload_count() const { return auto_reload_count_; }
172
173   bool ShouldSuppressErrorPage(FrameType frame_type, const GURL& url);
174
175   void set_timer_for_testing(scoped_ptr<base::Timer> timer) {
176     auto_reload_timer_.reset(timer.release());
177   }
178
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);
183
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);
189
190  private:
191   struct ErrorPageInfo;
192
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);
198
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();
203
204   blink::WebURLError GetUpdatedError(const blink::WebURLError& error) const;
205
206   void Reload();
207   bool MaybeStartAutoReloadTimer();
208   void StartAutoReloadTimer();
209   void AutoReloadTimerFired();
210
211   static bool IsReloadableError(const ErrorPageInfo& info);
212
213   Delegate* delegate_;
214
215   // The last DnsProbeStatus received from the browser.
216   chrome_common_net::DnsProbeStatus last_probe_status_;
217
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_;
221
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_;
225
226   NavigationCorrectionParams navigation_correction_params_;
227
228   bool auto_reload_enabled_;
229   scoped_ptr<base::Timer> auto_reload_timer_;
230
231   // Is the browser online?
232   bool online_;
233
234   int auto_reload_count_;
235   bool can_auto_reload_page_;
236
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
239   // in errors.
240   Button navigation_from_button_;
241 };
242
243 #endif  // CHROME_RENDERER_NET_NET_ERROR_HELPER_CORE_H_