• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 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_H_
6 #define CHROME_RENDERER_NET_NET_ERROR_HELPER_H_
7 
8 #include <string>
9 
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/common/localized_error.h"
13 #include "chrome/common/net/net_error_info.h"
14 #include "chrome/renderer/net/net_error_helper_core.h"
15 #include "content/public/renderer/render_frame_observer.h"
16 #include "content/public/renderer/render_frame_observer_tracker.h"
17 #include "content/public/renderer/render_process_observer.h"
18 
19 class GURL;
20 
21 namespace blink {
22 class WebFrame;
23 class WebURLResponse;
24 struct WebURLError;
25 }
26 
27 namespace content {
28 class ResourceFetcher;
29 }
30 
31 // Listens for NetErrorInfo messages from the NetErrorTabHelper on the
32 // browser side and updates the error page with more details (currently, just
33 // DNS probe results) if/when available.
34 class NetErrorHelper
35     : public content::RenderFrameObserver,
36       public content::RenderFrameObserverTracker<NetErrorHelper>,
37       public content::RenderProcessObserver,
38       public NetErrorHelperCore::Delegate {
39  public:
40   explicit NetErrorHelper(content::RenderFrame* render_view);
41   virtual ~NetErrorHelper();
42 
43   // Button press notification from error page.
44   void ReloadButtonPressed();
45   void LoadStaleButtonPressed();
46   void MoreButtonPressed();
47 
48   // RenderFrameObserver implementation.
49   virtual void DidStartProvisionalLoad() OVERRIDE;
50   virtual void DidCommitProvisionalLoad(bool is_new_navigation) OVERRIDE;
51   virtual void DidFinishLoad() OVERRIDE;
52   virtual void OnStop() OVERRIDE;
53   virtual void WasShown() OVERRIDE;
54   virtual void WasHidden() OVERRIDE;
55 
56   // IPC::Listener implementation.
57   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
58 
59   // RenderProcessObserver implementation.
60   virtual void NetworkStateChanged(bool online) OVERRIDE;
61 
62   // Examines |frame| and |error| to see if this is an error worthy of a DNS
63   // probe.  If it is, initializes |error_strings| based on |error|,
64   // |is_failed_post|, and |locale| with suitable strings and returns true.
65   // If not, returns false, in which case the caller should look up error
66   // strings directly using LocalizedError::GetNavigationErrorStrings.
67   //
68   // Updates the NetErrorHelper with the assumption the page will be loaded
69   // immediately.
70   void GetErrorHTML(blink::WebFrame* frame,
71                     const blink::WebURLError& error,
72                     bool is_failed_post,
73                     std::string* error_html);
74 
75   // Returns whether a load for |url| in |frame| should have its error page
76   // suppressed.
77   bool ShouldSuppressErrorPage(blink::WebFrame* frame, const GURL& url);
78 
79   // Called when a link with the given tracking ID is pressed.
80   void TrackClick(int tracking_id);
81 
82  private:
83   // NetErrorHelperCore::Delegate implementation:
84   virtual void GenerateLocalizedErrorPage(
85       const blink::WebURLError& error,
86       bool is_failed_post,
87       scoped_ptr<LocalizedError::ErrorPageParams> params,
88       bool* reload_button_shown,
89       bool* load_stale_button_shown,
90       std::string* html) const OVERRIDE;
91   virtual void LoadErrorPageInMainFrame(const std::string& html,
92                                         const GURL& failed_url) OVERRIDE;
93   virtual void EnablePageHelperFunctions() OVERRIDE;
94   virtual void UpdateErrorPage(const blink::WebURLError& error,
95                                bool is_failed_post) OVERRIDE;
96   virtual void FetchNavigationCorrections(
97       const GURL& navigation_correction_url,
98       const std::string& navigation_correction_request_body) OVERRIDE;
99   virtual void CancelFetchNavigationCorrections() OVERRIDE;
100   virtual void SendTrackingRequest(
101       const GURL& tracking_url,
102       const std::string& tracking_request_body) OVERRIDE;
103   virtual void ReloadPage() OVERRIDE;
104   virtual void LoadPageFromCache(const GURL& page_url) OVERRIDE;
105 
106   void OnNetErrorInfo(int status);
107   void OnSetNavigationCorrectionInfo(const GURL& navigation_correction_url,
108                                      const std::string& language,
109                                      const std::string& country_code,
110                                      const std::string& api_key,
111                                      const GURL& search_url);
112 
113   void OnNavigationCorrectionsFetched(const blink::WebURLResponse& response,
114                                       const std::string& data);
115 
116   void OnTrackingRequestComplete(const blink::WebURLResponse& response,
117                                  const std::string& data);
118 
119   scoped_ptr<content::ResourceFetcher> correction_fetcher_;
120   scoped_ptr<content::ResourceFetcher> tracking_fetcher_;
121 
122   scoped_ptr<NetErrorHelperCore> core_;
123 
124   DISALLOW_COPY_AND_ASSIGN(NetErrorHelper);
125 };
126 
127 #endif  // CHROME_RENDERER_NET_NET_ERROR_HELPER_H_
128