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

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: Changes 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::DidFailProvisionalLoad(
65 content::RenderFrameHost* render_frame_host,
66 const GURL& validated_url,
67 int error_code,
68 const base::string16& error_description,
69 bool was_ignored_by_handler) {
70 GURL last_redirect_from_url_copy = last_redirect_from_url_;
71 last_redirect_from_url_ = GURL();
72
73 // Skips non-main frame or load failure other than no network.
74 if (error_code != net::ERR_INTERNET_DISCONNECTED ||
75 render_frame_host->GetParent() != nullptr) {
76 return;
77 }
78
79 // Redirecting to offline version will only take effect when there is no
80 // network connection.
81 if (!net::NetworkChangeNotifier::IsOffline())
82 return;
83
84 // Skips if not loading an online version of saved page.
85 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL(
86 web_contents()->GetBrowserContext(), validated_url);
87 if (!offline_url.is_valid())
88 return;
89
90 // Avoids looping between online and offline redirections.
91 if (last_redirect_from_url_copy == offline_url)
92 return;
93 last_redirect_from_url_ = validated_url;
94
95 base::ThreadTaskRunnerHandle::Get()->PostTask(
96 FROM_HERE,
97 base::Bind(&OfflinePageTabHelper::RedirectFromOnlineToOffline,
98 weak_ptr_factory_.GetWeakPtr(),
99 offline_url));
100 }
101
102 void OfflinePageTabHelper::RedirectFromOfflineToOnline(const GURL& online_url) {
103 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1);
104 content::NavigationController::LoadURLParams load_params(online_url);
105 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT;
106 web_contents()->GetController().LoadURLWithParams(load_params);
107 }
108
109 void OfflinePageTabHelper::RedirectFromOnlineToOffline(
110 const GURL& offline_url) {
111 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1);
112 content::NavigationController::LoadURLParams load_params(offline_url);
113 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT;
114 web_contents()->GetController().LoadURLWithParams(load_params);
115 }
116
117 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698