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

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: Addressed comments 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/time/time.h"
12 #include "components/precache/core/precache_database.h"
13 #include "components/precache/core/precache_switches.h"
14 #include "components/precache/core/url_list_provider.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "sql/connection.h"
18
19 using content::BrowserThread;
20
21 namespace {
22
23 const base::FilePath::CharType kPrecacheDatabaseName[] =
24 FILE_PATH_LITERAL("PrecacheDatabase");
25
26 void InitPrecacheDatabaseOnDBThread(
27 base::FilePath dir_path,
28 scoped_refptr<precache::PrecacheDatabase> precache_database) {
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
30
31 scoped_ptr<sql::Connection> db(new sql::Connection());
32 if (!db->Open(dir_path.Append(kPrecacheDatabaseName))) {
33 DLOG(WARNING) << "Could not open precache database. No precache data will "
34 "be tracked.";
35 }
36
37 // Initialize the precache database with the connection even if it failed to
38 // open. The precache database will take ownership of the connection.
39 precache_database->Init(db.Pass());
40 }
41
42 } // namespace
43
44 namespace precache {
45
46 PrecacheManager::PrecacheManager(content::BrowserContext* browser_context)
47 : browser_context_(browser_context),
48 precache_database_(new PrecacheDatabase()),
49 is_precaching_(false) {
50 BrowserThread::PostTask(
51 BrowserThread::DB, FROM_HERE,
52 base::Bind(&InitPrecacheDatabaseOnDBThread, browser_context->GetPath(),
53 precache_database_));
54 }
55
56 PrecacheManager::~PrecacheManager() {}
57
58 // static
59 bool PrecacheManager::IsPrecachingEnabled() {
60 // TODO(sclittle): Test for Finch trial as well when it exists.
61 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePrecache);
62 }
63
64 void PrecacheManager::Shutdown() {
65 CancelPrecaching();
66 }
67
68 void PrecacheManager::OnDone() {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
70
71 {
72 base::AutoLock lock(lock_);
73 is_precaching_ = false;
74 }
75
76 precache_fetcher_.reset();
77 }
78
79 void PrecacheManager::StartPrecaching(
80 URLListProvider* interesting_urls_provider) {
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
82
83 {
84 base::AutoLock lock(lock_);
85 if (is_precaching_) {
86 DLOG(WARNING) << "Cannot start precaching because precaching is already "
87 "in progress.";
88 return;
89 }
90 is_precaching_ = true;
91 }
92
93 // The start of the day before today. Don't just use (base::Time::Now() -
94 // base::TimeDelta::FromDays(1)).LocalMidnight() because that wouldn't account
95 // for daylight savings, i.e. some days are 23 or 25 hours long.
96 base::Time yesterday = (base::Time::Now().LocalMidnight() -
97 base::TimeDelta::FromHours(1)).LocalMidnight();
98
99 // Report any UMA of precache stats for days before the current date and
100 // delete expired precache history.
101 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
102 base::Bind(&PrecacheDatabase::ReportAndDeleteOldStats,
103 precache_database_, yesterday));
104
105 interesting_urls_provider->GetURLs(
106 base::Bind(&PrecacheManager::OnURLsReceived, base::Unretained(this)));
107 }
108
109 void PrecacheManager::CancelPrecaching() {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
111
112 {
113 base::AutoLock lock(lock_);
114 if (!is_precaching_) {
115 // Do nothing if precaching is not in progress.
116 return;
117 }
118 is_precaching_ = false;
119 }
120
121 // Destroying the |precache_fetcher_| will cancel any fetch in progress.
122 precache_fetcher_.reset();
123 }
124
125 bool PrecacheManager::IsPrecaching() {
126 base::AutoLock lock(lock_);
127 return is_precaching_;
128 }
129
130 scoped_refptr<PrecacheDatabase> PrecacheManager::precache_database() {
131 return precache_database_;
132 }
133
134 void PrecacheManager::OnURLsReceived(const std::list<GURL>& interesting_urls) {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
136
137 if (!IsPrecaching()) {
138 // Don't start precaching if it was canceled while waiting for the list of
139 // interesting URLs.
140 return;
141 }
142
143 // Start precaching.
144 precache_fetcher_.reset(new PrecacheFetcher(
145 interesting_urls, browser_context_->GetRequestContext(), this));
146 precache_fetcher_->Start();
147 }
148
149 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698