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

Side by Side Diff: chrome/renderer/net/net_error_helper.cc

Issue 13270005: Display DNS probe results. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rework NetErrorHelper state machine a little Created 7 years, 6 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/renderer/net/net_error_helper.h" 5 #include "chrome/renderer/net/net_error_helper.h"
6 6
7 #include <string>
8
9 #include "base/json/json_writer.h"
10 #include "base/utf_string_conversions.h"
7 #include "base/values.h" 11 #include "base/values.h"
8 #include "chrome/common/localized_error.h" 12 #include "chrome/common/localized_error.h"
9 #include "chrome/common/render_messages.h" 13 #include "chrome/common/render_messages.h"
10 #include "chrome/common/net/net_error_info.h" 14 #include "chrome/common/net/net_error_info.h"
11 #include "content/public/common/content_client.h" 15 #include "content/public/common/content_client.h"
12 #include "content/public/common/url_constants.h" 16 #include "content/public/common/url_constants.h"
13 #include "content/public/renderer/content_renderer_client.h" 17 #include "content/public/renderer/content_renderer_client.h"
14 #include "content/public/renderer/render_thread.h" 18 #include "content/public/renderer/render_thread.h"
15 #include "content/public/renderer/render_view.h" 19 #include "content/public/renderer/render_view.h"
16 #include "googleurl/src/gurl.h" 20 #include "googleurl/src/gurl.h"
17 #include "ipc/ipc_message.h" 21 #include "ipc/ipc_message.h"
18 #include "ipc/ipc_message_macros.h" 22 #include "ipc/ipc_message_macros.h"
19 #include "net/base/net_errors.h" 23 #include "net/base/net_errors.h"
20 #include "third_party/WebKit/public/platform/WebURL.h" 24 #include "third_party/WebKit/public/platform/WebURL.h"
21 #include "third_party/WebKit/public/platform/WebURLError.h"
22 #include "third_party/WebKit/public/platform/WebURLRequest.h" 25 #include "third_party/WebKit/public/platform/WebURLRequest.h"
23 #include "third_party/WebKit/public/web/WebDataSource.h" 26 #include "third_party/WebKit/public/web/WebDataSource.h"
24 #include "third_party/WebKit/public/web/WebFrame.h" 27 #include "third_party/WebKit/public/web/WebFrame.h"
25 28
26 using base::DictionaryValue; 29 using base::DictionaryValue;
27 using chrome_common_net::DnsProbeResult; 30 using base::JSONWriter;
31 using chrome_common_net::DnsProbeStatus;
32 using chrome_common_net::DnsProbeStatusIsFinished;
33 using chrome_common_net::DnsProbeStatusToString;
34 using chrome_common_net::DnsProbesEnabledByFieldTrial;
28 using content::RenderThread; 35 using content::RenderThread;
29 using content::RenderView; 36 using content::RenderView;
30 using content::RenderViewObserver; 37 using content::RenderViewObserver;
31 using content::kUnreachableWebDataURL; 38 using content::kUnreachableWebDataURL;
32 39
33 namespace { 40 namespace {
34 41
35 GURL GetProvisionallyLoadingURLFromWebFrame(WebKit::WebFrame* frame) { 42 bool IsLoadingErrorPage(WebKit::WebFrame* frame) {
36 return frame->provisionalDataSource()->request().url(); 43 GURL url = frame->provisionalDataSource()->request().url();
44 return url.spec() == kUnreachableWebDataURL;
37 } 45 }
38 46
39 bool IsErrorPage(const GURL& url) { 47 bool IsMainFrame(const WebKit::WebFrame* frame) {
40 return (url.spec() == kUnreachableWebDataURL); 48 return !frame->parent();
41 } 49 }
42 50
43 // Returns whether |net_error| is a DNS-related error (and therefore whether 51 // Returns whether |net_error| is a DNS-related error (and therefore whether
44 // the tab helper should start a DNS probe after receiving it.) 52 // the tab helper should start a DNS probe after receiving it.)
45 bool IsDnsError(int net_error) { 53 bool IsDnsError(const WebKit::WebURLError& error) {
46 return net_error == net::ERR_NAME_NOT_RESOLVED || 54 return std::string(error.domain.utf8()) == net::kErrorDomain &&
47 net_error == net::ERR_NAME_RESOLUTION_FAILED; 55 (error.reason == net::ERR_NAME_NOT_RESOLVED ||
48 } 56 error.reason == net::ERR_NAME_RESOLUTION_FAILED);
49
50 NetErrorTracker::FrameType GetFrameType(WebKit::WebFrame* frame) {
51 return frame->parent() ? NetErrorTracker::FRAME_SUB
52 : NetErrorTracker::FRAME_MAIN;
53 }
54
55 NetErrorTracker::PageType GetPageType(WebKit::WebFrame* frame) {
56 bool error_page = IsErrorPage(GetProvisionallyLoadingURLFromWebFrame(frame));
57 return error_page ? NetErrorTracker::PAGE_ERROR
58 : NetErrorTracker::PAGE_NORMAL;
59 }
60
61 NetErrorTracker::ErrorType GetErrorType(const WebKit::WebURLError& error) {
62 return IsDnsError(error.reason) ? NetErrorTracker::ERROR_DNS
63 : NetErrorTracker::ERROR_OTHER;
64 }
65
66 // Converts a DNS probe result into a net error. Returns OK if the error page
67 // should not be changed from the original DNS error.
68 int DnsProbeResultToNetError(DnsProbeResult result) {
69 switch (result) {
70 case chrome_common_net::DNS_PROBE_UNKNOWN:
71 return net::OK;
72 case chrome_common_net::DNS_PROBE_NO_INTERNET:
73 // TODO(ttuttle): This is not the same error as when NCN returns this;
74 // ideally we should have two separate error codes for "no network" and
75 // "network with no internet".
76 return net::ERR_INTERNET_DISCONNECTED;
77 case chrome_common_net::DNS_PROBE_BAD_CONFIG:
78 // This is unspecific enough that we should still show the full DNS error
79 // page.
80 return net::OK;
81 case chrome_common_net::DNS_PROBE_NXDOMAIN:
82 return net::ERR_NAME_NOT_RESOLVED;
83 default:
84 NOTREACHED();
85 return net::OK;
86 }
87 }
88
89 WebKit::WebURLError NetErrorToWebURLError(int net_error) {
90 WebKit::WebURLError error;
91 error.domain = WebKit::WebString::fromUTF8(net::kErrorDomain);
92 error.reason = net_error;
93 return error;
94 } 57 }
95 58
96 } // namespace 59 } // namespace
97 60
98 NetErrorHelper::NetErrorHelper(RenderView* render_view) 61 NetErrorHelper::NetErrorHelper(RenderView* render_view)
99 : RenderViewObserver(render_view), 62 : RenderViewObserver(render_view),
100 tracker_(base::Bind(&NetErrorHelper::TrackerCallback, 63 last_probe_status_(chrome_common_net::DNS_PROBE_POSSIBLE),
101 base::Unretained(this))), 64 last_start_was_error_page_(false),
102 dns_error_page_state_(NetErrorTracker::DNS_ERROR_PAGE_NONE), 65 last_fail_was_dns_error_(false),
103 updated_error_page_(false), 66 forwarding_probe_results_(false),
104 is_failed_post_(false) { 67 is_failed_post_(false) {
105 } 68 }
106 69
107 NetErrorHelper::~NetErrorHelper() { 70 NetErrorHelper::~NetErrorHelper() {
108 } 71 }
109 72
110 void NetErrorHelper::DidStartProvisionalLoad(WebKit::WebFrame* frame) { 73 void NetErrorHelper::DidStartProvisionalLoad(WebKit::WebFrame* frame) {
111 tracker_.OnStartProvisionalLoad(GetFrameType(frame), GetPageType(frame)); 74 OnStartLoad(IsMainFrame(frame), IsLoadingErrorPage(frame));
112 } 75 }
113 76
114 void NetErrorHelper::DidFailProvisionalLoad(WebKit::WebFrame* frame, 77 void NetErrorHelper::DidFailProvisionalLoad(WebKit::WebFrame* frame,
115 const WebKit::WebURLError& error) { 78 const WebKit::WebURLError& error) {
116 WebKit::WebDataSource* data_source = frame->provisionalDataSource(); 79 const bool main_frame = IsMainFrame(frame);
117 const WebKit::WebURLRequest& failed_request = data_source->request(); 80 const bool dns_error = IsDnsError(error);
118 is_failed_post_ = EqualsASCII(failed_request.httpMethod(), "POST"); 81
119 tracker_.OnFailProvisionalLoad(GetFrameType(frame), GetErrorType(error)); 82 OnFailLoad(main_frame, dns_error);
83
84 if (main_frame && dns_error) {
85 last_error_ = error;
86
87 WebKit::WebDataSource* data_source = frame->provisionalDataSource();
88 const WebKit::WebURLRequest& failed_request = data_source->request();
89 is_failed_post_ = EqualsASCII(failed_request.httpMethod(), "POST");
90 }
120 } 91 }
121 92
122 void NetErrorHelper::DidCommitProvisionalLoad(WebKit::WebFrame* frame, 93 void NetErrorHelper::DidCommitProvisionalLoad(WebKit::WebFrame* frame,
123 bool is_new_navigation) { 94 bool is_new_navigation) {
124 tracker_.OnCommitProvisionalLoad(GetFrameType(frame)); 95 OnCommitLoad(IsMainFrame(frame));
125 } 96 }
126 97
127 void NetErrorHelper::DidFinishLoad(WebKit::WebFrame* frame) { 98 void NetErrorHelper::DidFinishLoad(WebKit::WebFrame* frame) {
128 tracker_.OnFinishLoad(GetFrameType(frame)); 99 OnFinishLoad(IsMainFrame(frame));
100 }
101
102 void NetErrorHelper::OnStartLoad(bool is_main_frame, bool is_error_page) {
103 DVLOG(1) << "OnStartLoad(is_main_frame=" << is_main_frame
104 << ", is_error_page=" << is_error_page << ")";
105 if (!is_main_frame)
106 return;
107
108 last_start_was_error_page_ = is_error_page;
109 }
110
111 void NetErrorHelper::OnFailLoad(bool is_main_frame, bool is_dns_error) {
112 DVLOG(1) << "OnFailLoad(is_main_frame=" << is_main_frame
113 << ", is_dns_error=" << is_dns_error << ")";
114
115 if (!is_main_frame)
116 return;
117
118 last_fail_was_dns_error_ = is_dns_error;
119
120 if (is_dns_error) {
121 last_probe_status_ = chrome_common_net::DNS_PROBE_POSSIBLE;
122 // If the helper was forwarding probe results and another DNS error has
123 // occurred, stop forwarding probe results until the corresponding (new)
124 // error page loads.
125 forwarding_probe_results_ = false;
126 }
127 }
128
129 void NetErrorHelper::OnCommitLoad(bool is_main_frame) {
130 DVLOG(1) << "OnCommitLoad(is_main_frame=" << is_main_frame << ")";
131
132 if (!is_main_frame)
133 return;
134
135 // If the helper was forwarding probe results and a normal page (or a non-DNS
136 // error page) has committed, stop forwarding probe results, since the DNS
137 // error page is gone.
138 if (!last_start_was_error_page_ || !last_fail_was_dns_error_)
139 forwarding_probe_results_ = false;
140 }
141
142 void NetErrorHelper::OnFinishLoad(bool is_main_frame) {
143 DVLOG(1) << "OnFinishLoad(is_main_frame=" << is_main_frame << ")";
144
145 if (!is_main_frame)
146 return;
147
148 // If a DNS error page just finished loading, start forwarding probe results
149 // to it.
150 forwarding_probe_results_ =
151 last_fail_was_dns_error_ && last_start_was_error_page_;
152
153 if (forwarding_probe_results_ &&
154 last_probe_status_ != chrome_common_net::DNS_PROBE_POSSIBLE) {
155 DVLOG(1) << "Error page finished loading; sending saved status.";
156 UpdateErrorPage();
157 }
129 } 158 }
130 159
131 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) { 160 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) {
132 bool handled = true; 161 bool handled = true;
133 162
134 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message) 163 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message)
135 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo) 164 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo)
136 IPC_MESSAGE_UNHANDLED(handled = false) 165 IPC_MESSAGE_UNHANDLED(handled = false)
137 IPC_END_MESSAGE_MAP() 166 IPC_END_MESSAGE_MAP()
138 167
139 return handled; 168 return handled;
140 } 169 }
141 170
142 void NetErrorHelper::OnNetErrorInfo(int dns_probe_result) { 171 bool NetErrorHelper::GetErrorStringsForDnsProbe(
143 DVLOG(1) << "Received DNS probe result " << dns_probe_result; 172 WebKit::WebFrame* frame,
173 const WebKit::WebURLError& error,
174 bool is_failed_post,
175 const std::string& locale,
176 base::DictionaryValue* error_strings) {
177 if (!IsMainFrame(frame))
178 return false;
144 179
145 if (dns_probe_result < 0 || 180 if (!IsDnsError(error))
146 dns_probe_result >= chrome_common_net::DNS_PROBE_MAX) { 181 return false;
147 DLOG(WARNING) << "Ignoring DNS probe result: invalid result " 182
148 << dns_probe_result; 183 // Get the strings for a fake "DNS probe possible" error.
184 WebKit::WebURLError fake_error;
185 fake_error.domain = WebKit::WebString::fromUTF8(
186 chrome_common_net::kDnsProbeErrorDomain);
187 fake_error.reason = chrome_common_net::DNS_PROBE_POSSIBLE;
188 fake_error.unreachableURL = error.unreachableURL;
189 LocalizedError::GetStrings(
190 fake_error, is_failed_post, locale, error_strings);
191 return true;
192 }
193
194 void NetErrorHelper::OnNetErrorInfo(int status_num) {
195 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX);
196
197 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num);
198
199 DnsProbeStatus status = static_cast<DnsProbeStatus>(status_num);
200 DCHECK_NE(chrome_common_net::DNS_PROBE_POSSIBLE, status);
201
202 if (!(last_fail_was_dns_error_ || forwarding_probe_results_)) {
203 DVLOG(1) << "Ignoring NetErrorInfo: no DNS error";
204 return;
205 }
206
207 last_probe_status_ = status;
208
209 if (forwarding_probe_results_)
210 UpdateErrorPage();
211 }
212
213 void NetErrorHelper::UpdateErrorPage() {
214 DCHECK(forwarding_probe_results_);
215
216 DictionaryValue error_strings;
217 LocalizedError::GetStrings(GetUpdatedError(),
218 is_failed_post_,
219 RenderThread::Get()->GetLocale(),
220 &error_strings);
221
222 std::string json;
223 JSONWriter::Write(&error_strings, &json);
224
225 std::string js = "if (window.updateForDnsProbe) "
226 "updateForDnsProbe(" + json + ");";
227 string16 js16;
228 if (!UTF8ToUTF16(js.c_str(), js.length(), &js16)) {
149 NOTREACHED(); 229 NOTREACHED();
150 return; 230 return;
151 } 231 }
152 232
153 if (dns_error_page_state_ != NetErrorTracker::DNS_ERROR_PAGE_LOADED) { 233 DVLOG(1) << "Updating error page with status "
154 DVLOG(1) << "Ignoring DNS probe result: not on DNS error page."; 234 << chrome_common_net::DnsProbeStatusToString(last_probe_status_);
155 return; 235 DVLOG(2) << "New strings: " << js;
236
237 string16 frame_xpath;
238 render_view()->EvaluateScript(frame_xpath, js16, 0, false);
239 }
240
241 WebKit::WebURLError NetErrorHelper::GetUpdatedError() const {
242 // If a probe didn't run or wasn't conclusive, restore the original error.
243 if (last_probe_status_ == chrome_common_net::DNS_PROBE_NOT_RUN ||
244 last_probe_status_ == chrome_common_net::DNS_PROBE_FINISHED_UNKNOWN) {
245 return last_error_;
156 } 246 }
157 247
158 if (updated_error_page_) { 248 WebKit::WebURLError error;
159 DVLOG(1) << "Ignoring DNS probe result: already updated error page."; 249 error.domain = WebKit::WebString::fromUTF8(
160 return; 250 chrome_common_net::kDnsProbeErrorDomain);
161 } 251 error.reason = last_probe_status_;
252 error.unreachableURL = last_error_.unreachableURL;
162 253
163 UpdateErrorPage(static_cast<DnsProbeResult>(dns_probe_result)); 254 return error;
164 updated_error_page_ = true;
165 } 255 }
166
167 void NetErrorHelper::TrackerCallback(
168 NetErrorTracker::DnsErrorPageState state) {
169 dns_error_page_state_ = state;
170
171 if (state == NetErrorTracker::DNS_ERROR_PAGE_LOADED)
172 updated_error_page_ = false;
173 }
174
175 void NetErrorHelper::UpdateErrorPage(DnsProbeResult dns_probe_result) {
176 DVLOG(1) << "Updating error page with result " << dns_probe_result;
177
178 int net_error = DnsProbeResultToNetError(dns_probe_result);
179 if (net_error == net::OK)
180 return;
181
182 DVLOG(1) << "net error code is " << net_error;
183
184 DictionaryValue error_strings;
185 LocalizedError::GetStrings(NetErrorToWebURLError(net_error),
186 is_failed_post_,
187 RenderThread::Get()->GetLocale(),
188 &error_strings);
189
190 // TODO(ttuttle): Update error page with error_strings.
191 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698