Chromium Code Reviews| Index: components/precache/content/precache_manager.cc |
| diff --git a/components/precache/content/precache_manager.cc b/components/precache/content/precache_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c5496042c7b77b87ebb28de82ac5d2ee1d1a6601 |
| --- /dev/null |
| +++ b/components/precache/content/precache_manager.cc |
| @@ -0,0 +1,141 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/precache/content/precache_manager.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/command_line.h" |
| +#include "base/files/file_path.h" |
| +#include "base/logging.h" |
| +#include "base/time/time.h" |
| +#include "components/precache/core/precache_database.h" |
| +#include "components/precache/core/precache_switches.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "sql/connection.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace precache { |
| + |
| +namespace { |
|
bengr
2013/10/23 19:03:36
Move this anonymous namespace about the precache n
sclittle
2013/10/24 22:11:38
Done.
|
| + |
| +const base::FilePath::CharType kPrecacheDatabaseName[] = |
| + FILE_PATH_LITERAL("PrecacheDatabase"); |
| + |
| +void InitPrecacheDatabaseOnDBThread( |
| + base::FilePath dir_path, |
| + scoped_refptr<PrecacheDatabase> precache_database) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| + |
| + sql::Connection* db = new sql::Connection(); |
| + DCHECK(db->Open(dir_path.Append(kPrecacheDatabaseName))); |
| + |
| + // The precache database will take ownership of the connection. |
| + precache_database->Init(db); |
| +} |
| + |
| +} // namespace |
| + |
| +PrecacheManager::PrecacheManager(content::BrowserContext* browser_context) |
| + : browser_context_(browser_context), |
| + precache_database_(new PrecacheDatabase()), |
| + is_precaching_(false) { |
| + BrowserThread::PostTask( |
| + BrowserThread::DB, FROM_HERE, |
| + base::Bind(&InitPrecacheDatabaseOnDBThread, browser_context->GetPath(), |
| + precache_database_)); |
| +} |
| + |
| +PrecacheManager::~PrecacheManager() {} |
| + |
| +// static |
| +bool PrecacheManager::IsPrecachingEnabled() { |
| + // TODO(sclittle): Test for Finch trial as well when it exists. |
| + return CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePrecache); |
|
bengr
2013/10/23 19:03:36
How does precache know which servers to query? Are
sclittle
2013/10/24 22:11:38
Those are set by flags already; see components/pre
|
| +} |
| + |
| +void PrecacheManager::Shutdown() { |
| + CancelPrecaching(); |
| +} |
| + |
| +void PrecacheManager::OnDone() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + { |
| + base::AutoLock lock(lock_); |
| + is_precaching_ = false; |
| + } |
| + |
| + precache_fetcher_.reset(); |
| +} |
| + |
| +void PrecacheManager::StartPrecaching( |
| + InterestingURLsProvider* interesting_urls_provider) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + { |
| + base::AutoLock lock(lock_); |
| + if (is_precaching_) { |
| + DLOG(WARNING) << "Cannot start precaching because precaching is already " |
| + "in progress."; |
| + return; |
| + } |
| + is_precaching_ = true; |
| + } |
| + |
| + // Report any UMA of precache stats for days before the current date and |
| + // delete expired precache history. |
| + BrowserThread::PostTask( |
| + BrowserThread::DB, FROM_HERE, |
| + base::Bind( |
| + &PrecacheDatabase::FlushOldStats, precache_database_, |
| + base::Time::Now().LocalMidnight() - base::TimeDelta::FromHours(1))); |
|
bengr
2013/10/23 19:03:36
Why 11pm? I don't follow.
sclittle
2013/10/24 22:11:38
It just has to be a time on the day before Now().
|
| + |
| + interesting_urls_provider->GetInterestingURLs(base::Bind( |
| + &PrecacheManager::OnInterestingURLsReceived, base::Unretained(this))); |
| +} |
| + |
| +void PrecacheManager::OnInterestingURLsReceived( |
| + const std::list<GURL>& interesting_urls) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + if (!is_precaching()) { |
| + // Don't start precaching if it was canceled while waiting for the list of |
| + // interesting URLs. |
| + return; |
| + } |
| + |
| + // Start precaching. |
| + precache_fetcher_.reset(new PrecacheFetcher( |
| + interesting_urls, browser_context_->GetRequestContext(), this)); |
| + precache_fetcher_->Start(); |
| +} |
| + |
| +void PrecacheManager::CancelPrecaching() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + { |
| + base::AutoLock lock(lock_); |
| + if (!is_precaching_) { |
| + // Do nothing if precaching is not in progress. |
| + return; |
| + } |
| + is_precaching_ = false; |
| + } |
| + |
| + // Destroying the |precache_fetcher_| will cancel any fetch in progress. |
| + precache_fetcher_.reset(); |
| +} |
| + |
| +bool PrecacheManager::is_precaching() { |
| + base::AutoLock lock(lock_); |
| + return is_precaching_; |
| +} |
| + |
| +scoped_refptr<PrecacheDatabase> PrecacheManager::precache_database() { |
| + return precache_database_; |
| +} |
| + |
| +} // namespace precache |