| 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 19 matching lines...) Expand all Loading... |
| 30 } | 30 } |
| 31 | 31 |
| 32 OfflinePageTabHelper::~OfflinePageTabHelper() {} | 32 OfflinePageTabHelper::~OfflinePageTabHelper() {} |
| 33 | 33 |
| 34 void OfflinePageTabHelper::DidStartNavigation( | 34 void OfflinePageTabHelper::DidStartNavigation( |
| 35 content::NavigationHandle* navigation_handle) { | 35 content::NavigationHandle* navigation_handle) { |
| 36 // Skips non-main frame. | 36 // Skips non-main frame. |
| 37 if (!navigation_handle->IsInMainFrame()) | 37 if (!navigation_handle->IsInMainFrame()) |
| 38 return; | 38 return; |
| 39 | 39 |
| 40 // Redirecting to online version will only take effect when there is network | |
| 41 // connection. | |
| 42 if (net::NetworkChangeNotifier::IsOffline()) | |
| 43 return; | |
| 44 | |
| 45 // Ignore navigations that are forward or back transitions in the nav stack | 40 // Ignore navigations that are forward or back transitions in the nav stack |
| 46 // which are not at the head of the stack. | 41 // which are not at the head of the stack. |
| 47 const content::NavigationController& controller = | 42 const content::NavigationController& controller = |
| 48 web_contents()->GetController(); | 43 web_contents()->GetController(); |
| 49 if (controller.GetEntryCount() > 0 && | 44 if (controller.GetEntryCount() > 0 && |
| 50 controller.GetCurrentEntryIndex() != -1 && | 45 controller.GetCurrentEntryIndex() != -1 && |
| 51 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { | 46 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { |
| 52 return; | 47 return; |
| 53 } | 48 } |
| 54 | 49 |
| 55 // Skips if not loading an offline copy of saved page. | 50 GURL redirect_url; |
| 56 GURL online_url = offline_pages::OfflinePageUtils::GetOnlineURLForOfflineURL( | 51 if (net::NetworkChangeNotifier::IsOffline()) { |
| 52 // When the network is disconnected, loading online page will result in |
| 53 // immediate redirection to offline copy. |
| 54 redirect_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( |
| 57 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); | 55 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); |
| 58 if (!online_url.is_valid()) | 56 } else { |
| 57 // When the network is connected, loading offline copy will result in |
| 58 // immediate redirection to online page. |
| 59 redirect_url = offline_pages::OfflinePageUtils::GetOnlineURLForOfflineURL( |
| 60 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); |
| 61 } |
| 62 |
| 63 // Bails out if no redirection is needed. |
| 64 if (!redirect_url.is_valid()) |
| 59 return; | 65 return; |
| 60 | 66 |
| 61 // Avoids looping between online and offline redirections. | 67 // Avoids looping between online and offline redirections. |
| 62 content::NavigationEntry* entry = controller.GetPendingEntry(); | 68 content::NavigationEntry* entry = controller.GetPendingEntry(); |
| 63 if (entry && !entry->GetRedirectChain().empty() && | 69 if (entry && !entry->GetRedirectChain().empty() && |
| 64 entry->GetRedirectChain().back() == online_url) { | 70 entry->GetRedirectChain().back() == redirect_url) { |
| 65 return; | 71 return; |
| 66 } | 72 } |
| 67 | 73 |
| 68 base::ThreadTaskRunnerHandle::Get()->PostTask( | 74 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 69 FROM_HERE, | 75 FROM_HERE, |
| 70 base::Bind(&OfflinePageTabHelper::Redirect, | 76 base::Bind(&OfflinePageTabHelper::Redirect, |
| 71 weak_ptr_factory_.GetWeakPtr(), | 77 weak_ptr_factory_.GetWeakPtr(), |
| 72 navigation_handle->GetURL(), online_url)); | 78 navigation_handle->GetURL(), redirect_url)); |
| 73 } | 79 } |
| 74 | 80 |
| 75 void OfflinePageTabHelper::DidFinishNavigation( | 81 void OfflinePageTabHelper::DidFinishNavigation( |
| 76 content::NavigationHandle* navigation_handle) { | 82 content::NavigationHandle* navigation_handle) { |
| 77 // Skips non-main frame. | 83 // Skips non-main frame. |
| 78 if (!navigation_handle->IsInMainFrame()) | 84 if (!navigation_handle->IsInMainFrame()) |
| 79 return; | 85 return; |
| 80 | 86 |
| 87 // If the offline page is being loaded successfully, set the access record. |
| 88 net::Error error_code = navigation_handle->GetNetErrorCode(); |
| 89 if (error_code == net::OK && |
| 90 OfflinePageUtils::IsOfflinePage( |
| 91 web_contents()->GetBrowserContext(), navigation_handle->GetURL())) { |
| 92 OfflinePageUtils::MarkPageAccessed( |
| 93 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); |
| 94 } |
| 95 |
| 81 // Skips load failure other than no network. | 96 // Skips load failure other than no network. |
| 82 net::Error error_code = navigation_handle->GetNetErrorCode(); | |
| 83 if (error_code != net::ERR_INTERNET_DISCONNECTED && | 97 if (error_code != net::ERR_INTERNET_DISCONNECTED && |
| 84 error_code != net::ERR_NAME_NOT_RESOLVED && | 98 error_code != net::ERR_NAME_NOT_RESOLVED && |
| 85 error_code != net::ERR_ADDRESS_UNREACHABLE && | 99 error_code != net::ERR_ADDRESS_UNREACHABLE && |
| 86 error_code != net::ERR_PROXY_CONNECTION_FAILED) { | 100 error_code != net::ERR_PROXY_CONNECTION_FAILED) { |
| 87 return; | 101 return; |
| 88 } | 102 } |
| 89 | 103 |
| 90 // On a forward or back transition, don't affect the order of the nav stack. | 104 // On a forward or back transition, don't affect the order of the nav stack. |
| 91 if (navigation_handle->GetPageTransition() == | 105 if (navigation_handle->GetPageTransition() == |
| 92 ui::PAGE_TRANSITION_FORWARD_BACK) { | 106 ui::PAGE_TRANSITION_FORWARD_BACK) { |
| 93 return; | 107 return; |
| 94 } | 108 } |
| 95 | 109 |
| 96 // Skips if not loading an online version of saved page. | 110 // When the navigation starts, we redirect immediately from online page to |
| 111 // offline version on the case that there is no network connection. If there |
| 112 // is still network connection but with no or poor network connectivity, the |
| 113 // navigation will eventually fail and we want to redirect to offline copy |
| 114 // in this case. |
| 97 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( | 115 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( |
| 98 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); | 116 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); |
| 99 if (!offline_url.is_valid()) | 117 if (!offline_url.is_valid()) |
| 100 return; | 118 return; |
| 101 | 119 |
| 102 base::ThreadTaskRunnerHandle::Get()->PostTask( | 120 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 103 FROM_HERE, | 121 FROM_HERE, |
| 104 base::Bind(&OfflinePageTabHelper::Redirect, | 122 base::Bind(&OfflinePageTabHelper::Redirect, |
| 105 weak_ptr_factory_.GetWeakPtr(), | 123 weak_ptr_factory_.GetWeakPtr(), |
| 106 navigation_handle->GetURL(), offline_url)); | 124 navigation_handle->GetURL(), offline_url)); |
| 107 } | 125 } |
| 108 | 126 |
| 109 void OfflinePageTabHelper::Redirect( | 127 void OfflinePageTabHelper::Redirect( |
| 110 const GURL& from_url, const GURL& to_url) { | 128 const GURL& from_url, const GURL& to_url) { |
| 111 if (to_url.SchemeIsFile()) | 129 if (to_url.SchemeIsFile()) { |
| 112 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); | 130 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); |
| 113 else | 131 } else { |
| 114 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); | 132 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); |
| 133 } |
| 115 | 134 |
| 116 content::NavigationController::LoadURLParams load_params(to_url); | 135 content::NavigationController::LoadURLParams load_params(to_url); |
| 117 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | 136 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; |
| 118 load_params.redirect_chain.push_back(from_url); | 137 load_params.redirect_chain.push_back(from_url); |
| 119 web_contents()->GetController().LoadURLWithParams(load_params); | 138 web_contents()->GetController().LoadURLWithParams(load_params); |
| 120 } | 139 } |
| 121 | 140 |
| 122 } // namespace offline_pages | 141 } // namespace offline_pages |
| OLD | NEW |