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

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 bug where offline page is reset while navigating to an offline page. 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()) {
jianli 2016/06/17 21:48:58 nit: remove brackets
63 offline_page_ = nullptr;
64 }
65
56 // Ignore navigations that are forward or back transitions in the nav stack 66 // Ignore navigations that are forward or back transitions in the nav stack
57 // which are not at the head of the stack. 67 // which are not at the head of the stack.
58 const content::NavigationController& controller = 68 const content::NavigationController& controller =
59 web_contents()->GetController(); 69 web_contents()->GetController();
60 if (controller.GetEntryCount() > 0 && 70 if (controller.GetEntryCount() > 0 &&
61 controller.GetCurrentEntryIndex() != -1 && 71 controller.GetCurrentEntryIndex() != -1 &&
62 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { 72 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) {
63 return; 73 return;
64 } 74 }
65 75
66 content::BrowserContext* context = web_contents()->GetBrowserContext(); 76 content::BrowserContext* context = web_contents()->GetBrowserContext();
67 GURL navigated_url = navigation_handle->GetURL(); 77 OfflinePageModel* offline_page_model =
68 auto redirect_url_callback = 78 OfflinePageModelFactory::GetForBrowserContext(context);
69 base::Bind(&OfflinePageTabHelper::GotRedirectURLForStartedNavigation, 79 if (!offline_page_model)
70 weak_ptr_factory_.GetWeakPtr(), navigated_url); 80 return;
81
71 if (net::NetworkChangeNotifier::IsOffline()) { 82 if (net::NetworkChangeNotifier::IsOffline()) {
72 OfflinePageUtils::GetOfflineURLForOnlineURL(context, navigated_url, 83 offline_page_model->GetBestPageForOnlineURL(
73 redirect_url_callback); 84 navigated_url,
85 base::Bind(&OfflinePageTabHelper::TryRedirectToOffline,
86 weak_ptr_factory_.GetWeakPtr(),
87 RedirectReason::DISCONNECTED_NETWORK, navigated_url));
74 } else { 88 } else {
75 OfflinePageUtils::GetOnlineURLForOfflineURL(context, navigated_url, 89 offline_page_model->GetPageByOfflineURL(
76 redirect_url_callback); 90 navigated_url,
91 base::Bind(&OfflinePageTabHelper::RedirectToOnline,
92 weak_ptr_factory_.GetWeakPtr(), navigated_url));
77 } 93 }
78 } 94 }
79 95
80 void OfflinePageTabHelper::DidFinishNavigation( 96 void OfflinePageTabHelper::DidFinishNavigation(
81 content::NavigationHandle* navigation_handle) { 97 content::NavigationHandle* navigation_handle) {
82 // Skips non-main frame. 98 // Skips non-main frame.
83 if (!navigation_handle->IsInMainFrame()) 99 if (!navigation_handle->IsInMainFrame())
84 return; 100 return;
85 101
86 GURL navigated_url = navigation_handle->GetURL(); 102 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 119 // 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 120 // 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. 121 // the error page and not an offline page, so do nothing.
106 if (error_code != net::ERR_INTERNET_DISCONNECTED && 122 if (error_code != net::ERR_INTERNET_DISCONNECTED &&
107 error_code != net::ERR_NAME_NOT_RESOLVED && 123 error_code != net::ERR_NAME_NOT_RESOLVED &&
108 error_code != net::ERR_ADDRESS_UNREACHABLE && 124 error_code != net::ERR_ADDRESS_UNREACHABLE &&
109 error_code != net::ERR_PROXY_CONNECTION_FAILED) { 125 error_code != net::ERR_PROXY_CONNECTION_FAILED) {
110 return; 126 return;
111 } 127 }
112 128
129 OfflinePageModel* offline_page_model =
130 OfflinePageModelFactory::GetForBrowserContext(browser_context);
131 if (!offline_page_model)
132 return;
133
113 // Otherwise, get the offline URL for this url, and attempt a redirect if 134 // Otherwise, get the offline URL for this url, and attempt a redirect if
114 // necessary. 135 // necessary.
115 OfflinePageUtils::GetOfflineURLForOnlineURL( 136 RedirectReason reason =
116 browser_context, navigated_url, 137 navigation_handle->GetPageTransition() == ui::PAGE_TRANSITION_FORWARD_BACK
117 base::Bind(&OfflinePageTabHelper::GotRedirectURLForSupportedErrorCode, 138 ? RedirectReason::FLAKY_NETWORK_FORWARD_BACK
118 weak_ptr_factory_.GetWeakPtr(), 139 : RedirectReason::FLAKY_NETWORK;
119 navigation_handle->GetPageTransition(), navigated_url)); 140 offline_page_model->GetBestPageForOnlineURL(
141 navigated_url,
142 base::Bind(&OfflinePageTabHelper::TryRedirectToOffline,
143 weak_ptr_factory_.GetWeakPtr(), reason, navigated_url));
120 } 144 }
121 145
122 void OfflinePageTabHelper::GotRedirectURLForSupportedErrorCode( 146 void OfflinePageTabHelper::RedirectToOnline(
123 ui::PageTransition transition, 147 const GURL& navigated_url,
124 const GURL& from_url, 148 const OfflinePageItem* offline_page) {
125 const GURL& redirect_url) { 149 // Bails out if no redirection is needed.
126 // If we didn't find an offline URL, or we are doing a forward/back 150 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; 151 return;
134 152
135 base::ThreadTaskRunnerHandle::Get()->PostTask( 153 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 154
148 const content::NavigationController& controller = 155 const content::NavigationController& controller =
149 web_contents()->GetController(); 156 web_contents()->GetController();
150 157
151 // Avoids looping between online and offline redirections. 158 // Avoids looping between online and offline redirections.
152 content::NavigationEntry* entry = controller.GetPendingEntry(); 159 content::NavigationEntry* entry = controller.GetPendingEntry();
153 if (entry && !entry->GetRedirectChain().empty() && 160 if (entry && !entry->GetRedirectChain().empty() &&
154 entry->GetRedirectChain().back() == redirect_url) { 161 entry->GetRedirectChain().back() == redirect_url) {
155 return; 162 return;
156 } 163 }
157 164
158 base::ThreadTaskRunnerHandle::Get()->PostTask( 165 Redirect(navigated_url, redirect_url);
159 FROM_HERE, 166 // Clear the offline page since we are redirecting to online.
160 base::Bind(&OfflinePageTabHelper::Redirect, 167 offline_page_ = nullptr;
161 weak_ptr_factory_.GetWeakPtr(), from_url, redirect_url)); 168
169 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1);
162 } 170 }
163 171
164 void OfflinePageTabHelper::Redirect( 172 void OfflinePageTabHelper::TryRedirectToOffline(
165 const GURL& from_url, const GURL& to_url) { 173 RedirectReason redirect_reason,
166 if (to_url.SchemeIsFile()) { 174 const GURL& from_url,
167 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); 175 const OfflinePageItem* offline_page) {
176 if (!offline_page)
177 return;
178
179 GURL redirect_url = offline_page->GetOfflineURL();
180
181 if (!redirect_url.is_valid())
182 return;
183
184 if (redirect_reason == RedirectReason::FLAKY_NETWORK ||
185 redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK) {
186 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork",
187 redirect_reason == RedirectReason::FLAKY_NETWORK);
188 // Don't actually want to redirect on a forward/back nav.
189 if (redirect_reason == RedirectReason::FLAKY_NETWORK_FORWARD_BACK)
190 return;
168 } else { 191 } else {
169 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); 192 const content::NavigationController& controller =
193 web_contents()->GetController();
194
195 // Avoids looping between online and offline redirections.
196 content::NavigationEntry* entry = controller.GetPendingEntry();
197 if (entry && !entry->GetRedirectChain().empty() &&
198 entry->GetRedirectChain().back() == redirect_url) {
199 return;
200 }
170 } 201 }
171 202
203 Redirect(from_url, redirect_url);
204 offline_page_ = base::MakeUnique<OfflinePageItem>(*offline_page);
205 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1);
206 }
207
208 void OfflinePageTabHelper::Redirect(const GURL& from_url, const GURL& to_url) {
172 content::NavigationController::LoadURLParams load_params(to_url); 209 content::NavigationController::LoadURLParams load_params(to_url);
173 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; 210 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT;
174 load_params.redirect_chain.push_back(from_url); 211 load_params.redirect_chain.push_back(from_url);
175 web_contents()->GetController().LoadURLWithParams(load_params); 212 web_contents()->GetController().LoadURLWithParams(load_params);
176 } 213 }
177 214
178 } // namespace offline_pages 215 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698