Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: components/precache/content/precache_manager.cc

Issue 27047003: Precache tracking database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@precache
Patch Set: Added field trials Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "components/precache/content/precache_manager.h"
6
7 #include "base/bind.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 "sql/connection.h"
19
20 using content::BrowserThread;
21
22 namespace {
23
24 const char kPrecacheFieldTrialName[] = "Precache";
25 const char kPrecacheFieldTrialEnabledGroup[] = "PrecacheEnabled";
26
27 const base::FilePath::CharType kPrecacheDatabaseName[] =
28 FILE_PATH_LITERAL("PrecacheDatabase");
29
30 void InitPrecacheDatabaseOnDBThread(
31 base::FilePath dir_path,
32 scoped_refptr<precache::PrecacheDatabase> precache_database) {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
34
35 scoped_ptr<sql::Connection> db(new sql::Connection());
36 if (!db->Open(dir_path.Append(kPrecacheDatabaseName))) {
37 DLOG(WARNING) << "Could not open precache database. No precache data will "
38 "be tracked.";
39 }
40
41 // Initialize the precache database with the connection even if it failed to
42 // open. The precache database will take ownership of the connection.
43 precache_database->Init(db.Pass());
44 }
45
46 } // namespace
47
48 namespace precache {
49
50 PrecacheManager::PrecacheManager(content::BrowserContext* browser_context)
51 : browser_context_(browser_context),
52 precache_database_(new PrecacheDatabase()),
53 is_precaching_(false) {
54 BrowserThread::PostTask(
55 BrowserThread::DB, FROM_HERE,
56 base::Bind(&InitPrecacheDatabaseOnDBThread, browser_context->GetPath(),
57 precache_database_));
58 }
59
60 PrecacheManager::~PrecacheManager() {}
61
62 // static
63 bool PrecacheManager::IsPrecachingEnabled() {
64 return base::FieldTrialList::FindFullName(kPrecacheFieldTrialName) ==
65 kPrecacheFieldTrialEnabledGroup ||
66 CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePrecache);
67 }
68
69 void PrecacheManager::Shutdown() {
70 CancelPrecaching();
71 }
72
73 void PrecacheManager::OnDone() {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75
76 {
77 base::AutoLock lock(lock_);
78 is_precaching_ = false;
79 }
80
81 precache_fetcher_.reset();
82 }
83
84 void PrecacheManager::StartPrecaching(
85 URLListProvider* interesting_urls_provider) {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
87
88 {
89 base::AutoLock lock(lock_);
90 if (is_precaching_) {
91 DLOG(WARNING) << "Cannot start precaching because precaching is already "
92 "in progress.";
93 return;
94 }
95 is_precaching_ = true;
96 }
97
98 // The start of the day before today. Don't just use (base::Time::Now() -
99 // base::TimeDelta::FromDays(1)).LocalMidnight() because that wouldn't account
100 // for daylight savings, i.e. some days are 23 or 25 hours long.
101 base::Time yesterday = (base::Time::Now().LocalMidnight() -
102 base::TimeDelta::FromHours(1)).LocalMidnight();
103
104 // Report any UMA of precache stats for days before the current date and
105 // delete expired precache history.
106 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
107 base::Bind(&PrecacheDatabase::ReportAndDeleteOldStats,
108 precache_database_, yesterday));
109
110 interesting_urls_provider->GetURLs(
111 base::Bind(&PrecacheManager::OnURLsReceived, base::Unretained(this)));
112 }
113
114 void PrecacheManager::CancelPrecaching() {
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
116
117 {
118 base::AutoLock lock(lock_);
119 if (!is_precaching_) {
120 // Do nothing if precaching is not in progress.
121 return;
122 }
123 is_precaching_ = false;
124 }
125
126 // Destroying the |precache_fetcher_| will cancel any fetch in progress.
127 precache_fetcher_.reset();
128 }
129
130 bool PrecacheManager::IsPrecaching() {
131 base::AutoLock lock(lock_);
132 return is_precaching_;
133 }
134
135 scoped_refptr<PrecacheDatabase> PrecacheManager::precache_database() {
136 return precache_database_;
137 }
138
139 void PrecacheManager::OnURLsReceived(const std::list<GURL>& interesting_urls) {
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
141
142 if (!IsPrecaching()) {
143 // Don't start precaching if it was canceled while waiting for the list of
144 // interesting URLs.
145 return;
146 }
147
148 // Start precaching.
149 precache_fetcher_.reset(new PrecacheFetcher(
150 interesting_urls, browser_context_->GetRequestContext(), this));
151 precache_fetcher_->Start();
152 }
153
154 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698