| 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/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" |
| 13 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 14 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 15 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 16 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h" |
| 17 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service_factory.h" |
| 18 #include "chrome/browser/profiles/profile.h" |
| 15 #include "components/offline_pages/client_namespace_constants.h" | 19 #include "components/offline_pages/client_namespace_constants.h" |
| 16 #include "components/offline_pages/offline_page_model.h" | 20 #include "components/offline_pages/offline_page_model.h" |
| 21 #include "components/previews/previews_experiments.h" |
| 17 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/navigation_controller.h" | 23 #include "content/public/browser/navigation_controller.h" |
| 19 #include "content/public/browser/navigation_entry.h" | 24 #include "content/public/browser/navigation_entry.h" |
| 20 #include "content/public/browser/navigation_handle.h" | 25 #include "content/public/browser/navigation_handle.h" |
| 21 #include "content/public/browser/render_frame_host.h" | 26 #include "content/public/browser/render_frame_host.h" |
| 22 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 23 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 24 #include "net/base/network_change_notifier.h" | 29 #include "net/base/network_change_notifier.h" |
| 30 #include "net/nqe/network_quality_estimator.h" |
| 25 #include "ui/base/page_transition_types.h" | 31 #include "ui/base/page_transition_types.h" |
| 26 | 32 |
| 27 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper); | 33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper); |
| 28 | 34 |
| 29 namespace offline_pages { | 35 namespace offline_pages { |
| 30 namespace { | 36 namespace { |
| 31 | 37 |
| 32 void ReportAccessedOfflinePage(content::BrowserContext* browser_context, | 38 void ReportAccessedOfflinePage(content::BrowserContext* browser_context, |
| 33 const GURL& navigated_url, | 39 const GURL& navigated_url, |
| 34 const GURL& online_url) { | 40 const GURL& online_url) { |
| 35 // If there is a valid online URL for this navigated URL, then we are looking | 41 // If there is a valid online URL for this navigated URL, then we are looking |
| 36 // at an offline page. | 42 // at an offline page. |
| 37 if (online_url.is_valid()) | 43 if (online_url.is_valid()) |
| 38 OfflinePageUtils::MarkPageAccessed(browser_context, navigated_url); | 44 OfflinePageUtils::MarkPageAccessed(browser_context, navigated_url); |
| 39 } | 45 } |
| 40 | 46 |
| 47 // Whether using offline pages for slow networks is allowed and the network is |
| 48 // currently estimated to be prohibitively slow. |
| 49 bool ShouldUseOfflineForSlowNetwork(content::BrowserContext* context) { |
| 50 if (!previews::IsOfflinePreviewsEnabled()) |
| 51 return false; |
| 52 Profile* profile = Profile::FromBrowserContext(context); |
| 53 UINetworkQualityEstimatorService* nqe_service = |
| 54 UINetworkQualityEstimatorServiceFactory::GetForProfile(profile); |
| 55 if (!nqe_service) |
| 56 return false; |
| 57 net::NetworkQualityEstimator::EffectiveConnectionType |
| 58 effective_connection_type = nqe_service->GetEffectiveConnectionType(); |
| 59 return effective_connection_type >= |
| 60 net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE && |
| 61 effective_connection_type <= |
| 62 net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G; |
| 63 } |
| 64 |
| 41 class DefaultDelegate : public OfflinePageTabHelper::Delegate { | 65 class DefaultDelegate : public OfflinePageTabHelper::Delegate { |
| 42 public: | 66 public: |
| 43 DefaultDelegate() {} | 67 DefaultDelegate() {} |
| 44 // offline_pages::OfflinePageTabHelper::Delegate implementation: | 68 // offline_pages::OfflinePageTabHelper::Delegate implementation: |
| 45 bool GetTabId(content::WebContents* web_contents, | 69 bool GetTabId(content::WebContents* web_contents, |
| 46 std::string* tab_id) const override { | 70 std::string* tab_id) const override { |
| 47 int temp_tab_id; | 71 int temp_tab_id; |
| 48 if (!OfflinePageUtils::GetTabId(web_contents, &temp_tab_id)) | 72 if (!OfflinePageUtils::GetTabId(web_contents, &temp_tab_id)) |
| 49 return false; | 73 return false; |
| 50 *tab_id = base::IntToString(temp_tab_id); | 74 *tab_id = base::IntToString(temp_tab_id); |
| 51 return true; | 75 return true; |
| 52 } | 76 } |
| 77 base::Time Now() const override { return base::Time::Now(); } |
| 53 }; | 78 }; |
| 54 } // namespace | 79 } // namespace |
| 55 | 80 |
| 56 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) | 81 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) |
| 57 : content::WebContentsObserver(web_contents), | 82 : content::WebContentsObserver(web_contents), |
| 58 delegate_(new DefaultDelegate()), | 83 delegate_(new DefaultDelegate()), |
| 59 weak_ptr_factory_(this) { | 84 weak_ptr_factory_(this) { |
| 60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 61 } | 86 } |
| 62 | 87 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 88 // which are not at the head of the stack. | 113 // which are not at the head of the stack. |
| 89 // TODO(dimich): Not sure this is needed. Clarify and remove. Bug 624216. | 114 // TODO(dimich): Not sure this is needed. Clarify and remove. Bug 624216. |
| 90 const content::NavigationController& controller = | 115 const content::NavigationController& controller = |
| 91 web_contents()->GetController(); | 116 web_contents()->GetController(); |
| 92 if (controller.GetEntryCount() > 0 && | 117 if (controller.GetEntryCount() > 0 && |
| 93 controller.GetCurrentEntryIndex() != -1 && | 118 controller.GetCurrentEntryIndex() != -1 && |
| 94 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { | 119 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { |
| 95 return; | 120 return; |
| 96 } | 121 } |
| 97 | 122 |
| 98 content::BrowserContext* context = web_contents()->GetBrowserContext(); | |
| 99 if (net::NetworkChangeNotifier::IsOffline()) { | 123 if (net::NetworkChangeNotifier::IsOffline()) { |
| 100 GetPagesForRedirectToOffline( | 124 GetPagesForRedirectToOffline( |
| 101 RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK, navigated_url); | 125 RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK, navigated_url); |
| 102 return; | 126 return; |
| 103 } | 127 } |
| 104 | 128 |
| 129 content::BrowserContext* context = web_contents()->GetBrowserContext(); |
| 130 if (ShouldUseOfflineForSlowNetwork(context)) { |
| 131 GetPagesForRedirectToOffline( |
| 132 RedirectResult::REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK, |
| 133 navigated_url); |
| 134 return; |
| 135 } |
| 136 |
| 105 OfflinePageModel* offline_page_model = | 137 OfflinePageModel* offline_page_model = |
| 106 OfflinePageModelFactory::GetForBrowserContext(context); | 138 OfflinePageModelFactory::GetForBrowserContext(context); |
| 107 if (!offline_page_model) | 139 if (!offline_page_model) |
| 108 return; | 140 return; |
| 109 | 141 |
| 110 offline_page_model->GetPageByOfflineURL( | 142 offline_page_model->GetPageByOfflineURL( |
| 111 navigated_url, base::Bind(&OfflinePageTabHelper::RedirectToOnline, | 143 navigated_url, base::Bind(&OfflinePageTabHelper::RedirectToOnline, |
| 112 weak_ptr_factory_.GetWeakPtr(), navigated_url)); | 144 weak_ptr_factory_.GetWeakPtr(), navigated_url)); |
| 113 } | 145 } |
| 114 | 146 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 online_url, | 224 online_url, |
| 193 base::Bind(&OfflinePageTabHelper::SelectBestPageForRedirectToOffline, | 225 base::Bind(&OfflinePageTabHelper::SelectBestPageForRedirectToOffline, |
| 194 weak_ptr_factory_.GetWeakPtr(), result, online_url)); | 226 weak_ptr_factory_.GetWeakPtr(), result, online_url)); |
| 195 } | 227 } |
| 196 | 228 |
| 197 void OfflinePageTabHelper::SelectBestPageForRedirectToOffline( | 229 void OfflinePageTabHelper::SelectBestPageForRedirectToOffline( |
| 198 RedirectResult result, | 230 RedirectResult result, |
| 199 const GURL& online_url, | 231 const GURL& online_url, |
| 200 const MultipleOfflinePageItemResult& pages) { | 232 const MultipleOfflinePageItemResult& pages) { |
| 201 DCHECK(result == RedirectResult::REDIRECTED_ON_FLAKY_NETWORK || | 233 DCHECK(result == RedirectResult::REDIRECTED_ON_FLAKY_NETWORK || |
| 202 result == RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK); | 234 result == RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK || |
| 235 result == RedirectResult::REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK); |
| 203 | 236 |
| 204 // When there is no valid tab android there is nowhere to show the offline | 237 // When there is no valid tab android there is nowhere to show the offline |
| 205 // page, so we can leave. | 238 // page, so we can leave. |
| 206 std::string tab_id; | 239 std::string tab_id; |
| 207 if (!delegate_->GetTabId(web_contents(), &tab_id)) { | 240 if (!delegate_->GetTabId(web_contents(), &tab_id)) { |
| 208 ReportRedirectResultUMA(RedirectResult::NO_TAB_ID); | 241 ReportRedirectResultUMA(RedirectResult::NO_TAB_ID); |
| 209 return; | 242 return; |
| 210 } | 243 } |
| 211 | 244 |
| 212 const OfflinePageItem* selected_page = nullptr; | 245 const OfflinePageItem* selected_page = nullptr; |
| 213 for (const auto& offline_page : pages) { | 246 for (const auto& offline_page : pages) { |
| 214 if ((offline_page.client_id.name_space == kBookmarkNamespace) || | 247 if ((offline_page.client_id.name_space == kBookmarkNamespace) || |
| 215 (offline_page.client_id.name_space == kAsyncNamespace) || | 248 (offline_page.client_id.name_space == kAsyncNamespace) || |
| 216 (offline_page.client_id.name_space == kLastNNamespace && | 249 (offline_page.client_id.name_space == kLastNNamespace && |
| 217 offline_page.client_id.id == tab_id)) { | 250 offline_page.client_id.id == tab_id)) { |
| 218 if (!selected_page || | 251 if (!selected_page || |
| 219 offline_page.creation_time > selected_page->creation_time) { | 252 offline_page.creation_time > selected_page->creation_time) { |
| 220 selected_page = &offline_page; | 253 selected_page = &offline_page; |
| 221 } | 254 } |
| 222 } | 255 } |
| 223 } | 256 } |
| 224 | 257 |
| 225 if (!selected_page) { | 258 if (!selected_page) { |
| 259 switch (result) { |
| 260 case RedirectResult::REDIRECTED_ON_FLAKY_NETWORK: |
| 261 ReportRedirectResultUMA( |
| 262 RedirectResult::PAGE_NOT_FOUND_ON_FLAKY_NETWORK); |
| 263 return; |
| 264 case RedirectResult::REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK: |
| 265 ReportRedirectResultUMA( |
| 266 RedirectResult::PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); |
| 267 return; |
| 268 case RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK: |
| 269 ReportRedirectResultUMA( |
| 270 RedirectResult::PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); |
| 271 return; |
| 272 default: |
| 273 NOTREACHED(); |
| 274 return; |
| 275 } |
| 276 } |
| 277 |
| 278 // If the page is being loaded on a slow network, only use the offline page |
| 279 // if it was created within the past day. |
| 280 if (result == RedirectResult::REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK && |
| 281 delegate_->Now() - selected_page->creation_time > |
| 282 base::TimeDelta::FromDays(1)) { |
| 226 ReportRedirectResultUMA( | 283 ReportRedirectResultUMA( |
| 227 result == RedirectResult::REDIRECTED_ON_FLAKY_NETWORK ? | 284 RedirectResult::PAGE_NOT_FRESH_ON_PROHIBITIVELY_SLOW_NETWORK); |
| 228 RedirectResult::PAGE_NOT_FOUND_ON_FLAKY_NETWORK : | |
| 229 RedirectResult::PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); | |
| 230 return; | 285 return; |
| 231 } | 286 } |
| 232 | 287 |
| 233 TryRedirectToOffline(result, online_url, *selected_page); | 288 TryRedirectToOffline(result, online_url, *selected_page); |
| 234 } | 289 } |
| 235 | 290 |
| 236 void OfflinePageTabHelper::TryRedirectToOffline( | 291 void OfflinePageTabHelper::TryRedirectToOffline( |
| 237 RedirectResult result, | 292 RedirectResult result, |
| 238 const GURL& from_url, | 293 const GURL& from_url, |
| 239 const OfflinePageItem& offline_page) { | 294 const OfflinePageItem& offline_page) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 266 return entry && | 321 return entry && |
| 267 !entry->GetRedirectChain().empty() && | 322 !entry->GetRedirectChain().empty() && |
| 268 entry->GetRedirectChain().back() == to_url; | 323 entry->GetRedirectChain().back() == to_url; |
| 269 } | 324 } |
| 270 | 325 |
| 271 void OfflinePageTabHelper::ReportRedirectResultUMA(RedirectResult result) { | 326 void OfflinePageTabHelper::ReportRedirectResultUMA(RedirectResult result) { |
| 272 UMA_HISTOGRAM_ENUMERATION("OfflinePages.RedirectResult", | 327 UMA_HISTOGRAM_ENUMERATION("OfflinePages.RedirectResult", |
| 273 static_cast<int>(result), | 328 static_cast<int>(result), |
| 274 static_cast<int>(RedirectResult::REDIRECT_RESULT_MAX)); | 329 static_cast<int>(RedirectResult::REDIRECT_RESULT_MAX)); |
| 275 } | 330 } |
| 331 |
| 276 } // namespace offline_pages | 332 } // namespace offline_pages |
| OLD | NEW |