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

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

Issue 1902443003: Redirect immediately to offline copy on no network (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 7 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/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
(...skipping 19 matching lines...) Expand all
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 // Redirecting to online version will only take effect when there is network
41 // connection.
42 if (net::NetworkChangeNotifier::IsOffline())
43 return;
44
45 // Ignore navigations that are forward or back transitions in the nav stack 40 // Ignore navigations that are forward or back transitions in the nav stack
46 // which are not at the head of the stack. 41 // which are not at the head of the stack.
47 const content::NavigationController& controller = 42 const content::NavigationController& controller =
48 web_contents()->GetController(); 43 web_contents()->GetController();
49 if (controller.GetEntryCount() > 0 && 44 if (controller.GetEntryCount() > 0 &&
50 controller.GetCurrentEntryIndex() != -1 && 45 controller.GetCurrentEntryIndex() != -1 &&
51 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) { 46 controller.GetCurrentEntryIndex() < controller.GetEntryCount() - 1) {
52 return; 47 return;
53 } 48 }
54 49
55 // Skips if not loading an offline copy of saved page. 50 GURL redirect_url;
56 GURL online_url = offline_pages::OfflinePageUtils::GetOnlineURLForOfflineURL( 51 if (net::NetworkChangeNotifier::IsOffline()) {
52 // When the network is disconnected, loading online page will result in
53 // immediate redirection to offline copy.
54 redirect_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL(
57 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); 55 web_contents()->GetBrowserContext(), navigation_handle->GetURL());
58 if (!online_url.is_valid()) 56 } else {
57 // When the network is connected, loading offline copy will result in
58 // immediate redirection to online page.
59 redirect_url = offline_pages::OfflinePageUtils::GetOnlineURLForOfflineURL(
60 web_contents()->GetBrowserContext(), navigation_handle->GetURL());
61 }
62
63 // Bails out if no redirection is needed.
64 if (!redirect_url.is_valid())
59 return; 65 return;
60 66
61 // Avoids looping between online and offline redirections. 67 // Avoids looping between online and offline redirections.
62 content::NavigationEntry* entry = controller.GetPendingEntry(); 68 content::NavigationEntry* entry = controller.GetPendingEntry();
63 if (entry && !entry->GetRedirectChain().empty() && 69 if (entry && !entry->GetRedirectChain().empty() &&
64 entry->GetRedirectChain().back() == online_url) { 70 entry->GetRedirectChain().back() == redirect_url) {
65 return; 71 return;
66 } 72 }
67 73
68 base::ThreadTaskRunnerHandle::Get()->PostTask( 74 base::ThreadTaskRunnerHandle::Get()->PostTask(
69 FROM_HERE, 75 FROM_HERE,
70 base::Bind(&OfflinePageTabHelper::Redirect, 76 base::Bind(&OfflinePageTabHelper::Redirect,
71 weak_ptr_factory_.GetWeakPtr(), 77 weak_ptr_factory_.GetWeakPtr(),
72 navigation_handle->GetURL(), online_url)); 78 navigation_handle->GetURL(), redirect_url));
73 } 79 }
74 80
75 void OfflinePageTabHelper::DidFinishNavigation( 81 void OfflinePageTabHelper::DidFinishNavigation(
76 content::NavigationHandle* navigation_handle) { 82 content::NavigationHandle* navigation_handle) {
77 // Skips non-main frame. 83 // Skips non-main frame.
78 if (!navigation_handle->IsInMainFrame()) 84 if (!navigation_handle->IsInMainFrame())
79 return; 85 return;
80 86
81 // Skips load failure other than no network. 87 // Skips load failure other than no network.
82 net::Error error_code = navigation_handle->GetNetErrorCode(); 88 net::Error error_code = navigation_handle->GetNetErrorCode();
83 if (error_code != net::ERR_INTERNET_DISCONNECTED && 89 if (error_code != net::ERR_INTERNET_DISCONNECTED &&
84 error_code != net::ERR_NAME_NOT_RESOLVED && 90 error_code != net::ERR_NAME_NOT_RESOLVED &&
85 error_code != net::ERR_ADDRESS_UNREACHABLE && 91 error_code != net::ERR_ADDRESS_UNREACHABLE &&
86 error_code != net::ERR_PROXY_CONNECTION_FAILED) { 92 error_code != net::ERR_PROXY_CONNECTION_FAILED) {
87 return; 93 return;
88 } 94 }
89 95
90 // On a forward or back transition, don't affect the order of the nav stack. 96 // On a forward or back transition, don't affect the order of the nav stack.
91 if (navigation_handle->GetPageTransition() == 97 if (navigation_handle->GetPageTransition() ==
92 ui::PAGE_TRANSITION_FORWARD_BACK) { 98 ui::PAGE_TRANSITION_FORWARD_BACK) {
93 return; 99 return;
94 } 100 }
95 101
96 // Skips if not loading an online version of saved page. 102 // Skips if not loading an online version of saved page.
97 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( 103 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL(
fgorski 2016/05/04 21:45:12 no change here?
jianli 2016/05/05 00:23:13 The plan is that we still access the sync API for
fgorski 2016/05/05 04:19:30 OK. What I think I didn't get was, the fact that i
98 web_contents()->GetBrowserContext(), navigation_handle->GetURL()); 104 web_contents()->GetBrowserContext(), navigation_handle->GetURL());
99 if (!offline_url.is_valid()) 105 if (!offline_url.is_valid())
100 return; 106 return;
101 107
102 base::ThreadTaskRunnerHandle::Get()->PostTask( 108 base::ThreadTaskRunnerHandle::Get()->PostTask(
103 FROM_HERE, 109 FROM_HERE,
104 base::Bind(&OfflinePageTabHelper::Redirect, 110 base::Bind(&OfflinePageTabHelper::Redirect,
105 weak_ptr_factory_.GetWeakPtr(), 111 weak_ptr_factory_.GetWeakPtr(),
106 navigation_handle->GetURL(), offline_url)); 112 navigation_handle->GetURL(), offline_url));
107 } 113 }
108 114
109 void OfflinePageTabHelper::Redirect( 115 void OfflinePageTabHelper::Redirect(
110 const GURL& from_url, const GURL& to_url) { 116 const GURL& from_url, const GURL& to_url) {
111 if (to_url.SchemeIsFile()) 117 if (to_url.SchemeIsFile()) {
112 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); 118 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1);
113 else 119 OfflinePageUtils::MarkPageAccessed(
fgorski 2016/05/04 21:45:12 should we mark page access when it is loaded witho
jianli 2016/05/05 00:23:13 Done.
120 web_contents()->GetBrowserContext(), to_url);
121 } else {
114 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); 122 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1);
123 }
115 124
116 content::NavigationController::LoadURLParams load_params(to_url); 125 content::NavigationController::LoadURLParams load_params(to_url);
117 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; 126 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT;
118 load_params.redirect_chain.push_back(from_url); 127 load_params.redirect_chain.push_back(from_url);
119 web_contents()->GetController().LoadURLWithParams(load_params); 128 web_contents()->GetController().LoadURLWithParams(load_params);
120 } 129 }
121 130
122 } // namespace offline_pages 131 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698