| 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/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 13 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 13 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 14 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 15 #include "components/offline_pages/client_namespace_constants.h" |
| 14 #include "components/offline_pages/offline_page_model.h" | 16 #include "components/offline_pages/offline_page_model.h" |
| 15 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
| 17 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
| 18 #include "content/public/browser/navigation_handle.h" | 20 #include "content/public/browser/navigation_handle.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 21 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 21 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 22 #include "net/base/network_change_notifier.h" | 24 #include "net/base/network_change_notifier.h" |
| 23 #include "ui/base/page_transition_types.h" | 25 #include "ui/base/page_transition_types.h" |
| 24 | 26 |
| 25 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper); | 27 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper); |
| 26 | 28 |
| 27 namespace offline_pages { | 29 namespace offline_pages { |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 void ReportAccessedOfflinePage(content::BrowserContext* browser_context, | 32 void ReportAccessedOfflinePage(content::BrowserContext* browser_context, |
| 31 const GURL& navigated_url, | 33 const GURL& navigated_url, |
| 32 const GURL& online_url) { | 34 const GURL& online_url) { |
| 33 // If there is a valid online URL for this navigated URL, then we are looking | 35 // If there is a valid online URL for this navigated URL, then we are looking |
| 34 // at an offline page. | 36 // at an offline page. |
| 35 if (online_url.is_valid()) | 37 if (online_url.is_valid()) |
| 36 OfflinePageUtils::MarkPageAccessed(browser_context, navigated_url); | 38 OfflinePageUtils::MarkPageAccessed(browser_context, navigated_url); |
| 37 } | 39 } |
| 38 | 40 |
| 41 class DefaultDelegate : public OfflinePageTabHelper::Delegate { |
| 42 public: |
| 43 DefaultDelegate() {} |
| 44 // offline_pages::OfflinePageTabHelper::Delegate implementation: |
| 45 bool GetTabId(content::WebContents* web_contents, |
| 46 std::string* tab_id) const override { |
| 47 int temp_tab_id; |
| 48 if (!OfflinePageUtils::GetTabId(web_contents, &temp_tab_id)) |
| 49 return false; |
| 50 *tab_id = base::IntToString(temp_tab_id); |
| 51 return true; |
| 52 } |
| 53 }; |
| 39 } // namespace | 54 } // namespace |
| 40 | 55 |
| 41 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) | 56 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) |
| 42 : content::WebContentsObserver(web_contents), | 57 : content::WebContentsObserver(web_contents), |
| 58 delegate_(new DefaultDelegate()), |
| 43 weak_ptr_factory_(this) { | 59 weak_ptr_factory_(this) { |
| 44 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 45 } | 61 } |
| 46 | 62 |
| 47 OfflinePageTabHelper::~OfflinePageTabHelper() {} | 63 OfflinePageTabHelper::~OfflinePageTabHelper() {} |
| 48 | 64 |
| 65 void OfflinePageTabHelper::SetDelegateForTesting( |
| 66 std::unique_ptr<OfflinePageTabHelper::Delegate> delegate) { |
| 67 DCHECK(delegate); |
| 68 delegate_ = std::move(delegate); |
| 69 } |
| 70 |
| 49 void OfflinePageTabHelper::DidStartNavigation( | 71 void OfflinePageTabHelper::DidStartNavigation( |
| 50 content::NavigationHandle* navigation_handle) { | 72 content::NavigationHandle* navigation_handle) { |
| 51 // Skips non-main frame. | 73 // Skips non-main frame. |
| 52 if (!navigation_handle->IsInMainFrame()) | 74 if (!navigation_handle->IsInMainFrame()) |
| 53 return; | 75 return; |
| 54 | 76 |
| 55 // This is a new navigation so we can invalidate any previously scheduled | 77 // This is a new navigation so we can invalidate any previously scheduled |
| 56 // operations. | 78 // operations. |
| 57 weak_ptr_factory_.InvalidateWeakPtrs(); | 79 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 58 | 80 |
| 59 // Since this is a new navigation, we will reset the cached offline page, | 81 // Since this is a new navigation, we will reset the cached offline page, |
| 60 // unless we are currently looking at an offline page. | 82 // unless we are currently looking at an offline page. |
| 61 GURL navigated_url = navigation_handle->GetURL(); | 83 GURL navigated_url = navigation_handle->GetURL(); |
| 62 if (offline_page_ && navigated_url != offline_page_->GetOfflineURL()) | 84 if (offline_page_ && navigated_url != offline_page_->GetOfflineURL()) |
| 63 offline_page_ = nullptr; | 85 offline_page_ = nullptr; |
| 64 | 86 |
| 65 // Ignore navigations that are forward or back transitions in the nav stack | 87 // Ignore navigations that are forward or back transitions in the nav stack |
| 66 // which are not at the head of the stack. | 88 // which are not at the head of the stack. |
| 67 const content::NavigationController& controller = | 89 const content::NavigationController& controller = |
| 68 web_contents()->GetController(); | 90 web_contents()->GetController(); |
| 69 if (controller.GetEntryCount() > 0 && | 91 if (controller.GetEntryCount() > 0 && |
| 70 controller.GetCurrentEntryIndex() != -1 && | 92 controller.GetCurrentEntryIndex() != -1 && |
| 71 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { | 93 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { |
| 72 return; | 94 return; |
| 73 } | 95 } |
| 74 | 96 |
| 75 content::BrowserContext* context = web_contents()->GetBrowserContext(); | 97 content::BrowserContext* context = web_contents()->GetBrowserContext(); |
| 98 if (net::NetworkChangeNotifier::IsOffline()) { |
| 99 GetPagesForRedirectToOffline(navigated_url, |
| 100 RedirectReason::DISCONNECTED_NETWORK); |
| 101 return; |
| 102 } |
| 103 |
| 76 OfflinePageModel* offline_page_model = | 104 OfflinePageModel* offline_page_model = |
| 77 OfflinePageModelFactory::GetForBrowserContext(context); | 105 OfflinePageModelFactory::GetForBrowserContext(context); |
| 78 if (!offline_page_model) | 106 if (!offline_page_model) |
| 79 return; | 107 return; |
| 80 | 108 |
| 81 if (net::NetworkChangeNotifier::IsOffline()) { | 109 offline_page_model->GetPageByOfflineURL( |
| 82 offline_page_model->GetBestPageForOnlineURL( | 110 navigated_url, base::Bind(&OfflinePageTabHelper::RedirectToOnline, |
| 83 navigated_url, | 111 weak_ptr_factory_.GetWeakPtr(), navigated_url)); |
| 84 base::Bind(&OfflinePageTabHelper::TryRedirectToOffline, | |
| 85 weak_ptr_factory_.GetWeakPtr(), | |
| 86 RedirectReason::DISCONNECTED_NETWORK, navigated_url)); | |
| 87 } else { | |
| 88 offline_page_model->GetPageByOfflineURL( | |
| 89 navigated_url, | |
| 90 base::Bind(&OfflinePageTabHelper::RedirectToOnline, | |
| 91 weak_ptr_factory_.GetWeakPtr(), navigated_url)); | |
| 92 } | |
| 93 } | 112 } |
| 94 | 113 |
| 95 void OfflinePageTabHelper::DidFinishNavigation( | 114 void OfflinePageTabHelper::DidFinishNavigation( |
| 96 content::NavigationHandle* navigation_handle) { | 115 content::NavigationHandle* navigation_handle) { |
| 97 // Skips non-main frame. | 116 // Skips non-main frame. |
| 98 if (!navigation_handle->IsInMainFrame()) | 117 if (!navigation_handle->IsInMainFrame()) |
| 99 return; | 118 return; |
| 100 | 119 |
| 101 GURL navigated_url = navigation_handle->GetURL(); | 120 GURL navigated_url = navigation_handle->GetURL(); |
| 102 net::Error error_code = navigation_handle->GetNetErrorCode(); | 121 net::Error error_code = navigation_handle->GetNetErrorCode(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 118 // navigation will eventually fail and we want to redirect to offline copy | 137 // navigation will eventually fail and we want to redirect to offline copy |
| 119 // in this case. If error code doesn't match this list, then we still show | 138 // in this case. If error code doesn't match this list, then we still show |
| 120 // the error page and not an offline page, so do nothing. | 139 // the error page and not an offline page, so do nothing. |
| 121 if (error_code != net::ERR_INTERNET_DISCONNECTED && | 140 if (error_code != net::ERR_INTERNET_DISCONNECTED && |
| 122 error_code != net::ERR_NAME_NOT_RESOLVED && | 141 error_code != net::ERR_NAME_NOT_RESOLVED && |
| 123 error_code != net::ERR_ADDRESS_UNREACHABLE && | 142 error_code != net::ERR_ADDRESS_UNREACHABLE && |
| 124 error_code != net::ERR_PROXY_CONNECTION_FAILED) { | 143 error_code != net::ERR_PROXY_CONNECTION_FAILED) { |
| 125 return; | 144 return; |
| 126 } | 145 } |
| 127 | 146 |
| 128 OfflinePageModel* offline_page_model = | |
| 129 OfflinePageModelFactory::GetForBrowserContext(browser_context); | |
| 130 if (!offline_page_model) | |
| 131 return; | |
| 132 | |
| 133 // Otherwise, get the offline URL for this url, and attempt a redirect if | 147 // Otherwise, get the offline URL for this url, and attempt a redirect if |
| 134 // necessary. | 148 // necessary. |
| 135 RedirectReason reason = | 149 RedirectReason reason = |
| 136 ui::PageTransitionTypeIncludingQualifiersIs( | 150 ui::PageTransitionTypeIncludingQualifiersIs( |
| 137 navigation_handle->GetPageTransition(), | 151 navigation_handle->GetPageTransition(), |
| 138 ui::PAGE_TRANSITION_FORWARD_BACK) | 152 ui::PAGE_TRANSITION_FORWARD_BACK) |
| 139 ? RedirectReason::FLAKY_NETWORK_FORWARD_BACK | 153 ? RedirectReason::FLAKY_NETWORK_FORWARD_BACK |
| 140 : RedirectReason::FLAKY_NETWORK; | 154 : RedirectReason::FLAKY_NETWORK; |
| 141 offline_page_model->GetBestPageForOnlineURL( | 155 GetPagesForRedirectToOffline(navigated_url, reason); |
| 142 navigated_url, | |
| 143 base::Bind(&OfflinePageTabHelper::TryRedirectToOffline, | |
| 144 weak_ptr_factory_.GetWeakPtr(), reason, navigated_url)); | |
| 145 } | 156 } |
| 146 | 157 |
| 147 void OfflinePageTabHelper::RedirectToOnline( | 158 void OfflinePageTabHelper::RedirectToOnline( |
| 148 const GURL& navigated_url, | 159 const GURL& navigated_url, |
| 149 const OfflinePageItem* offline_page) { | 160 const OfflinePageItem* offline_page) { |
| 150 // Bails out if no redirection is needed. | 161 // Bails out if no redirection is needed. |
| 151 if (!offline_page) | 162 if (!offline_page) |
| 152 return; | 163 return; |
| 153 | 164 |
| 154 GURL redirect_url = offline_page->url; | 165 GURL redirect_url = offline_page->url; |
| 155 | 166 |
| 156 const content::NavigationController& controller = | 167 const content::NavigationController& controller = |
| 157 web_contents()->GetController(); | 168 web_contents()->GetController(); |
| 158 | 169 |
| 159 // Avoids looping between online and offline redirections. | 170 // Avoids looping between online and offline redirections. |
| 160 content::NavigationEntry* entry = controller.GetPendingEntry(); | 171 content::NavigationEntry* entry = controller.GetPendingEntry(); |
| 161 if (entry && !entry->GetRedirectChain().empty() && | 172 if (entry && !entry->GetRedirectChain().empty() && |
| 162 entry->GetRedirectChain().back() == redirect_url) { | 173 entry->GetRedirectChain().back() == redirect_url) { |
| 163 return; | 174 return; |
| 164 } | 175 } |
| 165 | 176 |
| 166 Redirect(navigated_url, redirect_url); | 177 Redirect(navigated_url, redirect_url); |
| 167 // Clear the offline page since we are redirecting to online. | 178 // Clear the offline page since we are redirecting to online. |
| 168 offline_page_ = nullptr; | 179 offline_page_ = nullptr; |
| 169 | 180 |
| 170 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); | 181 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); |
| 171 } | 182 } |
| 172 | 183 |
| 184 void OfflinePageTabHelper::GetPagesForRedirectToOffline(const GURL& online_url, |
| 185 RedirectReason reason) { |
| 186 OfflinePageModel* offline_page_model = |
| 187 OfflinePageModelFactory::GetForBrowserContext( |
| 188 web_contents()->GetBrowserContext()); |
| 189 if (!offline_page_model) |
| 190 return; |
| 191 |
| 192 offline_page_model->GetPagesByOnlineURL( |
| 193 online_url, |
| 194 base::Bind(&OfflinePageTabHelper::SelectBestPageForRedirectToOffline, |
| 195 weak_ptr_factory_.GetWeakPtr(), online_url, reason)); |
| 196 } |
| 197 |
| 198 void OfflinePageTabHelper::SelectBestPageForRedirectToOffline( |
| 199 const GURL& online_url, |
| 200 RedirectReason reason, |
| 201 const MultipleOfflinePageItemResult& pages) { |
| 202 // When there is no valid tab android there is nowhere to show the offline |
| 203 // page, so we can leave. |
| 204 std::string tab_id; |
| 205 if (!delegate_->GetTabId(web_contents(), &tab_id)) |
| 206 return; |
| 207 |
| 208 const OfflinePageItem* selected_page = nullptr; |
| 209 for (const auto& offline_page : pages) { |
| 210 if ((offline_page.client_id.name_space == kBookmarkNamespace) || |
| 211 (offline_page.client_id.name_space == kLastNNamespace && |
| 212 offline_page.client_id.id == tab_id)) { |
| 213 if (!selected_page || |
| 214 offline_page.creation_time > selected_page->creation_time) { |
| 215 selected_page = &offline_page; |
| 216 } |
| 217 } |
| 218 } |
| 219 |
| 220 if (!selected_page) |
| 221 return; |
| 222 |
| 223 TryRedirectToOffline(reason, online_url, *selected_page); |
| 224 } |
| 225 |
| 173 void OfflinePageTabHelper::TryRedirectToOffline( | 226 void OfflinePageTabHelper::TryRedirectToOffline( |
| 174 RedirectReason redirect_reason, | 227 RedirectReason redirect_reason, |
| 175 const GURL& from_url, | 228 const GURL& from_url, |
| 176 const OfflinePageItem* offline_page) { | 229 const OfflinePageItem& offline_page) { |
| 177 if (!offline_page) | 230 GURL redirect_url = offline_page.GetOfflineURL(); |
| 178 return; | |
| 179 | |
| 180 GURL redirect_url = offline_page->GetOfflineURL(); | |
| 181 | |
| 182 if (!redirect_url.is_valid()) | 231 if (!redirect_url.is_valid()) |
| 183 return; | 232 return; |
| 184 | 233 |
| 185 if (redirect_reason == RedirectReason::FLAKY_NETWORK || | 234 if (redirect_reason == RedirectReason::FLAKY_NETWORK || |
| 186 redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) { | 235 redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) { |
| 187 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork", | 236 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork", |
| 188 redirect_reason == RedirectReason::FLAKY_NETWORK); | 237 redirect_reason == RedirectReason::FLAKY_NETWORK); |
| 189 // Don't actually want to redirect on a forward/back nav. | 238 // Don't actually want to redirect on a forward/back nav. |
| 190 if (redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) | 239 if (redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) |
| 191 return; | 240 return; |
| 192 } else { | 241 } else { |
| 193 const content::NavigationController& controller = | 242 const content::NavigationController& controller = |
| 194 web_contents()->GetController(); | 243 web_contents()->GetController(); |
| 195 | 244 |
| 196 // Avoids looping between online and offline redirections. | 245 // Avoids looping between online and offline redirections. |
| 197 content::NavigationEntry* entry = controller.GetPendingEntry(); | 246 content::NavigationEntry* entry = controller.GetPendingEntry(); |
| 198 if (entry && !entry->GetRedirectChain().empty() && | 247 if (entry && !entry->GetRedirectChain().empty() && |
| 199 entry->GetRedirectChain().back() == redirect_url) { | 248 entry->GetRedirectChain().back() == redirect_url) { |
| 200 return; | 249 return; |
| 201 } | 250 } |
| 202 } | 251 } |
| 203 | 252 |
| 204 Redirect(from_url, redirect_url); | 253 Redirect(from_url, redirect_url); |
| 205 offline_page_ = base::MakeUnique<OfflinePageItem>(*offline_page); | 254 offline_page_ = base::MakeUnique<OfflinePageItem>(offline_page); |
| 206 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); | 255 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); |
| 207 } | 256 } |
| 208 | 257 |
| 209 void OfflinePageTabHelper::Redirect(const GURL& from_url, const GURL& to_url) { | 258 void OfflinePageTabHelper::Redirect(const GURL& from_url, const GURL& to_url) { |
| 210 content::NavigationController::LoadURLParams load_params(to_url); | 259 content::NavigationController::LoadURLParams load_params(to_url); |
| 211 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | 260 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; |
| 212 load_params.redirect_chain.push_back(from_url); | 261 load_params.redirect_chain.push_back(from_url); |
| 213 web_contents()->GetController().LoadURLWithParams(load_params); | 262 web_contents()->GetController().LoadURLWithParams(load_params); |
| 214 } | 263 } |
| 215 | 264 |
| 216 } // namespace offline_pages | 265 } // namespace offline_pages |
| OLD | NEW |