Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/Source/Platform/chromium/public/WebURL.h" | 24 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h" |
| 21 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h" | 25 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h" |
| 22 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h" | 26 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 25 | 29 |
| 26 using base::DictionaryValue; | 30 using base::DictionaryValue; |
| 27 using chrome_common_net::DnsProbeResult; | 31 using base::JSONWriter; |
| 32 using chrome_common_net::DnsProbeStatus; | |
| 33 using chrome_common_net::DnsProbeStatusIsFinished; | |
| 34 using chrome_common_net::DnsProbeStatusToString; | |
| 35 using chrome_common_net::DnsProbesEnabledByFieldTrial; | |
| 28 using content::RenderThread; | 36 using content::RenderThread; |
| 29 using content::RenderView; | 37 using content::RenderView; |
| 30 using content::RenderViewObserver; | 38 using content::RenderViewObserver; |
| 31 using content::kUnreachableWebDataURL; | 39 using content::kUnreachableWebDataURL; |
| 32 | 40 |
| 33 namespace { | 41 namespace { |
| 34 | 42 |
| 35 GURL GetProvisionallyLoadingURLFromWebFrame(WebKit::WebFrame* frame) { | 43 bool IsLoadingErrorPage(WebKit::WebFrame* frame) { |
| 36 return frame->provisionalDataSource()->request().url(); | 44 GURL url = frame->provisionalDataSource()->request().url(); |
| 45 return url.spec() == kUnreachableWebDataURL; | |
| 37 } | 46 } |
| 38 | 47 |
| 39 bool IsErrorPage(const GURL& url) { | 48 bool IsMainFrame(const WebKit::WebFrame* frame) { |
| 40 return (url.spec() == kUnreachableWebDataURL); | 49 return !frame->parent(); |
| 41 } | 50 } |
| 42 | 51 |
| 43 // Returns whether |net_error| is a DNS-related error (and therefore whether | 52 // Returns whether |net_error| is a DNS-related error (and therefore whether |
| 44 // the tab helper should start a DNS probe after receiving it.) | 53 // the tab helper should start a DNS probe after receiving it.) |
| 45 bool IsDnsError(int net_error) { | 54 bool IsDnsError(const WebKit::WebURLError& error) { |
| 46 return net_error == net::ERR_NAME_NOT_RESOLVED || | 55 return std::string(error.domain.utf8()) == net::kErrorDomain && |
| 47 net_error == net::ERR_NAME_RESOLUTION_FAILED; | 56 (error.reason == net::ERR_NAME_NOT_RESOLVED || |
| 48 } | 57 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 } | 58 } |
| 95 | 59 |
| 96 } // namespace | 60 } // namespace |
| 97 | 61 |
| 98 NetErrorHelper::NetErrorHelper(RenderView* render_view) | 62 NetErrorHelper::NetErrorHelper(RenderView* render_view) |
| 99 : RenderViewObserver(render_view), | 63 : RenderViewObserver(render_view), |
| 100 tracker_(base::Bind(&NetErrorHelper::TrackerCallback, | 64 dns_error_active_(false), |
| 101 base::Unretained(this))), | 65 page_loaded_(false), |
| 102 dns_error_page_state_(NetErrorTracker::DNS_ERROR_PAGE_NONE), | 66 last_status_(chrome_common_net::DNS_PROBE_POSSIBLE), |
| 103 updated_error_page_(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 if (!IsMainFrame(frame)) |
| 75 return; | |
| 76 | |
| 77 is_error_page_ = IsLoadingErrorPage(frame); | |
| 112 } | 78 } |
| 113 | 79 |
| 114 void NetErrorHelper::DidFailProvisionalLoad(WebKit::WebFrame* frame, | 80 void NetErrorHelper::DidFailProvisionalLoad(WebKit::WebFrame* frame, |
| 115 const WebKit::WebURLError& error) { | 81 const WebKit::WebURLError& error) { |
| 82 if (!IsMainFrame(frame) || !IsDnsError(error)) | |
| 83 return; | |
| 84 | |
| 85 dns_error_active_ = true; | |
| 86 | |
| 87 last_status_ = chrome_common_net::DNS_PROBE_POSSIBLE; | |
| 88 | |
| 89 last_error_ = error; | |
| 90 | |
| 116 WebKit::WebDataSource* data_source = frame->provisionalDataSource(); | 91 WebKit::WebDataSource* data_source = frame->provisionalDataSource(); |
| 117 const WebKit::WebURLRequest& failed_request = data_source->request(); | 92 const WebKit::WebURLRequest& failed_request = data_source->request(); |
| 118 is_failed_post_ = EqualsASCII(failed_request.httpMethod(), "POST"); | 93 is_failed_post_ = EqualsASCII(failed_request.httpMethod(), "POST"); |
| 119 tracker_.OnFailProvisionalLoad(GetFrameType(frame), GetErrorType(error)); | |
| 120 } | 94 } |
| 121 | 95 |
| 122 void NetErrorHelper::DidCommitProvisionalLoad(WebKit::WebFrame* frame, | 96 void NetErrorHelper::DidCommitProvisionalLoad(WebKit::WebFrame* frame, |
| 123 bool is_new_navigation) { | 97 bool is_new_navigation) { |
| 124 tracker_.OnCommitProvisionalLoad(GetFrameType(frame)); | 98 if (!IsMainFrame(frame)) |
| 99 return; | |
| 100 | |
| 101 if (!is_error_page_) | |
| 102 dns_error_active_ = false; | |
| 103 | |
| 104 page_loaded_ = false; | |
| 125 } | 105 } |
| 126 | 106 |
| 127 void NetErrorHelper::DidFinishLoad(WebKit::WebFrame* frame) { | 107 void NetErrorHelper::DidFinishLoad(WebKit::WebFrame* frame) { |
| 128 tracker_.OnFinishLoad(GetFrameType(frame)); | 108 if (!IsMainFrame(frame)) |
| 109 return; | |
| 110 | |
| 111 page_loaded_ = true; | |
| 112 | |
| 113 if (dns_error_active_ && is_error_page_) { | |
| 114 DVLOG(1) << "Error page finished loading; updating."; | |
| 115 UpdateErrorPage(); | |
| 116 } | |
| 129 } | 117 } |
| 130 | 118 |
| 131 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) { | 119 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) { |
| 132 bool handled = true; | 120 bool handled = true; |
| 133 | 121 |
| 134 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message) | 122 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message) |
| 135 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo) | 123 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo) |
| 136 IPC_MESSAGE_UNHANDLED(handled = false) | 124 IPC_MESSAGE_UNHANDLED(handled = false) |
| 137 IPC_END_MESSAGE_MAP() | 125 IPC_END_MESSAGE_MAP() |
| 138 | 126 |
| 139 return handled; | 127 return handled; |
| 140 } | 128 } |
| 141 | 129 |
| 142 void NetErrorHelper::OnNetErrorInfo(int dns_probe_result) { | 130 bool NetErrorHelper::GetErrorStringsForDnsProbe( |
| 143 DVLOG(1) << "Received DNS probe result " << dns_probe_result; | 131 WebKit::WebFrame* frame, |
| 132 const WebKit::WebURLError& error, | |
| 133 bool is_failed_post, | |
| 134 const std::string& locale, | |
| 135 base::DictionaryValue* error_strings) { | |
| 136 if (!IsMainFrame(frame)) | |
| 137 return false; | |
| 144 | 138 |
| 145 if (dns_probe_result < 0 || | 139 if (!IsDnsError(error)) |
| 146 dns_probe_result >= chrome_common_net::DNS_PROBE_MAX) { | 140 return false; |
| 147 DLOG(WARNING) << "Ignoring DNS probe result: invalid result " | 141 |
| 148 << dns_probe_result; | 142 // Get the strings for a fake "DNS probe possible" error |
|
mmenke
2013/05/28 18:20:13
nit: period.
Deprecated (see juliatuttle)
2013/06/05 21:01:47
Done.
| |
| 143 WebKit::WebURLError fake_error; | |
| 144 fake_error.domain = WebKit::WebString::fromUTF8( | |
| 145 chrome_common_net::kDnsProbeErrorDomain); | |
| 146 fake_error.reason = chrome_common_net::DNS_PROBE_POSSIBLE; | |
| 147 fake_error.unreachableURL = error.unreachableURL; | |
| 148 LocalizedError::GetStrings( | |
| 149 fake_error, is_failed_post, locale, error_strings); | |
| 150 return true; | |
| 151 } | |
| 152 | |
| 153 void NetErrorHelper::OnNetErrorInfo(int status_num) { | |
| 154 if (status_num < 0 || | |
| 155 status_num >= chrome_common_net::DNS_PROBE_MAX) { | |
| 156 LOG(WARNING) << "Ignoring NetErrorInfo: invalid status " << status_num; | |
| 149 NOTREACHED(); | 157 NOTREACHED(); |
| 150 return; | 158 return; |
| 151 } | 159 } |
| 152 | 160 |
| 153 if (dns_error_page_state_ != NetErrorTracker::DNS_ERROR_PAGE_LOADED) { | 161 DVLOG(1) << "Received status " |
| 154 DVLOG(1) << "Ignoring DNS probe result: not on DNS error page."; | 162 << DnsProbeStatusToString(status_num); |
| 163 | |
| 164 DnsProbeStatus status = static_cast<DnsProbeStatus>(status_num); | |
| 165 if (status == chrome_common_net::DNS_PROBE_POSSIBLE) { | |
| 166 NOTREACHED(); | |
| 155 return; | 167 return; |
| 156 } | 168 } |
| 157 | 169 |
| 158 if (updated_error_page_) { | 170 if (!dns_error_active_) { |
| 159 DVLOG(1) << "Ignoring DNS probe result: already updated error page."; | 171 DVLOG(1) << "Ignoring NetErrorInfo: no active DNS error"; |
| 160 return; | 172 return; |
| 161 } | 173 } |
| 162 | 174 |
| 163 UpdateErrorPage(static_cast<DnsProbeResult>(dns_probe_result)); | 175 last_status_ = status; |
| 164 updated_error_page_ = true; | 176 |
| 177 if (page_loaded_) | |
| 178 UpdateErrorPage(); | |
| 165 } | 179 } |
| 166 | 180 |
| 167 void NetErrorHelper::TrackerCallback( | 181 void NetErrorHelper::UpdateErrorPage() { |
| 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; | 182 DictionaryValue error_strings; |
| 185 LocalizedError::GetStrings(NetErrorToWebURLError(net_error), | 183 LocalizedError::GetStrings(GetUpdatedError(), |
| 186 is_failed_post_, | 184 is_failed_post_, |
| 187 RenderThread::Get()->GetLocale(), | 185 RenderThread::Get()->GetLocale(), |
| 188 &error_strings); | 186 &error_strings); |
| 189 | 187 |
| 190 // TODO(ttuttle): Update error page with error_strings. | 188 std::string json; |
| 189 JSONWriter::Write(&error_strings, &json); | |
| 190 | |
| 191 std::string js = "if (window.updateForDnsProbe) " | |
| 192 "updateForDnsProbe(" + json + ");"; | |
| 193 string16 js16; | |
| 194 if (!UTF8ToUTF16(js.c_str(), js.length(), &js16)) { | |
| 195 NOTREACHED(); | |
| 196 return; | |
| 197 } | |
| 198 | |
| 199 DVLOG(1) << "Updating error page with status " | |
| 200 << chrome_common_net::DnsProbeStatusToString(last_status_); | |
| 201 DVLOG(2) << "New strings: " << js; | |
| 202 | |
| 203 string16 frame_xpath; | |
| 204 render_view()->EvaluateScript(frame_xpath, js16, 0, false); | |
| 191 } | 205 } |
| 206 | |
| 207 WebKit::WebURLError NetErrorHelper::GetUpdatedError() | |
| 208 const { | |
|
mmenke
2013/05/28 18:20:13
nit: const should go on previous line.
Deprecated (see juliatuttle)
2013/06/05 21:01:47
Done.
| |
| 209 // If a probe didn't run or wasn't conclusive, restore the original error. | |
| 210 if (last_status_ == chrome_common_net::DNS_PROBE_NOT_RUN || | |
| 211 last_status_ == chrome_common_net::DNS_PROBE_FINISHED_UNKNOWN) { | |
| 212 return last_error_; | |
| 213 } | |
| 214 | |
| 215 WebKit::WebURLError error; | |
| 216 error.domain = WebKit::WebString::fromUTF8( | |
| 217 chrome_common_net::kDnsProbeErrorDomain); | |
| 218 error.reason = last_status_; | |
| 219 error.unreachableURL = last_error_.unreachableURL; | |
| 220 | |
| 221 return error; | |
| 222 } | |
| OLD | NEW |