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

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

Issue 1721103002: Switch between online and offline version per network connection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Created 4 years, 10 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/android/offline_pages/offline_page_utils.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/navigation_handle.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/network_change_notifier.h"
19
20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper);
21
22 namespace offline_pages {
23
24 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents)
25 : content::WebContentsObserver(web_contents),
26 weak_ptr_factory_(this) {
27 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
28 }
29
30 OfflinePageTabHelper::~OfflinePageTabHelper() {}
31
32 void OfflinePageTabHelper::DidStartNavigation(
33 content::NavigationHandle* navigation_handle) {
34 GURL last_redirect_from_url_copy = last_redirect_from_url_;
35 last_redirect_from_url_ = GURL();
36
37 // Skips non-main frame.
38 if (!navigation_handle->IsInMainFrame())
39 return;
40
41 // Redirecting to online version will only take effect when there is network
42 // connection.
43 if (net::NetworkChangeNotifier::IsOffline())
44 return;
45
46 // Skips if not loading an offline copy of saved page.
47 GURL online_url = offline_pages::OfflinePageUtils::GetOnlineURLForOfflineURL(
48 web_contents()->GetBrowserContext(), navigation_handle->GetURL());
49 if (!online_url.is_valid())
50 return;
51
52 // Avoids looping between online and offline redirections.
53 if (last_redirect_from_url_copy == online_url)
54 return;
55 last_redirect_from_url_ = navigation_handle->GetURL();
56
57 base::ThreadTaskRunnerHandle::Get()->PostTask(
58 FROM_HERE,
59 base::Bind(&OfflinePageTabHelper::RedirectFromOfflineToOnline,
60 weak_ptr_factory_.GetWeakPtr(),
61 online_url));
62 }
63
64 void OfflinePageTabHelper::DidFinishNavigation(
65 content::NavigationHandle* navigation_handle) {
66 GURL last_redirect_from_url_copy = last_redirect_from_url_;
67 last_redirect_from_url_ = GURL();
68
69 // Skips non-main frame or load failure other than no network.
70 if (navigation_handle->GetNetErrorCode() != net::ERR_INTERNET_DISCONNECTED ||
71 !navigation_handle->IsInMainFrame()) {
72 return;
73 }
74
75 // Redirecting to offline version will only take effect when there is no
76 // network connection.
77 if (!net::NetworkChangeNotifier::IsOffline())
78 return;
79
80 // Skips if not loading an online version of saved page.
81 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL(
82 web_contents()->GetBrowserContext(), navigation_handle->GetURL());
83 if (!offline_url.is_valid())
84 return;
85
86 // Avoids looping between online and offline redirections.
87 if (last_redirect_from_url_ == offline_url)
88 return;
89 last_redirect_from_url_ = navigation_handle->GetURL();
90
91 base::ThreadTaskRunnerHandle::Get()->PostTask(
92 FROM_HERE,
93 base::Bind(&OfflinePageTabHelper::RedirectFromOnlineToOffline,
94 weak_ptr_factory_.GetWeakPtr(),
95 offline_url));
96 }
97
98 void OfflinePageTabHelper::RedirectFromOfflineToOnline(const GURL& online_url) {
99 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1);
100 content::NavigationController::LoadURLParams load_params(online_url);
101 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT;
102 web_contents()->GetController().LoadURLWithParams(load_params);
103 }
104
105 void OfflinePageTabHelper::RedirectFromOnlineToOffline(
106 const GURL& offline_url) {
107 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1);
108 content::NavigationController::LoadURLParams load_params(offline_url);
109 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT;
110 web_contents()->GetController().LoadURLWithParams(load_params);
111 }
112
113 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698