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/time/time.h"
13 #include "components/precache/core/precache_database.h"
14 #include "components/precache/core/precache_switches.h"
15 #include "components/precache/core/url_list_provider.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "net/base/network_change_notifier.h"
20 using content::BrowserThread
;
24 const char kPrecacheFieldTrialName
[] = "Precache";
25 const char kPrecacheFieldTrialEnabledGroup
[] = "Enabled";
31 PrecacheManager::PrecacheManager(content::BrowserContext
* browser_context
)
32 : browser_context_(browser_context
),
33 precache_database_(new PrecacheDatabase()),
34 is_precaching_(false) {
35 base::FilePath
db_path(browser_context_
->GetPath().Append(
36 base::FilePath(FILE_PATH_LITERAL("PrecacheDatabase"))));
38 BrowserThread::PostTask(
39 BrowserThread::DB
, FROM_HERE
,
40 base::Bind(base::IgnoreResult(&PrecacheDatabase::Init
),
41 precache_database_
, db_path
));
44 PrecacheManager::~PrecacheManager() {}
47 bool PrecacheManager::IsPrecachingEnabled() {
48 return base::FieldTrialList::FindFullName(kPrecacheFieldTrialName
) ==
49 kPrecacheFieldTrialEnabledGroup
||
50 CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePrecache
);
53 void PrecacheManager::StartPrecaching(
54 const PrecacheCompletionCallback
& precache_completion_callback
,
55 URLListProvider
* url_list_provider
) {
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
59 DLOG(WARNING
) << "Cannot start precaching because precaching is already "
63 is_precaching_
= true;
65 BrowserThread::PostTask(
66 BrowserThread::DB
, FROM_HERE
,
67 base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory
,
68 precache_database_
, base::Time::Now()));
70 precache_completion_callback_
= precache_completion_callback
;
72 url_list_provider
->GetURLs(
73 base::Bind(&PrecacheManager::OnURLsReceived
, AsWeakPtr()));
76 void PrecacheManager::CancelPrecaching() {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
79 if (!is_precaching_
) {
80 // Do nothing if precaching is not in progress.
83 is_precaching_
= false;
85 // Destroying the |precache_fetcher_| will cancel any fetch in progress.
86 precache_fetcher_
.reset();
88 // Uninitialize the callback so that any scoped_refptrs in it are released.
89 precache_completion_callback_
.Reset();
92 bool PrecacheManager::IsPrecaching() const {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
94 return is_precaching_
;
97 void PrecacheManager::RecordStatsForFetch(const GURL
& url
,
98 const base::Time
& fetch_time
,
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
103 if (size
== 0 || url
.is_empty() || !url
.SchemeIsHTTPOrHTTPS()) {
104 // Ignore empty responses, empty URLs, or URLs that aren't HTTP or HTTPS.
108 if (is_precaching_
) {
109 // Assume that precache is responsible for all requests made while
110 // precaching is currently in progress.
111 // TODO(sclittle): Make PrecacheFetcher explicitly mark precache-motivated
112 // fetches, and use that to determine whether or not a fetch was motivated
114 BrowserThread::PostTask(
115 BrowserThread::DB
, FROM_HERE
,
116 base::Bind(&PrecacheDatabase::RecordURLPrecached
, precache_database_
,
117 url
, fetch_time
, size
, was_cached
));
119 bool is_connection_cellular
=
120 net::NetworkChangeNotifier::IsConnectionCellular(
121 net::NetworkChangeNotifier::GetConnectionType());
123 BrowserThread::PostTask(
124 BrowserThread::DB
, FROM_HERE
,
125 base::Bind(&PrecacheDatabase::RecordURLFetched
, precache_database_
, url
,
126 fetch_time
, size
, was_cached
, is_connection_cellular
));
130 void PrecacheManager::Shutdown() {
134 void PrecacheManager::OnDone() {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
137 // If OnDone has been called, then we should just be finishing precaching.
138 DCHECK(is_precaching_
);
139 is_precaching_
= false;
141 precache_fetcher_
.reset();
143 precache_completion_callback_
.Run();
144 // Uninitialize the callback so that any scoped_refptrs in it are released.
145 precache_completion_callback_
.Reset();
148 void PrecacheManager::OnURLsReceived(const std::list
<GURL
>& urls
) {
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
151 if (!is_precaching_
) {
152 // Don't start precaching if it was canceled while waiting for the list of
158 precache_fetcher_
.reset(
159 new PrecacheFetcher(urls
, browser_context_
->GetRequestContext(), this));
160 precache_fetcher_
->Start();
163 } // namespace precache