Chromium Code Reviews| OLD | NEW |
|---|---|
| (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::IsOffline()) | |
| 46 return; | |
| 47 | |
| 48 // Skips if not loading an offline copy of saved page. | |
| 49 GURL online_url = offline_pages::OfflinePageUtils::GetOnlineURLForOfflineURL( | |
| 50 web_contents()->GetBrowserContext(), validated_url); | |
|
nasko
2016/02/26 00:12:19
Is there any way to confirm that validated_url is
jianli
2016/02/26 03:03:22
We don't need to do this because GetOfflinePageFor
| |
| 51 if (!online_url.is_valid()) | |
| 52 return; | |
| 53 | |
| 54 // Avoids looping between online and offline redirections. | |
| 55 if (last_redirect_from_url_copy == online_url) | |
| 56 return; | |
| 57 last_redirect_from_url_ = validated_url; | |
| 58 | |
| 59 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 60 FROM_HERE, | |
| 61 base::Bind(&OfflinePageTabHelper::RedirectFromOfflineToOnline, | |
| 62 weak_ptr_factory_.GetWeakPtr(), | |
| 63 online_url)); | |
| 64 } | |
| 65 | |
| 66 void OfflinePageTabHelper::DidFailProvisionalLoad( | |
| 67 content::RenderFrameHost* render_frame_host, | |
| 68 const GURL& validated_url, | |
| 69 int error_code, | |
| 70 const base::string16& error_description, | |
| 71 bool was_ignored_by_handler) { | |
| 72 GURL last_redirect_from_url_copy = last_redirect_from_url_; | |
| 73 last_redirect_from_url_ = GURL(); | |
| 74 | |
| 75 // Skips non-main frame or load failure other than no network. | |
| 76 if (error_code != net::ERR_INTERNET_DISCONNECTED || | |
| 77 render_frame_host->GetParent() != nullptr) { | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 // Redirecting to offline version will only take effect when there is no | |
| 82 // network connection. | |
| 83 if (!net::NetworkChangeNotifier::IsOffline()) | |
| 84 return; | |
| 85 | |
| 86 // Skips if not loading an online version of saved page. | |
| 87 GURL offline_url = offline_pages::OfflinePageUtils::GetOfflineURLForOnlineURL( | |
| 88 web_contents()->GetBrowserContext(), validated_url); | |
| 89 if (!offline_url.is_valid()) | |
| 90 return; | |
| 91 | |
| 92 // Avoids looping between online and offline redirections. | |
| 93 if (last_redirect_from_url_ == offline_url) | |
| 94 return; | |
| 95 last_redirect_from_url_ = validated_url; | |
| 96 | |
| 97 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 98 FROM_HERE, | |
| 99 base::Bind(&OfflinePageTabHelper::RedirectFromOnlineToOffline, | |
| 100 weak_ptr_factory_.GetWeakPtr(), | |
| 101 offline_url)); | |
| 102 } | |
| 103 | |
| 104 void OfflinePageTabHelper::RedirectFromOfflineToOnline(const GURL& online_url) { | |
| 105 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOnlineCount", 1); | |
| 106 content::NavigationController::LoadURLParams load_params(online_url); | |
| 107 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | |
| 108 web_contents()->GetController().LoadURLWithParams(load_params); | |
| 109 } | |
| 110 | |
| 111 void OfflinePageTabHelper::RedirectFromOnlineToOffline( | |
| 112 const GURL& offline_url) { | |
| 113 UMA_HISTOGRAM_COUNTS("OfflinePages.RedirectToOfflineCount", 1); | |
| 114 content::NavigationController::LoadURLParams load_params(offline_url); | |
| 115 load_params.transition_type = ui::PAGE_TRANSITION_CLIENT_REDIRECT; | |
| 116 web_contents()->GetController().LoadURLWithParams(load_params); | |
| 117 } | |
| 118 | |
| 119 } // namespace offline_pages | |
| OLD | NEW |