| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/browser/android/offline_pages/offline_page_tab_helper.h" | 5 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 return; | 54 return; |
| 55 last_redirect_from_url_ = navigation_handle->GetURL(); | 55 last_redirect_from_url_ = navigation_handle->GetURL(); |
| 56 | 56 |
| 57 base::ThreadTaskRunnerHandle::Get()->PostTask( | 57 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 58 FROM_HERE, | 58 FROM_HERE, |
| 59 base::Bind(&OfflinePageTabHelper::RedirectFromOfflineToOnline, | 59 base::Bind(&OfflinePageTabHelper::RedirectFromOfflineToOnline, |
| 60 weak_ptr_factory_.GetWeakPtr(), | 60 weak_ptr_factory_.GetWeakPtr(), |
| 61 online_url)); | 61 online_url)); |
| 62 } | 62 } |
| 63 | 63 |
| 64 void OfflinePageTabHelper::DidFailProvisionalLoad( | 64 void OfflinePageTabHelper::DidFinishNavigation( |
| 65 content::RenderFrameHost* render_frame_host, | 65 content::NavigationHandle* navigation_handle) { |
| 66 const GURL& validated_url, | |
| 67 int error_code, | |
| 68 const base::string16& error_description, | |
| 69 bool was_ignored_by_handler) { | |
| 70 GURL last_redirect_from_url_copy = last_redirect_from_url_; | 66 GURL last_redirect_from_url_copy = last_redirect_from_url_; |
| 71 last_redirect_from_url_ = GURL(); | 67 last_redirect_from_url_ = GURL(); |
| 72 | 68 |
| 73 // Skips non-main frame or load failure other than no network. | 69 // Skips non-main frame or load failure other than no network. |
| 74 if (error_code != net::ERR_INTERNET_DISCONNECTED || | 70 if (navigation_handle->GetNetErrorCode() != net::ERR_INTERNET_DISCONNECTED || |
| 75 render_frame_host->GetParent() != nullptr) { | 71 !navigation_handle->IsInMainFrame()) { |
| 76 return; | 72 return; |
| 77 } | 73 } |
| 78 | 74 |
| 79 // Redirecting to offline version will only take effect when there is no | 75 // Redirecting to offline version will only take effect when there is no |
| 80 // network connection. | 76 // network connection. |
| 81 if (!net::NetworkChangeNotifier::IsOffline()) | 77 if (!net::NetworkChangeNotifier::IsOffline()) |
| 82 return; | 78 return; |
| 83 | 79 |
| 84 // Skips if not loading an online version of saved page. | 80 // Skips if not loading an online version of saved page. |
| 85 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( | 81 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( |
| 86 web_contents()->GetBrowserContext(), validated_url); | 82 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); |
| 87 if (!offline_url.is_valid()) | 83 if (!offline_url.is_valid()) |
| 88 return; | 84 return; |
| 89 | 85 |
| 90 // Avoids looping between online and offline redirections. | 86 // Avoids looping between online and offline redirections. |
| 91 if (last_redirect_from_url_copy == offline_url) | 87 if (last_redirect_from_url_copy == offline_url) |
| 92 return; | 88 return; |
| 93 last_redirect_from_url_ = validated_url; | 89 last_redirect_from_url_ = navigation_handle->GetURL(); |
| 94 | 90 |
| 95 base::ThreadTaskRunnerHandle::Get()->PostTask( | 91 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 96 FROM_HERE, | 92 FROM_HERE, |
| 97 base::Bind(&OfflinePageTabHelper::RedirectFromOnlineToOffline, | 93 base::Bind(&OfflinePageTabHelper::RedirectFromOnlineToOffline, |
| 98 weak_ptr_factory_.GetWeakPtr(), | 94 weak_ptr_factory_.GetWeakPtr(), |
| 99 offline_url)); | 95 offline_url)); |
| 100 } | 96 } |
| 101 | 97 |
| 102 void OfflinePageTabHelper::RedirectFromOfflineToOnline(const GURL& online_url) { | 98 void OfflinePageTabHelper::RedirectFromOfflineToOnline(const GURL& online_url) { |
| 103 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); | 99 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); |
| 104 content::NavigationController::LoadURLParams load_params(online_url); | 100 content::NavigationController::LoadURLParams load_params(online_url); |
| 105 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | 101 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; |
| 106 web_contents()->GetController().LoadURLWithParams(load_params); | 102 web_contents()->GetController().LoadURLWithParams(load_params); |
| 107 } | 103 } |
| 108 | 104 |
| 109 void OfflinePageTabHelper::RedirectFromOnlineToOffline( | 105 void OfflinePageTabHelper::RedirectFromOnlineToOffline( |
| 110 const GURL& offline_url) { | 106 const GURL& offline_url) { |
| 111 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); | 107 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); |
| 112 content::NavigationController::LoadURLParams load_params(offline_url); | 108 content::NavigationController::LoadURLParams load_params(offline_url); |
| 113 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | 109 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; |
| 114 web_contents()->GetController().LoadURLWithParams(load_params); | 110 web_contents()->GetController().LoadURLWithParams(load_params); |
| 115 } | 111 } |
| 116 | 112 |
| 117 } // namespace offline_pages | 113 } // namespace offline_pages |
| OLD | NEW |