OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/net/net_error_tab_helper.h" | 5 #include "chrome/browser/net/net_error_tab_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 // Returns whether |net_error| is a DNS-related error (and therefore whether | 39 // Returns whether |net_error| is a DNS-related error (and therefore whether |
40 // the tab helper should start a DNS probe after receiving it.) | 40 // the tab helper should start a DNS probe after receiving it.) |
41 bool IsDnsError(int net_error) { | 41 bool IsDnsError(int net_error) { |
42 return net_error == net::ERR_NAME_NOT_RESOLVED || | 42 return net_error == net::ERR_NAME_NOT_RESOLVED || |
43 net_error == net::ERR_NAME_RESOLUTION_FAILED; | 43 net_error == net::ERR_NAME_RESOLUTION_FAILED; |
44 } | 44 } |
45 | 45 |
46 void OnDnsProbeFinishedOnIOThread( | 46 void OnDnsProbeFinishedOnIOThread( |
47 const base::Callback<void(DnsProbeStatus)>& callback, | 47 const base::Callback<void(DnsProbeStatus)>& callback, |
48 DnsProbeStatus result) { | 48 DnsProbeStatus result) { |
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 49 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
50 | 50 |
51 BrowserThread::PostTask( | 51 BrowserThread::PostTask( |
52 BrowserThread::UI, | 52 BrowserThread::UI, |
53 FROM_HERE, | 53 FROM_HERE, |
54 base::Bind(callback, result)); | 54 base::Bind(callback, result)); |
55 } | 55 } |
56 | 56 |
57 // Can only access g_browser_process->io_thread() from the browser thread, | 57 // Can only access g_browser_process->io_thread() from the browser thread, |
58 // so have to pass it in to the callback instead of dereferencing it here. | 58 // so have to pass it in to the callback instead of dereferencing it here. |
59 void StartDnsProbeOnIOThread( | 59 void StartDnsProbeOnIOThread( |
60 const base::Callback<void(DnsProbeStatus)>& callback, | 60 const base::Callback<void(DnsProbeStatus)>& callback, |
61 IOThread* io_thread) { | 61 IOThread* io_thread) { |
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 62 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
63 | 63 |
64 DnsProbeService* probe_service = | 64 DnsProbeService* probe_service = |
65 io_thread->globals()->dns_probe_service.get(); | 65 io_thread->globals()->dns_probe_service.get(); |
66 | 66 |
67 probe_service->ProbeDns(base::Bind(&OnDnsProbeFinishedOnIOThread, callback)); | 67 probe_service->ProbeDns(base::Bind(&OnDnsProbeFinishedOnIOThread, callback)); |
68 } | 68 } |
69 | 69 |
70 } // namespace | 70 } // namespace |
71 | 71 |
72 NetErrorTabHelper::~NetErrorTabHelper() { | 72 NetErrorTabHelper::~NetErrorTabHelper() { |
73 } | 73 } |
74 | 74 |
75 // static | 75 // static |
76 void NetErrorTabHelper::set_state_for_testing(TestingState state) { | 76 void NetErrorTabHelper::set_state_for_testing(TestingState state) { |
77 testing_state_ = state; | 77 testing_state_ = state; |
78 } | 78 } |
79 | 79 |
80 void NetErrorTabHelper::DidStartNavigationToPendingEntry( | 80 void NetErrorTabHelper::DidStartNavigationToPendingEntry( |
81 const GURL& url, | 81 const GURL& url, |
82 content::NavigationController::ReloadType reload_type) { | 82 content::NavigationController::ReloadType reload_type) { |
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 83 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
84 | 84 |
85 if (!is_error_page_) | 85 if (!is_error_page_) |
86 return; | 86 return; |
87 | 87 |
88 // Only record reloads. | 88 // Only record reloads. |
89 if (reload_type != content::NavigationController::NO_RELOAD) { | 89 if (reload_type != content::NavigationController::NO_RELOAD) { |
90 chrome_common_net::RecordEvent( | 90 chrome_common_net::RecordEvent( |
91 chrome_common_net::NETWORK_ERROR_PAGE_BROWSER_INITIATED_RELOAD); | 91 chrome_common_net::NETWORK_ERROR_PAGE_BROWSER_INITIATED_RELOAD); |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 void NetErrorTabHelper::DidStartProvisionalLoadForFrame( | 95 void NetErrorTabHelper::DidStartProvisionalLoadForFrame( |
96 content::RenderFrameHost* render_frame_host, | 96 content::RenderFrameHost* render_frame_host, |
97 const GURL& validated_url, | 97 const GURL& validated_url, |
98 bool is_error_page, | 98 bool is_error_page, |
99 bool is_iframe_srcdoc) { | 99 bool is_iframe_srcdoc) { |
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 100 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
101 | 101 |
102 if (render_frame_host->GetParent()) | 102 if (render_frame_host->GetParent()) |
103 return; | 103 return; |
104 | 104 |
105 is_error_page_ = is_error_page; | 105 is_error_page_ = is_error_page; |
106 } | 106 } |
107 | 107 |
108 void NetErrorTabHelper::DidCommitProvisionalLoadForFrame( | 108 void NetErrorTabHelper::DidCommitProvisionalLoadForFrame( |
109 content::RenderFrameHost* render_frame_host, | 109 content::RenderFrameHost* render_frame_host, |
110 const GURL& url, | 110 const GURL& url, |
111 PageTransition transition_type) { | 111 PageTransition transition_type) { |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
113 | 113 |
114 if (render_frame_host->GetParent()) | 114 if (render_frame_host->GetParent()) |
115 return; | 115 return; |
116 | 116 |
117 // Resend status every time an error page commits; this is somewhat spammy, | 117 // Resend status every time an error page commits; this is somewhat spammy, |
118 // but ensures that the status will make it to the real error page, even if | 118 // but ensures that the status will make it to the real error page, even if |
119 // the link doctor loads a blank intermediate page or the tab switches | 119 // the link doctor loads a blank intermediate page or the tab switches |
120 // renderer processes. | 120 // renderer processes. |
121 if (is_error_page_ && dns_error_active_) { | 121 if (is_error_page_ && dns_error_active_) { |
122 dns_error_page_committed_ = true; | 122 dns_error_page_committed_ = true; |
123 DVLOG(1) << "Committed error page; resending status."; | 123 DVLOG(1) << "Committed error page; resending status."; |
124 SendInfo(); | 124 SendInfo(); |
125 } else { | 125 } else { |
126 dns_error_active_ = false; | 126 dns_error_active_ = false; |
127 dns_error_page_committed_ = false; | 127 dns_error_page_committed_ = false; |
128 } | 128 } |
129 } | 129 } |
130 | 130 |
131 void NetErrorTabHelper::DidFailProvisionalLoad( | 131 void NetErrorTabHelper::DidFailProvisionalLoad( |
132 content::RenderFrameHost* render_frame_host, | 132 content::RenderFrameHost* render_frame_host, |
133 const GURL& validated_url, | 133 const GURL& validated_url, |
134 int error_code, | 134 int error_code, |
135 const base::string16& error_description) { | 135 const base::string16& error_description) { |
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 136 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
137 | 137 |
138 if (render_frame_host->GetParent()) | 138 if (render_frame_host->GetParent()) |
139 return; | 139 return; |
140 | 140 |
141 if (IsDnsError(error_code)) { | 141 if (IsDnsError(error_code)) { |
142 dns_error_active_ = true; | 142 dns_error_active_ = true; |
143 OnMainFrameDnsError(); | 143 OnMainFrameDnsError(); |
144 } | 144 } |
145 } | 145 } |
146 | 146 |
147 NetErrorTabHelper::NetErrorTabHelper(WebContents* contents) | 147 NetErrorTabHelper::NetErrorTabHelper(WebContents* contents) |
148 : WebContentsObserver(contents), | 148 : WebContentsObserver(contents), |
149 is_error_page_(false), | 149 is_error_page_(false), |
150 dns_error_active_(false), | 150 dns_error_active_(false), |
151 dns_error_page_committed_(false), | 151 dns_error_page_committed_(false), |
152 dns_probe_status_(chrome_common_net::DNS_PROBE_POSSIBLE), | 152 dns_probe_status_(chrome_common_net::DNS_PROBE_POSSIBLE), |
153 weak_factory_(this) { | 153 weak_factory_(this) { |
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 154 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
155 | 155 |
156 // If this helper is under test, it won't have a WebContents. | 156 // If this helper is under test, it won't have a WebContents. |
157 if (contents) | 157 if (contents) |
158 InitializePref(contents); | 158 InitializePref(contents); |
159 } | 159 } |
160 | 160 |
161 void NetErrorTabHelper::OnMainFrameDnsError() { | 161 void NetErrorTabHelper::OnMainFrameDnsError() { |
162 if (ProbesAllowed()) { | 162 if (ProbesAllowed()) { |
163 // Don't start more than one probe at a time. | 163 // Don't start more than one probe at a time. |
164 if (dns_probe_status_ != chrome_common_net::DNS_PROBE_STARTED) { | 164 if (dns_probe_status_ != chrome_common_net::DNS_PROBE_STARTED) { |
165 StartDnsProbe(); | 165 StartDnsProbe(); |
166 dns_probe_status_ = chrome_common_net::DNS_PROBE_STARTED; | 166 dns_probe_status_ = chrome_common_net::DNS_PROBE_STARTED; |
167 } | 167 } |
168 } else { | 168 } else { |
169 dns_probe_status_ = chrome_common_net::DNS_PROBE_NOT_RUN; | 169 dns_probe_status_ = chrome_common_net::DNS_PROBE_NOT_RUN; |
170 } | 170 } |
171 } | 171 } |
172 | 172 |
173 void NetErrorTabHelper::StartDnsProbe() { | 173 void NetErrorTabHelper::StartDnsProbe() { |
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 174 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
175 DCHECK(dns_error_active_); | 175 DCHECK(dns_error_active_); |
176 DCHECK_NE(chrome_common_net::DNS_PROBE_STARTED, dns_probe_status_); | 176 DCHECK_NE(chrome_common_net::DNS_PROBE_STARTED, dns_probe_status_); |
177 | 177 |
178 DVLOG(1) << "Starting DNS probe."; | 178 DVLOG(1) << "Starting DNS probe."; |
179 | 179 |
180 BrowserThread::PostTask( | 180 BrowserThread::PostTask( |
181 BrowserThread::IO, | 181 BrowserThread::IO, |
182 FROM_HERE, | 182 FROM_HERE, |
183 base::Bind(&StartDnsProbeOnIOThread, | 183 base::Bind(&StartDnsProbeOnIOThread, |
184 base::Bind(&NetErrorTabHelper::OnDnsProbeFinished, | 184 base::Bind(&NetErrorTabHelper::OnDnsProbeFinished, |
185 weak_factory_.GetWeakPtr()), | 185 weak_factory_.GetWeakPtr()), |
186 g_browser_process->io_thread())); | 186 g_browser_process->io_thread())); |
187 } | 187 } |
188 | 188 |
189 void NetErrorTabHelper::OnDnsProbeFinished(DnsProbeStatus result) { | 189 void NetErrorTabHelper::OnDnsProbeFinished(DnsProbeStatus result) { |
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 190 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
191 DCHECK_EQ(chrome_common_net::DNS_PROBE_STARTED, dns_probe_status_); | 191 DCHECK_EQ(chrome_common_net::DNS_PROBE_STARTED, dns_probe_status_); |
192 DCHECK(chrome_common_net::DnsProbeStatusIsFinished(result)); | 192 DCHECK(chrome_common_net::DnsProbeStatusIsFinished(result)); |
193 | 193 |
194 DVLOG(1) << "Finished DNS probe with result " | 194 DVLOG(1) << "Finished DNS probe with result " |
195 << DnsProbeStatusToString(result) << "."; | 195 << DnsProbeStatusToString(result) << "."; |
196 | 196 |
197 dns_probe_status_ = result; | 197 dns_probe_status_ = result; |
198 | 198 |
199 if (dns_error_page_committed_) | 199 if (dns_error_page_committed_) |
200 SendInfo(); | 200 SendInfo(); |
(...skipping 24 matching lines...) Expand all Loading... |
225 DVLOG(1) << "Sending status " << DnsProbeStatusToString(dns_probe_status_); | 225 DVLOG(1) << "Sending status " << DnsProbeStatusToString(dns_probe_status_); |
226 content::RenderFrameHost* rfh = web_contents()->GetMainFrame(); | 226 content::RenderFrameHost* rfh = web_contents()->GetMainFrame(); |
227 rfh->Send(new ChromeViewMsg_NetErrorInfo(rfh->GetRoutingID(), | 227 rfh->Send(new ChromeViewMsg_NetErrorInfo(rfh->GetRoutingID(), |
228 dns_probe_status_)); | 228 dns_probe_status_)); |
229 | 229 |
230 if (!dns_probe_status_snoop_callback_.is_null()) | 230 if (!dns_probe_status_snoop_callback_.is_null()) |
231 dns_probe_status_snoop_callback_.Run(dns_probe_status_); | 231 dns_probe_status_snoop_callback_.Run(dns_probe_status_); |
232 } | 232 } |
233 | 233 |
234 } // namespace chrome_browser_net | 234 } // namespace chrome_browser_net |
OLD | NEW |