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 <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/i18n/rtl.h" | 12 #include "base/i18n/rtl.h" |
13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/render_messages.h" | 19 #include "chrome/common/render_messages.h" |
20 #include "components/error_page/common/error_page_params.h" | 20 #include "components/error_page/common/error_page_params.h" |
21 #include "components/error_page/common/localized_error.h" | 21 #include "components/error_page/common/localized_error.h" |
22 #include "components/error_page/common/net_error_info.h" | 22 #include "components/error_page/common/net_error_info.h" |
23 #include "components/grit/components_resources.h" | 23 #include "components/grit/components_resources.h" |
| 24 #include "content/public/common/associated_interface_provider.h" |
| 25 #include "content/public/common/associated_interface_registry.h" |
24 #include "content/public/common/content_client.h" | 26 #include "content/public/common/content_client.h" |
25 #include "content/public/common/url_constants.h" | 27 #include "content/public/common/url_constants.h" |
26 #include "content/public/renderer/content_renderer_client.h" | 28 #include "content/public/renderer/content_renderer_client.h" |
27 #include "content/public/renderer/document_state.h" | 29 #include "content/public/renderer/document_state.h" |
28 #include "content/public/renderer/render_frame.h" | 30 #include "content/public/renderer/render_frame.h" |
29 #include "content/public/renderer/render_thread.h" | 31 #include "content/public/renderer/render_thread.h" |
30 #include "content/public/renderer/render_view.h" | 32 #include "content/public/renderer/render_view.h" |
31 #include "content/public/renderer/resource_fetcher.h" | 33 #include "content/public/renderer/resource_fetcher.h" |
32 #include "ipc/ipc_message.h" | 34 #include "ipc/ipc_message.h" |
33 #include "ipc/ipc_message_macros.h" | 35 #include "ipc/ipc_message_macros.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 if (render_frame->IsMainFrame()) | 77 if (render_frame->IsMainFrame()) |
76 return NetErrorHelperCore::MAIN_FRAME; | 78 return NetErrorHelperCore::MAIN_FRAME; |
77 return NetErrorHelperCore::SUB_FRAME; | 79 return NetErrorHelperCore::SUB_FRAME; |
78 } | 80 } |
79 | 81 |
80 } // namespace | 82 } // namespace |
81 | 83 |
82 NetErrorHelper::NetErrorHelper(RenderFrame* render_frame) | 84 NetErrorHelper::NetErrorHelper(RenderFrame* render_frame) |
83 : RenderFrameObserver(render_frame), | 85 : RenderFrameObserver(render_frame), |
84 content::RenderFrameObserverTracker<NetErrorHelper>(render_frame), | 86 content::RenderFrameObserverTracker<NetErrorHelper>(render_frame), |
| 87 network_diagnostics_client_binding_(this), |
85 weak_controller_delegate_factory_(this) { | 88 weak_controller_delegate_factory_(this) { |
86 RenderThread::Get()->AddObserver(this); | 89 RenderThread::Get()->AddObserver(this); |
87 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 90 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
88 bool auto_reload_enabled = | 91 bool auto_reload_enabled = |
89 command_line->HasSwitch(switches::kEnableOfflineAutoReload); | 92 command_line->HasSwitch(switches::kEnableOfflineAutoReload); |
90 bool auto_reload_visible_only = | 93 bool auto_reload_visible_only = |
91 command_line->HasSwitch(switches::kEnableOfflineAutoReloadVisibleOnly); | 94 command_line->HasSwitch(switches::kEnableOfflineAutoReloadVisibleOnly); |
92 // TODO(mmenke): Consider only creating a NetErrorHelperCore for main frames. | 95 // TODO(mmenke): Consider only creating a NetErrorHelperCore for main frames. |
93 // subframes don't need any of the NetErrorHelperCore's extra logic. | 96 // subframes don't need any of the NetErrorHelperCore's extra logic. |
94 core_.reset(new NetErrorHelperCore(this, | 97 core_.reset(new NetErrorHelperCore(this, |
95 auto_reload_enabled, | 98 auto_reload_enabled, |
96 auto_reload_visible_only, | 99 auto_reload_visible_only, |
97 !render_frame->IsHidden())); | 100 !render_frame->IsHidden())); |
| 101 |
| 102 render_frame->GetAssociatedInterfaceRegistry()->AddInterface( |
| 103 base::Bind(&NetErrorHelper::OnNetworkDiagnosticsClientRequest, |
| 104 base::Unretained(this))); |
98 } | 105 } |
99 | 106 |
100 NetErrorHelper::~NetErrorHelper() { | 107 NetErrorHelper::~NetErrorHelper() { |
101 RenderThread::Get()->RemoveObserver(this); | 108 RenderThread::Get()->RemoveObserver(this); |
102 } | 109 } |
103 | 110 |
104 void NetErrorHelper::ButtonPressed( | 111 void NetErrorHelper::ButtonPressed( |
105 error_page::NetErrorHelperCore::Button button) { | 112 error_page::NetErrorHelperCore::Button button) { |
106 core_->ExecuteButtonPress(button); | 113 core_->ExecuteButtonPress(button); |
107 } | 114 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 152 |
146 void NetErrorHelper::WasHidden() { | 153 void NetErrorHelper::WasHidden() { |
147 core_->OnWasHidden(); | 154 core_->OnWasHidden(); |
148 } | 155 } |
149 | 156 |
150 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) { | 157 bool NetErrorHelper::OnMessageReceived(const IPC::Message& message) { |
151 bool handled = true; | 158 bool handled = true; |
152 | 159 |
153 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message) | 160 IPC_BEGIN_MESSAGE_MAP(NetErrorHelper, message) |
154 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo) | 161 IPC_MESSAGE_HANDLER(ChromeViewMsg_NetErrorInfo, OnNetErrorInfo) |
155 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetCanShowNetworkDiagnosticsDialog, | |
156 OnSetCanShowNetworkDiagnosticsDialog); | |
157 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetNavigationCorrectionInfo, | 162 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetNavigationCorrectionInfo, |
158 OnSetNavigationCorrectionInfo); | 163 OnSetNavigationCorrectionInfo); |
159 #if defined(OS_ANDROID) | 164 #if defined(OS_ANDROID) |
160 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetHasOfflinePages, OnSetHasOfflinePages) | 165 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetHasOfflinePages, OnSetHasOfflinePages) |
161 #endif | 166 #endif |
162 IPC_MESSAGE_UNHANDLED(handled = false) | 167 IPC_MESSAGE_UNHANDLED(handled = false) |
163 IPC_END_MESSAGE_MAP() | 168 IPC_END_MESSAGE_MAP() |
164 | 169 |
165 return handled; | 170 return handled; |
166 } | 171 } |
(...skipping 11 matching lines...) Expand all Loading... |
178 bool is_ignoring_cache, | 183 bool is_ignoring_cache, |
179 std::string* error_html) { | 184 std::string* error_html) { |
180 core_->GetErrorHTML(GetFrameType(render_frame()), error, is_failed_post, | 185 core_->GetErrorHTML(GetFrameType(render_frame()), error, is_failed_post, |
181 is_ignoring_cache, error_html); | 186 is_ignoring_cache, error_html); |
182 } | 187 } |
183 | 188 |
184 bool NetErrorHelper::ShouldSuppressErrorPage(const GURL& url) { | 189 bool NetErrorHelper::ShouldSuppressErrorPage(const GURL& url) { |
185 return core_->ShouldSuppressErrorPage(GetFrameType(render_frame()), url); | 190 return core_->ShouldSuppressErrorPage(GetFrameType(render_frame()), url); |
186 } | 191 } |
187 | 192 |
| 193 mojom::NetworkDiagnostics* NetErrorHelper::GetRemoteNetworkDiagnostics() { |
| 194 if (!remote_network_diagnostics_) { |
| 195 render_frame()->GetRemoteAssociatedInterfaces() |
| 196 ->GetInterface(&remote_network_diagnostics_); |
| 197 } |
| 198 return remote_network_diagnostics_.get(); |
| 199 } |
| 200 |
188 void NetErrorHelper::GenerateLocalizedErrorPage( | 201 void NetErrorHelper::GenerateLocalizedErrorPage( |
189 const blink::WebURLError& error, | 202 const blink::WebURLError& error, |
190 bool is_failed_post, | 203 bool is_failed_post, |
191 bool can_show_network_diagnostics_dialog, | 204 bool can_show_network_diagnostics_dialog, |
192 bool has_offline_pages, | 205 bool has_offline_pages, |
193 std::unique_ptr<ErrorPageParams> params, | 206 std::unique_ptr<ErrorPageParams> params, |
194 bool* reload_button_shown, | 207 bool* reload_button_shown, |
195 bool* show_saved_copy_button_shown, | 208 bool* show_saved_copy_button_shown, |
196 bool* show_cached_copy_button_shown, | 209 bool* show_cached_copy_button_shown, |
197 bool* show_offline_pages_button_shown, | 210 bool* show_offline_pages_button_shown, |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 base::StringPiece16(web_frame->dataSource()->request().httpMethod()), | 328 base::StringPiece16(web_frame->dataSource()->request().httpMethod()), |
316 "POST")); | 329 "POST")); |
317 | 330 |
318 blink::WebURLRequest request(page_url); | 331 blink::WebURLRequest request(page_url); |
319 request.setCachePolicy(blink::WebCachePolicy::ReturnCacheDataDontLoad); | 332 request.setCachePolicy(blink::WebCachePolicy::ReturnCacheDataDontLoad); |
320 request.setRequestorOrigin(blink::WebSecurityOrigin::createUnique()); | 333 request.setRequestorOrigin(blink::WebSecurityOrigin::createUnique()); |
321 web_frame->loadRequest(request); | 334 web_frame->loadRequest(request); |
322 } | 335 } |
323 | 336 |
324 void NetErrorHelper::DiagnoseError(const GURL& page_url) { | 337 void NetErrorHelper::DiagnoseError(const GURL& page_url) { |
325 render_frame()->Send(new ChromeViewHostMsg_RunNetworkDiagnostics( | 338 GetRemoteNetworkDiagnostics()->RunNetworkDiagnostics(page_url); |
326 render_frame()->GetRoutingID(), page_url)); | |
327 } | 339 } |
328 | 340 |
329 void NetErrorHelper::ShowOfflinePages() { | 341 void NetErrorHelper::ShowOfflinePages() { |
330 #if defined(OS_ANDROID) | 342 #if defined(OS_ANDROID) |
331 render_frame()->Send(new ChromeViewHostMsg_ShowOfflinePages( | 343 render_frame()->Send(new ChromeViewHostMsg_ShowOfflinePages( |
332 render_frame()->GetRoutingID())); | 344 render_frame()->GetRoutingID())); |
333 #endif // defined(OS_ANDROID) | 345 #endif // defined(OS_ANDROID) |
334 } | 346 } |
335 | 347 |
336 void NetErrorHelper::OnNetErrorInfo(int status_num) { | 348 void NetErrorHelper::OnNetErrorInfo(int status_num) { |
337 DCHECK(status_num >= 0 && status_num < error_page::DNS_PROBE_MAX); | 349 DCHECK(status_num >= 0 && status_num < error_page::DNS_PROBE_MAX); |
338 | 350 |
339 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); | 351 DVLOG(1) << "Received status " << DnsProbeStatusToString(status_num); |
340 | 352 |
341 core_->OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); | 353 core_->OnNetErrorInfo(static_cast<DnsProbeStatus>(status_num)); |
342 } | 354 } |
343 | 355 |
344 void NetErrorHelper::OnSetCanShowNetworkDiagnosticsDialog( | |
345 bool can_use_local_diagnostics_service) { | |
346 core_->OnSetCanShowNetworkDiagnosticsDialog( | |
347 can_use_local_diagnostics_service); | |
348 } | |
349 | |
350 void NetErrorHelper::OnSetNavigationCorrectionInfo( | 356 void NetErrorHelper::OnSetNavigationCorrectionInfo( |
351 const GURL& navigation_correction_url, | 357 const GURL& navigation_correction_url, |
352 const std::string& language, | 358 const std::string& language, |
353 const std::string& country_code, | 359 const std::string& country_code, |
354 const std::string& api_key, | 360 const std::string& api_key, |
355 const GURL& search_url) { | 361 const GURL& search_url) { |
356 core_->OnSetNavigationCorrectionInfo(navigation_correction_url, language, | 362 core_->OnSetNavigationCorrectionInfo(navigation_correction_url, language, |
357 country_code, api_key, search_url); | 363 country_code, api_key, search_url); |
358 } | 364 } |
359 | 365 |
360 void NetErrorHelper::OnNavigationCorrectionsFetched( | 366 void NetErrorHelper::OnNavigationCorrectionsFetched( |
361 const blink::WebURLResponse& response, | 367 const blink::WebURLResponse& response, |
362 const std::string& data) { | 368 const std::string& data) { |
363 // The fetcher may only be deleted after |data| is passed to |core_|. Move | 369 // The fetcher may only be deleted after |data| is passed to |core_|. Move |
364 // it to a temporary to prevent any potential re-entrancy issues. | 370 // it to a temporary to prevent any potential re-entrancy issues. |
365 std::unique_ptr<content::ResourceFetcher> fetcher( | 371 std::unique_ptr<content::ResourceFetcher> fetcher( |
366 correction_fetcher_.release()); | 372 correction_fetcher_.release()); |
367 bool success = (!response.isNull() && response.httpStatusCode() == 200); | 373 bool success = (!response.isNull() && response.httpStatusCode() == 200); |
368 core_->OnNavigationCorrectionsFetched(success ? data : "", | 374 core_->OnNavigationCorrectionsFetched(success ? data : "", |
369 base::i18n::IsRTL()); | 375 base::i18n::IsRTL()); |
370 } | 376 } |
371 | 377 |
372 void NetErrorHelper::OnTrackingRequestComplete( | 378 void NetErrorHelper::OnTrackingRequestComplete( |
373 const blink::WebURLResponse& response, | 379 const blink::WebURLResponse& response, |
374 const std::string& data) { | 380 const std::string& data) { |
375 tracking_fetcher_.reset(); | 381 tracking_fetcher_.reset(); |
376 } | 382 } |
377 | 383 |
| 384 void NetErrorHelper::OnNetworkDiagnosticsClientRequest( |
| 385 mojom::NetworkDiagnosticsClientAssociatedRequest request) { |
| 386 DCHECK(!network_diagnostics_client_binding_.is_bound()); |
| 387 network_diagnostics_client_binding_.Bind(std::move(request)); |
| 388 } |
| 389 |
| 390 void NetErrorHelper::SetCanShowNetworkDiagnosticsDialog(bool can_show) { |
| 391 core_->OnSetCanShowNetworkDiagnosticsDialog(can_show); |
| 392 } |
| 393 |
378 #if defined(OS_ANDROID) | 394 #if defined(OS_ANDROID) |
379 void NetErrorHelper::OnSetHasOfflinePages(bool has_offline_pages) { | 395 void NetErrorHelper::OnSetHasOfflinePages(bool has_offline_pages) { |
380 core_->OnSetHasOfflinePages(has_offline_pages); | 396 core_->OnSetHasOfflinePages(has_offline_pages); |
381 } | 397 } |
382 #endif // defined(OS_ANDROID) | 398 #endif // defined(OS_ANDROID) |
OLD | NEW |