1 // Copyright 2013 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.
5 #include "components/precache/content/precache_manager.h"
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/time/time.h"
14 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
15 #include "components/precache/core/precache_database.h"
16 #include "components/precache/core/precache_switches.h"
17 #include "components/precache/core/url_list_provider.h"
18 #include "components/user_prefs/user_prefs.h"
19 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "net/base/network_change_notifier.h"
23 using content::BrowserThread
;
27 const char kPrecacheFieldTrialName
[] = "Precache";
28 const char kPrecacheFieldTrialEnabledGroup
[] = "Enabled";
34 PrecacheManager::PrecacheManager(content::BrowserContext
* browser_context
)
35 : browser_context_(browser_context
),
36 precache_database_(new PrecacheDatabase()),
37 is_precaching_(false) {
38 base::FilePath
db_path(browser_context_
->GetPath().Append(
39 base::FilePath(FILE_PATH_LITERAL("PrecacheDatabase"))));
41 BrowserThread::PostTask(
42 BrowserThread::DB
, FROM_HERE
,
43 base::Bind(base::IgnoreResult(&PrecacheDatabase::Init
),
44 precache_database_
, db_path
));
47 PrecacheManager::~PrecacheManager() {}
50 bool PrecacheManager::IsPrecachingEnabled() {
51 return base::FieldTrialList::FindFullName(kPrecacheFieldTrialName
) ==
52 kPrecacheFieldTrialEnabledGroup
||
53 base::CommandLine::ForCurrentProcess()->HasSwitch(
54 switches::kEnablePrecache
);
57 bool PrecacheManager::IsPrecachingAllowed() {
58 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
59 return user_prefs::UserPrefs::Get(browser_context_
)->GetBoolean(
60 data_reduction_proxy::prefs::kDataReductionProxyEnabled
);
63 void PrecacheManager::StartPrecaching(
64 const PrecacheCompletionCallback
& precache_completion_callback
,
65 URLListProvider
* url_list_provider
) {
66 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
69 DLOG(WARNING
) << "Cannot start precaching because precaching is already "
73 is_precaching_
= true;
75 BrowserThread::PostTask(
76 BrowserThread::DB
, FROM_HERE
,
77 base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory
,
78 precache_database_
, base::Time::Now()));
80 precache_completion_callback_
= precache_completion_callback
;
82 url_list_provider
->GetURLs(
83 base::Bind(&PrecacheManager::OnURLsReceived
, AsWeakPtr()));
86 void PrecacheManager::CancelPrecaching() {
87 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
89 if (!is_precaching_
) {
90 // Do nothing if precaching is not in progress.
93 is_precaching_
= false;
95 // Destroying the |precache_fetcher_| will cancel any fetch in progress.
96 precache_fetcher_
.reset();
98 // Uninitialize the callback so that any scoped_refptrs in it are released.
99 precache_completion_callback_
.Reset();
102 bool PrecacheManager::IsPrecaching() const {
103 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
104 return is_precaching_
;
107 void PrecacheManager::RecordStatsForFetch(const GURL
& url
,
108 const base::Time
& fetch_time
,
111 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
113 if (size
== 0 || url
.is_empty() || !url
.SchemeIsHTTPOrHTTPS()) {
114 // Ignore empty responses, empty URLs, or URLs that aren't HTTP or HTTPS.
118 if (is_precaching_
) {
119 // Assume that precache is responsible for all requests made while
120 // precaching is currently in progress.
121 // TODO(sclittle): Make PrecacheFetcher explicitly mark precache-motivated
122 // fetches, and use that to determine whether or not a fetch was motivated
124 BrowserThread::PostTask(
125 BrowserThread::DB
, FROM_HERE
,
126 base::Bind(&PrecacheDatabase::RecordURLPrecached
, precache_database_
,
127 url
, fetch_time
, size
, was_cached
));
129 bool is_connection_cellular
=
130 net::NetworkChangeNotifier::IsConnectionCellular(
131 net::NetworkChangeNotifier::GetConnectionType());
133 BrowserThread::PostTask(
134 BrowserThread::DB
, FROM_HERE
,
135 base::Bind(&PrecacheDatabase::RecordURLFetched
, precache_database_
, url
,
136 fetch_time
, size
, was_cached
, is_connection_cellular
));
140 void PrecacheManager::Shutdown() {
144 void PrecacheManager::OnDone() {
145 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
147 // If OnDone has been called, then we should just be finishing precaching.
148 DCHECK(is_precaching_
);
149 is_precaching_
= false;
151 precache_fetcher_
.reset();
153 precache_completion_callback_
.Run();
154 // Uninitialize the callback so that any scoped_refptrs in it are released.
155 precache_completion_callback_
.Reset();
158 void PrecacheManager::OnURLsReceived(const std::list
<GURL
>& urls
) {
159 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
161 if (!is_precaching_
) {
162 // Don't start precaching if it was canceled while waiting for the list of
168 precache_fetcher_
.reset(
169 new PrecacheFetcher(urls
, browser_context_
->GetRequestContext(), this));
170 precache_fetcher_
->Start();
173 } // namespace precache