| 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" | |
| 10 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 11 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/values.h" | 12 #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 navigation correction service to return | 48 // Number of seconds to wait for the alternate error page server. If it takes |
| 49 // suggestions. If it takes too long, just use the local error page. | 49 // too long, just use the local error page. |
| 50 static const int kNavigationCorrectionFetchTimeoutSec = 3; | 50 static const int kAlterErrorPageFetchTimeoutSec = 3000; |
| 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 | |
| 77 } // namespace | 65 } // namespace |
| 78 | 66 |
| 79 NetErrorHelper::NetErrorHelper(RenderFrame* render_view) | 67 NetErrorHelper::NetErrorHelper(RenderFrame* render_view) |
| 80 : RenderFrameObserver(render_view), | 68 : RenderFrameObserver(render_view), |
| 81 content::RenderFrameObserverTracker<NetErrorHelper>(render_view), | 69 content::RenderFrameObserverTracker<NetErrorHelper>(render_view), |
| 82 core_(this) { | 70 core_(this) { |
| 83 } | 71 } |
| 84 | 72 |
| 85 NetErrorHelper::~NetErrorHelper() { | 73 NetErrorHelper::~NetErrorHelper() { |
| 86 } | 74 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 102 | 90 |
| 103 void NetErrorHelper::OnStop() { | 91 void NetErrorHelper::OnStop() { |
| 104 core_.OnStop(); | 92 core_.OnStop(); |
| 105 } | 93 } |
| 106 | 94 |
| 107 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) { | 95 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) { |
| 108 bool handled = true; | 96 bool handled = true; |
| 109 | 97 |
| 110 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message) | 98 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message) |
| 111 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo) | 99 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo) |
| 112 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetNavigationCorrectionInfo, | 100 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetAltErrorPageURL, OnSetAltErrorPageURL); |
| 113 OnSetNavigationCorrectionInfo); | |
| 114 IPC_MESSAGE_UNHANDLED(handled = false) | 101 IPC_MESSAGE_UNHANDLED(handled = false) |
| 115 IPC_END_MESSAGE_MAP() | 102 IPC_END_MESSAGE_MAP() |
| 116 | 103 |
| 117 return handled; | 104 return handled; |
| 118 } | 105 } |
| 119 | 106 |
| 120 void NetErrorHelper::GetErrorHTML( | 107 void NetErrorHelper::GetErrorHTML( |
| 121 blink::WebFrame* frame, | 108 blink::WebFrame* frame, |
| 122 const blink::WebURLError& error, | 109 const blink::WebURLError& error, |
| 123 bool is_failed_post, | 110 bool is_failed_post, |
| 124 std::string* error_html) { | 111 std::string* error_html) { |
| 125 core_.GetErrorHTML(GetFrameType(frame), error, is_failed_post, error_html); | 112 core_.GetErrorHTML(GetFrameType(frame), error, is_failed_post, error_html); |
| 126 } | 113 } |
| 127 | 114 |
| 128 void NetErrorHelper::GenerateLocalizedErrorPage( | 115 void NetErrorHelper::GenerateLocalizedErrorPage(const blink::WebURLError& error, |
| 129 const blink::WebURLError& error, | 116 bool is_failed_post, |
| 130 bool is_failed_post, | 117 std::string* error_html) const { |
| 131 scoped_ptr<LocalizedError::ErrorPageParams> params, | |
| 132 std::string* error_html) const { | |
| 133 error_html->clear(); | 118 error_html->clear(); |
| 134 | 119 |
| 135 int resource_id = IDR_NET_ERROR_HTML; | 120 int resource_id = IDR_NET_ERROR_HTML; |
| 136 const base::StringPiece template_html( | 121 const base::StringPiece template_html( |
| 137 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id)); | 122 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id)); |
| 138 if (template_html.empty()) { | 123 if (template_html.empty()) { |
| 139 NOTREACHED() << "unable to load template."; | 124 NOTREACHED() << "unable to load template."; |
| 140 } else { | 125 } else { |
| 141 base::DictionaryValue error_strings; | 126 base::DictionaryValue error_strings; |
| 142 LocalizedError::GetStrings(error.reason, error.domain.utf8(), | 127 LocalizedError::GetStrings(error.reason, error.domain.utf8(), |
| 143 error.unreachableURL, is_failed_post, | 128 error.unreachableURL, is_failed_post, |
| 144 error.staleCopyInCache, | 129 error.staleCopyInCache, |
| 145 RenderThread::Get()->GetLocale(), | 130 RenderThread::Get()->GetLocale(), |
| 146 render_frame()->GetRenderView()-> | 131 render_frame()->GetRenderView()-> |
| 147 GetAcceptLanguages(), | 132 GetAcceptLanguages(), |
| 148 params.Pass(), &error_strings); | 133 &error_strings); |
| 149 // "t" is the id of the template's root node. | 134 // "t" is the id of the template's root node. |
| 150 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t"); | 135 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t"); |
| 151 } | 136 } |
| 152 } | 137 } |
| 153 | 138 |
| 154 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html, | 139 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html, |
| 155 const GURL& failed_url) { | 140 const GURL& failed_url) { |
| 156 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); | 141 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); |
| 157 if (!web_view) | 142 if (!web_view) |
| 158 return; | 143 return; |
| 159 blink::WebFrame* frame = web_view->mainFrame(); | 144 blink::WebFrame* frame = web_view->mainFrame(); |
| 160 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true); | 145 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true); |
| 161 } | 146 } |
| 162 | 147 |
| 163 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error, | 148 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error, |
| 164 bool is_failed_post) { | 149 bool is_failed_post) { |
| 165 base::DictionaryValue error_strings; | 150 base::DictionaryValue error_strings; |
| 166 LocalizedError::GetStrings(error.reason, | 151 LocalizedError::GetStrings(error.reason, |
| 167 error.domain.utf8(), | 152 error.domain.utf8(), |
| 168 error.unreachableURL, | 153 error.unreachableURL, |
| 169 is_failed_post, | 154 is_failed_post, |
| 170 error.staleCopyInCache, | 155 error.staleCopyInCache, |
| 171 RenderThread::Get()->GetLocale(), | 156 RenderThread::Get()->GetLocale(), |
| 172 render_frame()->GetRenderView()-> | 157 render_frame()->GetRenderView()-> |
| 173 GetAcceptLanguages(), | 158 GetAcceptLanguages(), |
| 174 scoped_ptr<LocalizedError::ErrorPageParams>(), | |
| 175 &error_strings); | 159 &error_strings); |
| 176 | 160 |
| 177 std::string json; | 161 std::string json; |
| 178 JSONWriter::Write(&error_strings, &json); | 162 JSONWriter::Write(&error_strings, &json); |
| 179 | 163 |
| 180 std::string js = "if (window.updateForDnsProbe) " | 164 std::string js = "if (window.updateForDnsProbe) " |
| 181 "updateForDnsProbe(" + json + ");"; | 165 "updateForDnsProbe(" + json + ");"; |
| 182 base::string16 js16; | 166 base::string16 js16; |
| 183 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) { | 167 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) { |
| 184 NOTREACHED(); | 168 NOTREACHED(); |
| 185 return; | 169 return; |
| 186 } | 170 } |
| 187 | 171 |
| 188 base::string16 frame_xpath; | 172 base::string16 frame_xpath; |
| 189 render_frame()->GetRenderView()->EvaluateScript(frame_xpath, js16, 0, false); | 173 render_frame()->GetRenderView()->EvaluateScript(frame_xpath, js16, 0, false); |
| 190 } | 174 } |
| 191 | 175 |
| 192 void NetErrorHelper::FetchNavigationCorrections( | 176 void NetErrorHelper::FetchErrorPage(const GURL& url) { |
| 193 const GURL& navigation_correction_url, | 177 DCHECK(!alt_error_page_fetcher_.get()); |
| 194 const std::string& navigation_correction_request_body) { | |
| 195 DCHECK(!correction_fetcher_.get()); | |
| 196 | 178 |
| 197 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); | 179 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); |
| 198 if (!web_view) | 180 if (!web_view) |
| 199 return; | 181 return; |
| 200 blink::WebFrame* frame = web_view->mainFrame(); | 182 blink::WebFrame* frame = web_view->mainFrame(); |
| 201 | 183 |
| 202 correction_fetcher_.reset( | 184 alt_error_page_fetcher_.reset(content::ResourceFetcher::Create(url)); |
| 203 content::ResourceFetcher::Create(navigation_correction_url)); | 185 |
| 204 correction_fetcher_->SetMethod("POST"); | 186 alt_error_page_fetcher_->Start( |
| 205 correction_fetcher_->SetBody(navigation_correction_request_body); | |
| 206 correction_fetcher_->SetHeader("Content-Type", "application/json"); | |
| 207 correction_fetcher_->Start( | |
| 208 frame, blink::WebURLRequest::TargetIsMainFrame, | 187 frame, blink::WebURLRequest::TargetIsMainFrame, |
| 209 base::Bind(&NetErrorHelper::OnNavigationCorrectionsFetched, | 188 base::Bind(&NetErrorHelper::OnAlternateErrorPageRetrieved, |
| 210 base::Unretained(this))); | 189 base::Unretained(this))); |
| 211 | 190 |
| 212 correction_fetcher_->SetTimeout( | 191 alt_error_page_fetcher_->SetTimeout( |
| 213 base::TimeDelta::FromSeconds(kNavigationCorrectionFetchTimeoutSec)); | 192 base::TimeDelta::FromSeconds(kAlterErrorPageFetchTimeoutSec)); |
| 214 } | 193 } |
| 215 | 194 |
| 216 void NetErrorHelper::CancelFetchNavigationCorrections() { | 195 void NetErrorHelper::CancelFetchErrorPage() { |
| 217 correction_fetcher_.reset(); | 196 alt_error_page_fetcher_.reset(); |
| 218 } | 197 } |
| 219 | 198 |
| 220 void NetErrorHelper::OnNetErrorInfo(int status_num) { | 199 void NetErrorHelper::OnNetErrorInfo(int status_num) { |
| 221 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX); | 200 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX); |
| 222 | 201 |
| 223 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); | 202 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); |
| 224 | 203 |
| 225 core_.OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); | 204 core_.OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); |
| 226 } | 205 } |
| 227 | 206 |
| 228 void NetErrorHelper::OnSetNavigationCorrectionInfo( | 207 void NetErrorHelper::OnSetAltErrorPageURL(const GURL& alt_error_page_url) { |
| 229 const GURL& navigation_correction_url, | 208 core_.set_alt_error_page_url(alt_error_page_url); |
| 230 const std::string& language, | |
| 231 const std::string& country_code, | |
| 232 const std::string& api_key, | |
| 233 const GURL& search_url) { | |
| 234 core_.OnSetNavigationCorrectionInfo(navigation_correction_url, language, | |
| 235 country_code, api_key, search_url); | |
| 236 } | 209 } |
| 237 | 210 |
| 238 void NetErrorHelper::OnNavigationCorrectionsFetched( | 211 void NetErrorHelper::OnAlternateErrorPageRetrieved( |
| 239 const blink::WebURLResponse& response, | 212 const blink::WebURLResponse& response, |
| 240 const std::string& data) { | 213 const std::string& data) { |
| 241 // The fetcher may only be deleted after |data| is passed to |core_|. Move | 214 // The fetcher may only be deleted after |data| is passed to |core_|. Move |
| 242 // it to a temporary to prevent any potential re-entrancy issues. | 215 // it to a temporary to prevent any potential re-entrancy issues. |
| 243 scoped_ptr<content::ResourceFetcher> fetcher( | 216 scoped_ptr<content::ResourceFetcher> fetcher( |
| 244 correction_fetcher_.release()); | 217 alt_error_page_fetcher_.release()); |
| 245 if (!response.isNull() && response.httpStatusCode() == 200) { | 218 if (!response.isNull() && response.httpStatusCode() == 200) { |
| 246 core_.OnNavigationCorrectionsFetched( | 219 core_.OnAlternateErrorPageFetched(data); |
| 247 data, render_frame()->GetRenderView()->GetAcceptLanguages(), | |
| 248 LocaleIsRTL()); | |
| 249 } else { | 220 } else { |
| 250 core_.OnNavigationCorrectionsFetched( | 221 core_.OnAlternateErrorPageFetched(""); |
| 251 "", render_frame()->GetRenderView()->GetAcceptLanguages(), | |
| 252 LocaleIsRTL()); | |
| 253 } | 222 } |
| 254 } | 223 } |
| OLD | NEW |