Chromium Code Reviews| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 content::NavigationHandle* navigation_handle) { | 82 content::NavigationHandle* navigation_handle) { |
| 83 // Skips non-main frame. | 83 // Skips non-main frame. |
| 84 if (!navigation_handle->IsInMainFrame()) | 84 if (!navigation_handle->IsInMainFrame()) |
| 85 return; | 85 return; |
| 86 | 86 |
| 87 // If the offline page is being loaded successfully, set the access record. | 87 // If the offline page is being loaded successfully, set the access record. |
| 88 net::Error error_code = navigation_handle->GetNetErrorCode(); | 88 net::Error error_code = navigation_handle->GetNetErrorCode(); |
| 89 if (error_code == net::OK && | 89 if (error_code == net::OK && |
| 90 OfflinePageUtils::IsOfflinePage( | 90 OfflinePageUtils::IsOfflinePage( |
| 91 web_contents()->GetBrowserContext(), navigation_handle->GetURL())) { | 91 web_contents()->GetBrowserContext(), navigation_handle->GetURL())) { |
| 92 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageWhenOffline", true); | |
|
fgorski
2016/05/17 03:53:23
nit: this is a bit forward looking and perhaps we
| |
| 92 OfflinePageUtils::MarkPageAccessed( | 93 OfflinePageUtils::MarkPageAccessed( |
| 93 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); | 94 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); |
| 94 } | 95 } |
| 95 | 96 |
| 96 // Skips load failure other than no network. | 97 // Skips load failure other than no network. |
| 97 if (error_code != net::ERR_INTERNET_DISCONNECTED && | 98 if (error_code != net::ERR_INTERNET_DISCONNECTED && |
| 98 error_code != net::ERR_NAME_NOT_RESOLVED && | 99 error_code != net::ERR_NAME_NOT_RESOLVED && |
| 99 error_code != net::ERR_ADDRESS_UNREACHABLE && | 100 error_code != net::ERR_ADDRESS_UNREACHABLE && |
| 100 error_code != net::ERR_PROXY_CONNECTION_FAILED) { | 101 error_code != net::ERR_PROXY_CONNECTION_FAILED) { |
| 101 return; | 102 return; |
| 102 } | 103 } |
| 103 | 104 |
| 104 // On a forward or back transition, don't affect the order of the nav stack. | 105 // On a forward or back transition, don't affect the order of the nav stack. |
| 105 if (navigation_handle->GetPageTransition() == | 106 if (navigation_handle->GetPageTransition() == |
| 106 ui::PAGE_TRANSITION_FORWARD_BACK) { | 107 ui::PAGE_TRANSITION_FORWARD_BACK) { |
| 108 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageWhenOffline", false); | |
| 107 return; | 109 return; |
| 108 } | 110 } |
| 109 | 111 |
| 110 // When the navigation starts, we redirect immediately from online page to | 112 // 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 | 113 // 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 | 114 // 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 | 115 // navigation will eventually fail and we want to redirect to offline copy |
| 114 // in this case. | 116 // in this case. |
| 115 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( | 117 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( |
| 116 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); | 118 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); |
| 117 if (!offline_url.is_valid()) | 119 if (!offline_url.is_valid()) { |
| 120 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageWhenOffline", false); | |
| 118 return; | 121 return; |
| 122 } | |
| 119 | 123 |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask( | 124 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 121 FROM_HERE, | 125 FROM_HERE, |
| 122 base::Bind(&OfflinePageTabHelper::Redirect, | 126 base::Bind(&OfflinePageTabHelper::Redirect, |
| 123 weak_ptr_factory_.GetWeakPtr(), | 127 weak_ptr_factory_.GetWeakPtr(), |
| 124 navigation_handle->GetURL(), offline_url)); | 128 navigation_handle->GetURL(), offline_url)); |
| 125 } | 129 } |
| 126 | 130 |
| 127 void OfflinePageTabHelper::Redirect( | 131 void OfflinePageTabHelper::Redirect( |
| 128 const GURL& from_url, const GURL& to_url) { | 132 const GURL& from_url, const GURL& to_url) { |
| 129 if (to_url.SchemeIsFile()) { | 133 if (to_url.SchemeIsFile()) { |
| 130 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); | 134 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); |
| 131 } else { | 135 } else { |
| 132 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); | 136 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); |
| 133 } | 137 } |
| 134 | 138 |
| 135 content::NavigationController::LoadURLParams load_params(to_url); | 139 content::NavigationController::LoadURLParams load_params(to_url); |
| 136 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | 140 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; |
| 137 load_params.redirect_chain.push_back(from_url); | 141 load_params.redirect_chain.push_back(from_url); |
| 138 web_contents()->GetController().LoadURLWithParams(load_params); | 142 web_contents()->GetController().LoadURLWithParams(load_params); |
| 139 } | 143 } |
| 140 | 144 |
| 141 } // namespace offline_pages | 145 } // namespace offline_pages |
| OLD | NEW |