This source file includes following definitions.
- is_precaching_
- IsPrecachingEnabled
- StartPrecaching
- CancelPrecaching
- IsPrecaching
- RecordStatsForFetch
- Shutdown
- OnDone
- OnURLsReceived
#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/metrics/field_trial.h"
#include "base/time/time.h"
#include "components/precache/core/precache_database.h"
#include "components/precache/core/precache_switches.h"
#include "components/precache/core/url_list_provider.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/network_change_notifier.h"
using content::BrowserThread;
namespace {
const char kPrecacheFieldTrialName[] = "Precache";
const char kPrecacheFieldTrialEnabledGroup[] = "Enabled";
}
namespace precache {
PrecacheManager::PrecacheManager(content::BrowserContext* browser_context)
: browser_context_(browser_context),
precache_database_(new PrecacheDatabase()),
is_precaching_(false) {
base::FilePath db_path(browser_context_->GetPath().Append(
base::FilePath(FILE_PATH_LITERAL("PrecacheDatabase"))));
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&PrecacheDatabase::Init),
precache_database_, db_path));
}
PrecacheManager::~PrecacheManager() {}
bool PrecacheManager::IsPrecachingEnabled() {
return base::FieldTrialList::FindFullName(kPrecacheFieldTrialName) ==
kPrecacheFieldTrialEnabledGroup ||
CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePrecache);
}
void PrecacheManager::StartPrecaching(
const PrecacheCompletionCallback& precache_completion_callback,
URLListProvider* url_list_provider) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (is_precaching_) {
DLOG(WARNING) << "Cannot start precaching because precaching is already "
"in progress.";
return;
}
is_precaching_ = true;
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory,
precache_database_, base::Time::Now()));
precache_completion_callback_ = precache_completion_callback;
url_list_provider->GetURLs(
base::Bind(&PrecacheManager::OnURLsReceived, AsWeakPtr()));
}
void PrecacheManager::CancelPrecaching() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!is_precaching_) {
return;
}
is_precaching_ = false;
precache_fetcher_.reset();
precache_completion_callback_.Reset();
}
bool PrecacheManager::IsPrecaching() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return is_precaching_;
}
void PrecacheManager::RecordStatsForFetch(const GURL& url,
const base::Time& fetch_time,
int64 size,
bool was_cached) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (size == 0 || url.is_empty() || !url.SchemeIsHTTPOrHTTPS()) {
return;
}
if (is_precaching_) {
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(&PrecacheDatabase::RecordURLPrecached, precache_database_,
url, fetch_time, size, was_cached));
} else {
bool is_connection_cellular =
net::NetworkChangeNotifier::IsConnectionCellular(
net::NetworkChangeNotifier::GetConnectionType());
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(&PrecacheDatabase::RecordURLFetched, precache_database_, url,
fetch_time, size, was_cached, is_connection_cellular));
}
}
void PrecacheManager::Shutdown() {
CancelPrecaching();
}
void PrecacheManager::OnDone() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(is_precaching_);
is_precaching_ = false;
precache_fetcher_.reset();
precache_completion_callback_.Run();
precache_completion_callback_.Reset();
}
void PrecacheManager::OnURLsReceived(const std::list<GURL>& urls) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!is_precaching_) {
return;
}
precache_fetcher_.reset(
new PrecacheFetcher(urls, browser_context_->GetRequestContext(), this));
precache_fetcher_->Start();
}
}