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

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: General fixes, added tests, and moved PrecacheManager into component Created 7 years, 2 months 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/time/time.h"
12 #include "components/precache/core/precache_database.h"
13 #include "components/precache/core/precache_switches.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "sql/connection.h"
17
18 using content::BrowserThread;
19
20 namespace precache {
21
22 namespace {
bengr 2013/10/23 19:03:36 Move this anonymous namespace about the precache n
sclittle 2013/10/24 22:11:38 Done.
23
24 const base::FilePath::CharType kPrecacheDatabaseName[] =
25 FILE_PATH_LITERAL("PrecacheDatabase");
26
27 void InitPrecacheDatabaseOnDBThread(
28 base::FilePath dir_path,
29 scoped_refptr<PrecacheDatabase> precache_database) {
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
31
32 sql::Connection* db = new sql::Connection();
33 DCHECK(db->Open(dir_path.Append(kPrecacheDatabaseName)));
34
35 // The precache database will take ownership of the connection.
36 precache_database->Init(db);
37 }
38
39 } // namespace
40
41 PrecacheManager::PrecacheManager(content::BrowserContext* browser_context)
42 : browser_context_(browser_context),
43 precache_database_(new PrecacheDatabase()),
44 is_precaching_(false) {
45 BrowserThread::PostTask(
46 BrowserThread::DB, FROM_HERE,
47 base::Bind(&InitPrecacheDatabaseOnDBThread, browser_context->GetPath(),
48 precache_database_));
49 }
50
51 PrecacheManager::~PrecacheManager() {}
52
53 // static
54 bool PrecacheManager::IsPrecachingEnabled() {
55 // TODO(sclittle): Test for Finch trial as well when it exists.
56 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
57 }
58
59 void PrecacheManager::Shutdown() {
60 CancelPrecaching();
61 }
62
63 void PrecacheManager::OnDone() {
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
65
66 {
67 base::AutoLock lock(lock_);
68 is_precaching_ = false;
69 }
70
71 precache_fetcher_.reset();
72 }
73
74 void PrecacheManager::StartPrecaching(
75 InterestingURLsProvider* interesting_urls_provider) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
77
78 {
79 base::AutoLock lock(lock_);
80 if (is_precaching_) {
81 DLOG(WARNING) << "Cannot start precaching because precaching is already "
82 "in progress.";
83 return;
84 }
85 is_precaching_ = true;
86 }
87
88 // Report any UMA of precache stats for days before the current date and
89 // delete expired precache history.
90 BrowserThread::PostTask(
91 BrowserThread::DB, FROM_HERE,
92 base::Bind(
93 &PrecacheDatabase::FlushOldStats, precache_database_,
94 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().
95
96 interesting_urls_provider->GetInterestingURLs(base::Bind(
97 &PrecacheManager::OnInterestingURLsReceived, base::Unretained(this)));
98 }
99
100 void PrecacheManager::OnInterestingURLsReceived(
101 const std::list<GURL>& interesting_urls) {
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
103
104 if (!is_precaching()) {
105 // Don't start precaching if it was canceled while waiting for the list of
106 // interesting URLs.
107 return;
108 }
109
110 // Start precaching.
111 precache_fetcher_.reset(new PrecacheFetcher(
112 interesting_urls, browser_context_->GetRequestContext(), this));
113 precache_fetcher_->Start();
114 }
115
116 void PrecacheManager::CancelPrecaching() {
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
118
119 {
120 base::AutoLock lock(lock_);
121 if (!is_precaching_) {
122 // Do nothing if precaching is not in progress.
123 return;
124 }
125 is_precaching_ = false;
126 }
127
128 // Destroying the |precache_fetcher_| will cancel any fetch in progress.
129 precache_fetcher_.reset();
130 }
131
132 bool PrecacheManager::is_precaching() {
133 base::AutoLock lock(lock_);
134 return is_precaching_;
135 }
136
137 scoped_refptr<PrecacheDatabase> PrecacheManager::precache_database() {
138 return precache_database_;
139 }
140
141 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698