| 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> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" |
| 9 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 10 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/common/localized_error.h" | |
| 14 #include "chrome/common/net/net_error_info.h" | 14 #include "chrome/common/net/net_error_info.h" |
| 15 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
| 16 #include "content/public/common/content_client.h" | 16 #include "content/public/common/content_client.h" |
| 17 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
| 18 #include "content/public/renderer/content_renderer_client.h" | 18 #include "content/public/renderer/content_renderer_client.h" |
| 19 #include "content/public/renderer/render_frame.h" | 19 #include "content/public/renderer/render_frame.h" |
| 20 #include "content/public/renderer/render_thread.h" | 20 #include "content/public/renderer/render_thread.h" |
| 21 #include "content/public/renderer/render_view.h" | 21 #include "content/public/renderer/render_view.h" |
| 22 #include "content/public/renderer/resource_fetcher.h" | 22 #include "content/public/renderer/resource_fetcher.h" |
| 23 #include "grit/renderer_resources.h" | 23 #include "grit/renderer_resources.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 38 using base::JSONWriter; | 38 using base::JSONWriter; |
| 39 using chrome_common_net::DnsProbeStatus; | 39 using chrome_common_net::DnsProbeStatus; |
| 40 using chrome_common_net::DnsProbeStatusToString; | 40 using chrome_common_net::DnsProbeStatusToString; |
| 41 using content::RenderFrame; | 41 using content::RenderFrame; |
| 42 using content::RenderFrameObserver; | 42 using content::RenderFrameObserver; |
| 43 using content::RenderThread; | 43 using content::RenderThread; |
| 44 using content::kUnreachableWebDataURL; | 44 using content::kUnreachableWebDataURL; |
| 45 | 45 |
| 46 namespace { | 46 namespace { |
| 47 | 47 |
| 48 // Number of seconds to wait for the alternate error page server. If it takes | 48 // Number of seconds to wait for the Link Doctor FixURL service to return |
| 49 // too long, just use the local error page. | 49 // suggestions. If it takes too long, just use the local error page. |
| 50 static const int kAlterErrorPageFetchTimeoutSec = 3000; | 50 static const int kLinkDoctorFetchTimeoutSec = 3; |
| 51 | 51 |
| 52 NetErrorHelperCore::PageType GetLoadingPageType(const blink::WebFrame* frame) { | 52 NetErrorHelperCore::PageType GetLoadingPageType(const blink::WebFrame* frame) { |
| 53 GURL url = frame->provisionalDataSource()->request().url(); | 53 GURL url = frame->provisionalDataSource()->request().url(); |
| 54 if (!url.is_valid() || url.spec() != kUnreachableWebDataURL) | 54 if (!url.is_valid() || url.spec() != kUnreachableWebDataURL) |
| 55 return NetErrorHelperCore::NON_ERROR_PAGE; | 55 return NetErrorHelperCore::NON_ERROR_PAGE; |
| 56 return NetErrorHelperCore::ERROR_PAGE; | 56 return NetErrorHelperCore::ERROR_PAGE; |
| 57 } | 57 } |
| 58 | 58 |
| 59 NetErrorHelperCore::FrameType GetFrameType(const blink::WebFrame* frame) { | 59 NetErrorHelperCore::FrameType GetFrameType(const blink::WebFrame* frame) { |
| 60 if (!frame->parent()) | 60 if (!frame->parent()) |
| 61 return NetErrorHelperCore::MAIN_FRAME; | 61 return NetErrorHelperCore::MAIN_FRAME; |
| 62 return NetErrorHelperCore::SUB_FRAME; | 62 return NetErrorHelperCore::SUB_FRAME; |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Copied from localized_error.cc. |
| 66 // TODO(mmenke): Share code? |
| 67 bool LocaleIsRTL() { |
| 68 #if defined(TOOLKIT_GTK) |
| 69 // base::i18n::IsRTL() uses the GTK text direction, which doesn't work within |
| 70 // the renderer sandbox. |
| 71 return base::i18n::ICUIsRTL(); |
| 72 #else |
| 73 return base::i18n::IsRTL(); |
| 74 #endif |
| 75 } |
| 76 |
| 65 } // namespace | 77 } // namespace |
| 66 | 78 |
| 67 NetErrorHelper::NetErrorHelper(RenderFrame* render_view) | 79 NetErrorHelper::NetErrorHelper(RenderFrame* render_view) |
| 68 : RenderFrameObserver(render_view), | 80 : RenderFrameObserver(render_view), |
| 69 content::RenderFrameObserverTracker<NetErrorHelper>(render_view), | 81 content::RenderFrameObserverTracker<NetErrorHelper>(render_view), |
| 70 core_(this) { | 82 core_(this) { |
| 71 } | 83 } |
| 72 | 84 |
| 73 NetErrorHelper::~NetErrorHelper() { | 85 NetErrorHelper::~NetErrorHelper() { |
| 74 } | 86 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 90 | 102 |
| 91 void NetErrorHelper::OnStop() { | 103 void NetErrorHelper::OnStop() { |
| 92 core_.OnStop(); | 104 core_.OnStop(); |
| 93 } | 105 } |
| 94 | 106 |
| 95 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) { | 107 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) { |
| 96 bool handled = true; | 108 bool handled = true; |
| 97 | 109 |
| 98 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message) | 110 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message) |
| 99 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo) | 111 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo) |
| 100 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAltErrorPageURL, OnSetAltErrorPageURL); | 112 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetLinkDoctorInfo, OnSetLinkDoctorInfo); |
| 101 IPC_MESSAGE_UNHANDLED(handled = false) | 113 IPC_MESSAGE_UNHANDLED(handled = false) |
| 102 IPC_END_MESSAGE_MAP() | 114 IPC_END_MESSAGE_MAP() |
| 103 | 115 |
| 104 return handled; | 116 return handled; |
| 105 } | 117 } |
| 106 | 118 |
| 107 void NetErrorHelper::GetErrorHTML( | 119 void NetErrorHelper::GetErrorHTML( |
| 108 blink::WebFrame* frame, | 120 blink::WebFrame* frame, |
| 109 const blink::WebURLError& error, | 121 const blink::WebURLError& error, |
| 110 bool is_failed_post, | 122 bool is_failed_post, |
| 111 std::string* error_html) { | 123 std::string* error_html) { |
| 112 core_.GetErrorHTML(GetFrameType(frame), error, is_failed_post, error_html); | 124 core_.GetErrorHTML(GetFrameType(frame), error, is_failed_post, error_html); |
| 113 } | 125 } |
| 114 | 126 |
| 115 void NetErrorHelper::GenerateLocalizedErrorPage(const blink::WebURLError& error, | 127 void NetErrorHelper::GenerateLocalizedErrorPage( |
| 116 bool is_failed_post, | 128 const blink::WebURLError& error, |
| 117 std::string* error_html) const { | 129 bool is_failed_post, |
| 130 scoped_ptr<LocalizedError::ErrorPageParams> params, |
| 131 std::string* error_html) const { |
| 118 error_html->clear(); | 132 error_html->clear(); |
| 119 | 133 |
| 120 int resource_id = IDR_NET_ERROR_HTML; | 134 int resource_id = IDR_NET_ERROR_HTML; |
| 121 const base::StringPiece template_html( | 135 const base::StringPiece template_html( |
| 122 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id)); | 136 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id)); |
| 123 if (template_html.empty()) { | 137 if (template_html.empty()) { |
| 124 NOTREACHED() << "unable to load template."; | 138 NOTREACHED() << "unable to load template."; |
| 125 } else { | 139 } else { |
| 126 base::DictionaryValue error_strings; | 140 base::DictionaryValue error_strings; |
| 127 LocalizedError::GetStrings(error.reason, error.domain.utf8(), | 141 LocalizedError::GetStrings(error.reason, error.domain.utf8(), |
| 128 error.unreachableURL, is_failed_post, | 142 error.unreachableURL, is_failed_post, |
| 129 error.staleCopyInCache, | 143 error.staleCopyInCache, |
| 130 RenderThread::Get()->GetLocale(), | 144 RenderThread::Get()->GetLocale(), |
| 131 render_frame()->GetRenderView()-> | 145 render_frame()->GetRenderView()-> |
| 132 GetAcceptLanguages(), | 146 GetAcceptLanguages(), |
| 133 &error_strings); | 147 params.Pass(), &error_strings); |
| 134 // "t" is the id of the template's root node. | 148 // "t" is the id of the template's root node. |
| 135 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t"); | 149 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t"); |
| 136 } | 150 } |
| 137 } | 151 } |
| 138 | 152 |
| 139 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html, | 153 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html, |
| 140 const GURL& failed_url) { | 154 const GURL& failed_url) { |
| 141 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); | 155 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); |
| 142 if (!web_view) | 156 if (!web_view) |
| 143 return; | 157 return; |
| 144 blink::WebFrame* frame = web_view->mainFrame(); | 158 blink::WebFrame* frame = web_view->mainFrame(); |
| 145 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true); | 159 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true); |
| 146 } | 160 } |
| 147 | 161 |
| 148 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error, | 162 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error, |
| 149 bool is_failed_post) { | 163 bool is_failed_post) { |
| 150 base::DictionaryValue error_strings; | 164 base::DictionaryValue error_strings; |
| 151 LocalizedError::GetStrings(error.reason, | 165 LocalizedError::GetStrings(error.reason, |
| 152 error.domain.utf8(), | 166 error.domain.utf8(), |
| 153 error.unreachableURL, | 167 error.unreachableURL, |
| 154 is_failed_post, | 168 is_failed_post, |
| 155 error.staleCopyInCache, | 169 error.staleCopyInCache, |
| 156 RenderThread::Get()->GetLocale(), | 170 RenderThread::Get()->GetLocale(), |
| 157 render_frame()->GetRenderView()-> | 171 render_frame()->GetRenderView()-> |
| 158 GetAcceptLanguages(), | 172 GetAcceptLanguages(), |
| 173 scoped_ptr<LocalizedError::ErrorPageParams>(), |
| 159 &error_strings); | 174 &error_strings); |
| 160 | 175 |
| 161 std::string json; | 176 std::string json; |
| 162 JSONWriter::Write(&error_strings, &json); | 177 JSONWriter::Write(&error_strings, &json); |
| 163 | 178 |
| 164 std::string js = "if (window.updateForDnsProbe) " | 179 std::string js = "if (window.updateForDnsProbe) " |
| 165 "updateForDnsProbe(" + json + ");"; | 180 "updateForDnsProbe(" + json + ");"; |
| 166 base::string16 js16; | 181 base::string16 js16; |
| 167 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) { | 182 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) { |
| 168 NOTREACHED(); | 183 NOTREACHED(); |
| 169 return; | 184 return; |
| 170 } | 185 } |
| 171 | 186 |
| 172 base::string16 frame_xpath; | 187 base::string16 frame_xpath; |
| 173 render_frame()->GetRenderView()->EvaluateScript(frame_xpath, js16, 0, false); | 188 render_frame()->GetRenderView()->EvaluateScript(frame_xpath, js16, 0, false); |
| 174 } | 189 } |
| 175 | 190 |
| 176 void NetErrorHelper::FetchErrorPage(const GURL& url) { | 191 void NetErrorHelper::FetchLinkDoctorSuggestions( |
| 177 DCHECK(!alt_error_page_fetcher_.get()); | 192 const GURL& link_doctor_url, |
| 193 const std::string& link_doctor_url_request_body) { |
| 194 DCHECK(!link_doctor_fetcher_.get()); |
| 178 | 195 |
| 179 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); | 196 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); |
| 180 if (!web_view) | 197 if (!web_view) |
| 181 return; | 198 return; |
| 182 blink::WebFrame* frame = web_view->mainFrame(); | 199 blink::WebFrame* frame = web_view->mainFrame(); |
| 183 | 200 |
| 184 alt_error_page_fetcher_.reset(content::ResourceFetcher::Create(url)); | 201 link_doctor_fetcher_.reset( |
| 185 | 202 content::ResourceFetcher::Create(link_doctor_url)); |
| 186 alt_error_page_fetcher_->Start( | 203 link_doctor_fetcher_->SetMethod("POST"); |
| 204 link_doctor_fetcher_->SetBody(link_doctor_url_request_body); |
| 205 link_doctor_fetcher_->SetHeader("Content-Type", "application/json"); |
| 206 link_doctor_fetcher_->Start( |
| 187 frame, blink::WebURLRequest::TargetIsMainFrame, | 207 frame, blink::WebURLRequest::TargetIsMainFrame, |
| 188 base::Bind(&NetErrorHelper::OnAlternateErrorPageRetrieved, | 208 base::Bind(&NetErrorHelper::OnLinkDoctorSuggestionsFetched, |
| 189 base::Unretained(this))); | 209 base::Unretained(this))); |
| 190 | 210 |
| 191 alt_error_page_fetcher_->SetTimeout( | 211 link_doctor_fetcher_->SetTimeout( |
| 192 base::TimeDelta::FromSeconds(kAlterErrorPageFetchTimeoutSec)); | 212 base::TimeDelta::FromSeconds(kLinkDoctorFetchTimeoutSec)); |
| 193 } | 213 } |
| 194 | 214 |
| 195 void NetErrorHelper::CancelFetchErrorPage() { | 215 void NetErrorHelper::CancelFetchLinkDoctorSuggestions() { |
| 196 alt_error_page_fetcher_.reset(); | 216 link_doctor_fetcher_.reset(); |
| 197 } | 217 } |
| 198 | 218 |
| 199 void NetErrorHelper::OnNetErrorInfo(int status_num) { | 219 void NetErrorHelper::OnNetErrorInfo(int status_num) { |
| 200 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX); | 220 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX); |
| 201 | 221 |
| 202 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); | 222 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); |
| 203 | 223 |
| 204 core_.OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); | 224 core_.OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); |
| 205 } | 225 } |
| 206 | 226 |
| 207 void NetErrorHelper::OnSetAltErrorPageURL(const GURL& alt_error_page_url) { | 227 void NetErrorHelper::OnSetLinkDoctorInfo(const GURL& link_doctor_url, |
| 208 core_.set_alt_error_page_url(alt_error_page_url); | 228 const std::string& language, |
| 229 const std::string& country_code, |
| 230 const std::string& api_key, |
| 231 const GURL& search_url) { |
| 232 core_.OnSetLinkDoctorInfo(link_doctor_url, language, country_code, api_key, |
| 233 search_url); |
| 209 } | 234 } |
| 210 | 235 |
| 211 void NetErrorHelper::OnAlternateErrorPageRetrieved( | 236 void NetErrorHelper::OnLinkDoctorSuggestionsFetched( |
| 212 const blink::WebURLResponse& response, | 237 const blink::WebURLResponse& response, |
| 213 const std::string& data) { | 238 const std::string& data) { |
| 214 // The fetcher may only be deleted after |data| is passed to |core_|. Move | 239 // The fetcher may only be deleted after |data| is passed to |core_|. Move |
| 215 // it to a temporary to prevent any potential re-entrancy issues. | 240 // it to a temporary to prevent any potential re-entrancy issues. |
| 216 scoped_ptr<content::ResourceFetcher> fetcher( | 241 scoped_ptr<content::ResourceFetcher> fetcher( |
| 217 alt_error_page_fetcher_.release()); | 242 link_doctor_fetcher_.release()); |
| 218 if (!response.isNull() && response.httpStatusCode() == 200) { | 243 if (!response.isNull() && response.httpStatusCode() == 200) { |
| 219 core_.OnAlternateErrorPageFetched(data); | 244 core_.OnLinkDoctorSuggestionsFetched( |
| 245 data, render_frame()->GetRenderView()->GetAcceptLanguages(), |
| 246 LocaleIsRTL()); |
| 220 } else { | 247 } else { |
| 221 core_.OnAlternateErrorPageFetched(""); | 248 core_.OnLinkDoctorSuggestionsFetched( |
| 249 "", render_frame()->GetRenderView()->GetAcceptLanguages(), |
| 250 LocaleIsRTL()); |
| 222 } | 251 } |
| 223 } | 252 } |
| OLD | NEW |