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 "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/prefs/pref_member.h"
13 #include "chrome/browser/net/dns_probe_service.h"
14 #include "components/error_page/common/net_error_info.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "content/public/browser/web_contents_user_data.h"
18 namespace chrome_browser_net
{
20 // A TabHelper that monitors loads for certain types of network errors and
21 // does interesting things with them. Currently, starts DNS probes using the
22 // DnsProbeService whenever a page fails to load with a DNS-related error.
23 class NetErrorTabHelper
24 : public content::WebContentsObserver
,
25 public content::WebContentsUserData
<NetErrorTabHelper
> {
29 TESTING_FORCE_DISABLED
,
33 typedef base::Callback
<void(chrome_common_net::DnsProbeStatus
)>
34 DnsProbeStatusSnoopCallback
;
36 ~NetErrorTabHelper() override
;
38 static void set_state_for_testing(TestingState testing_state
);
40 // Sets a callback that will be called immediately after the helper sends
41 // a NetErrorHelper IPC. (Used by the DNS probe browser test to know when to
42 // check the error page for updates, instead of polling.)
43 void set_dns_probe_status_snoop_callback_for_testing(
44 const DnsProbeStatusSnoopCallback
& dns_probe_status_snoop_callback
) {
45 dns_probe_status_snoop_callback_
= dns_probe_status_snoop_callback
;
48 // content::WebContentsObserver implementation.
49 void RenderViewCreated(content::RenderViewHost
* render_view_host
) override
;
51 void DidStartNavigationToPendingEntry(
53 content::NavigationController::ReloadType reload_type
) override
;
55 void DidStartProvisionalLoadForFrame(
56 content::RenderFrameHost
* render_frame_host
,
57 const GURL
& validated_url
,
59 bool is_iframe_srcdoc
) override
;
61 void DidCommitProvisionalLoadForFrame(
62 content::RenderFrameHost
* render_frame_host
,
64 ui::PageTransition transition_type
) override
;
66 void DidFailProvisionalLoad(content::RenderFrameHost
* render_frame_host
,
67 const GURL
& validated_url
,
69 const base::string16
& error_description
,
70 bool was_ignored_by_handler
) override
;
72 bool OnMessageReceived(const IPC::Message
& message
,
73 content::RenderFrameHost
* render_frame_host
) override
;
77 // |contents| is the WebContents of the tab this NetErrorTabHelper is
79 explicit NetErrorTabHelper(content::WebContents
* contents
);
80 virtual void StartDnsProbe();
81 virtual void SendInfo();
82 void OnDnsProbeFinished(chrome_common_net::DnsProbeStatus result
);
84 chrome_common_net::DnsProbeStatus
dns_probe_status() const {
85 return dns_probe_status_
;
89 friend class content::WebContentsUserData
<NetErrorTabHelper
>;
91 void OnMainFrameDnsError();
93 void InitializePref(content::WebContents
* contents
);
94 bool ProbesAllowed() const;
96 // Sanitizes |url| and shows a dialog for it.
97 void RunNetworkDiagnostics(const GURL
& url
);
99 // Shows the diagnostics dialog after its been sanitized, virtual for
101 virtual void RunNetworkDiagnosticsHelper(const GURL
& sanitized_url
);
103 // True if the last provisional load that started was for an error page.
106 // True if the helper has seen a main frame page load fail with a DNS error,
107 // but has not yet seen a new page commit successfully afterwards.
108 bool dns_error_active_
;
110 // True if the helper has seen an error page commit while |dns_error_active_|
111 // is true. (This should never be true if |dns_error_active_| is false.)
112 bool dns_error_page_committed_
;
114 // The status of a DNS probe that may or may not have started or finished.
115 // Since the renderer can change out from under the helper (in cross-process
116 // navigations), it re-sends the status whenever an error page commits.
117 chrome_common_net::DnsProbeStatus dns_probe_status_
;
119 // Optional callback for browser test to snoop on outgoing NetErrorInfo IPCs.
120 DnsProbeStatusSnoopCallback dns_probe_status_snoop_callback_
;
122 // "Use a web service to resolve navigation errors" preference is required
124 BooleanPrefMember resolve_errors_with_web_service_
;
126 base::WeakPtrFactory
<NetErrorTabHelper
> weak_factory_
;
128 DISALLOW_COPY_AND_ASSIGN(NetErrorTabHelper
);
131 } // namespace chrome_browser_net
133 #endif // CHROME_BROWSER_NET_NET_ERROR_TAB_HELPER_H_