Give names to all utility processes.
[chromium-blink-merge.git] / chrome / renderer / net / net_error_helper.cc
blob00c2964ee89a47a0569a73d48b6e3d90104df284
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
3 // found in the LICENSE file.
5 #include "chrome/renderer/net/net_error_helper.h"
7 #include <string>
9 #include "base/command_line.h"
10 #include "base/i18n/rtl.h"
11 #include "base/json/json_writer.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/localized_error.h"
17 #include "chrome/common/render_messages.h"
18 #include "chrome/grit/renderer_resources.h"
19 #include "chrome/renderer/net/net_error_page_controller.h"
20 #include "components/error_page/common/error_page_params.h"
21 #include "components/error_page/common/net_error_info.h"
22 #include "content/public/common/content_client.h"
23 #include "content/public/common/url_constants.h"
24 #include "content/public/renderer/content_renderer_client.h"
25 #include "content/public/renderer/document_state.h"
26 #include "content/public/renderer/render_frame.h"
27 #include "content/public/renderer/render_thread.h"
28 #include "content/public/renderer/render_view.h"
29 #include "content/public/renderer/resource_fetcher.h"
30 #include "ipc/ipc_message.h"
31 #include "ipc/ipc_message_macros.h"
32 #include "third_party/WebKit/public/platform/WebURL.h"
33 #include "third_party/WebKit/public/platform/WebURLError.h"
34 #include "third_party/WebKit/public/platform/WebURLRequest.h"
35 #include "third_party/WebKit/public/platform/WebURLResponse.h"
36 #include "third_party/WebKit/public/web/WebDataSource.h"
37 #include "third_party/WebKit/public/web/WebDocument.h"
38 #include "third_party/WebKit/public/web/WebLocalFrame.h"
39 #include "third_party/WebKit/public/web/WebView.h"
40 #include "ui/base/resource/resource_bundle.h"
41 #include "ui/base/webui/jstemplate_builder.h"
42 #include "url/gurl.h"
44 using base::JSONWriter;
45 using chrome_common_net::DnsProbeStatus;
46 using chrome_common_net::DnsProbeStatusToString;
47 using content::DocumentState;
48 using content::RenderFrame;
49 using content::RenderFrameObserver;
50 using content::RenderThread;
51 using content::kUnreachableWebDataURL;
52 using error_page::ErrorPageParams;
53 using error_page::NetErrorHelperCore;
55 namespace {
57 // Number of seconds to wait for the navigation correction service to return
58 // suggestions. If it takes too long, just use the local error page.
59 static const int kNavigationCorrectionFetchTimeoutSec = 3;
61 NetErrorHelperCore::PageType GetLoadingPageType(const blink::WebFrame* frame) {
62 GURL url = frame->provisionalDataSource()->request().url();
63 if (!url.is_valid() || url.spec() != kUnreachableWebDataURL)
64 return NetErrorHelperCore::NON_ERROR_PAGE;
65 return NetErrorHelperCore::ERROR_PAGE;
68 NetErrorHelperCore::FrameType GetFrameType(const blink::WebFrame* frame) {
69 if (!frame->parent())
70 return NetErrorHelperCore::MAIN_FRAME;
71 return NetErrorHelperCore::SUB_FRAME;
74 } // namespace
76 NetErrorHelper::NetErrorHelper(RenderFrame* render_frame)
77 : RenderFrameObserver(render_frame),
78 content::RenderFrameObserverTracker<NetErrorHelper>(render_frame) {
79 RenderThread::Get()->AddObserver(this);
80 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
81 bool auto_reload_enabled =
82 command_line->HasSwitch(switches::kEnableOfflineAutoReload);
83 bool auto_reload_visible_only =
84 command_line->HasSwitch(switches::kEnableOfflineAutoReloadVisibleOnly);
85 core_.reset(new NetErrorHelperCore(this,
86 auto_reload_enabled,
87 auto_reload_visible_only,
88 !render_frame->IsHidden()));
91 NetErrorHelper::~NetErrorHelper() {
92 RenderThread::Get()->RemoveObserver(this);
95 void NetErrorHelper::ReloadButtonPressed() {
96 core_->ExecuteButtonPress(NetErrorHelperCore::RELOAD_BUTTON);
99 void NetErrorHelper::ShowSavedCopyButtonPressed() {
100 core_->ExecuteButtonPress(NetErrorHelperCore::SHOW_SAVED_COPY_BUTTON);
103 void NetErrorHelper::MoreButtonPressed() {
104 core_->ExecuteButtonPress(NetErrorHelperCore::MORE_BUTTON);
107 void NetErrorHelper::TrackActivatedEasterEgg() {
108 core_->ExecuteButtonPress(NetErrorHelperCore::EASTER_EGG);
111 void NetErrorHelper::DidStartProvisionalLoad() {
112 blink::WebFrame* frame = render_frame()->GetWebFrame();
113 core_->OnStartLoad(GetFrameType(frame), GetLoadingPageType(frame));
116 void NetErrorHelper::DidCommitProvisionalLoad(bool is_new_navigation,
117 bool is_same_page_navigation) {
118 blink::WebFrame* frame = render_frame()->GetWebFrame();
119 core_->OnCommitLoad(GetFrameType(frame), frame->document().url());
122 void NetErrorHelper::DidFinishLoad() {
123 blink::WebFrame* frame = render_frame()->GetWebFrame();
124 core_->OnFinishLoad(GetFrameType(frame));
127 void NetErrorHelper::OnStop() {
128 core_->OnStop();
131 void NetErrorHelper::WasShown() {
132 core_->OnWasShown();
135 void NetErrorHelper::WasHidden() {
136 core_->OnWasHidden();
139 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) {
140 bool handled = true;
142 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message)
143 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo)
144 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetNavigationCorrectionInfo,
145 OnSetNavigationCorrectionInfo);
146 IPC_MESSAGE_UNHANDLED(handled = false)
147 IPC_END_MESSAGE_MAP()
149 return handled;
152 void NetErrorHelper::NetworkStateChanged(bool enabled) {
153 core_->NetworkStateChanged(enabled);
156 void NetErrorHelper::GetErrorHTML(
157 blink::WebFrame* frame,
158 const blink::WebURLError& error,
159 bool is_failed_post,
160 std::string* error_html) {
161 core_->GetErrorHTML(GetFrameType(frame), error, is_failed_post, error_html);
164 bool NetErrorHelper::ShouldSuppressErrorPage(blink::WebFrame* frame,
165 const GURL& url) {
166 return core_->ShouldSuppressErrorPage(GetFrameType(frame), url);
169 void NetErrorHelper::TrackClick(int tracking_id) {
170 core_->TrackClick(tracking_id);
173 void NetErrorHelper::GenerateLocalizedErrorPage(
174 const blink::WebURLError& error,
175 bool is_failed_post,
176 scoped_ptr<ErrorPageParams> params,
177 bool* reload_button_shown,
178 bool* show_saved_copy_button_shown,
179 std::string* error_html) const {
180 error_html->clear();
182 int resource_id = IDR_NET_ERROR_HTML;
183 const base::StringPiece template_html(
184 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id));
185 if (template_html.empty()) {
186 NOTREACHED() << "unable to load template.";
187 } else {
188 base::DictionaryValue error_strings;
189 LocalizedError::GetStrings(error.reason, error.domain.utf8(),
190 error.unreachableURL, is_failed_post,
191 error.staleCopyInCache,
192 RenderThread::Get()->GetLocale(),
193 render_frame()->GetRenderView()->
194 GetAcceptLanguages(),
195 params.Pass(), &error_strings);
196 *reload_button_shown = error_strings.Get("reloadButton", NULL);
197 *show_saved_copy_button_shown =
198 error_strings.Get("showSavedCopyButton", NULL);
200 // "t" is the id of the template's root node.
201 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t");
205 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html,
206 const GURL& failed_url) {
207 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView();
208 if (!web_view)
209 return;
210 blink::WebFrame* frame = web_view->mainFrame();
211 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true);
214 void NetErrorHelper::EnablePageHelperFunctions() {
215 NetErrorPageController::Install(render_frame());
218 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error,
219 bool is_failed_post) {
220 base::DictionaryValue error_strings;
221 LocalizedError::GetStrings(error.reason,
222 error.domain.utf8(),
223 error.unreachableURL,
224 is_failed_post,
225 error.staleCopyInCache,
226 RenderThread::Get()->GetLocale(),
227 render_frame()->GetRenderView()->
228 GetAcceptLanguages(),
229 scoped_ptr<ErrorPageParams>(),
230 &error_strings);
232 std::string json;
233 JSONWriter::Write(&error_strings, &json);
235 std::string js = "if (window.updateForDnsProbe) "
236 "updateForDnsProbe(" + json + ");";
237 base::string16 js16;
238 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) {
239 NOTREACHED();
240 return;
243 render_frame()->ExecuteJavaScript(js16);
246 void NetErrorHelper::FetchNavigationCorrections(
247 const GURL& navigation_correction_url,
248 const std::string& navigation_correction_request_body) {
249 DCHECK(!correction_fetcher_.get());
251 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView();
252 if (!web_view)
253 return;
254 blink::WebFrame* frame = web_view->mainFrame();
256 correction_fetcher_.reset(
257 content::ResourceFetcher::Create(navigation_correction_url));
258 correction_fetcher_->SetMethod("POST");
259 correction_fetcher_->SetBody(navigation_correction_request_body);
260 correction_fetcher_->SetHeader("Content-Type", "application/json");
262 correction_fetcher_->Start(
263 frame,
264 blink::WebURLRequest::RequestContextInternal,
265 blink::WebURLRequest::FrameTypeTopLevel,
266 content::ResourceFetcher::PLATFORM_LOADER,
267 base::Bind(&NetErrorHelper::OnNavigationCorrectionsFetched,
268 base::Unretained(this)));
270 correction_fetcher_->SetTimeout(
271 base::TimeDelta::FromSeconds(kNavigationCorrectionFetchTimeoutSec));
274 void NetErrorHelper::CancelFetchNavigationCorrections() {
275 correction_fetcher_.reset();
278 void NetErrorHelper::SendTrackingRequest(
279 const GURL& tracking_url,
280 const std::string& tracking_request_body) {
281 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView();
282 if (!web_view)
283 return;
284 blink::WebFrame* frame = web_view->mainFrame();
286 // If there's already a pending tracking request, this will cancel it.
287 tracking_fetcher_.reset(content::ResourceFetcher::Create(tracking_url));
288 tracking_fetcher_->SetMethod("POST");
289 tracking_fetcher_->SetBody(tracking_request_body);
290 tracking_fetcher_->SetHeader("Content-Type", "application/json");
292 tracking_fetcher_->Start(
293 frame,
294 blink::WebURLRequest::RequestContextInternal,
295 blink::WebURLRequest::FrameTypeTopLevel,
296 content::ResourceFetcher::PLATFORM_LOADER,
297 base::Bind(&NetErrorHelper::OnTrackingRequestComplete,
298 base::Unretained(this)));
301 void NetErrorHelper::ReloadPage() {
302 render_frame()->GetWebFrame()->reload(false);
305 void NetErrorHelper::LoadPageFromCache(const GURL& page_url) {
306 blink::WebFrame* web_frame = render_frame()->GetWebFrame();
307 DCHECK(!EqualsASCII(web_frame->dataSource()->request().httpMethod(), "POST"));
309 blink::WebURLRequest request(page_url);
310 request.setCachePolicy(blink::WebURLRequest::ReturnCacheDataDontLoad);
312 web_frame->loadRequest(request);
315 void NetErrorHelper::OnNetErrorInfo(int status_num) {
316 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX);
318 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num);
320 core_->OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num));
323 void NetErrorHelper::OnSetNavigationCorrectionInfo(
324 const GURL& navigation_correction_url,
325 const std::string& language,
326 const std::string& country_code,
327 const std::string& api_key,
328 const GURL& search_url) {
329 core_->OnSetNavigationCorrectionInfo(navigation_correction_url, language,
330 country_code, api_key, search_url);
333 void NetErrorHelper::OnNavigationCorrectionsFetched(
334 const blink::WebURLResponse& response,
335 const std::string& data) {
336 // The fetcher may only be deleted after |data| is passed to |core_|. Move
337 // it to a temporary to prevent any potential re-entrancy issues.
338 scoped_ptr<content::ResourceFetcher> fetcher(
339 correction_fetcher_.release());
340 bool success = (!response.isNull() && response.httpStatusCode() == 200);
341 core_->OnNavigationCorrectionsFetched(
342 success ? data : "",
343 render_frame()->GetRenderView()->GetAcceptLanguages(),
344 base::i18n::IsRTL());
347 void NetErrorHelper::OnTrackingRequestComplete(
348 const blink::WebURLResponse& response,
349 const std::string& data) {
350 tracking_fetcher_.reset();