Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / net / net_error_tab_helper.h
blob490b4325ec5157224718b9598efd6b43271de8b6
1 // Copyright (c) 2012 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_BROWSER_NET_NET_ERROR_TAB_HELPER_H_
6 #define CHROME_BROWSER_NET_NET_ERROR_TAB_HELPER_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/prefs/pref_member.h"
15 #include "chrome/browser/net/dns_probe_service.h"
16 #include "components/error_page/common/net_error_info.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "content/public/browser/web_contents_user_data.h"
20 namespace chrome_browser_net {
22 // A TabHelper that monitors loads for certain types of network errors and
23 // does interesting things with them. Currently, starts DNS probes using the
24 // DnsProbeService whenever a page fails to load with a DNS-related error.
25 class NetErrorTabHelper
26 : public content::WebContentsObserver,
27 public content::WebContentsUserData<NetErrorTabHelper> {
28 public:
29 enum TestingState {
30 TESTING_DEFAULT,
31 TESTING_FORCE_DISABLED,
32 TESTING_FORCE_ENABLED
35 typedef base::Callback<void(error_page::DnsProbeStatus)>
36 DnsProbeStatusSnoopCallback;
38 ~NetErrorTabHelper() override;
40 static void set_state_for_testing(TestingState testing_state);
42 // Sets a callback that will be called immediately after the helper sends
43 // a NetErrorHelper IPC. (Used by the DNS probe browser test to know when to
44 // check the error page for updates, instead of polling.)
45 void set_dns_probe_status_snoop_callback_for_testing(
46 const DnsProbeStatusSnoopCallback& dns_probe_status_snoop_callback) {
47 dns_probe_status_snoop_callback_ = dns_probe_status_snoop_callback;
50 // content::WebContentsObserver implementation.
51 void RenderViewCreated(content::RenderViewHost* render_view_host) override;
53 void DidStartNavigationToPendingEntry(
54 const GURL& url,
55 content::NavigationController::ReloadType reload_type) override;
57 void DidStartProvisionalLoadForFrame(
58 content::RenderFrameHost* render_frame_host,
59 const GURL& validated_url,
60 bool is_error_page,
61 bool is_iframe_srcdoc) override;
63 void DidCommitProvisionalLoadForFrame(
64 content::RenderFrameHost* render_frame_host,
65 const GURL& url,
66 ui::PageTransition transition_type) override;
68 void DidFailProvisionalLoad(content::RenderFrameHost* render_frame_host,
69 const GURL& validated_url,
70 int error_code,
71 const base::string16& error_description,
72 bool was_ignored_by_handler) override;
74 bool OnMessageReceived(const IPC::Message& message,
75 content::RenderFrameHost* render_frame_host) override;
78 protected:
79 // |contents| is the WebContents of the tab this NetErrorTabHelper is
80 // attached to.
81 explicit NetErrorTabHelper(content::WebContents* contents);
82 virtual void StartDnsProbe();
83 virtual void SendInfo();
84 void OnDnsProbeFinished(error_page::DnsProbeStatus result);
86 error_page::DnsProbeStatus dns_probe_status() const {
87 return dns_probe_status_;
90 private:
91 friend class content::WebContentsUserData<NetErrorTabHelper>;
93 void OnMainFrameDnsError();
95 void InitializePref(content::WebContents* contents);
96 bool ProbesAllowed() const;
98 // Sanitizes |url| and shows a dialog for it.
99 void RunNetworkDiagnostics(const GURL& url);
101 // Shows the diagnostics dialog after its been sanitized, virtual for
102 // testing.
103 virtual void RunNetworkDiagnosticsHelper(const std::string& sanitized_url);
105 // True if the last provisional load that started was for an error page.
106 bool is_error_page_;
108 // True if the helper has seen a main frame page load fail with a DNS error,
109 // but has not yet seen a new page commit successfully afterwards.
110 bool dns_error_active_;
112 // True if the helper has seen an error page commit while |dns_error_active_|
113 // is true. (This should never be true if |dns_error_active_| is false.)
114 bool dns_error_page_committed_;
116 // The status of a DNS probe that may or may not have started or finished.
117 // Since the renderer can change out from under the helper (in cross-process
118 // navigations), it re-sends the status whenever an error page commits.
119 error_page::DnsProbeStatus dns_probe_status_;
121 // Optional callback for browser test to snoop on outgoing NetErrorInfo IPCs.
122 DnsProbeStatusSnoopCallback dns_probe_status_snoop_callback_;
124 // "Use a web service to resolve navigation errors" preference is required
125 // to allow probes.
126 BooleanPrefMember resolve_errors_with_web_service_;
128 base::WeakPtrFactory<NetErrorTabHelper> weak_factory_;
130 DISALLOW_COPY_AND_ASSIGN(NetErrorTabHelper);
133 } // namespace chrome_browser_net
135 #endif // CHROME_BROWSER_NET_NET_ERROR_TAB_HELPER_H_