Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: chrome/browser/net/net_error_tab_helper.cc

Issue 13270005: Display DNS probe results. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix one last nit Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/metrics/field_trial.h" 8 #include "base/metrics/field_trial.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"
11 #include "chrome/browser/io_thread.h" 11 #include "chrome/browser/io_thread.h"
12 #include "chrome/browser/net/dns_probe_service.h" 12 #include "chrome/browser/net/dns_probe_service.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/net/net_error_info.h" 14 #include "chrome/common/net/net_error_info.h"
15 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/render_messages.h" 16 #include "chrome/common/render_messages.h"
17 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 #include "net/base/net_errors.h" 18 #include "net/base/net_errors.h"
19 19
20 using base::FieldTrialList; 20 using base::FieldTrialList;
21 using chrome_common_net::DnsProbeResult; 21 using chrome_common_net::DnsProbeStatus;
22 using chrome_common_net::DnsProbeStatusToString;
22 using content::BrowserContext; 23 using content::BrowserContext;
23 using content::BrowserThread; 24 using content::BrowserThread;
24 using content::PageTransition; 25 using content::PageTransition;
25 using content::RenderViewHost; 26 using content::RenderViewHost;
26 using content::WebContents; 27 using content::WebContents;
27 using content::WebContentsObserver; 28 using content::WebContentsObserver;
28 29
29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome_browser_net::NetErrorTabHelper); 30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome_browser_net::NetErrorTabHelper);
30 31
31 namespace chrome_browser_net { 32 namespace chrome_browser_net {
32 33
33 namespace { 34 namespace {
34 35
35 const char kDnsProbeFieldTrialName[] = "DnsProbe-Enable";
36 const char kDnsProbeFieldTrialEnableGroupName[] = "enable";
37
38 static NetErrorTabHelper::TestingState testing_state_ = 36 static NetErrorTabHelper::TestingState testing_state_ =
39 NetErrorTabHelper::TESTING_DEFAULT; 37 NetErrorTabHelper::TESTING_DEFAULT;
40 38
41 // 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
42 // the tab helper should start a DNS probe after receiving it.) 40 // the tab helper should start a DNS probe after receiving it.)
43 bool IsDnsError(int net_error) { 41 bool IsDnsError(int net_error) {
44 return net_error == net::ERR_NAME_NOT_RESOLVED || 42 return net_error == net::ERR_NAME_NOT_RESOLVED ||
45 net_error == net::ERR_NAME_RESOLUTION_FAILED; 43 net_error == net::ERR_NAME_RESOLUTION_FAILED;
46 } 44 }
47 45
48 bool GetEnabledByTrial() {
49 return (FieldTrialList::FindFullName(kDnsProbeFieldTrialName)
50 == kDnsProbeFieldTrialEnableGroupName);
51 }
52
53 NetErrorTracker::FrameType GetFrameType(bool is_main_frame) {
54 return is_main_frame ? NetErrorTracker::FRAME_MAIN
55 : NetErrorTracker::FRAME_SUB;
56 }
57
58 NetErrorTracker::PageType GetPageType(bool is_error_page) {
59 return is_error_page ? NetErrorTracker::PAGE_ERROR
60 : NetErrorTracker::PAGE_NORMAL;
61 }
62
63 NetErrorTracker::ErrorType GetErrorType(int net_error) {
64 return IsDnsError(net_error) ? NetErrorTracker::ERROR_DNS
65 : NetErrorTracker::ERROR_OTHER;
66 }
67
68 void OnDnsProbeFinishedOnIOThread( 46 void OnDnsProbeFinishedOnIOThread(
69 const base::Callback<void(DnsProbeResult)>& callback, 47 const base::Callback<void(DnsProbeStatus)>& callback,
70 DnsProbeResult result) { 48 DnsProbeStatus result) {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
72 50
73 DVLOG(1) << "DNS probe finished with result " << result;
74
75 BrowserThread::PostTask( 51 BrowserThread::PostTask(
76 BrowserThread::UI, 52 BrowserThread::UI,
77 FROM_HERE, 53 FROM_HERE,
78 base::Bind(callback, result)); 54 base::Bind(callback, result));
79 } 55 }
80 56
81 // We 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,
82 // so we 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.
83 void StartDnsProbeOnIOThread( 59 void StartDnsProbeOnIOThread(
84 const base::Callback<void(DnsProbeResult)>& callback, 60 const base::Callback<void(DnsProbeStatus)>& callback,
85 IOThread* io_thread) { 61 IOThread* io_thread) {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
87 63
88 DVLOG(1) << "Starting DNS probe";
89
90 DnsProbeService* probe_service = 64 DnsProbeService* probe_service =
91 io_thread->globals()->dns_probe_service.get(); 65 io_thread->globals()->dns_probe_service.get();
92 66
93 probe_service->ProbeDns(base::Bind(&OnDnsProbeFinishedOnIOThread, callback)); 67 probe_service->ProbeDns(base::Bind(&OnDnsProbeFinishedOnIOThread, callback));
94 } 68 }
95 69
96 } // namespace 70 } // namespace
97 71
98 NetErrorTabHelper::~NetErrorTabHelper() { 72 NetErrorTabHelper::~NetErrorTabHelper() {
99 } 73 }
100 74
101 // static 75 // static
102 void NetErrorTabHelper::set_state_for_testing(TestingState state) { 76 void NetErrorTabHelper::set_state_for_testing(TestingState state) {
103 testing_state_ = state; 77 testing_state_ = state;
104 } 78 }
105 79
106 void NetErrorTabHelper::DidStartProvisionalLoadForFrame( 80 void NetErrorTabHelper::DidStartProvisionalLoadForFrame(
107 int64 frame_id, 81 int64 frame_id,
108 int64 parent_frame_id, 82 int64 parent_frame_id,
109 bool is_main_frame, 83 bool is_main_frame,
110 const GURL& validated_url, 84 const GURL& validated_url,
111 bool is_error_page, 85 bool is_error_page,
112 bool is_iframe_srcdoc, 86 bool is_iframe_srcdoc,
113 RenderViewHost* render_view_host) { 87 RenderViewHost* render_view_host) {
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
115 89
116 tracker_.OnStartProvisionalLoad(GetFrameType(is_main_frame), 90 if (!is_main_frame)
117 GetPageType(is_error_page)); 91 return;
92
93 is_error_page_ = is_error_page;
118 } 94 }
119 95
120 void NetErrorTabHelper::DidCommitProvisionalLoadForFrame( 96 void NetErrorTabHelper::DidCommitProvisionalLoadForFrame(
121 int64 frame_id, 97 int64 frame_id,
122 bool is_main_frame, 98 bool is_main_frame,
123 const GURL& url, 99 const GURL& url,
124 PageTransition transition_type, 100 PageTransition transition_type,
125 RenderViewHost* render_view_host) { 101 RenderViewHost* render_view_host) {
126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
127 103
128 tracker_.OnCommitProvisionalLoad(GetFrameType(is_main_frame)); 104 if (!is_main_frame)
105 return;
106
107 // Resend status every time an error page commits; this is somewhat spammy,
108 // but ensures that the status will make it to the real error page, even if
109 // the link doctor loads a blank intermediate page or the tab switches
110 // renderer processes.
111 if (is_error_page_ && dns_error_active_) {
112 dns_error_page_committed_ = true;
113 DVLOG(1) << "Committed error page; resending status.";
114 SendInfo();
115 } else {
116 dns_error_active_ = false;
117 dns_error_page_committed_ = false;
118 }
129 } 119 }
130 120
131 void NetErrorTabHelper::DidFailProvisionalLoad( 121 void NetErrorTabHelper::DidFailProvisionalLoad(
132 int64 frame_id, 122 int64 frame_id,
133 bool is_main_frame, 123 bool is_main_frame,
134 const GURL& validated_url, 124 const GURL& validated_url,
135 int error_code, 125 int error_code,
136 const string16& error_description, 126 const string16& error_description,
137 RenderViewHost* render_view_host) { 127 RenderViewHost* render_view_host) {
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
139 129
140 tracker_.OnFailProvisionalLoad(GetFrameType(is_main_frame), 130 if (!is_main_frame)
141 GetErrorType(error_code)); 131 return;
142 }
143 132
144 void NetErrorTabHelper::DidFinishLoad( 133 if (IsDnsError(error_code)) {
145 int64 frame_id, 134 dns_error_active_ = true;
146 const GURL& validated_url, 135 OnMainFrameDnsError();
147 bool is_main_frame, 136 }
148 RenderViewHost* render_view_host) {
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
150
151 tracker_.OnFinishLoad(GetFrameType(is_main_frame));
152 } 137 }
153 138
154 NetErrorTabHelper::NetErrorTabHelper(WebContents* contents) 139 NetErrorTabHelper::NetErrorTabHelper(WebContents* contents)
155 : WebContentsObserver(contents), 140 : WebContentsObserver(contents),
156 weak_factory_(this), 141 weak_factory_(this),
157 tracker_(base::Bind(&NetErrorTabHelper::TrackerCallback, 142 is_error_page_(false),
158 weak_factory_.GetWeakPtr())), 143 dns_error_active_(false),
159 dns_error_page_state_(NetErrorTracker::DNS_ERROR_PAGE_NONE), 144 dns_error_page_committed_(false),
160 dns_probe_state_(DNS_PROBE_NONE), 145 dns_probe_status_(chrome_common_net::DNS_PROBE_POSSIBLE),
161 enabled_by_trial_(GetEnabledByTrial()) { 146 enabled_by_trial_(chrome_common_net::DnsProbesEnabledByFieldTrial()) {
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
163 148
164 InitializePref(contents); 149 // If this helper is under test, it won't have a WebContents.
150 if (contents)
151 InitializePref(contents);
165 } 152 }
166 153
167 void NetErrorTabHelper::TrackerCallback( 154 void NetErrorTabHelper::OnMainFrameDnsError() {
168 NetErrorTracker::DnsErrorPageState state) { 155 if (ProbesAllowed()) {
169 dns_error_page_state_ = state; 156 // Don't start more than one probe at a time.
170 157 if (dns_probe_status_ != chrome_common_net::DNS_PROBE_STARTED) {
171 MaybePostStartDnsProbeTask(); 158 StartDnsProbe();
172 MaybeSendInfo(); 159 dns_probe_status_ = chrome_common_net::DNS_PROBE_STARTED;
173 } 160 }
174 161 } else {
175 void NetErrorTabHelper::MaybePostStartDnsProbeTask() { 162 dns_probe_status_ = chrome_common_net::DNS_PROBE_NOT_RUN;
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
177
178 if (dns_error_page_state_ != NetErrorTracker::DNS_ERROR_PAGE_NONE &&
179 dns_probe_state_ != DNS_PROBE_STARTED &&
180 ProbesAllowed()) {
181 BrowserThread::PostTask(
182 BrowserThread::IO,
183 FROM_HERE,
184 base::Bind(&StartDnsProbeOnIOThread,
185 base::Bind(&NetErrorTabHelper::OnDnsProbeFinished,
186 weak_factory_.GetWeakPtr()),
187 g_browser_process->io_thread()));
188 dns_probe_state_ = DNS_PROBE_STARTED;
189 } 163 }
190 } 164 }
191 165
192 void NetErrorTabHelper::OnDnsProbeFinished(DnsProbeResult result) { 166 void NetErrorTabHelper::StartDnsProbe() {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
194 DCHECK_EQ(DNS_PROBE_STARTED, dns_probe_state_); 168 DCHECK(dns_error_active_);
169 DCHECK_NE(chrome_common_net::DNS_PROBE_STARTED, dns_probe_status_);
195 170
196 dns_probe_result_ = result; 171 DVLOG(1) << "Starting DNS probe.";
197 dns_probe_state_ = DNS_PROBE_FINISHED;
198 172
199 MaybeSendInfo(); 173 BrowserThread::PostTask(
174 BrowserThread::IO,
175 FROM_HERE,
176 base::Bind(&StartDnsProbeOnIOThread,
177 base::Bind(&NetErrorTabHelper::OnDnsProbeFinished,
178 weak_factory_.GetWeakPtr()),
179 g_browser_process->io_thread()));
200 } 180 }
201 181
202 void NetErrorTabHelper::MaybeSendInfo() { 182 void NetErrorTabHelper::OnDnsProbeFinished(DnsProbeStatus result) {
203 if (dns_error_page_state_ == NetErrorTracker::DNS_ERROR_PAGE_LOADED && 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
204 dns_probe_state_ == DNS_PROBE_FINISHED) { 184 DCHECK_EQ(chrome_common_net::DNS_PROBE_STARTED, dns_probe_status_);
205 DVLOG(1) << "Sending result " << dns_probe_result_ << " to renderer"; 185 DCHECK(chrome_common_net::DnsProbeStatusIsFinished(result));
206 Send(new ChromeViewMsg_NetErrorInfo(routing_id(), dns_probe_result_)); 186
207 dns_probe_state_ = DNS_PROBE_NONE; 187 DVLOG(1) << "Finished DNS probe with result "
208 } 188 << DnsProbeStatusToString(result) << ".";
189
190 dns_probe_status_ = result;
191
192 if (dns_error_page_committed_)
193 SendInfo();
209 } 194 }
210 195
211 void NetErrorTabHelper::InitializePref(WebContents* contents) { 196 void NetErrorTabHelper::InitializePref(WebContents* contents) {
212 DCHECK(contents); 197 DCHECK(contents);
213 198
214 BrowserContext* browser_context = contents->GetBrowserContext(); 199 BrowserContext* browser_context = contents->GetBrowserContext();
215 Profile* profile = Profile::FromBrowserContext(browser_context); 200 Profile* profile = Profile::FromBrowserContext(browser_context);
216 resolve_errors_with_web_service_.Init( 201 resolve_errors_with_web_service_.Init(
217 prefs::kAlternateErrorPagesEnabled, 202 prefs::kAlternateErrorPagesEnabled,
218 profile->GetPrefs()); 203 profile->GetPrefs());
219 } 204 }
220 205
221 bool NetErrorTabHelper::ProbesAllowed() const { 206 bool NetErrorTabHelper::ProbesAllowed() const {
222 if (testing_state_ != TESTING_DEFAULT) 207 if (testing_state_ != TESTING_DEFAULT)
223 return testing_state_ == TESTING_FORCE_ENABLED; 208 return testing_state_ == TESTING_FORCE_ENABLED;
224 209
225 // TODO(ttuttle): Disable on mobile? 210 // TODO(ttuttle): Disable on mobile?
226 return enabled_by_trial_ && *resolve_errors_with_web_service_; 211 return enabled_by_trial_ && *resolve_errors_with_web_service_;
227 } 212 }
228 213
214 void NetErrorTabHelper::SendInfo() {
215 DCHECK_NE(chrome_common_net::DNS_PROBE_POSSIBLE, dns_probe_status_);
216 DCHECK(dns_error_page_committed_);
217
218 DVLOG(1) << "Sending status " << DnsProbeStatusToString(dns_probe_status_);
219 Send(new ChromeViewMsg_NetErrorInfo(routing_id(), dns_probe_status_));
220
221 if (!dns_probe_status_snoop_callback_.is_null())
222 dns_probe_status_snoop_callback_.Run(dns_probe_status_);
223 }
224
229 } // namespace chrome_browser_net 225 } // namespace chrome_browser_net
OLDNEW
« no previous file with comments | « chrome/browser/net/net_error_tab_helper.h ('k') | chrome/browser/net/net_error_tab_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698