| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 int* tab_id) const override { | 67 int* tab_id) const override { |
| 68 return OfflinePageUtils::GetTabId(web_contents, tab_id); | 68 return OfflinePageUtils::GetTabId(web_contents, tab_id); |
| 69 } | 69 } |
| 70 base::Time Now() const override { return base::Time::Now(); } | 70 base::Time Now() const override { return base::Time::Now(); } |
| 71 }; | 71 }; |
| 72 } // namespace | 72 } // namespace |
| 73 | 73 |
| 74 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) | 74 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) |
| 75 : content::WebContentsObserver(web_contents), | 75 : content::WebContentsObserver(web_contents), |
| 76 delegate_(new DefaultDelegate()), | 76 delegate_(new DefaultDelegate()), |
| 77 is_preview_(false), |
| 77 weak_ptr_factory_(this) { | 78 weak_ptr_factory_(this) { |
| 78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 79 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 79 } | 80 } |
| 80 | 81 |
| 81 OfflinePageTabHelper::~OfflinePageTabHelper() {} | 82 OfflinePageTabHelper::~OfflinePageTabHelper() {} |
| 82 | 83 |
| 83 void OfflinePageTabHelper::SetDelegateForTesting( | 84 void OfflinePageTabHelper::SetDelegateForTesting( |
| 84 std::unique_ptr<OfflinePageTabHelper::Delegate> delegate) { | 85 std::unique_ptr<OfflinePageTabHelper::Delegate> delegate) { |
| 85 DCHECK(delegate); | 86 DCHECK(delegate); |
| 86 delegate_ = std::move(delegate); | 87 delegate_ = std::move(delegate); |
| 87 } | 88 } |
| 88 | 89 |
| 89 void OfflinePageTabHelper::DidStartNavigation( | 90 void OfflinePageTabHelper::DidStartNavigation( |
| 90 content::NavigationHandle* navigation_handle) { | 91 content::NavigationHandle* navigation_handle) { |
| 91 // Skips non-main frame. | 92 // Skips non-main frame. |
| 92 if (!navigation_handle->IsInMainFrame()) | 93 if (!navigation_handle->IsInMainFrame()) |
| 93 return; | 94 return; |
| 94 | 95 |
| 95 // This is a new navigation so we can invalidate any previously scheduled | 96 // This is a new navigation so we can invalidate any previously scheduled |
| 96 // operations. | 97 // operations. |
| 97 weak_ptr_factory_.InvalidateWeakPtrs(); | 98 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 98 | 99 |
| 99 // Since this is a new navigation, we will reset the cached offline page, | 100 // Since this is a new navigation, we will reset the cached offline page, |
| 100 // unless we are currently looking at an offline page. | 101 // unless we are currently looking at an offline page. |
| 101 GURL navigated_url = navigation_handle->GetURL(); | 102 GURL navigated_url = navigation_handle->GetURL(); |
| 102 if (offline_page_ && navigated_url != offline_page_->GetOfflineURL()) | 103 if (offline_page_ && navigated_url != offline_page_->GetOfflineURL()) { |
| 103 offline_page_ = nullptr; | 104 offline_page_ = nullptr; |
| 105 is_preview_ = false; |
| 106 } |
| 104 | 107 |
| 105 // Ignore navigations that are forward or back transitions in the nav stack | 108 // Ignore navigations that are forward or back transitions in the nav stack |
| 106 // which are not at the head of the stack. | 109 // which are not at the head of the stack. |
| 107 // TODO(dimich): Not sure this is needed. Clarify and remove. Bug 624216. | 110 // TODO(dimich): Not sure this is needed. Clarify and remove. Bug 624216. |
| 108 const content::NavigationController& controller = | 111 const content::NavigationController& controller = |
| 109 web_contents()->GetController(); | 112 web_contents()->GetController(); |
| 110 if (controller.GetEntryCount() > 0 && | 113 if (controller.GetEntryCount() > 0 && |
| 111 controller.GetCurrentEntryIndex() != -1 && | 114 controller.GetCurrentEntryIndex() != -1 && |
| 112 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { | 115 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { |
| 113 return; | 116 return; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 197 |
| 195 GURL redirect_url = offline_page->url; | 198 GURL redirect_url = offline_page->url; |
| 196 if (IsInRedirectLoop(redirect_url)) { | 199 if (IsInRedirectLoop(redirect_url)) { |
| 197 ReportRedirectResultUMA(RedirectResult::REDIRECT_LOOP_ONLINE); | 200 ReportRedirectResultUMA(RedirectResult::REDIRECT_LOOP_ONLINE); |
| 198 return; | 201 return; |
| 199 } | 202 } |
| 200 | 203 |
| 201 Redirect(navigated_url, redirect_url); | 204 Redirect(navigated_url, redirect_url); |
| 202 // Clear the offline page since we are redirecting to online. | 205 // Clear the offline page since we are redirecting to online. |
| 203 offline_page_ = nullptr; | 206 offline_page_ = nullptr; |
| 207 is_preview_ = false; |
| 204 | 208 |
| 205 ReportRedirectResultUMA(RedirectResult::REDIRECTED_ON_CONNECTED_NETWORK); | 209 ReportRedirectResultUMA(RedirectResult::REDIRECTED_ON_CONNECTED_NETWORK); |
| 206 } | 210 } |
| 207 | 211 |
| 208 void OfflinePageTabHelper::GetBestPageForRedirectToOffline( | 212 void OfflinePageTabHelper::GetBestPageForRedirectToOffline( |
| 209 RedirectResult result, const GURL& online_url) { | 213 RedirectResult result, const GURL& online_url) { |
| 210 // When there is no valid tab android there is nowhere to show the offline | 214 // When there is no valid tab android there is nowhere to show the offline |
| 211 // page, so we can leave. | 215 // page, so we can leave. |
| 212 int tab_id; | 216 int tab_id; |
| 213 if (!delegate_->GetTabId(web_contents(), &tab_id)) { | 217 if (!delegate_->GetTabId(web_contents(), &tab_id)) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 if (!redirect_url.is_valid()) | 276 if (!redirect_url.is_valid()) |
| 273 return; | 277 return; |
| 274 | 278 |
| 275 if (IsInRedirectLoop(redirect_url)) { | 279 if (IsInRedirectLoop(redirect_url)) { |
| 276 ReportRedirectResultUMA(RedirectResult::REDIRECT_LOOP_OFFLINE); | 280 ReportRedirectResultUMA(RedirectResult::REDIRECT_LOOP_OFFLINE); |
| 277 return; | 281 return; |
| 278 } | 282 } |
| 279 | 283 |
| 280 Redirect(from_url, redirect_url); | 284 Redirect(from_url, redirect_url); |
| 281 offline_page_ = base::MakeUnique<OfflinePageItem>(offline_page); | 285 offline_page_ = base::MakeUnique<OfflinePageItem>(offline_page); |
| 286 is_preview_ = |
| 287 (result == RedirectResult::REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK); |
| 282 ReportRedirectResultUMA(result); | 288 ReportRedirectResultUMA(result); |
| 283 } | 289 } |
| 284 | 290 |
| 285 void OfflinePageTabHelper::Redirect(const GURL& from_url, const GURL& to_url) { | 291 void OfflinePageTabHelper::Redirect(const GURL& from_url, const GURL& to_url) { |
| 286 content::NavigationController::LoadURLParams load_params(to_url); | 292 content::NavigationController::LoadURLParams load_params(to_url); |
| 287 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | 293 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; |
| 288 load_params.redirect_chain.push_back(from_url); | 294 load_params.redirect_chain.push_back(from_url); |
| 289 web_contents()->GetController().LoadURLWithParams(load_params); | 295 web_contents()->GetController().LoadURLWithParams(load_params); |
| 290 } | 296 } |
| 291 | 297 |
| 292 bool OfflinePageTabHelper::IsInRedirectLoop(const GURL& to_url) const { | 298 bool OfflinePageTabHelper::IsInRedirectLoop(const GURL& to_url) const { |
| 293 // Detects looping between online and offline redirections. | 299 // Detects looping between online and offline redirections. |
| 294 const content::NavigationController& controller = | 300 const content::NavigationController& controller = |
| 295 web_contents()->GetController(); | 301 web_contents()->GetController(); |
| 296 content::NavigationEntry* entry = controller.GetPendingEntry(); | 302 content::NavigationEntry* entry = controller.GetPendingEntry(); |
| 297 return entry && | 303 return entry && |
| 298 !entry->GetRedirectChain().empty() && | 304 !entry->GetRedirectChain().empty() && |
| 299 entry->GetRedirectChain().back() == to_url; | 305 entry->GetRedirectChain().back() == to_url; |
| 300 } | 306 } |
| 301 | 307 |
| 302 void OfflinePageTabHelper::ReportRedirectResultUMA(RedirectResult result) { | 308 void OfflinePageTabHelper::ReportRedirectResultUMA(RedirectResult result) { |
| 303 UMA_HISTOGRAM_ENUMERATION("OfflinePages.RedirectResult", | 309 UMA_HISTOGRAM_ENUMERATION("OfflinePages.RedirectResult", |
| 304 static_cast<int>(result), | 310 static_cast<int>(result), |
| 305 static_cast<int>(RedirectResult::REDIRECT_RESULT_MAX)); | 311 static_cast<int>(RedirectResult::REDIRECT_RESULT_MAX)); |
| 306 } | 312 } |
| 307 | 313 |
| 308 } // namespace offline_pages | 314 } // namespace offline_pages |
| OLD | NEW |