Chromium Code Reviews

Side by Side Diff: chrome/renderer/net/net_error_helper.cc

Issue 137623011: Switch to using the new Link Doctor API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Response to pkasting's comments, part 2 Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
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...)
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...)
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::FetchLinkDoctorSuggestions(
175 DCHECK(!alt_error_page_fetcher_.get()); 190 const GURL& link_doctor_url,
191 const std::string& link_doctor_url_request_body) {
192 DCHECK(!link_doctor_fetcher_.get());
176 193
177 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView(); 194 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView();
178 if (!web_view) 195 if (!web_view)
179 return; 196 return;
180 blink::WebFrame* frame = web_view->mainFrame(); 197 blink::WebFrame* frame = web_view->mainFrame();
181 198
182 alt_error_page_fetcher_.reset(content::ResourceFetcher::Create(url)); 199 link_doctor_fetcher_.reset(
183 200 content::ResourceFetcher::Create(link_doctor_url));
184 alt_error_page_fetcher_->Start( 201 link_doctor_fetcher_->SetMethod("POST");
202 link_doctor_fetcher_->SetBody(link_doctor_url_request_body);
203 link_doctor_fetcher_->SetHeader("Content-Type", "application/json");
204 link_doctor_fetcher_->Start(
185 frame, blink::WebURLRequest::TargetIsMainFrame, 205 frame, blink::WebURLRequest::TargetIsMainFrame,
186 base::Bind(&NetErrorHelper::OnAlternateErrorPageRetrieved, 206 base::Bind(&NetErrorHelper::OnLinkDoctorSuggestionsFetched,
187 base::Unretained(this))); 207 base::Unretained(this)));
188 208
189 alt_error_page_fetcher_->SetTimeout( 209 link_doctor_fetcher_->SetTimeout(
190 base::TimeDelta::FromSeconds(kAlterErrorPageFetchTimeoutSec)); 210 base::TimeDelta::FromSeconds(kLinkDoctorFetchTimeoutSec));
191 } 211 }
192 212
193 void NetErrorHelper::CancelFetchErrorPage() { 213 void NetErrorHelper::CancelFetchLinkDoctorSuggestions() {
194 alt_error_page_fetcher_.reset(); 214 link_doctor_fetcher_.reset();
195 } 215 }
196 216
197 void NetErrorHelper::OnNetErrorInfo(int status_num) { 217 void NetErrorHelper::OnNetErrorInfo(int status_num) {
198 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX); 218 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX);
199 219
200 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); 220 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num);
201 221
202 core_.OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); 222 core_.OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num));
203 } 223 }
204 224
205 void NetErrorHelper::OnSetAltErrorPageURL(const GURL& alt_error_page_url) { 225 void NetErrorHelper::OnSetLinkDoctorInfo(const GURL& link_doctor_url,
206 core_.set_alt_error_page_url(alt_error_page_url); 226 const std::string& language,
227 const std::string& country_code,
228 const std::string& api_key,
229 const GURL& search_url) {
230 core_.OnSetLinkDoctorInfo(link_doctor_url, language, country_code, api_key,
231 search_url);
207 } 232 }
208 233
209 void NetErrorHelper::OnAlternateErrorPageRetrieved( 234 void NetErrorHelper::OnLinkDoctorSuggestionsFetched(
210 const blink::WebURLResponse& response, 235 const blink::WebURLResponse& response,
211 const std::string& data) { 236 const std::string& data) {
212 // The fetcher may only be deleted after |data| is passed to |core_|. Move 237 // 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. 238 // it to a temporary to prevent any potential re-entrancy issues.
214 scoped_ptr<content::ResourceFetcher> fetcher( 239 scoped_ptr<content::ResourceFetcher> fetcher(
215 alt_error_page_fetcher_.release()); 240 link_doctor_fetcher_.release());
216 if (!response.isNull() && response.httpStatusCode() == 200) { 241 if (!response.isNull() && response.httpStatusCode() == 200) {
217 core_.OnAlternateErrorPageFetched(data); 242 core_.OnLinkDoctorSuggestionsFetched(
243 data, render_frame()->GetRenderView()->GetAcceptLanguages(),
244 LocaleIsRTL());
218 } else { 245 } else {
219 core_.OnAlternateErrorPageFetched(""); 246 core_.OnLinkDoctorSuggestionsFetched(
247 "", render_frame()->GetRenderView()->GetAcceptLanguages(),
248 LocaleIsRTL());
220 } 249 }
221 } 250 }
OLDNEW

Powered by Google App Engine