Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / renderer / net / net_error_helper.cc
blob3ec8b91fc6c8f5115e651c1a3c994309ef8f73df
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::ButtonPressed(
96 error_page::NetErrorHelperCore::Button button) {
97 GURL url = render_frame()->GetWebFrame()->document().url();
98 bool is_error_page = (url == GURL(content::kUnreachableWebDataURL));
99 core_->ExecuteButtonPress(is_error_page, button);
102 void NetErrorHelper::DidStartProvisionalLoad() {
103 blink::WebFrame* frame = render_frame()->GetWebFrame();
104 core_->OnStartLoad(GetFrameType(frame), GetLoadingPageType(frame));
107 void NetErrorHelper::DidCommitProvisionalLoad(bool is_new_navigation,
108 bool is_same_page_navigation) {
109 blink::WebFrame* frame = render_frame()->GetWebFrame();
110 core_->OnCommitLoad(GetFrameType(frame), frame->document().url());
113 void NetErrorHelper::DidFinishLoad() {
114 blink::WebFrame* frame = render_frame()->GetWebFrame();
115 core_->OnFinishLoad(GetFrameType(frame));
118 void NetErrorHelper::OnStop() {
119 core_->OnStop();
122 void NetErrorHelper::WasShown() {
123 core_->OnWasShown();
126 void NetErrorHelper::WasHidden() {
127 core_->OnWasHidden();
130 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) {
131 bool handled = true;
133 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message)
134 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo)
135 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetNavigationCorrectionInfo,
136 OnSetNavigationCorrectionInfo);
137 IPC_MESSAGE_UNHANDLED(handled = false)
138 IPC_END_MESSAGE_MAP()
140 return handled;
143 void NetErrorHelper::NetworkStateChanged(bool enabled) {
144 core_->NetworkStateChanged(enabled);
147 void NetErrorHelper::GetErrorHTML(
148 blink::WebFrame* frame,
149 const blink::WebURLError& error,
150 bool is_failed_post,
151 std::string* error_html) {
152 core_->GetErrorHTML(GetFrameType(frame), error, is_failed_post, error_html);
155 bool NetErrorHelper::ShouldSuppressErrorPage(blink::WebFrame* frame,
156 const GURL& url) {
157 return core_->ShouldSuppressErrorPage(GetFrameType(frame), url);
160 void NetErrorHelper::TrackClick(int tracking_id) {
161 core_->TrackClick(tracking_id);
164 void NetErrorHelper::GenerateLocalizedErrorPage(
165 const blink::WebURLError& error,
166 bool is_failed_post,
167 scoped_ptr<ErrorPageParams> params,
168 bool* reload_button_shown,
169 bool* show_saved_copy_button_shown,
170 bool* show_cached_copy_button_shown,
171 bool* show_cached_page_button_shown,
172 std::string* error_html) const {
173 error_html->clear();
175 int resource_id = IDR_NET_ERROR_HTML;
176 const base::StringPiece template_html(
177 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id));
178 if (template_html.empty()) {
179 NOTREACHED() << "unable to load template.";
180 } else {
181 base::DictionaryValue error_strings;
182 LocalizedError::GetStrings(error.reason, error.domain.utf8(),
183 error.unreachableURL, is_failed_post,
184 error.staleCopyInCache,
185 RenderThread::Get()->GetLocale(),
186 render_frame()->GetRenderView()->
187 GetAcceptLanguages(),
188 params.Pass(), &error_strings);
189 *reload_button_shown = error_strings.Get("reloadButton", NULL);
190 *show_saved_copy_button_shown =
191 error_strings.Get("showSavedCopyButton", NULL);
193 bool show_cache_copy_button_default_label;
194 bool showing_cache_copy_experiment =
195 error_strings.GetBoolean("cacheButton.defaultLabel",
196 &show_cache_copy_button_default_label);
197 if (showing_cache_copy_experiment) {
198 if (show_cache_copy_button_default_label) {
199 *show_cached_copy_button_shown = false;
200 *show_cached_page_button_shown = true;
201 } else {
202 *show_cached_page_button_shown = false;
203 *show_cached_copy_button_shown = true;
206 // "t" is the id of the template's root node.
207 *error_html = webui::GetTemplatesHtml(template_html, &error_strings, "t");
211 void NetErrorHelper::LoadErrorPageInMainFrame(const std::string& html,
212 const GURL& failed_url) {
213 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView();
214 if (!web_view)
215 return;
216 blink::WebFrame* frame = web_view->mainFrame();
217 frame->loadHTMLString(html, GURL(kUnreachableWebDataURL), failed_url, true);
220 void NetErrorHelper::EnablePageHelperFunctions() {
221 NetErrorPageController::Install(render_frame());
224 void NetErrorHelper::UpdateErrorPage(const blink::WebURLError& error,
225 bool is_failed_post) {
226 base::DictionaryValue error_strings;
227 LocalizedError::GetStrings(error.reason,
228 error.domain.utf8(),
229 error.unreachableURL,
230 is_failed_post,
231 error.staleCopyInCache,
232 RenderThread::Get()->GetLocale(),
233 render_frame()->GetRenderView()->
234 GetAcceptLanguages(),
235 scoped_ptr<ErrorPageParams>(),
236 &error_strings);
238 std::string json;
239 JSONWriter::Write(error_strings, &json);
241 std::string js = "if (window.updateForDnsProbe) "
242 "updateForDnsProbe(" + json + ");";
243 base::string16 js16;
244 if (!base::UTF8ToUTF16(js.c_str(), js.length(), &js16)) {
245 NOTREACHED();
246 return;
249 render_frame()->ExecuteJavaScript(js16);
252 void NetErrorHelper::FetchNavigationCorrections(
253 const GURL& navigation_correction_url,
254 const std::string& navigation_correction_request_body) {
255 DCHECK(!correction_fetcher_.get());
257 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView();
258 if (!web_view)
259 return;
260 blink::WebFrame* frame = web_view->mainFrame();
262 correction_fetcher_.reset(
263 content::ResourceFetcher::Create(navigation_correction_url));
264 correction_fetcher_->SetMethod("POST");
265 correction_fetcher_->SetBody(navigation_correction_request_body);
266 correction_fetcher_->SetHeader("Content-Type", "application/json");
268 correction_fetcher_->Start(
269 frame,
270 blink::WebURLRequest::RequestContextInternal,
271 blink::WebURLRequest::FrameTypeNone,
272 content::ResourceFetcher::PLATFORM_LOADER,
273 base::Bind(&NetErrorHelper::OnNavigationCorrectionsFetched,
274 base::Unretained(this)));
276 correction_fetcher_->SetTimeout(
277 base::TimeDelta::FromSeconds(kNavigationCorrectionFetchTimeoutSec));
280 void NetErrorHelper::CancelFetchNavigationCorrections() {
281 correction_fetcher_.reset();
284 void NetErrorHelper::SendTrackingRequest(
285 const GURL& tracking_url,
286 const std::string& tracking_request_body) {
287 blink::WebView* web_view = render_frame()->GetRenderView()->GetWebView();
288 if (!web_view)
289 return;
290 blink::WebFrame* frame = web_view->mainFrame();
292 // If there's already a pending tracking request, this will cancel it.
293 tracking_fetcher_.reset(content::ResourceFetcher::Create(tracking_url));
294 tracking_fetcher_->SetMethod("POST");
295 tracking_fetcher_->SetBody(tracking_request_body);
296 tracking_fetcher_->SetHeader("Content-Type", "application/json");
298 tracking_fetcher_->Start(
299 frame,
300 blink::WebURLRequest::RequestContextInternal,
301 blink::WebURLRequest::FrameTypeTopLevel,
302 content::ResourceFetcher::PLATFORM_LOADER,
303 base::Bind(&NetErrorHelper::OnTrackingRequestComplete,
304 base::Unretained(this)));
307 void NetErrorHelper::ReloadPage() {
308 render_frame()->GetWebFrame()->reload(false);
311 void NetErrorHelper::LoadPageFromCache(const GURL& page_url) {
312 blink::WebFrame* web_frame = render_frame()->GetWebFrame();
313 DCHECK(!base::EqualsASCII(
314 base::StringPiece16(web_frame->dataSource()->request().httpMethod()),
315 "POST"));
317 blink::WebURLRequest request(page_url);
318 request.setCachePolicy(blink::WebURLRequest::ReturnCacheDataDontLoad);
320 web_frame->loadRequest(request);
323 void NetErrorHelper::OnNetErrorInfo(int status_num) {
324 DCHECK(status_num >= 0 && status_num < chrome_common_net::DNS_PROBE_MAX);
326 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num);
328 core_->OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num));
331 void NetErrorHelper::OnSetNavigationCorrectionInfo(
332 const GURL& navigation_correction_url,
333 const std::string& language,
334 const std::string& country_code,
335 const std::string& api_key,
336 const GURL& search_url) {
337 core_->OnSetNavigationCorrectionInfo(navigation_correction_url, language,
338 country_code, api_key, search_url);
341 void NetErrorHelper::OnNavigationCorrectionsFetched(
342 const blink::WebURLResponse& response,
343 const std::string& data) {
344 // The fetcher may only be deleted after |data| is passed to |core_|. Move
345 // it to a temporary to prevent any potential re-entrancy issues.
346 scoped_ptr<content::ResourceFetcher> fetcher(
347 correction_fetcher_.release());
348 bool success = (!response.isNull() && response.httpStatusCode() == 200);
349 core_->OnNavigationCorrectionsFetched(
350 success ? data : "",
351 render_frame()->GetRenderView()->GetAcceptLanguages(),
352 base::i18n::IsRTL());
355 void NetErrorHelper::OnTrackingRequestComplete(
356 const blink::WebURLResponse& response,
357 const std::string& data) {
358 tracking_fetcher_.reset();