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

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: Patch 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/render_frame_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/network_change_notifier.h"
18
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper);
20
21 namespace offline_pages {
22
23 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents)
24 : content::WebContentsObserver(web_contents),
25 weak_ptr_factory_(this) {
26 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
27 }
28
29 OfflinePageTabHelper::~OfflinePageTabHelper() {}
30
31 void OfflinePageTabHelper::DidStartProvisionalLoadForFrame(
32 content::RenderFrameHost* render_frame_host,
33 const GURL& validated_url,
34 bool is_error_page,
35 bool is_iframe_srcdoc) {
36 GURL last_redirect_from_url_copy = last_redirect_from_url_;
37 last_redirect_from_url_ = GURL();
38
39 // Skips non-main frame.
40 if (render_frame_host->GetParent() != nullptr)
41 return;
42
43 // Redirecting to online version will only take effect when there is network
44 // connection.
45 if (net::NetworkChangeNotifier::GetConnectionType() ==
46 net::NetworkChangeNotifier::CONNECTION_NONE) {
Pete Williamson 2016/02/25 18:05:30 Why not use if (!NetworkChangeNotifier::IsOnline()
jianli 2016/02/25 21:34:52 Changed to call IsOffline.
47 return;
48 }
49
50 // Skips if not loading an offline copy of saved page.
51 GURL online_url = offline_pages::OfflinePageUtils::GetOnlineURLForOfflineURL(
52 web_contents()->GetBrowserContext(), validated_url);
53 if (!online_url.is_valid())
54 return;
55
56 // Avoids loop between online and offline redirections.
57 if (last_redirect_from_url_copy == online_url)
Pete Williamson 2016/02/25 18:05:31 What if our last succesful transfer just happened
jianli 2016/02/25 21:34:52 last_redirect_from_url_copy is just a saved value
58 return;
59 last_redirect_from_url_ = validated_url;
60
61 base::ThreadTaskRunnerHandle::Get()->PostTask(
Pete Williamson 2016/02/25 18:05:31 So if we delay the redirect, it lets the existing
jianli 2016/02/25 21:34:52 This is to let other observers finish their work.
62 FROM_HERE,
63 base::Bind(&OfflinePageTabHelper::RedirectFromOfflineToOnline,
64 weak_ptr_factory_.GetWeakPtr(),
65 online_url));
66 }
67
68 void OfflinePageTabHelper::DidFailProvisionalLoad(
69 content::RenderFrameHost* render_frame_host,
70 const GURL& validated_url,
71 int error_code,
72 const base::string16& error_description,
73 bool was_ignored_by_handler) {
74 GURL last_redirect_from_url_copy = last_redirect_from_url_;
75 last_redirect_from_url_ = GURL();
76
77 // Skips non-main frame or load failure other than no network.
78 if (error_code != net::ERR_INTERNET_DISCONNECTED ||
79 render_frame_host->GetParent() != nullptr) {
80 return;
81 }
82
83 // Redirecting to offline version will only take effect when there is no
84 // network connection.
85 if (net::NetworkChangeNotifier::GetConnectionType() !=
86 net::NetworkChangeNotifier::CONNECTION_NONE) {
Pete Williamson 2016/02/25 18:05:30 NetworkChangeNotifier::IsOnline() ?
jianli 2016/02/25 21:34:52 Changed to call IsOffline.
87 return;
88 }
89
90 // Skips if not loading an online version of saved page.
91 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL(
92 web_contents()->GetBrowserContext(), validated_url);
93 if (!offline_url.is_valid())
94 return;
95
96 // Avoids loop between online and offline redirections.
97 if (last_redirect_from_url_ == offline_url)
98 return;
99 last_redirect_from_url_ = validated_url;
100
101 base::ThreadTaskRunnerHandle::Get()->PostTask(
102 FROM_HERE,
103 base::Bind(&OfflinePageTabHelper::RedirectFromOnlineToOffline,
104 weak_ptr_factory_.GetWeakPtr(),
105 offline_url));
106 }
107
108 void OfflinePageTabHelper::RedirectFromOfflineToOnline(const GURL& online_url) {
109 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1);
110 content::NavigationController::LoadURLParams load_params(online_url);
111 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT;
112 web_contents()->GetController().LoadURLWithParams(load_params);
113 }
114
115 void OfflinePageTabHelper::RedirectFromOnlineToOffline(
116 const GURL& offline_url) {
117 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1);
118 content::NavigationController::LoadURLParams load_params(offline_url);
119 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT;
120 web_contents()->GetController().LoadURLWithParams(load_params);
121 }
122
123 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698