| 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" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 int* tab_id) const override { | 68 int* tab_id) const override { |
| 69 return OfflinePageUtils::GetTabId(web_contents, tab_id); | 69 return OfflinePageUtils::GetTabId(web_contents, tab_id); |
| 70 } | 70 } |
| 71 base::Time Now() const override { return base::Time::Now(); } | 71 base::Time Now() const override { return base::Time::Now(); } |
| 72 }; | 72 }; |
| 73 } // namespace | 73 } // namespace |
| 74 | 74 |
| 75 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) | 75 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) |
| 76 : content::WebContentsObserver(web_contents), | 76 : content::WebContentsObserver(web_contents), |
| 77 delegate_(new DefaultDelegate()), | 77 delegate_(new DefaultDelegate()), |
| 78 is_offline_preview_(false), |
| 78 weak_ptr_factory_(this) { | 79 weak_ptr_factory_(this) { |
| 79 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 80 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 80 } | 81 } |
| 81 | 82 |
| 82 OfflinePageTabHelper::~OfflinePageTabHelper() {} | 83 OfflinePageTabHelper::~OfflinePageTabHelper() {} |
| 83 | 84 |
| 84 void OfflinePageTabHelper::SetDelegateForTesting( | 85 void OfflinePageTabHelper::SetDelegateForTesting( |
| 85 std::unique_ptr<OfflinePageTabHelper::Delegate> delegate) { | 86 std::unique_ptr<OfflinePageTabHelper::Delegate> delegate) { |
| 86 DCHECK(delegate); | 87 DCHECK(delegate); |
| 87 delegate_ = std::move(delegate); | 88 delegate_ = std::move(delegate); |
| 88 } | 89 } |
| 89 | 90 |
| 90 void OfflinePageTabHelper::DidStartNavigation( | 91 void OfflinePageTabHelper::DidStartNavigation( |
| 91 content::NavigationHandle* navigation_handle) { | 92 content::NavigationHandle* navigation_handle) { |
| 92 // Skips non-main frame. | 93 // Skips non-main frame. |
| 93 if (!navigation_handle->IsInMainFrame()) | 94 if (!navigation_handle->IsInMainFrame()) |
| 94 return; | 95 return; |
| 95 | 96 |
| 96 // This is a new navigation so we can invalidate any previously scheduled | 97 // This is a new navigation so we can invalidate any previously scheduled |
| 97 // operations. | 98 // operations. |
| 98 weak_ptr_factory_.InvalidateWeakPtrs(); | 99 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 99 | 100 |
| 100 // Since this is a new navigation, we will reset the cached offline page, | 101 // Since this is a new navigation, we will reset the cached offline page, |
| 101 // unless we are currently looking at an offline page. | 102 // unless we are currently looking at an offline page. |
| 102 GURL navigated_url = navigation_handle->GetURL(); | 103 GURL navigated_url = navigation_handle->GetURL(); |
| 103 if (offline_page_ && navigated_url != offline_page_->GetOfflineURL()) | 104 if (offline_page_ && navigated_url != offline_page_->GetOfflineURL()) { |
| 104 offline_page_ = nullptr; | 105 offline_page_ = nullptr; |
| 106 is_offline_preview_ = false; |
| 107 } |
| 105 | 108 |
| 106 // If an offline download file is opened, don't do redirect per network | 109 // If an offline download file is opened, don't do redirect per network |
| 107 // conditions. Also store a cached copy here such that Tab knows that | 110 // conditions. Also store a cached copy here such that Tab knows that |
| 108 // the offline page is opened. | 111 // the offline page is opened. |
| 109 if (OfflinePageUtils::MightBeOfflineURL(navigated_url)) { | 112 if (OfflinePageUtils::MightBeOfflineURL(navigated_url)) { |
| 110 OfflinePageModel* offline_page_model = | 113 OfflinePageModel* offline_page_model = |
| 111 OfflinePageModelFactory::GetForBrowserContext( | 114 OfflinePageModelFactory::GetForBrowserContext( |
| 112 web_contents()->GetBrowserContext()); | 115 web_contents()->GetBrowserContext()); |
| 113 if (offline_page_model) { | 116 if (offline_page_model) { |
| 114 const OfflinePageItem* offline_page = | 117 const OfflinePageItem* offline_page = |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 216 |
| 214 GURL redirect_url = offline_page->url; | 217 GURL redirect_url = offline_page->url; |
| 215 if (IsInRedirectLoop(redirect_url)) { | 218 if (IsInRedirectLoop(redirect_url)) { |
| 216 ReportRedirectResultUMA(RedirectResult::REDIRECT_LOOP_ONLINE); | 219 ReportRedirectResultUMA(RedirectResult::REDIRECT_LOOP_ONLINE); |
| 217 return; | 220 return; |
| 218 } | 221 } |
| 219 | 222 |
| 220 Redirect(navigated_url, redirect_url); | 223 Redirect(navigated_url, redirect_url); |
| 221 // Clear the offline page since we are redirecting to online. | 224 // Clear the offline page since we are redirecting to online. |
| 222 offline_page_ = nullptr; | 225 offline_page_ = nullptr; |
| 226 is_offline_preview_ = false; |
| 223 | 227 |
| 224 ReportRedirectResultUMA(RedirectResult::REDIRECTED_ON_CONNECTED_NETWORK); | 228 ReportRedirectResultUMA(RedirectResult::REDIRECTED_ON_CONNECTED_NETWORK); |
| 225 } | 229 } |
| 226 | 230 |
| 227 void OfflinePageTabHelper::GetBestPageForRedirectToOffline( | 231 void OfflinePageTabHelper::GetBestPageForRedirectToOffline( |
| 228 RedirectResult result, const GURL& online_url) { | 232 RedirectResult result, const GURL& online_url) { |
| 229 // 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 |
| 230 // page, so we can leave. | 234 // page, so we can leave. |
| 231 int tab_id; | 235 int tab_id; |
| 232 if (!delegate_->GetTabId(web_contents(), &tab_id)) { | 236 if (!delegate_->GetTabId(web_contents(), &tab_id)) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 if (!redirect_url.is_valid()) | 295 if (!redirect_url.is_valid()) |
| 292 return; | 296 return; |
| 293 | 297 |
| 294 if (IsInRedirectLoop(redirect_url)) { | 298 if (IsInRedirectLoop(redirect_url)) { |
| 295 ReportRedirectResultUMA(RedirectResult::REDIRECT_LOOP_OFFLINE); | 299 ReportRedirectResultUMA(RedirectResult::REDIRECT_LOOP_OFFLINE); |
| 296 return; | 300 return; |
| 297 } | 301 } |
| 298 | 302 |
| 299 Redirect(from_url, redirect_url); | 303 Redirect(from_url, redirect_url); |
| 300 offline_page_ = base::MakeUnique<OfflinePageItem>(offline_page); | 304 offline_page_ = base::MakeUnique<OfflinePageItem>(offline_page); |
| 305 is_offline_preview_ = |
| 306 (result == RedirectResult::REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK); |
| 301 ReportRedirectResultUMA(result); | 307 ReportRedirectResultUMA(result); |
| 302 } | 308 } |
| 303 | 309 |
| 304 void OfflinePageTabHelper::Redirect(const GURL& from_url, const GURL& to_url) { | 310 void OfflinePageTabHelper::Redirect(const GURL& from_url, const GURL& to_url) { |
| 305 content::NavigationController::LoadURLParams load_params(to_url); | 311 content::NavigationController::LoadURLParams load_params(to_url); |
| 306 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | 312 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; |
| 307 load_params.redirect_chain.push_back(from_url); | 313 load_params.redirect_chain.push_back(from_url); |
| 308 web_contents()->GetController().LoadURLWithParams(load_params); | 314 web_contents()->GetController().LoadURLWithParams(load_params); |
| 309 } | 315 } |
| 310 | 316 |
| 311 bool OfflinePageTabHelper::IsInRedirectLoop(const GURL& to_url) const { | 317 bool OfflinePageTabHelper::IsInRedirectLoop(const GURL& to_url) const { |
| 312 // Detects looping between online and offline redirections. | 318 // Detects looping between online and offline redirections. |
| 313 const content::NavigationController& controller = | 319 const content::NavigationController& controller = |
| 314 web_contents()->GetController(); | 320 web_contents()->GetController(); |
| 315 content::NavigationEntry* entry = controller.GetPendingEntry(); | 321 content::NavigationEntry* entry = controller.GetPendingEntry(); |
| 316 return entry && | 322 return entry && |
| 317 !entry->GetRedirectChain().empty() && | 323 !entry->GetRedirectChain().empty() && |
| 318 entry->GetRedirectChain().back() == to_url; | 324 entry->GetRedirectChain().back() == to_url; |
| 319 } | 325 } |
| 320 | 326 |
| 321 void OfflinePageTabHelper::ReportRedirectResultUMA(RedirectResult result) { | 327 void OfflinePageTabHelper::ReportRedirectResultUMA(RedirectResult result) { |
| 322 UMA_HISTOGRAM_ENUMERATION("OfflinePages.RedirectResult", | 328 UMA_HISTOGRAM_ENUMERATION("OfflinePages.RedirectResult", |
| 323 static_cast<int>(result), | 329 static_cast<int>(result), |
| 324 static_cast<int>(RedirectResult::REDIRECT_RESULT_MAX)); | 330 static_cast<int>(RedirectResult::REDIRECT_RESULT_MAX)); |
| 325 } | 331 } |
| 326 | 332 |
| 327 } // namespace offline_pages | 333 } // namespace offline_pages |
| OLD | NEW |