Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: chrome/browser/android/offline_pages/offline_page_tab_helper.cc

Issue 2040163003: Refactors offline page tab helper to stash the offline page item on redirect. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@single-result
Patch Set: fix nit. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
10 #include "base/threading/thread_task_runner_handle.h" 11 #include "base/threading/thread_task_runner_handle.h"
12 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
11 #include "chrome/browser/android/offline_pages/offline_page_utils.h" 13 #include "chrome/browser/android/offline_pages/offline_page_utils.h"
14 #include "components/offline_pages/offline_page_model.h"
12 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/navigation_controller.h" 16 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/navigation_entry.h" 17 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/navigation_handle.h" 18 #include "content/public/browser/navigation_handle.h"
16 #include "content/public/browser/render_frame_host.h" 19 #include "content/public/browser/render_frame_host.h"
17 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
18 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
19 #include "net/base/network_change_notifier.h" 22 #include "net/base/network_change_notifier.h"
20 #include "ui/base/page_transition_types.h" 23 #include "ui/base/page_transition_types.h"
21 24
(...skipping 24 matching lines...) Expand all
46 void OfflinePageTabHelper::DidStartNavigation( 49 void OfflinePageTabHelper::DidStartNavigation(
47 content::NavigationHandle* navigation_handle) { 50 content::NavigationHandle* navigation_handle) {
48 // Skips non-main frame. 51 // Skips non-main frame.
49 if (!navigation_handle->IsInMainFrame()) 52 if (!navigation_handle->IsInMainFrame())
50 return; 53 return;
51 54
52 // This is a new navigation so we can invalidate any previously scheduled 55 // This is a new navigation so we can invalidate any previously scheduled
53 // operations. 56 // operations.
54 weak_ptr_factory_.InvalidateWeakPtrs(); 57 weak_ptr_factory_.InvalidateWeakPtrs();
55 58
59 // Since this is a new navigation, we will reset the cached offline page,
60 // unless we are currently looking at an offline page.
61 GURL navigated_url = navigation_handle->GetURL();
62 if (offline_page_ && navigated_url != offline_page_->GetOfflineURL())
63 offline_page_ = nullptr;
64
56 // Ignore navigations that are forward or back transitions in the nav stack 65 // Ignore navigations that are forward or back transitions in the nav stack
57 // which are not at the head of the stack. 66 // which are not at the head of the stack.
58 const content::NavigationController& controller = 67 const content::NavigationController& controller =
59 web_contents()->GetController(); 68 web_contents()->GetController();
60 if (controller.GetEntryCount() > 0 && 69 if (controller.GetEntryCount() > 0 &&
61 controller.GetCurrentEntryIndex() != -1 && 70 controller.GetCurrentEntryIndex() != -1 &&
62 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { 71 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) {
63 return; 72 return;
64 } 73 }
65 74
66 content::BrowserContext* context = web_contents()->GetBrowserContext(); 75 content::BrowserContext* context = web_contents()->GetBrowserContext();
67 GURL navigated_url = navigation_handle->GetURL(); 76 OfflinePageModel* offline_page_model =
68 auto redirect_url_callback = 77 OfflinePageModelFactory::GetForBrowserContext(context);
69 base::Bind(&OfflinePageTabHelper::GotRedirectURLForStartedNavigation, 78 if (!offline_page_model)
70 weak_ptr_factory_.GetWeakPtr(), navigated_url); 79 return;
80
71 if (net::NetworkChangeNotifier::IsOffline()) { 81 if (net::NetworkChangeNotifier::IsOffline()) {
72 OfflinePageUtils::GetOfflineURLForOnlineURL(context, navigated_url, 82 offline_page_model->GetBestPageForOnlineURL(
73 redirect_url_callback); 83 navigated_url,
84 base::Bind(&OfflinePageTabHelper::TryRedirectToOffline,
85 weak_ptr_factory_.GetWeakPtr(),
86 RedirectReason::DISCONNECTED_NETWORK, navigated_url));
74 } else { 87 } else {
75 OfflinePageUtils::GetOnlineURLForOfflineURL(context, navigated_url, 88 offline_page_model->GetPageByOfflineURL(
76 redirect_url_callback); 89 navigated_url,
90 base::Bind(&OfflinePageTabHelper::RedirectToOnline,
91 weak_ptr_factory_.GetWeakPtr(), navigated_url));
77 } 92 }
78 } 93 }
79 94
80 void OfflinePageTabHelper::DidFinishNavigation( 95 void OfflinePageTabHelper::DidFinishNavigation(
81 content::NavigationHandle* navigation_handle) { 96 content::NavigationHandle* navigation_handle) {
82 // Skips non-main frame. 97 // Skips non-main frame.
83 if (!navigation_handle->IsInMainFrame()) 98 if (!navigation_handle->IsInMainFrame())
84 return; 99 return;
85 100
86 GURL navigated_url = navigation_handle->GetURL(); 101 GURL navigated_url = navigation_handle->GetURL();
(...skipping 16 matching lines...) Expand all
103 // navigation will eventually fail and we want to redirect to offline copy 118 // 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 119 // 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. 120 // the error page and not an offline page, so do nothing.
106 if (error_code != net::ERR_INTERNET_DISCONNECTED && 121 if (error_code != net::ERR_INTERNET_DISCONNECTED &&
107 error_code != net::ERR_NAME_NOT_RESOLVED && 122 error_code != net::ERR_NAME_NOT_RESOLVED &&
108 error_code != net::ERR_ADDRESS_UNREACHABLE && 123 error_code != net::ERR_ADDRESS_UNREACHABLE &&
109 error_code != net::ERR_PROXY_CONNECTION_FAILED) { 124 error_code != net::ERR_PROXY_CONNECTION_FAILED) {
110 return; 125 return;
111 } 126 }
112 127
128 OfflinePageModel* offline_page_model =
129 OfflinePageModelFactory::GetForBrowserContext(browser_context);
130 if (!offline_page_model)
131 return;
132
113 // Otherwise, get the offline URL for this url, and attempt a redirect if 133 // Otherwise, get the offline URL for this url, and attempt a redirect if
114 // necessary. 134 // necessary.
115 OfflinePageUtils::GetOfflineURLForOnlineURL( 135 RedirectReason reason =
116 browser_context, navigated_url, 136 navigation_handle->GetPageTransition() == ui::PAGE_TRANSITION_FORWARD_BACK
117 base::Bind(&OfflinePageTabHelper::GotRedirectURLForSupportedErrorCode, 137 ? RedirectReason::FLAKY_NETWORK_FORWARD_BACK
118 weak_ptr_factory_.GetWeakPtr(), 138 : RedirectReason::FLAKY_NETWORK;
119 navigation_handle->GetPageTransition(), navigated_url)); 139 offline_page_model->GetBestPageForOnlineURL(
140 navigated_url,
141 base::Bind(&OfflinePageTabHelper::TryRedirectToOffline,
142 weak_ptr_factory_.GetWeakPtr(), reason, navigated_url));
120 } 143 }
121 144
122 void OfflinePageTabHelper::GotRedirectURLForSupportedErrorCode( 145 void OfflinePageTabHelper::RedirectToOnline(
123 ui::PageTransition transition, 146 const GURL& navigated_url,
124 const GURL& from_url, 147 const OfflinePageItem* offline_page) {
125 const GURL& redirect_url) { 148 // Bails out if no redirection is needed.
126 // If we didn't find an offline URL, or we are doing a forward/back 149 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; 150 return;
134 151
135 base::ThreadTaskRunnerHandle::Get()->PostTask( 152 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 153
148 const content::NavigationController& controller = 154 const content::NavigationController& controller =
149 web_contents()->GetController(); 155 web_contents()->GetController();
150 156
151 // Avoids looping between online and offline redirections. 157 // Avoids looping between online and offline redirections.
152 content::NavigationEntry* entry = controller.GetPendingEntry(); 158 content::NavigationEntry* entry = controller.GetPendingEntry();
153 if (entry && !entry->GetRedirectChain().empty() && 159 if (entry && !entry->GetRedirectChain().empty() &&
154 entry->GetRedirectChain().back() == redirect_url) { 160 entry->GetRedirectChain().back() == redirect_url) {
155 return; 161 return;
156 } 162 }
157 163
158 base::ThreadTaskRunnerHandle::Get()->PostTask( 164 Redirect(navigated_url, redirect_url);
159 FROM_HERE, 165 // Clear the offline page since we are redirecting to online.
160 base::Bind(&OfflinePageTabHelper::Redirect, 166 offline_page_ = nullptr;
161 weak_ptr_factory_.GetWeakPtr(), from_url, redirect_url)); 167
168 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1);
162 } 169 }
163 170
164 void OfflinePageTabHelper::Redirect( 171 void OfflinePageTabHelper::TryRedirectToOffline(
165 const GURL& from_url, const GURL& to_url) { 172 RedirectReason redirect_reason,
166 if (to_url.SchemeIsFile()) { 173 const GURL& from_url,
167 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); 174 const OfflinePageItem* offline_page) {
175 if (!offline_page)
176 return;
177
178 GURL redirect_url = offline_page->GetOfflineURL();
179
180 if (!redirect_url.is_valid())
181 return;
182
183 if (redirect_reason == RedirectReason::FLAKY_NETWORK ||
184 redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) {
185 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork",
186 redirect_reason == RedirectReason::FLAKY_NETWORK);
187 // Don't actually want to redirect on a forward/back nav.
188 if (redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK)
189 return;
168 } else { 190 } else {
169 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); 191 const content::NavigationController& controller =
192 web_contents()->GetController();
193
194 // Avoids looping between online and offline redirections.
195 content::NavigationEntry* entry = controller.GetPendingEntry();
196 if (entry && !entry->GetRedirectChain().empty() &&
197 entry->GetRedirectChain().back() == redirect_url) {
198 return;
199 }
170 } 200 }
171 201
202 Redirect(from_url, redirect_url);
203 offline_page_ = base::MakeUnique<OfflinePageItem>(*offline_page);
204 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1);
205 }
206
207 void OfflinePageTabHelper::Redirect(const GURL& from_url, const GURL& to_url) {
172 content::NavigationController::LoadURLParams load_params(to_url); 208 content::NavigationController::LoadURLParams load_params(to_url);
173 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; 209 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT;
174 load_params.redirect_chain.push_back(from_url); 210 load_params.redirect_chain.push_back(from_url);
175 web_contents()->GetController().LoadURLWithParams(load_params); 211 web_contents()->GetController().LoadURLWithParams(load_params);
176 } 212 }
177 213
178 } // namespace offline_pages 214 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698