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/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 if (effective_connection_type >= | |
|
Lei Zhang
2016/07/28 20:41:52
if (comparision_evals_to_bool)
return true;
retu
RyanSturm
2016/07/28 22:07:18
Done.
| |
| 60 net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE && | |
| 61 effective_connection_type <= | |
| 62 net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G) { | |
| 63 return true; | |
| 64 } | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 41 class DefaultDelegate : public OfflinePageTabHelper::Delegate { | 68 class DefaultDelegate : public OfflinePageTabHelper::Delegate { |
| 42 public: | 69 public: |
| 43 DefaultDelegate() {} | 70 DefaultDelegate() {} |
| 44 // offline_pages::OfflinePageTabHelper::Delegate implementation: | 71 // offline_pages::OfflinePageTabHelper::Delegate implementation: |
| 45 bool GetTabId(content::WebContents* web_contents, | 72 bool GetTabId(content::WebContents* web_contents, |
| 46 std::string* tab_id) const override { | 73 std::string* tab_id) const override { |
| 47 int temp_tab_id; | 74 int temp_tab_id; |
| 48 if (!OfflinePageUtils::GetTabId(web_contents, &temp_tab_id)) | 75 if (!OfflinePageUtils::GetTabId(web_contents, &temp_tab_id)) |
| 49 return false; | 76 return false; |
| 50 *tab_id = base::IntToString(temp_tab_id); | 77 *tab_id = base::IntToString(temp_tab_id); |
| 51 return true; | 78 return true; |
| 52 } | 79 } |
| 80 base::Time Now() const override { return base::Time::Now(); } | |
| 53 }; | 81 }; |
| 54 } // namespace | 82 } // namespace |
| 55 | 83 |
| 56 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) | 84 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) |
| 57 : content::WebContentsObserver(web_contents), | 85 : content::WebContentsObserver(web_contents), |
| 58 delegate_(new DefaultDelegate()), | 86 delegate_(new DefaultDelegate()), |
| 59 weak_ptr_factory_(this) { | 87 weak_ptr_factory_(this) { |
| 60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 88 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 61 } | 89 } |
| 62 | 90 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 88 // which are not at the head of the stack. | 116 // which are not at the head of the stack. |
| 89 // TODO(dimich): Not sure this is needed. Clarify and remove. Bug 624216. | 117 // TODO(dimich): Not sure this is needed. Clarify and remove. Bug 624216. |
| 90 const content::NavigationController& controller = | 118 const content::NavigationController& controller = |
| 91 web_contents()->GetController(); | 119 web_contents()->GetController(); |
| 92 if (controller.GetEntryCount() > 0 && | 120 if (controller.GetEntryCount() > 0 && |
| 93 controller.GetCurrentEntryIndex() != -1 && | 121 controller.GetCurrentEntryIndex() != -1 && |
| 94 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { | 122 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { |
| 95 return; | 123 return; |
| 96 } | 124 } |
| 97 | 125 |
| 98 content::BrowserContext* context = web_contents()->GetBrowserContext(); | |
| 99 if (net::NetworkChangeNotifier::IsOffline()) { | 126 if (net::NetworkChangeNotifier::IsOffline()) { |
| 100 GetPagesForRedirectToOffline( | 127 GetPagesForRedirectToOffline( |
| 101 RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK, navigated_url); | 128 RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK, navigated_url); |
| 102 return; | 129 return; |
| 103 } | 130 } |
| 104 | 131 |
| 132 content::BrowserContext* context = web_contents()->GetBrowserContext(); | |
| 133 if (ShouldUseOfflineForSlowNetwork(context)) { | |
| 134 GetPagesForRedirectToOffline( | |
| 135 RedirectResult::REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK, | |
| 136 navigated_url); | |
| 137 return; | |
| 138 } | |
| 139 | |
| 105 OfflinePageModel* offline_page_model = | 140 OfflinePageModel* offline_page_model = |
| 106 OfflinePageModelFactory::GetForBrowserContext(context); | 141 OfflinePageModelFactory::GetForBrowserContext(context); |
| 107 if (!offline_page_model) | 142 if (!offline_page_model) |
| 108 return; | 143 return; |
| 109 | 144 |
| 110 offline_page_model->GetPageByOfflineURL( | 145 offline_page_model->GetPageByOfflineURL( |
| 111 navigated_url, base::Bind(&OfflinePageTabHelper::RedirectToOnline, | 146 navigated_url, base::Bind(&OfflinePageTabHelper::RedirectToOnline, |
| 112 weak_ptr_factory_.GetWeakPtr(), navigated_url)); | 147 weak_ptr_factory_.GetWeakPtr(), navigated_url)); |
| 113 } | 148 } |
| 114 | 149 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 online_url, | 227 online_url, |
| 193 base::Bind(&OfflinePageTabHelper::SelectBestPageForRedirectToOffline, | 228 base::Bind(&OfflinePageTabHelper::SelectBestPageForRedirectToOffline, |
| 194 weak_ptr_factory_.GetWeakPtr(), result, online_url)); | 229 weak_ptr_factory_.GetWeakPtr(), result, online_url)); |
| 195 } | 230 } |
| 196 | 231 |
| 197 void OfflinePageTabHelper::SelectBestPageForRedirectToOffline( | 232 void OfflinePageTabHelper::SelectBestPageForRedirectToOffline( |
| 198 RedirectResult result, | 233 RedirectResult result, |
| 199 const GURL& online_url, | 234 const GURL& online_url, |
| 200 const MultipleOfflinePageItemResult& pages) { | 235 const MultipleOfflinePageItemResult& pages) { |
| 201 DCHECK(result == RedirectResult::REDIRECTED_ON_FLAKY_NETWORK || | 236 DCHECK(result == RedirectResult::REDIRECTED_ON_FLAKY_NETWORK || |
| 202 result == RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK); | 237 result == RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK || |
| 238 result == RedirectResult::REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK); | |
| 203 | 239 |
| 204 // When there is no valid tab android there is nowhere to show the offline | 240 // When there is no valid tab android there is nowhere to show the offline |
| 205 // page, so we can leave. | 241 // page, so we can leave. |
| 206 std::string tab_id; | 242 std::string tab_id; |
| 207 if (!delegate_->GetTabId(web_contents(), &tab_id)) { | 243 if (!delegate_->GetTabId(web_contents(), &tab_id)) { |
| 208 ReportRedirectResultUMA(RedirectResult::NO_TAB_ID); | 244 ReportRedirectResultUMA(RedirectResult::NO_TAB_ID); |
| 209 return; | 245 return; |
| 210 } | 246 } |
| 211 | 247 |
| 212 const OfflinePageItem* selected_page = nullptr; | 248 const OfflinePageItem* selected_page = nullptr; |
| 213 for (const auto& offline_page : pages) { | 249 for (const auto& offline_page : pages) { |
| 214 if ((offline_page.client_id.name_space == kBookmarkNamespace) || | 250 if ((offline_page.client_id.name_space == kBookmarkNamespace) || |
| 215 (offline_page.client_id.name_space == kAsyncNamespace) || | 251 (offline_page.client_id.name_space == kAsyncNamespace) || |
| 216 (offline_page.client_id.name_space == kLastNNamespace && | 252 (offline_page.client_id.name_space == kLastNNamespace && |
| 217 offline_page.client_id.id == tab_id)) { | 253 offline_page.client_id.id == tab_id)) { |
| 218 if (!selected_page || | 254 if (!selected_page || |
| 219 offline_page.creation_time > selected_page->creation_time) { | 255 offline_page.creation_time > selected_page->creation_time) { |
| 220 selected_page = &offline_page; | 256 selected_page = &offline_page; |
| 221 } | 257 } |
| 222 } | 258 } |
| 223 } | 259 } |
| 224 | 260 |
| 225 if (!selected_page) { | 261 if (!selected_page) { |
| 262 switch (result) { | |
| 263 case RedirectResult::REDIRECTED_ON_FLAKY_NETWORK: | |
| 264 ReportRedirectResultUMA( | |
| 265 RedirectResult::PAGE_NOT_FOUND_ON_FLAKY_NETWORK); | |
| 266 return; | |
| 267 case RedirectResult::REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK: | |
| 268 ReportRedirectResultUMA( | |
| 269 RedirectResult::PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); | |
| 270 return; | |
| 271 case RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK: | |
| 272 ReportRedirectResultUMA( | |
| 273 RedirectResult::PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); | |
| 274 return; | |
| 275 default: | |
| 276 NOTREACHED(); | |
| 277 return; | |
| 278 } | |
| 279 } | |
| 280 | |
| 281 // If the page is being loaded on a slow network, only use the offline page | |
| 282 // if it was created within the past day. | |
| 283 if (result == RedirectResult::REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK && | |
| 284 delegate_->Now() - selected_page->creation_time > | |
| 285 base::TimeDelta::FromDays(1)) { | |
| 226 ReportRedirectResultUMA( | 286 ReportRedirectResultUMA( |
| 227 result == RedirectResult::REDIRECTED_ON_FLAKY_NETWORK ? | 287 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; | 288 return; |
| 231 } | 289 } |
| 232 | 290 |
| 233 TryRedirectToOffline(result, online_url, *selected_page); | 291 TryRedirectToOffline(result, online_url, *selected_page); |
| 234 } | 292 } |
| 235 | 293 |
| 236 void OfflinePageTabHelper::TryRedirectToOffline( | 294 void OfflinePageTabHelper::TryRedirectToOffline( |
| 237 RedirectResult result, | 295 RedirectResult result, |
| 238 const GURL& from_url, | 296 const GURL& from_url, |
| 239 const OfflinePageItem& offline_page) { | 297 const OfflinePageItem& offline_page) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 266 return entry && | 324 return entry && |
| 267 !entry->GetRedirectChain().empty() && | 325 !entry->GetRedirectChain().empty() && |
| 268 entry->GetRedirectChain().back() == to_url; | 326 entry->GetRedirectChain().back() == to_url; |
| 269 } | 327 } |
| 270 | 328 |
| 271 void OfflinePageTabHelper::ReportRedirectResultUMA(RedirectResult result) { | 329 void OfflinePageTabHelper::ReportRedirectResultUMA(RedirectResult result) { |
| 272 UMA_HISTOGRAM_ENUMERATION("OfflinePages.RedirectResult", | 330 UMA_HISTOGRAM_ENUMERATION("OfflinePages.RedirectResult", |
| 273 static_cast<int>(result), | 331 static_cast<int>(result), |
| 274 static_cast<int>(RedirectResult::REDIRECT_RESULT_MAX)); | 332 static_cast<int>(RedirectResult::REDIRECT_RESULT_MAX)); |
| 275 } | 333 } |
| 334 | |
| 276 } // namespace offline_pages | 335 } // namespace offline_pages |
| OLD | NEW |