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" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 } | 30 } |
31 | 31 |
32 OfflinePageTabHelper::~OfflinePageTabHelper() {} | 32 OfflinePageTabHelper::~OfflinePageTabHelper() {} |
33 | 33 |
34 void OfflinePageTabHelper::DidStartNavigation( | 34 void OfflinePageTabHelper::DidStartNavigation( |
35 content::NavigationHandle* navigation_handle) { | 35 content::NavigationHandle* navigation_handle) { |
36 // Skips non-main frame. | 36 // Skips non-main frame. |
37 if (!navigation_handle->IsInMainFrame()) | 37 if (!navigation_handle->IsInMainFrame()) |
38 return; | 38 return; |
39 | 39 |
40 // This is a new navigation so we can invalidate any previously scheduled | |
41 // operations. | |
42 weak_ptr_factory_.InvalidateWeakPtrs(); | |
43 | |
40 // Ignore navigations that are forward or back transitions in the nav stack | 44 // Ignore navigations that are forward or back transitions in the nav stack |
41 // which are not at the head of the stack. | 45 // which are not at the head of the stack. |
42 const content::NavigationController& controller = | 46 const content::NavigationController& controller = |
43 web_contents()->GetController(); | 47 web_contents()->GetController(); |
44 if (controller.GetEntryCount() > 0 && | 48 if (controller.GetEntryCount() > 0 && |
45 controller.GetCurrentEntryIndex() != -1 && | 49 controller.GetCurrentEntryIndex() != -1 && |
46 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { | 50 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { |
47 return; | 51 return; |
48 } | 52 } |
49 | 53 |
50 GURL redirect_url; | 54 content::BrowserContext* context = web_contents()->GetBrowserContext(); |
55 GURL navigated_url = navigation_handle->GetURL(); | |
56 auto redirect_url_callback = | |
57 base::Bind(&OfflinePageTabHelper::GotRedirectURLForStartedNavigation, | |
58 weak_ptr_factory_.GetWeakPtr(), navigated_url); | |
51 if (net::NetworkChangeNotifier::IsOffline()) { | 59 if (net::NetworkChangeNotifier::IsOffline()) { |
52 // When the network is disconnected, loading online page will result in | 60 OfflinePageUtils::GetOfflineURLForOnlineURL(context, navigated_url, |
53 // immediate redirection to offline copy. | 61 redirect_url_callback); |
54 redirect_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( | |
55 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); | |
56 } else { | 62 } else { |
57 // When the network is connected, loading offline copy will result in | 63 OfflinePageUtils::GetOnlineURLForOfflineURL(context, navigated_url, |
58 // immediate redirection to online page. | 64 redirect_url_callback); |
59 redirect_url = offline_pages::OfflinePageUtils::GetOnlineURLForOfflineURL( | |
60 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); | |
61 } | 65 } |
66 } | |
62 | 67 |
63 // Bails out if no redirection is needed. | 68 void ReportAccessedOfflinePage(content::BrowserContext* browser_context, |
jianli
2016/06/04 00:27:20
Please move it to anonymous namespace.
dewittj
2016/06/06 16:53:15
Done.
| |
64 if (!redirect_url.is_valid()) | 69 const GURL& navigated_url, |
65 return; | 70 const GURL& online_url) { |
66 | 71 // If there is a valid online URL for this navigated URL, then we are looking |
67 // Avoids looping between online and offline redirections. | 72 // at an offline page. |
68 content::NavigationEntry* entry = controller.GetPendingEntry(); | 73 if (online_url.is_valid()) { |
jianli
2016/06/04 00:27:19
nit: remove {}
dewittj
2016/06/06 16:53:15
Done.
| |
69 if (entry && !entry->GetRedirectChain().empty() && | 74 OfflinePageUtils::MarkPageAccessed(browser_context, navigated_url); |
70 entry->GetRedirectChain().back() == redirect_url) { | |
71 return; | |
72 } | 75 } |
73 | |
74 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
75 FROM_HERE, | |
76 base::Bind(&OfflinePageTabHelper::Redirect, | |
77 weak_ptr_factory_.GetWeakPtr(), | |
78 navigation_handle->GetURL(), redirect_url)); | |
79 } | 76 } |
80 | 77 |
81 void OfflinePageTabHelper::DidFinishNavigation( | 78 void OfflinePageTabHelper::DidFinishNavigation( |
82 content::NavigationHandle* navigation_handle) { | 79 content::NavigationHandle* navigation_handle) { |
83 // Skips non-main frame. | 80 // Skips non-main frame. |
84 if (!navigation_handle->IsInMainFrame()) | 81 if (!navigation_handle->IsInMainFrame()) |
85 return; | 82 return; |
86 | 83 |
87 // If the offline page is being loaded successfully, set the access record. | 84 GURL navigated_url = navigation_handle->GetURL(); |
88 net::Error error_code = navigation_handle->GetNetErrorCode(); | 85 net::Error error_code = navigation_handle->GetNetErrorCode(); |
89 if (error_code == net::OK && | 86 content::BrowserContext* browser_context = |
90 OfflinePageUtils::IsOfflinePage( | 87 web_contents()->GetBrowserContext(); |
91 web_contents()->GetBrowserContext(), navigation_handle->GetURL())) { | 88 |
92 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork", true); | 89 // If the offline page is being loaded successfully, set the access record but |
93 OfflinePageUtils::MarkPageAccessed( | 90 // no need to do anything else. |
94 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); | 91 if (error_code == net::OK) { |
92 OfflinePageUtils::GetOnlineURLForOfflineURL( | |
93 browser_context, navigated_url, | |
94 base::Bind(&ReportAccessedOfflinePage, browser_context, navigated_url)); | |
95 return; | |
95 } | 96 } |
96 | 97 |
97 // Skips load failure other than no network. | 98 // When the navigation starts, we redirect immediately from online page to |
99 // offline version on the case that there is no network connection. If there | |
100 // is still network connection but with no or poor network connectivity, the | |
101 // navigation will eventually fail and we want to redirect to offline copy | |
102 // in this case. If error code doesn't match this list, then we still show | |
103 // the error page and not an offline page, so do nothing. | |
98 if (error_code != net::ERR_INTERNET_DISCONNECTED && | 104 if (error_code != net::ERR_INTERNET_DISCONNECTED && |
99 error_code != net::ERR_NAME_NOT_RESOLVED && | 105 error_code != net::ERR_NAME_NOT_RESOLVED && |
100 error_code != net::ERR_ADDRESS_UNREACHABLE && | 106 error_code != net::ERR_ADDRESS_UNREACHABLE && |
101 error_code != net::ERR_PROXY_CONNECTION_FAILED) { | 107 error_code != net::ERR_PROXY_CONNECTION_FAILED) { |
102 return; | 108 return; |
103 } | 109 } |
104 | 110 |
105 // On a forward or back transition, don't affect the order of the nav stack. | 111 // Otherwise, get the offline URL for this url, and attempt a redirect if |
106 if (navigation_handle->GetPageTransition() == | 112 // necessary. |
107 ui::PAGE_TRANSITION_FORWARD_BACK) { | 113 OfflinePageUtils::GetOfflineURLForOnlineURL( |
114 browser_context, navigated_url, | |
115 base::Bind(&OfflinePageTabHelper::GotRedirectURLForSupportedErrorCode, | |
116 weak_ptr_factory_.GetWeakPtr(), | |
117 navigation_handle->GetPageTransition(), navigated_url)); | |
118 } | |
119 | |
120 void OfflinePageTabHelper::GotRedirectURLForSupportedErrorCode( | |
121 ui::PageTransition transition, | |
122 const GURL& from_url, | |
123 const GURL& redirect_url) { | |
124 // If we didn't find an offline URL, or we are doing a forward/back | |
125 // transition, don't redirect. | |
126 if (!redirect_url.is_valid() || | |
127 transition == ui::PAGE_TRANSITION_FORWARD_BACK) { | |
108 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork", false); | 128 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork", false); |
jianli
2016/06/04 00:27:20
Can we combine both UMA reporting lines, like:
b
dewittj
2016/06/06 16:53:15
great idea. Done.
| |
109 return; | 129 return; |
110 } | 130 } |
111 | 131 |
112 // When the navigation starts, we redirect immediately from online page to | 132 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork", true); |
113 // offline version on the case that there is no network connection. If there | 133 |
114 // is still network connection but with no or poor network connectivity, the | 134 base::ThreadTaskRunnerHandle::Get()->PostTask( |
115 // navigation will eventually fail and we want to redirect to offline copy | 135 FROM_HERE, |
116 // in this case. | 136 base::Bind(&OfflinePageTabHelper::Redirect, |
117 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( | 137 weak_ptr_factory_.GetWeakPtr(), from_url, redirect_url)); |
118 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); | 138 } |
119 if (!offline_url.is_valid()) { | 139 |
120 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ShowOfflinePageOnBadNetwork", false); | 140 void OfflinePageTabHelper::GotRedirectURLForStartedNavigation( |
141 const GURL& from_url, | |
142 const GURL& redirect_url) { | |
143 // Bails out if no redirection is needed. | |
144 if (!redirect_url.is_valid()) { | |
jianli
2016/06/04 00:27:20
nit: remove {}
dewittj
2016/06/06 16:53:15
Done.
| |
145 return; | |
146 } | |
147 | |
148 const content::NavigationController& controller = | |
149 web_contents()->GetController(); | |
150 | |
151 // Avoids looping between online and offline redirections. | |
152 content::NavigationEntry* entry = controller.GetPendingEntry(); | |
153 if (entry && !entry->GetRedirectChain().empty() && | |
154 entry->GetRedirectChain().back() == redirect_url) { | |
121 return; | 155 return; |
122 } | 156 } |
123 | 157 |
124 base::ThreadTaskRunnerHandle::Get()->PostTask( | 158 base::ThreadTaskRunnerHandle::Get()->PostTask( |
125 FROM_HERE, | 159 FROM_HERE, |
126 base::Bind(&OfflinePageTabHelper::Redirect, | 160 base::Bind(&OfflinePageTabHelper::Redirect, |
127 weak_ptr_factory_.GetWeakPtr(), | 161 weak_ptr_factory_.GetWeakPtr(), from_url, redirect_url)); |
128 navigation_handle->GetURL(), offline_url)); | |
129 } | 162 } |
130 | 163 |
131 void OfflinePageTabHelper::Redirect( | 164 void OfflinePageTabHelper::Redirect( |
132 const GURL& from_url, const GURL& to_url) { | 165 const GURL& from_url, const GURL& to_url) { |
133 if (to_url.SchemeIsFile()) { | 166 if (to_url.SchemeIsFile()) { |
134 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); | 167 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); |
135 } else { | 168 } else { |
136 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); | 169 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); |
137 } | 170 } |
138 | 171 |
139 content::NavigationController::LoadURLParams load_params(to_url); | 172 content::NavigationController::LoadURLParams load_params(to_url); |
140 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | 173 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; |
141 load_params.redirect_chain.push_back(from_url); | 174 load_params.redirect_chain.push_back(from_url); |
142 web_contents()->GetController().LoadURLWithParams(load_params); | 175 web_contents()->GetController().LoadURLWithParams(load_params); |
143 } | 176 } |
144 | 177 |
145 } // namespace offline_pages | 178 } // namespace offline_pages |
OLD | NEW |