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

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

Issue 2100043004: [Offline pages] Filter out pages cached by different tabs on tab helper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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/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 "chrome/browser/android/tab_android.h"
16 #include "components/offline_pages/client_namespace_constants.h"
14 #include "components/offline_pages/offline_page_model.h" 17 #include "components/offline_pages/offline_page_model.h"
15 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/navigation_controller.h" 19 #include "content/public/browser/navigation_controller.h"
17 #include "content/public/browser/navigation_entry.h" 20 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/navigation_handle.h" 21 #include "content/public/browser/navigation_handle.h"
19 #include "content/public/browser/render_frame_host.h" 22 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
21 #include "net/base/net_errors.h" 24 #include "net/base/net_errors.h"
22 #include "net/base/network_change_notifier.h" 25 #include "net/base/network_change_notifier.h"
23 #include "ui/base/page_transition_types.h" 26 #include "ui/base/page_transition_types.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 // which are not at the head of the stack. 69 // which are not at the head of the stack.
67 const content::NavigationController& controller = 70 const content::NavigationController& controller =
68 web_contents()->GetController(); 71 web_contents()->GetController();
69 if (controller.GetEntryCount() > 0 && 72 if (controller.GetEntryCount() > 0 &&
70 controller.GetCurrentEntryIndex() != -1 && 73 controller.GetCurrentEntryIndex() != -1 &&
71 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { 74 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) {
72 return; 75 return;
73 } 76 }
74 77
75 content::BrowserContext* context = web_contents()->GetBrowserContext(); 78 content::BrowserContext* context = web_contents()->GetBrowserContext();
79 if (net::NetworkChangeNotifier::IsOffline()) {
80 GetBestPageAndTryRedirect(navigated_url,
81 RedirectReason::DISCONNECTED_NETWORK);
82 return;
83 }
84
76 OfflinePageModel* offline_page_model = 85 OfflinePageModel* offline_page_model =
77 OfflinePageModelFactory::GetForBrowserContext(context); 86 OfflinePageModelFactory::GetForBrowserContext(context);
78 if (!offline_page_model) 87 if (!offline_page_model)
79 return; 88 return;
80 89
81 if (net::NetworkChangeNotifier::IsOffline()) { 90 offline_page_model->GetPageByOfflineURL(
82 offline_page_model->GetBestPageForOnlineURL( 91 navigated_url,
83 navigated_url, 92 base::Bind(&OfflinePageTabHelper::RedirectToOnline,
84 base::Bind(&OfflinePageTabHelper::TryRedirectToOffline, 93 weak_ptr_factory_.GetWeakPtr(), navigated_url));
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 } 94 }
94 95
95 void OfflinePageTabHelper::DidFinishNavigation( 96 void OfflinePageTabHelper::DidFinishNavigation(
96 content::NavigationHandle* navigation_handle) { 97 content::NavigationHandle* navigation_handle) {
97 // Skips non-main frame. 98 // Skips non-main frame.
98 if (!navigation_handle->IsInMainFrame()) 99 if (!navigation_handle->IsInMainFrame())
99 return; 100 return;
100 101
101 GURL navigated_url = navigation_handle->GetURL(); 102 GURL navigated_url = navigation_handle->GetURL();
102 net::Error error_code = navigation_handle->GetNetErrorCode(); 103 net::Error error_code = navigation_handle->GetNetErrorCode();
(...skipping 15 matching lines...) Expand all
118 // navigation will eventually fail and we want to redirect to offline copy 119 // 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 120 // 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. 121 // the error page and not an offline page, so do nothing.
121 if (error_code != net::ERR_INTERNET_DISCONNECTED && 122 if (error_code != net::ERR_INTERNET_DISCONNECTED &&
122 error_code != net::ERR_NAME_NOT_RESOLVED && 123 error_code != net::ERR_NAME_NOT_RESOLVED &&
123 error_code != net::ERR_ADDRESS_UNREACHABLE && 124 error_code != net::ERR_ADDRESS_UNREACHABLE &&
124 error_code != net::ERR_PROXY_CONNECTION_FAILED) { 125 error_code != net::ERR_PROXY_CONNECTION_FAILED) {
125 return; 126 return;
126 } 127 }
127 128
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 129 // Otherwise, get the offline URL for this url, and attempt a redirect if
134 // necessary. 130 // necessary.
135 RedirectReason reason = 131 RedirectReason reason =
136 ui::PageTransitionTypeIncludingQualifiersIs( 132 ui::PageTransitionTypeIncludingQualifiersIs(
137 navigation_handle->GetPageTransition(), 133 navigation_handle->GetPageTransition(),
138 ui::PAGE_TRANSITION_FORWARD_BACK) 134 ui::PAGE_TRANSITION_FORWARD_BACK)
139 ? RedirectReason::FLAKY_NETWORK_FORWARD_BACK 135 ? RedirectReason::FLAKY_NETWORK_FORWARD_BACK
140 : RedirectReason::FLAKY_NETWORK; 136 : RedirectReason::FLAKY_NETWORK;
141 offline_page_model->GetBestPageForOnlineURL( 137 GetBestPageAndTryRedirect(navigated_url, reason);
142 navigated_url,
143 base::Bind(&OfflinePageTabHelper::TryRedirectToOffline,
144 weak_ptr_factory_.GetWeakPtr(), reason, navigated_url));
145 } 138 }
146 139
147 void OfflinePageTabHelper::RedirectToOnline( 140 void OfflinePageTabHelper::RedirectToOnline(
148 const GURL& navigated_url, 141 const GURL& navigated_url,
149 const OfflinePageItem* offline_page) { 142 const OfflinePageItem* offline_page) {
150 // Bails out if no redirection is needed. 143 // Bails out if no redirection is needed.
151 if (!offline_page) 144 if (!offline_page)
152 return; 145 return;
153 146
154 GURL redirect_url = offline_page->url; 147 GURL redirect_url = offline_page->url;
(...skipping 11 matching lines...) Expand all
166 Redirect(navigated_url, redirect_url); 159 Redirect(navigated_url, redirect_url);
167 // Clear the offline page since we are redirecting to online. 160 // Clear the offline page since we are redirecting to online.
168 offline_page_ = nullptr; 161 offline_page_ = nullptr;
169 162
170 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); 163 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1);
171 } 164 }
172 165
173 void OfflinePageTabHelper::TryRedirectToOffline( 166 void OfflinePageTabHelper::TryRedirectToOffline(
174 RedirectReason redirect_reason, 167 RedirectReason redirect_reason,
175 const GURL& from_url, 168 const GURL& from_url,
176 const OfflinePageItem* offline_page) { 169 const OfflinePageItem* offline_page) {
dewittj 2016/06/27 21:52:50 nit: const OfflinePageItem& if there is no case wh
fgorski 2016/06/27 22:40:09 Done.
177 if (!offline_page)
178 return;
179
180 GURL redirect_url = offline_page->GetOfflineURL(); 170 GURL redirect_url = offline_page->GetOfflineURL();
181
182 if (!redirect_url.is_valid()) 171 if (!redirect_url.is_valid())
183 return; 172 return;
184 173
185 if (redirect_reason == RedirectReason::FLAKY_NETWORK || 174 if (redirect_reason == RedirectReason::FLAKY_NETWORK ||
186 redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) { 175 redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) {
187 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork", 176 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork",
188 redirect_reason == RedirectReason::FLAKY_NETWORK); 177 redirect_reason == RedirectReason::FLAKY_NETWORK);
189 // Don't actually want to redirect on a forward/back nav. 178 // Don't actually want to redirect on a forward/back nav.
190 if (redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) 179 if (redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK)
191 return; 180 return;
(...skipping 14 matching lines...) Expand all
206 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); 195 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1);
207 } 196 }
208 197
209 void OfflinePageTabHelper::Redirect(const GURL& from_url, const GURL& to_url) { 198 void OfflinePageTabHelper::Redirect(const GURL& from_url, const GURL& to_url) {
210 content::NavigationController::LoadURLParams load_params(to_url); 199 content::NavigationController::LoadURLParams load_params(to_url);
211 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; 200 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT;
212 load_params.redirect_chain.push_back(from_url); 201 load_params.redirect_chain.push_back(from_url);
213 web_contents()->GetController().LoadURLWithParams(load_params); 202 web_contents()->GetController().LoadURLWithParams(load_params);
214 } 203 }
215 204
205 void OfflinePageTabHelper::GetBestPageAndTryRedirect(
dewittj 2016/06/27 21:52:50 nit: pass in the model here so we only have to get
fgorski 2016/06/27 22:40:09 We call the method in 2 places.
206 const GURL& online_url,
207 RedirectReason reason) {
208 OfflinePageModel* offline_page_model =
209 OfflinePageModelFactory::GetForBrowserContext(
210 web_contents()->GetBrowserContext());
211 if (!offline_page_model)
212 return;
213
214 offline_page_model->GetPagesByOnlineURL(
215 online_url,
216 base::Bind(&OfflinePageTabHelper::SelectBestPageAndTryRedirect,
217 weak_ptr_factory_.GetWeakPtr(), reason, online_url));
218 }
219
220 void OfflinePageTabHelper::SelectBestPageAndTryRedirect(
221 RedirectReason reason,
222 const GURL& online_url,
223 const MultipleOfflinePageItemResult& pages) {
224 TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents());
225 std::string tab_id;
226 if (tab_android)
Dmitry Titov 2016/06/27 22:06:03 How about just early return here: If (!tab_android
fgorski 2016/06/27 22:40:09 Good point.
227 tab_id = base::IntToString(tab_android->GetAndroidId());
228
dewittj 2016/06/27 21:52:50 Need to check for a bad android ID?
fgorski 2016/06/27 22:40:09 Dmitry's comment pretty much resolved the problem.
229 const OfflinePageItem* selected_page = nullptr;
230 for (const auto& offline_page : pages) {
231 if (offline_page.client_id.name_space != kLastNNamespace ||
Dmitry Titov 2016/06/27 22:06:03 shouldn't it be: if (namespace == kLastN && id ==
fgorski 2016/06/27 22:40:09 Updated to explicit check for bookmark namespace a
232 offline_page.client_id.id == tab_id) {
233 if (!selected_page ||
234 offline_page.creation_time > selected_page->creation_time)
235 selected_page = &offline_page;
236 }
237 }
238
239 if (!selected_page)
240 return;
241
242 TryRedirectToOffline(reason, online_url, selected_page);
243 }
244
216 } // namespace offline_pages 245 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698