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/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | |
| 11 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 12 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 13 #include "components/offline_pages/offline_page_model.h" | |
| 12 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/navigation_controller.h" | 15 #include "content/public/browser/navigation_controller.h" |
| 14 #include "content/public/browser/navigation_entry.h" | 16 #include "content/public/browser/navigation_entry.h" |
| 15 #include "content/public/browser/navigation_handle.h" | 17 #include "content/public/browser/navigation_handle.h" |
| 16 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
| 17 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 18 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 19 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
| 20 #include "ui/base/page_transition_types.h" | 22 #include "ui/base/page_transition_types.h" |
| 21 | 23 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 45 | 47 |
| 46 void OfflinePageTabHelper::DidStartNavigation( | 48 void OfflinePageTabHelper::DidStartNavigation( |
| 47 content::NavigationHandle* navigation_handle) { | 49 content::NavigationHandle* navigation_handle) { |
| 48 // Skips non-main frame. | 50 // Skips non-main frame. |
| 49 if (!navigation_handle->IsInMainFrame()) | 51 if (!navigation_handle->IsInMainFrame()) |
| 50 return; | 52 return; |
| 51 | 53 |
| 52 // This is a new navigation so we can invalidate any previously scheduled | 54 // This is a new navigation so we can invalidate any previously scheduled |
| 53 // operations. | 55 // operations. |
| 54 weak_ptr_factory_.InvalidateWeakPtrs(); | 56 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 57 OfflinePageUtils::SetCurrentOfflinePage(web_contents(), nullptr); | |
| 55 | 58 |
| 56 // Ignore navigations that are forward or back transitions in the nav stack | 59 // Ignore navigations that are forward or back transitions in the nav stack |
| 57 // which are not at the head of the stack. | 60 // which are not at the head of the stack. |
| 58 const content::NavigationController& controller = | 61 const content::NavigationController& controller = |
| 59 web_contents()->GetController(); | 62 web_contents()->GetController(); |
| 60 if (controller.GetEntryCount() > 0 && | 63 if (controller.GetEntryCount() > 0 && |
| 61 controller.GetCurrentEntryIndex() != -1 && | 64 controller.GetCurrentEntryIndex() != -1 && |
| 62 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { | 65 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { |
| 63 return; | 66 return; |
| 64 } | 67 } |
| 65 | 68 |
| 66 content::BrowserContext* context = web_contents()->GetBrowserContext(); | 69 content::BrowserContext* context = web_contents()->GetBrowserContext(); |
| 70 OfflinePageModel* offline_page_model = | |
| 71 OfflinePageModelFactory::GetForBrowserContext(context); | |
| 72 if (!offline_page_model) | |
| 73 return; | |
| 74 | |
| 67 GURL navigated_url = navigation_handle->GetURL(); | 75 GURL navigated_url = navigation_handle->GetURL(); |
| 68 auto redirect_url_callback = | 76 |
| 69 base::Bind(&OfflinePageTabHelper::GotRedirectURLForStartedNavigation, | |
| 70 weak_ptr_factory_.GetWeakPtr(), navigated_url); | |
| 71 if (net::NetworkChangeNotifier::IsOffline()) { | 77 if (net::NetworkChangeNotifier::IsOffline()) { |
| 72 OfflinePageUtils::GetOfflineURLForOnlineURL(context, navigated_url, | 78 offline_page_model->GetBestPageForOnlineURL( |
| 73 redirect_url_callback); | 79 navigated_url, |
| 80 base::Bind(&OfflinePageTabHelper::TryRedirectToOffline, | |
| 81 weak_ptr_factory_.GetWeakPtr(), | |
| 82 RedirectReason::DISCONNECTED_NETWORK, navigated_url)); | |
| 74 } else { | 83 } else { |
| 75 OfflinePageUtils::GetOnlineURLForOfflineURL(context, navigated_url, | 84 offline_page_model->GetPageByOfflineURL( |
| 76 redirect_url_callback); | 85 navigated_url, |
| 86 base::Bind(&OfflinePageTabHelper::RedirectToOnline, | |
| 87 weak_ptr_factory_.GetWeakPtr(), navigated_url)); | |
| 77 } | 88 } |
| 78 } | 89 } |
| 79 | 90 |
| 80 void OfflinePageTabHelper::DidFinishNavigation( | 91 void OfflinePageTabHelper::DidFinishNavigation( |
| 81 content::NavigationHandle* navigation_handle) { | 92 content::NavigationHandle* navigation_handle) { |
| 82 // Skips non-main frame. | 93 // Skips non-main frame. |
| 83 if (!navigation_handle->IsInMainFrame()) | 94 if (!navigation_handle->IsInMainFrame()) |
| 84 return; | 95 return; |
| 85 | 96 |
| 86 GURL navigated_url = navigation_handle->GetURL(); | 97 GURL navigated_url = navigation_handle->GetURL(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 103 // navigation will eventually fail and we want to redirect to offline copy | 114 // navigation will eventually fail and we want to redirect to offline copy |
| 104 // in this case. If error code doesn't match this list, then we still show | 115 // in this case. If error code doesn't match this list, then we still show |
| 105 // the error page and not an offline page, so do nothing. | 116 // the error page and not an offline page, so do nothing. |
| 106 if (error_code != net::ERR_INTERNET_DISCONNECTED && | 117 if (error_code != net::ERR_INTERNET_DISCONNECTED && |
| 107 error_code != net::ERR_NAME_NOT_RESOLVED && | 118 error_code != net::ERR_NAME_NOT_RESOLVED && |
| 108 error_code != net::ERR_ADDRESS_UNREACHABLE && | 119 error_code != net::ERR_ADDRESS_UNREACHABLE && |
| 109 error_code != net::ERR_PROXY_CONNECTION_FAILED) { | 120 error_code != net::ERR_PROXY_CONNECTION_FAILED) { |
| 110 return; | 121 return; |
| 111 } | 122 } |
| 112 | 123 |
| 124 OfflinePageModel* offline_page_model = | |
| 125 OfflinePageModelFactory::GetForBrowserContext(browser_context); | |
| 126 if (!offline_page_model) | |
| 127 return; | |
| 128 | |
| 113 // Otherwise, get the offline URL for this url, and attempt a redirect if | 129 // Otherwise, get the offline URL for this url, and attempt a redirect if |
| 114 // necessary. | 130 // necessary. |
| 115 OfflinePageUtils::GetOfflineURLForOnlineURL( | 131 RedirectReason reason = |
| 116 browser_context, navigated_url, | 132 navigation_handle->GetPageTransition() == ui::PAGE_TRANSITION_FORWARD_BACK |
| 117 base::Bind(&OfflinePageTabHelper::GotRedirectURLForSupportedErrorCode, | 133 ? RedirectReason::FLAKY_NETWORK_FORWARD_BACK |
| 118 weak_ptr_factory_.GetWeakPtr(), | 134 : RedirectReason::FLAKY_NETWORK; |
| 119 navigation_handle->GetPageTransition(), navigated_url)); | 135 offline_page_model->GetBestPageForOnlineURL( |
| 136 navigated_url, | |
| 137 base::Bind(&OfflinePageTabHelper::TryRedirectToOffline, | |
| 138 weak_ptr_factory_.GetWeakPtr(), reason, navigated_url)); | |
| 120 } | 139 } |
| 121 | 140 |
| 122 void OfflinePageTabHelper::GotRedirectURLForSupportedErrorCode( | 141 void OfflinePageTabHelper::RedirectToOnline( |
| 123 ui::PageTransition transition, | 142 const GURL& navigated_url, |
| 124 const GURL& from_url, | 143 const OfflinePageItem* offline_page) { |
| 125 const GURL& redirect_url) { | 144 // Bails out if no redirection is needed. |
| 126 // If we didn't find an offline URL, or we are doing a forward/back | 145 if (!offline_page) |
| 127 // transition, don't redirect. | |
| 128 bool do_redirect = | |
| 129 redirect_url.is_valid() && transition != ui::PAGE_TRANSITION_FORWARD_BACK; | |
| 130 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork", | |
| 131 do_redirect); | |
| 132 if (!do_redirect) | |
| 133 return; | 146 return; |
| 134 | 147 |
| 135 base::ThreadTaskRunnerHandle::Get()->PostTask( | 148 GURL redirect_url = offline_page->url; |
| 136 FROM_HERE, | |
| 137 base::Bind(&OfflinePageTabHelper::Redirect, | |
| 138 weak_ptr_factory_.GetWeakPtr(), from_url, redirect_url)); | |
| 139 } | |
| 140 | |
| 141 void OfflinePageTabHelper::GotRedirectURLForStartedNavigation( | |
| 142 const GURL& from_url, | |
| 143 const GURL& redirect_url) { | |
| 144 // Bails out if no redirection is needed. | |
| 145 if (!redirect_url.is_valid()) | |
| 146 return; | |
| 147 | 149 |
| 148 const content::NavigationController& controller = | 150 const content::NavigationController& controller = |
| 149 web_contents()->GetController(); | 151 web_contents()->GetController(); |
| 150 | 152 |
| 151 // Avoids looping between online and offline redirections. | 153 // Avoids looping between online and offline redirections. |
| 152 content::NavigationEntry* entry = controller.GetPendingEntry(); | 154 content::NavigationEntry* entry = controller.GetPendingEntry(); |
| 153 if (entry && !entry->GetRedirectChain().empty() && | 155 if (entry && !entry->GetRedirectChain().empty() && |
| 154 entry->GetRedirectChain().back() == redirect_url) { | 156 entry->GetRedirectChain().back() == redirect_url) { |
| 155 return; | 157 return; |
| 156 } | 158 } |
| 157 | 159 |
| 158 base::ThreadTaskRunnerHandle::Get()->PostTask( | 160 Redirect(navigated_url, redirect_url, nullptr); |
| 159 FROM_HERE, | |
| 160 base::Bind(&OfflinePageTabHelper::Redirect, | |
| 161 weak_ptr_factory_.GetWeakPtr(), from_url, redirect_url)); | |
| 162 } | 161 } |
| 163 | 162 |
| 164 void OfflinePageTabHelper::Redirect( | 163 void OfflinePageTabHelper::TryRedirectToOffline( |
| 165 const GURL& from_url, const GURL& to_url) { | 164 RedirectReason redirect_reason, |
| 165 const GURL& from_url, | |
| 166 const OfflinePageItem* offline_page) { | |
| 167 if (!offline_page) | |
| 168 return; | |
| 169 | |
| 170 GURL redirect_url = offline_page->GetOfflineURL(); | |
| 171 | |
| 172 if (!redirect_url.is_valid()) | |
| 173 return; | |
| 174 | |
| 175 if (redirect_reason == RedirectReason::FLAKY_NETWORK || | |
| 176 redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) { | |
| 177 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork", | |
| 178 redirect_reason == RedirectReason::FLAKY_NETWORK); | |
| 179 // Don't actually want to redirect on a forward/back nav. | |
| 180 if (redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) | |
| 181 return; | |
| 182 } else { | |
| 183 const content::NavigationController& controller = | |
| 184 web_contents()->GetController(); | |
| 185 | |
| 186 // Avoids looping between online and offline redirections. | |
| 187 content::NavigationEntry* entry = controller.GetPendingEntry(); | |
| 188 if (entry && !entry->GetRedirectChain().empty() && | |
| 189 entry->GetRedirectChain().back() == redirect_url) { | |
| 190 return; | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 Redirect(from_url, redirect_url, std::move(offline_page)); | |
|
jianli
2016/06/14 00:38:44
Do we need std::move?
dewittj
2016/06/14 19:43:26
Done.
| |
| 195 } | |
| 196 | |
| 197 void OfflinePageTabHelper::Redirect(const GURL& from_url, | |
| 198 const GURL& to_url, | |
| 199 const OfflinePageItem* offline_page) { | |
| 166 if (to_url.SchemeIsFile()) { | 200 if (to_url.SchemeIsFile()) { |
| 167 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); | 201 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); |
| 168 } else { | 202 } else { |
| 169 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); | 203 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); |
| 170 } | 204 } |
| 171 | 205 |
| 172 content::NavigationController::LoadURLParams load_params(to_url); | 206 content::NavigationController::LoadURLParams load_params(to_url); |
| 173 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | 207 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; |
| 174 load_params.redirect_chain.push_back(from_url); | 208 load_params.redirect_chain.push_back(from_url); |
| 175 web_contents()->GetController().LoadURLWithParams(load_params); | 209 web_contents()->GetController().LoadURLWithParams(load_params); |
| 210 | |
| 211 // We will always set the offline page if we redirect. Note that this will | |
| 212 // clear the offline page if we redirect to online. | |
| 213 OfflinePageUtils::SetCurrentOfflinePage(web_contents(), offline_page); | |
| 176 } | 214 } |
| 177 | 215 |
| 178 } // namespace offline_pages | 216 } // namespace offline_pages |
| OLD | NEW |