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/clock.h" | |
| 14 #include "base/time/default_clock.h" | |
| 15 #include "base/time/time.h" | |
| 13 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 16 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 14 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 17 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 18 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h" | |
| 19 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service_factory.h" | |
| 20 #include "chrome/browser/profiles/profile.h" | |
| 21 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s.h" | |
| 15 #include "components/offline_pages/client_namespace_constants.h" | 22 #include "components/offline_pages/client_namespace_constants.h" |
| 16 #include "components/offline_pages/offline_page_model.h" | 23 #include "components/offline_pages/offline_page_model.h" |
| 17 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/navigation_controller.h" | 25 #include "content/public/browser/navigation_controller.h" |
| 19 #include "content/public/browser/navigation_entry.h" | 26 #include "content/public/browser/navigation_entry.h" |
| 20 #include "content/public/browser/navigation_handle.h" | 27 #include "content/public/browser/navigation_handle.h" |
| 21 #include "content/public/browser/render_frame_host.h" | 28 #include "content/public/browser/render_frame_host.h" |
| 22 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 23 #include "net/base/net_errors.h" | 30 #include "net/base/net_errors.h" |
| 24 #include "net/base/network_change_notifier.h" | 31 #include "net/base/network_change_notifier.h" |
| 32 #include "net/nqe/network_quality_estimator.h" | |
| 25 #include "ui/base/page_transition_types.h" | 33 #include "ui/base/page_transition_types.h" |
| 26 | 34 |
| 27 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper); | 35 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper); |
| 28 | 36 |
| 29 namespace offline_pages { | 37 namespace offline_pages { |
| 30 namespace { | 38 namespace { |
| 31 | 39 |
| 32 void ReportAccessedOfflinePage(content::BrowserContext* browser_context, | 40 void ReportAccessedOfflinePage(content::BrowserContext* browser_context, |
| 33 const GURL& navigated_url, | 41 const GURL& navigated_url, |
| 34 const GURL& online_url) { | 42 const GURL& online_url) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 49 return false; | 57 return false; |
| 50 *tab_id = base::IntToString(temp_tab_id); | 58 *tab_id = base::IntToString(temp_tab_id); |
| 51 return true; | 59 return true; |
| 52 } | 60 } |
| 53 }; | 61 }; |
| 54 } // namespace | 62 } // namespace |
| 55 | 63 |
| 56 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) | 64 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) |
| 57 : content::WebContentsObserver(web_contents), | 65 : content::WebContentsObserver(web_contents), |
| 58 delegate_(new DefaultDelegate()), | 66 delegate_(new DefaultDelegate()), |
| 67 clock_(new base::DefaultClock), | |
| 59 weak_ptr_factory_(this) { | 68 weak_ptr_factory_(this) { |
| 60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 69 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 61 } | 70 } |
| 62 | 71 |
| 63 OfflinePageTabHelper::~OfflinePageTabHelper() {} | 72 OfflinePageTabHelper::~OfflinePageTabHelper() {} |
| 64 | 73 |
| 65 void OfflinePageTabHelper::SetDelegateForTesting( | 74 void OfflinePageTabHelper::SetDelegateForTesting( |
| 66 std::unique_ptr<OfflinePageTabHelper::Delegate> delegate) { | 75 std::unique_ptr<OfflinePageTabHelper::Delegate> delegate) { |
| 67 DCHECK(delegate); | 76 DCHECK(delegate); |
| 68 delegate_ = std::move(delegate); | 77 delegate_ = std::move(delegate); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 88 // which are not at the head of the stack. | 97 // which are not at the head of the stack. |
| 89 // TODO(dimich): Not sure this is needed. Clarify and remove. Bug 624216. | 98 // TODO(dimich): Not sure this is needed. Clarify and remove. Bug 624216. |
| 90 const content::NavigationController& controller = | 99 const content::NavigationController& controller = |
| 91 web_contents()->GetController(); | 100 web_contents()->GetController(); |
| 92 if (controller.GetEntryCount() > 0 && | 101 if (controller.GetEntryCount() > 0 && |
| 93 controller.GetCurrentEntryIndex() != -1 && | 102 controller.GetCurrentEntryIndex() != -1 && |
| 94 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { | 103 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { |
| 95 return; | 104 return; |
| 96 } | 105 } |
| 97 | 106 |
| 98 content::BrowserContext* context = web_contents()->GetBrowserContext(); | |
| 99 if (net::NetworkChangeNotifier::IsOffline()) { | 107 if (net::NetworkChangeNotifier::IsOffline()) { |
| 100 GetPagesForRedirectToOffline( | 108 GetPagesForRedirectToOffline( |
| 101 RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK, navigated_url); | 109 RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK, navigated_url); |
| 102 return; | 110 return; |
| 103 } | 111 } |
| 104 | 112 |
| 113 content::BrowserContext* context = web_contents()->GetBrowserContext(); | |
| 114 | |
| 115 // If using offline pages for slow networks is allowed, and the network is | |
| 116 // currently estimated to be prohibitivley slow, attempt to load an offline | |
| 117 // page. | |
| 118 if (data_reduction_proxy::params:: | |
|
tbansal1
2016/07/25 21:43:22
Might be cleaner to move this to a separate functi
RyanSturm
2016/07/26 19:48:11
Done.
| |
| 119 IsIncludedInOfflinePagesSlowConnectionFieldTrial()) { | |
| 120 Profile* profile = Profile::FromBrowserContext(context); | |
| 121 UINetworkQualityEstimatorService* nqe_service = | |
| 122 UINetworkQualityEstimatorServiceFactory::GetForProfile(profile); | |
| 123 if (nqe_service && | |
| 124 nqe_service->GetEffectiveConnectionType() == | |
| 125 net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G) { | |
|
tbansal1
2016/07/25 21:43:22
Instead of doing this, I will do something like
ec
RyanSturm
2016/07/26 19:48:11
I'm using the NCN::IsOffline above; either way I'd
| |
| 126 GetPagesForRedirectToOffline( | |
| 127 RedirectResult::REDIRECTED_ON_PROHIBITVELY_SLOW_NETWORK, | |
| 128 navigated_url); | |
| 129 return; | |
| 130 } | |
| 131 } | |
| 132 | |
| 105 OfflinePageModel* offline_page_model = | 133 OfflinePageModel* offline_page_model = |
| 106 OfflinePageModelFactory::GetForBrowserContext(context); | 134 OfflinePageModelFactory::GetForBrowserContext(context); |
| 107 if (!offline_page_model) | 135 if (!offline_page_model) |
| 108 return; | 136 return; |
| 109 | 137 |
| 110 offline_page_model->GetPageByOfflineURL( | 138 offline_page_model->GetPageByOfflineURL( |
| 111 navigated_url, base::Bind(&OfflinePageTabHelper::RedirectToOnline, | 139 navigated_url, base::Bind(&OfflinePageTabHelper::RedirectToOnline, |
| 112 weak_ptr_factory_.GetWeakPtr(), navigated_url)); | 140 weak_ptr_factory_.GetWeakPtr(), navigated_url)); |
| 113 } | 141 } |
| 114 | 142 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 online_url, | 220 online_url, |
| 193 base::Bind(&OfflinePageTabHelper::SelectBestPageForRedirectToOffline, | 221 base::Bind(&OfflinePageTabHelper::SelectBestPageForRedirectToOffline, |
| 194 weak_ptr_factory_.GetWeakPtr(), result, online_url)); | 222 weak_ptr_factory_.GetWeakPtr(), result, online_url)); |
| 195 } | 223 } |
| 196 | 224 |
| 197 void OfflinePageTabHelper::SelectBestPageForRedirectToOffline( | 225 void OfflinePageTabHelper::SelectBestPageForRedirectToOffline( |
| 198 RedirectResult result, | 226 RedirectResult result, |
| 199 const GURL& online_url, | 227 const GURL& online_url, |
| 200 const MultipleOfflinePageItemResult& pages) { | 228 const MultipleOfflinePageItemResult& pages) { |
| 201 DCHECK(result == RedirectResult::REDIRECTED_ON_FLAKY_NETWORK || | 229 DCHECK(result == RedirectResult::REDIRECTED_ON_FLAKY_NETWORK || |
| 202 result == RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK); | 230 result == RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK || |
| 231 result == RedirectResult::REDIRECTED_ON_PROHIBITVELY_SLOW_NETWORK); | |
|
tbansal1
2016/07/25 21:43:22
May be: s/PROHIBITVELY_//
bengr likes PROHIBITVELY
RyanSturm
2016/07/26 19:48:11
I think slow is <4G, but prohibitively slow is the
| |
| 203 | 232 |
| 204 // When there is no valid tab android there is nowhere to show the offline | 233 // When there is no valid tab android there is nowhere to show the offline |
| 205 // page, so we can leave. | 234 // page, so we can leave. |
| 206 std::string tab_id; | 235 std::string tab_id; |
| 207 if (!delegate_->GetTabId(web_contents(), &tab_id)) { | 236 if (!delegate_->GetTabId(web_contents(), &tab_id)) { |
| 208 ReportRedirectResultUMA(RedirectResult::NO_TAB_ID); | 237 ReportRedirectResultUMA(RedirectResult::NO_TAB_ID); |
| 209 return; | 238 return; |
| 210 } | 239 } |
| 211 | 240 |
| 212 const OfflinePageItem* selected_page = nullptr; | 241 const OfflinePageItem* selected_page = nullptr; |
| 213 for (const auto& offline_page : pages) { | 242 for (const auto& offline_page : pages) { |
| 214 if ((offline_page.client_id.name_space == kBookmarkNamespace) || | 243 if ((offline_page.client_id.name_space == kBookmarkNamespace) || |
| 215 (offline_page.client_id.name_space == kAsyncNamespace) || | 244 (offline_page.client_id.name_space == kAsyncNamespace) || |
| 216 (offline_page.client_id.name_space == kLastNNamespace && | 245 (offline_page.client_id.name_space == kLastNNamespace && |
| 217 offline_page.client_id.id == tab_id)) { | 246 offline_page.client_id.id == tab_id)) { |
| 218 if (!selected_page || | 247 if (!selected_page || |
| 219 offline_page.creation_time > selected_page->creation_time) { | 248 offline_page.creation_time > selected_page->creation_time) { |
| 220 selected_page = &offline_page; | 249 selected_page = &offline_page; |
| 221 } | 250 } |
| 222 } | 251 } |
| 223 } | 252 } |
| 224 | 253 |
| 225 if (!selected_page) { | 254 if (!selected_page) { |
| 255 switch (result) { | |
| 256 case RedirectResult::REDIRECTED_ON_FLAKY_NETWORK: | |
| 257 ReportRedirectResultUMA( | |
| 258 RedirectResult::PAGE_NOT_FOUND_ON_FLAKY_NETWORK); | |
| 259 break; | |
|
tbansal1
2016/07/25 21:43:22
Why not return here?
RyanSturm
2016/07/26 19:48:11
Done.
| |
| 260 case RedirectResult::REDIRECTED_ON_PROHIBITVELY_SLOW_NETWORK: | |
| 261 ReportRedirectResultUMA( | |
| 262 RedirectResult::PAGE_NOT_FOUND_ON_PROHIBITVELY_SLOW_NETWORK); | |
| 263 break; | |
| 264 case RedirectResult::REDIRECTED_ON_DISCONNECTED_NETWORK: | |
| 265 ReportRedirectResultUMA( | |
| 266 RedirectResult::PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); | |
| 267 break; | |
| 268 default: | |
| 269 NOTREACHED(); | |
| 270 } | |
| 271 return; | |
| 272 } | |
| 273 | |
| 274 // If the page is being loaded on a slow network, only use the offline page | |
| 275 // if it was created within the past day. | |
| 276 if (result == RedirectResult::REDIRECTED_ON_PROHIBITVELY_SLOW_NETWORK && | |
| 277 clock_->Now() - selected_page->creation_time > | |
| 278 base::TimeDelta::FromDays(1)) { | |
| 226 ReportRedirectResultUMA( | 279 ReportRedirectResultUMA( |
| 227 result == RedirectResult::REDIRECTED_ON_FLAKY_NETWORK ? | 280 RedirectResult::PAGE_NOT_FRESH_ON_PROHIBITVELY_SLOW_NETWORK); |
|
tbansal1
2016/07/25 21:35:26
Why is this needed?
RyanSturm
2016/07/26 19:48:11
It gives us the amount of times that we had a slow
| |
| 228 RedirectResult::PAGE_NOT_FOUND_ON_FLAKY_NETWORK : | |
| 229 RedirectResult::PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); | |
| 230 return; | 281 return; |
| 231 } | 282 } |
| 232 | 283 |
| 233 TryRedirectToOffline(result, online_url, *selected_page); | 284 TryRedirectToOffline(result, online_url, *selected_page); |
| 234 } | 285 } |
| 235 | 286 |
| 236 void OfflinePageTabHelper::TryRedirectToOffline( | 287 void OfflinePageTabHelper::TryRedirectToOffline( |
| 237 RedirectResult result, | 288 RedirectResult result, |
| 238 const GURL& from_url, | 289 const GURL& from_url, |
| 239 const OfflinePageItem& offline_page) { | 290 const OfflinePageItem& offline_page) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 266 return entry && | 317 return entry && |
| 267 !entry->GetRedirectChain().empty() && | 318 !entry->GetRedirectChain().empty() && |
| 268 entry->GetRedirectChain().back() == to_url; | 319 entry->GetRedirectChain().back() == to_url; |
| 269 } | 320 } |
| 270 | 321 |
| 271 void OfflinePageTabHelper::ReportRedirectResultUMA(RedirectResult result) { | 322 void OfflinePageTabHelper::ReportRedirectResultUMA(RedirectResult result) { |
| 272 UMA_HISTOGRAM_ENUMERATION("OfflinePages.RedirectResult", | 323 UMA_HISTOGRAM_ENUMERATION("OfflinePages.RedirectResult", |
| 273 static_cast<int>(result), | 324 static_cast<int>(result), |
| 274 static_cast<int>(RedirectResult::REDIRECT_RESULT_MAX)); | 325 static_cast<int>(RedirectResult::REDIRECT_RESULT_MAX)); |
| 275 } | 326 } |
| 327 | |
| 328 void OfflinePageTabHelper::SetClockForTesting( | |
| 329 std::unique_ptr<base::Clock> clock) { | |
| 330 clock_ = std::move(clock); | |
| 331 } | |
| 332 | |
| 276 } // namespace offline_pages | 333 } // namespace offline_pages |
| OLD | NEW |