| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 RenderThread::Get()->GetLocale(), | 143 RenderThread::Get()->GetLocale(), |
| 130 render_frame()->GetRenderView()-> | 144 render_frame()->GetRenderView()-> |
| 131 GetAcceptLanguages(), | 145 GetAcceptLanguages(), |
| 132 &error_strings); | 146 params.Pass(), &error_strings); |
| 133 // "t" is the id of the template's root node. | 147 // "t" is the id of the template's root node. |
| 134 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t"); | 148 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t"); |
| 135 } | 149 } |
| 136 } | 150 } |
| 137 | 151 |
| 138 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html, | 152 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html, |
| 139 const GURL& failed_url) { | 153 const GURL& failed_url) { |
| 140 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); | 154 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); |
| 141 if (!web_view) | 155 if (!web_view) |
| 142 return; | 156 return; |
| 143 blink::WebFrame* frame = web_view->mainFrame(); | 157 blink::WebFrame* frame = web_view->mainFrame(); |
| 144 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true); | 158 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true); |
| 145 } | 159 } |
| 146 | 160 |
| 147 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error, | 161 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error, |
| 148 bool is_failed_post) { | 162 bool is_failed_post) { |
| 149 base::DictionaryValue error_strings; | 163 base::DictionaryValue error_strings; |
| 150 LocalizedError::GetStrings(error.reason, | 164 LocalizedError::GetStrings(error.reason, |
| 151 error.domain.utf8(), | 165 error.domain.utf8(), |
| 152 error.unreachableURL, | 166 error.unreachableURL, |
| 153 is_failed_post, | 167 is_failed_post, |
| 154 RenderThread::Get()->GetLocale(), | 168 RenderThread::Get()->GetLocale(), |
| 155 render_frame()->GetRenderView()-> | 169 render_frame()->GetRenderView()-> |
| 156 GetAcceptLanguages(), | 170 GetAcceptLanguages(), |
| 171 scoped_ptr<LocalizedError::ErrorPageParams>(), |
| 157 &error_strings); | 172 &error_strings); |
| 158 | 173 |
| 159 std::string json; | 174 std::string json; |
| 160 JSONWriter::Write(&error_strings, &json); | 175 JSONWriter::Write(&error_strings, &json); |
| 161 | 176 |
| 162 std::string js = "if (window.updateForDnsProbe) " | 177 std::string js = "if (window.updateForDnsProbe) " |
| 163 "updateForDnsProbe(" + json + ");"; | 178 "updateForDnsProbe(" + json + ");"; |
| 164 base::string16 js16; | 179 base::string16 js16; |
| 165 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) { | 180 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) { |
| 166 NOTREACHED(); | 181 NOTREACHED(); |
| 167 return; | 182 return; |
| 168 } | 183 } |
| 169 | 184 |
| 170 base::string16 frame_xpath; | 185 base::string16 frame_xpath; |
| 171 render_frame()->GetRenderView()->EvaluateScript(frame_xpath, js16, 0, false); | 186 render_frame()->GetRenderView()->EvaluateScript(frame_xpath, js16, 0, false); |
| 172 } | 187 } |
| 173 | 188 |
| 174 void NetErrorHelper::FetchErrorPage(const GURL& url) { | 189 void NetErrorHelper::FetchErrorPage(const GURL& fix_url, |
| 190 const std::string& fix_url_request_body) { |
| 175 DCHECK(!alt_error_page_fetcher_.get()); | 191 DCHECK(!alt_error_page_fetcher_.get()); |
| 176 | 192 |
| 177 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); | 193 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); |
| 178 if (!web_view) | 194 if (!web_view) |
| 179 return; | 195 return; |
| 180 blink::WebFrame* frame = web_view->mainFrame(); | 196 blink::WebFrame* frame = web_view->mainFrame(); |
| 181 | 197 |
| 182 alt_error_page_fetcher_.reset(content::ResourceFetcher::Create(url)); | 198 alt_error_page_fetcher_.reset( |
| 183 | 199 content::ResourceFetcher::Create(fix_url)); |
| 200 alt_error_page_fetcher_->SetMethod("POST"); |
| 201 alt_error_page_fetcher_->SetBody(fix_url_request_body); |
| 202 alt_error_page_fetcher_->SetHeader("Content-Type", "application/json"); |
| 184 alt_error_page_fetcher_->Start( | 203 alt_error_page_fetcher_->Start( |
| 185 frame, blink::WebURLRequest::TargetIsMainFrame, | 204 frame, blink::WebURLRequest::TargetIsMainFrame, |
| 186 base::Bind(&NetErrorHelper::OnAlternateErrorPageRetrieved, | 205 base::Bind(&NetErrorHelper::OnAlternateErrorPageRetrieved, |
| 187 base::Unretained(this))); | 206 base::Unretained(this))); |
| 188 | 207 |
| 189 alt_error_page_fetcher_->SetTimeout( | 208 alt_error_page_fetcher_->SetTimeout( |
| 190 base::TimeDelta::FromSeconds(kAlterErrorPageFetchTimeoutSec)); | 209 base::TimeDelta::FromSeconds(kAlterErrorPageFetchTimeoutSec)); |
| 191 } | 210 } |
| 192 | 211 |
| 193 void NetErrorHelper::CancelFetchErrorPage() { | 212 void NetErrorHelper::CancelFetchErrorPage() { |
| 194 alt_error_page_fetcher_.reset(); | 213 alt_error_page_fetcher_.reset(); |
| 195 } | 214 } |
| 196 | 215 |
| 197 void NetErrorHelper::OnNetErrorInfo(int status_num) { | 216 void NetErrorHelper::OnNetErrorInfo(int status_num) { |
| 198 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX); | 217 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX); |
| 199 | 218 |
| 200 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); | 219 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); |
| 201 | 220 |
| 202 core_.OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); | 221 core_.OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); |
| 203 } | 222 } |
| 204 | 223 |
| 205 void NetErrorHelper::OnSetAltErrorPageURL(const GURL& alt_error_page_url) { | 224 void NetErrorHelper::OnSetLinkDoctorInfo(const GURL& link_doctor_url, |
| 206 core_.set_alt_error_page_url(alt_error_page_url); | 225 const std::string& language, |
| 226 const std::string& country_code, |
| 227 const std::string& api_key, |
| 228 const GURL& search_url) { |
| 229 core_.OnSetLinkDoctorInfo(link_doctor_url, language, country_code, api_key, |
| 230 search_url); |
| 207 } | 231 } |
| 208 | 232 |
| 209 void NetErrorHelper::OnAlternateErrorPageRetrieved( | 233 void NetErrorHelper::OnAlternateErrorPageRetrieved( |
| 210 const blink::WebURLResponse& response, | 234 const blink::WebURLResponse& response, |
| 211 const std::string& data) { | 235 const std::string& data) { |
| 212 // The fetcher may only be deleted after |data| is passed to |core_|. Move | 236 // The fetcher may only be deleted after |data| is passed to |core_|. Move |
| 213 // it to a temporary to prevent any potential re-entrancy issues. | 237 // it to a temporary to prevent any potential re-entrancy issues. |
| 214 scoped_ptr<content::ResourceFetcher> fetcher( | 238 scoped_ptr<content::ResourceFetcher> fetcher( |
| 215 alt_error_page_fetcher_.release()); | 239 alt_error_page_fetcher_.release()); |
| 216 if (!response.isNull() && response.httpStatusCode() == 200) { | 240 if (!response.isNull() && response.httpStatusCode() == 200) { |
| 217 core_.OnAlternateErrorPageFetched(data); | 241 core_.OnAlternateErrorPageFetched( |
| 242 data, render_frame()->GetRenderView()->GetAcceptLanguages(), |
| 243 LocaleIsRTL()); |
| 218 } else { | 244 } else { |
| 219 core_.OnAlternateErrorPageFetched(""); | 245 core_.OnAlternateErrorPageFetched( |
| 246 "", render_frame()->GetRenderView()->GetAcceptLanguages(), |
| 247 LocaleIsRTL()); |
| 220 } | 248 } |
| 221 } | 249 } |
| OLD | NEW |