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 navigation correction 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 kNavigationCorrectionFetchTimeoutSec = 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_SetNavigationCorrectionInfo, |
| 113 OnSetNavigationCorrectionInfo); |
101 IPC_MESSAGE_UNHANDLED(handled = false) | 114 IPC_MESSAGE_UNHANDLED(handled = false) |
102 IPC_END_MESSAGE_MAP() | 115 IPC_END_MESSAGE_MAP() |
103 | 116 |
104 return handled; | 117 return handled; |
105 } | 118 } |
106 | 119 |
107 void NetErrorHelper::GetErrorHTML( | 120 void NetErrorHelper::GetErrorHTML( |
108 blink::WebFrame* frame, | 121 blink::WebFrame* frame, |
109 const blink::WebURLError& error, | 122 const blink::WebURLError& error, |
110 bool is_failed_post, | 123 bool is_failed_post, |
111 std::string* error_html) { | 124 std::string* error_html) { |
112 core_.GetErrorHTML(GetFrameType(frame), error, is_failed_post, error_html); | 125 core_.GetErrorHTML(GetFrameType(frame), error, is_failed_post, error_html); |
113 } | 126 } |
114 | 127 |
115 void NetErrorHelper::GenerateLocalizedErrorPage(const blink::WebURLError& error, | 128 void NetErrorHelper::GenerateLocalizedErrorPage( |
116 bool is_failed_post, | 129 const blink::WebURLError& error, |
117 std::string* error_html) const { | 130 bool is_failed_post, |
| 131 scoped_ptr<LocalizedError::ErrorPageParams> params, |
| 132 std::string* error_html) const { |
118 error_html->clear(); | 133 error_html->clear(); |
119 | 134 |
120 int resource_id = IDR_NET_ERROR_HTML; | 135 int resource_id = IDR_NET_ERROR_HTML; |
121 const base::StringPiece template_html( | 136 const base::StringPiece template_html( |
122 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id)); | 137 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id)); |
123 if (template_html.empty()) { | 138 if (template_html.empty()) { |
124 NOTREACHED() << "unable to load template."; | 139 NOTREACHED() << "unable to load template."; |
125 } else { | 140 } else { |
126 base::DictionaryValue error_strings; | 141 base::DictionaryValue error_strings; |
127 LocalizedError::GetStrings(error.reason, error.domain.utf8(), | 142 LocalizedError::GetStrings(error.reason, error.domain.utf8(), |
128 error.unreachableURL, is_failed_post, | 143 error.unreachableURL, is_failed_post, |
129 error.staleCopyInCache, | 144 error.staleCopyInCache, |
130 RenderThread::Get()->GetLocale(), | 145 RenderThread::Get()->GetLocale(), |
131 render_frame()->GetRenderView()-> | 146 render_frame()->GetRenderView()-> |
132 GetAcceptLanguages(), | 147 GetAcceptLanguages(), |
133 &error_strings); | 148 params.Pass(), &error_strings); |
134 // "t" is the id of the template's root node. | 149 // "t" is the id of the template's root node. |
135 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t"); | 150 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t"); |
136 } | 151 } |
137 } | 152 } |
138 | 153 |
139 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html, | 154 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html, |
140 const GURL& failed_url) { | 155 const GURL& failed_url) { |
141 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); | 156 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); |
142 if (!web_view) | 157 if (!web_view) |
143 return; | 158 return; |
144 blink::WebFrame* frame = web_view->mainFrame(); | 159 blink::WebFrame* frame = web_view->mainFrame(); |
145 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true); | 160 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true); |
146 } | 161 } |
147 | 162 |
148 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error, | 163 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error, |
149 bool is_failed_post) { | 164 bool is_failed_post) { |
150 base::DictionaryValue error_strings; | 165 base::DictionaryValue error_strings; |
151 LocalizedError::GetStrings(error.reason, | 166 LocalizedError::GetStrings(error.reason, |
152 error.domain.utf8(), | 167 error.domain.utf8(), |
153 error.unreachableURL, | 168 error.unreachableURL, |
154 is_failed_post, | 169 is_failed_post, |
155 error.staleCopyInCache, | 170 error.staleCopyInCache, |
156 RenderThread::Get()->GetLocale(), | 171 RenderThread::Get()->GetLocale(), |
157 render_frame()->GetRenderView()-> | 172 render_frame()->GetRenderView()-> |
158 GetAcceptLanguages(), | 173 GetAcceptLanguages(), |
| 174 scoped_ptr<LocalizedError::ErrorPageParams>(), |
159 &error_strings); | 175 &error_strings); |
160 | 176 |
161 std::string json; | 177 std::string json; |
162 JSONWriter::Write(&error_strings, &json); | 178 JSONWriter::Write(&error_strings, &json); |
163 | 179 |
164 std::string js = "if (window.updateForDnsProbe) " | 180 std::string js = "if (window.updateForDnsProbe) " |
165 "updateForDnsProbe(" + json + ");"; | 181 "updateForDnsProbe(" + json + ");"; |
166 base::string16 js16; | 182 base::string16 js16; |
167 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) { | 183 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) { |
168 NOTREACHED(); | 184 NOTREACHED(); |
169 return; | 185 return; |
170 } | 186 } |
171 | 187 |
172 base::string16 frame_xpath; | 188 base::string16 frame_xpath; |
173 render_frame()->GetRenderView()->EvaluateScript(frame_xpath, js16, 0, false); | 189 render_frame()->GetRenderView()->EvaluateScript(frame_xpath, js16, 0, false); |
174 } | 190 } |
175 | 191 |
176 void NetErrorHelper::FetchErrorPage(const GURL& url) { | 192 void NetErrorHelper::FetchNavigationCorrections( |
177 DCHECK(!alt_error_page_fetcher_.get()); | 193 const GURL& navigation_correction_url, |
| 194 const std::string& navigation_correction_request_body) { |
| 195 DCHECK(!correction_fetcher_.get()); |
178 | 196 |
179 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); | 197 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); |
180 if (!web_view) | 198 if (!web_view) |
181 return; | 199 return; |
182 blink::WebFrame* frame = web_view->mainFrame(); | 200 blink::WebFrame* frame = web_view->mainFrame(); |
183 | 201 |
184 alt_error_page_fetcher_.reset(content::ResourceFetcher::Create(url)); | 202 correction_fetcher_.reset( |
185 | 203 content::ResourceFetcher::Create(navigation_correction_url)); |
186 alt_error_page_fetcher_->Start( | 204 correction_fetcher_->SetMethod("POST"); |
| 205 correction_fetcher_->SetBody(navigation_correction_request_body); |
| 206 correction_fetcher_->SetHeader("Content-Type", "application/json"); |
| 207 correction_fetcher_->Start( |
187 frame, blink::WebURLRequest::TargetIsMainFrame, | 208 frame, blink::WebURLRequest::TargetIsMainFrame, |
188 base::Bind(&NetErrorHelper::OnAlternateErrorPageRetrieved, | 209 base::Bind(&NetErrorHelper::OnNavigationCorrectionsFetched, |
189 base::Unretained(this))); | 210 base::Unretained(this))); |
190 | 211 |
191 alt_error_page_fetcher_->SetTimeout( | 212 correction_fetcher_->SetTimeout( |
192 base::TimeDelta::FromSeconds(kAlterErrorPageFetchTimeoutSec)); | 213 base::TimeDelta::FromSeconds(kNavigationCorrectionFetchTimeoutSec)); |
193 } | 214 } |
194 | 215 |
195 void NetErrorHelper::CancelFetchErrorPage() { | 216 void NetErrorHelper::CancelFetchNavigationCorrections() { |
196 alt_error_page_fetcher_.reset(); | 217 correction_fetcher_.reset(); |
197 } | 218 } |
198 | 219 |
199 void NetErrorHelper::OnNetErrorInfo(int status_num) { | 220 void NetErrorHelper::OnNetErrorInfo(int status_num) { |
200 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX); | 221 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX); |
201 | 222 |
202 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); | 223 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); |
203 | 224 |
204 core_.OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); | 225 core_.OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); |
205 } | 226 } |
206 | 227 |
207 void NetErrorHelper::OnSetAltErrorPageURL(const GURL& alt_error_page_url) { | 228 void NetErrorHelper::OnSetNavigationCorrectionInfo( |
208 core_.set_alt_error_page_url(alt_error_page_url); | 229 const GURL& navigation_correction_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); |
209 } | 236 } |
210 | 237 |
211 void NetErrorHelper::OnAlternateErrorPageRetrieved( | 238 void NetErrorHelper::OnNavigationCorrectionsFetched( |
212 const blink::WebURLResponse& response, | 239 const blink::WebURLResponse& response, |
213 const std::string& data) { | 240 const std::string& data) { |
214 // The fetcher may only be deleted after |data| is passed to |core_|. Move | 241 // 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. | 242 // it to a temporary to prevent any potential re-entrancy issues. |
216 scoped_ptr<content::ResourceFetcher> fetcher( | 243 scoped_ptr<content::ResourceFetcher> fetcher( |
217 alt_error_page_fetcher_.release()); | 244 correction_fetcher_.release()); |
218 if (!response.isNull() && response.httpStatusCode() == 200) { | 245 if (!response.isNull() && response.httpStatusCode() == 200) { |
219 core_.OnAlternateErrorPageFetched(data); | 246 core_.OnNavigationCorrectionsFetched( |
| 247 data, render_frame()->GetRenderView()->GetAcceptLanguages(), |
| 248 LocaleIsRTL()); |
220 } else { | 249 } else { |
221 core_.OnAlternateErrorPageFetched(""); | 250 core_.OnNavigationCorrectionsFetched( |
| 251 "", render_frame()->GetRenderView()->GetAcceptLanguages(), |
| 252 LocaleIsRTL()); |
222 } | 253 } |
223 } | 254 } |
OLD | NEW |