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 #include "chrome/browser/net/net_error_tab_helper.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/io_thread.h"
11 #include "chrome/browser/net/dns_probe_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/net/net_error_info.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/common/render_messages.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "net/base/net_errors.h"
19 using chrome_common_net::DnsProbeStatus
;
20 using chrome_common_net::DnsProbeStatusToString
;
21 using content::BrowserContext
;
22 using content::BrowserThread
;
23 using content::PageTransition
;
24 using content::RenderViewHost
;
25 using content::WebContents
;
26 using content::WebContentsObserver
;
28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome_browser_net::NetErrorTabHelper
);
30 namespace chrome_browser_net
{
34 static NetErrorTabHelper::TestingState testing_state_
=
35 NetErrorTabHelper::TESTING_DEFAULT
;
37 // Returns whether |net_error| is a DNS-related error (and therefore whether
38 // the tab helper should start a DNS probe after receiving it.)
39 bool IsDnsError(int net_error
) {
40 return net_error
== net::ERR_NAME_NOT_RESOLVED
||
41 net_error
== net::ERR_NAME_RESOLUTION_FAILED
;
44 void OnDnsProbeFinishedOnIOThread(
45 const base::Callback
<void(DnsProbeStatus
)>& callback
,
46 DnsProbeStatus result
) {
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
49 BrowserThread::PostTask(
52 base::Bind(callback
, result
));
55 // Can only access g_browser_process->io_thread() from the browser thread,
56 // so have to pass it in to the callback instead of dereferencing it here.
57 void StartDnsProbeOnIOThread(
58 const base::Callback
<void(DnsProbeStatus
)>& callback
,
59 IOThread
* io_thread
) {
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
62 DnsProbeService
* probe_service
=
63 io_thread
->globals()->dns_probe_service
.get();
65 probe_service
->ProbeDns(base::Bind(&OnDnsProbeFinishedOnIOThread
, callback
));
70 NetErrorTabHelper::~NetErrorTabHelper() {
74 void NetErrorTabHelper::set_state_for_testing(TestingState state
) {
75 testing_state_
= state
;
78 void NetErrorTabHelper::DidStartProvisionalLoadForFrame(
80 int64 parent_frame_id
,
82 const GURL
& validated_url
,
84 bool is_iframe_srcdoc
,
85 RenderViewHost
* render_view_host
) {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
91 is_error_page_
= is_error_page
;
94 void NetErrorTabHelper::DidCommitProvisionalLoadForFrame(
96 const base::string16
& frame_unique_name
,
99 PageTransition transition_type
,
100 RenderViewHost
* render_view_host
) {
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
106 // Resend status every time an error page commits; this is somewhat spammy,
107 // but ensures that the status will make it to the real error page, even if
108 // the link doctor loads a blank intermediate page or the tab switches
109 // renderer processes.
110 if (is_error_page_
&& dns_error_active_
) {
111 dns_error_page_committed_
= true;
112 DVLOG(1) << "Committed error page; resending status.";
115 dns_error_active_
= false;
116 dns_error_page_committed_
= false;
120 void NetErrorTabHelper::DidFailProvisionalLoad(
122 const base::string16
& frame_unique_name
,
124 const GURL
& validated_url
,
126 const base::string16
& error_description
,
127 RenderViewHost
* render_view_host
) {
128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
133 if (IsDnsError(error_code
)) {
134 dns_error_active_
= true;
135 OnMainFrameDnsError();
139 NetErrorTabHelper::NetErrorTabHelper(WebContents
* contents
)
140 : WebContentsObserver(contents
),
142 is_error_page_(false),
143 dns_error_active_(false),
144 dns_error_page_committed_(false),
145 dns_probe_status_(chrome_common_net::DNS_PROBE_POSSIBLE
) {
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
148 // If this helper is under test, it won't have a WebContents.
150 InitializePref(contents
);
153 void NetErrorTabHelper::OnMainFrameDnsError() {
154 if (ProbesAllowed()) {
155 // Don't start more than one probe at a time.
156 if (dns_probe_status_
!= chrome_common_net::DNS_PROBE_STARTED
) {
158 dns_probe_status_
= chrome_common_net::DNS_PROBE_STARTED
;
161 dns_probe_status_
= chrome_common_net::DNS_PROBE_NOT_RUN
;
165 void NetErrorTabHelper::StartDnsProbe() {
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
167 DCHECK(dns_error_active_
);
168 DCHECK_NE(chrome_common_net::DNS_PROBE_STARTED
, dns_probe_status_
);
170 DVLOG(1) << "Starting DNS probe.";
172 BrowserThread::PostTask(
175 base::Bind(&StartDnsProbeOnIOThread
,
176 base::Bind(&NetErrorTabHelper::OnDnsProbeFinished
,
177 weak_factory_
.GetWeakPtr()),
178 g_browser_process
->io_thread()));
181 void NetErrorTabHelper::OnDnsProbeFinished(DnsProbeStatus result
) {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
183 DCHECK_EQ(chrome_common_net::DNS_PROBE_STARTED
, dns_probe_status_
);
184 DCHECK(chrome_common_net::DnsProbeStatusIsFinished(result
));
186 DVLOG(1) << "Finished DNS probe with result "
187 << DnsProbeStatusToString(result
) << ".";
189 dns_probe_status_
= result
;
191 if (dns_error_page_committed_
)
195 void NetErrorTabHelper::InitializePref(WebContents
* contents
) {
198 BrowserContext
* browser_context
= contents
->GetBrowserContext();
199 Profile
* profile
= Profile::FromBrowserContext(browser_context
);
200 resolve_errors_with_web_service_
.Init(
201 prefs::kAlternateErrorPagesEnabled
,
202 profile
->GetPrefs());
205 bool NetErrorTabHelper::ProbesAllowed() const {
206 if (testing_state_
!= TESTING_DEFAULT
)
207 return testing_state_
== TESTING_FORCE_ENABLED
;
209 // TODO(ttuttle): Disable on mobile?
210 return *resolve_errors_with_web_service_
;
213 void NetErrorTabHelper::SendInfo() {
214 DCHECK_NE(chrome_common_net::DNS_PROBE_POSSIBLE
, dns_probe_status_
);
215 DCHECK(dns_error_page_committed_
);
217 DVLOG(1) << "Sending status " << DnsProbeStatusToString(dns_probe_status_
);
218 Send(new ChromeViewMsg_NetErrorInfo(routing_id(), dns_probe_status_
));
220 if (!dns_probe_status_snoop_callback_
.is_null())
221 dns_probe_status_snoop_callback_
.Run(dns_probe_status_
);
224 } // namespace chrome_browser_net