1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// 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 <string>
#include <utility>
#include <vector>
#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/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "components/history/core/browser/history_service.h"
#include "components/precache/core/precache_database.h"
#include "components/precache/core/precache_switches.h"
#include "components/sync_driver/sync_service.h"
#include "components/variations/variations_associated_data.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";
const char kPrecacheFieldTrialControlGroup[] = "Control";
const char kConfigURLParam[] = "config_url";
const char kManifestURLPrefixParam[] = "manifest_url_prefix";
const size_t kNumTopHosts = 100;
} // namespace
namespace precache {
size_t NumTopHosts() {
return kNumTopHosts;
}
PrecacheManager::PrecacheManager(
content::BrowserContext* browser_context,
const sync_driver::SyncService* const sync_service,
const history::HistoryService* const history_service)
: browser_context_(browser_context),
sync_service_(sync_service),
history_service_(history_service),
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::IsInExperimentGroup() const {
// Verify IsPrecachingAllowed() before calling FieldTrialList::FindFullName().
// This is because field trials are only assigned when requested. This allows
// us to create Control and Experiment groups that are limited to users for
// whom PrecachingAllowed() is true, thus accentuating the impact of
// precaching.
return IsPrecachingAllowed() &&
(base::StartsWith(
base::FieldTrialList::FindFullName(kPrecacheFieldTrialName),
kPrecacheFieldTrialEnabledGroup, base::CompareCase::SENSITIVE) ||
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnablePrecache));
}
bool PrecacheManager::IsInControlGroup() const {
// Verify IsPrecachingAllowed() before calling FindFullName(). See
// PrecacheManager::IsInExperimentGroup() for an explanation of why.
return IsPrecachingAllowed() &&
base::StartsWith(
base::FieldTrialList::FindFullName(kPrecacheFieldTrialName),
kPrecacheFieldTrialControlGroup, base::CompareCase::SENSITIVE);
}
bool PrecacheManager::IsPrecachingAllowed() const {
return PrecachingAllowed() == AllowedType::ALLOWED;
}
PrecacheManager::AllowedType PrecacheManager::PrecachingAllowed() const {
if (!(sync_service_ && sync_service_->IsBackendInitialized()))
return AllowedType::PENDING;
// SyncService delegates to SyncPrefs, which must be called on the UI thread.
if (history_service_ &&
sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) &&
!sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS))
return AllowedType::ALLOWED;
return AllowedType::DISALLOWED;
}
void PrecacheManager::StartPrecaching(
const PrecacheCompletionCallback& precache_completion_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (is_precaching_) {
DLOG(WARNING) << "Cannot start precaching because precaching is already "
"in progress.";
return;
}
precache_completion_callback_ = precache_completion_callback;
if (IsInExperimentGroup()) {
is_precaching_ = true;
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory,
precache_database_, base::Time::Now()));
// Request NumTopHosts() top hosts. Note that PrecacheFetcher is further
// bound by the value of PrecacheConfigurationSettings.top_sites_count, as
// retrieved from the server.
history_service_->TopHosts(
NumTopHosts(),
base::Bind(&PrecacheManager::OnHostsReceived, AsWeakPtr()));
} else if (IsInControlGroup()) {
// Set is_precaching_ so that the longer delay is placed between calls to
// TopHosts.
is_precaching_ = true;
// Calculate TopHosts solely for metrics purposes.
history_service_->TopHosts(
NumTopHosts(),
base::Bind(&PrecacheManager::OnHostsReceivedThenDone, AsWeakPtr()));
} else {
if (PrecachingAllowed() != AllowedType::PENDING) {
// We are not waiting on the sync backend to be initialized. The user
// either is not in the field trial, or does not have sync enabled.
// Pretend that precaching started, so that the PrecacheServiceLauncher
// doesn't try to start it again.
is_precaching_ = true;
}
OnDone();
}
}
void PrecacheManager::CancelPrecaching() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
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();
// Uninitialize the callback so that any scoped_refptrs in it are released.
precache_completion_callback_.Reset();
}
bool PrecacheManager::IsPrecaching() const {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return is_precaching_;
}
void PrecacheManager::RecordStatsForFetch(const GURL& url,
const GURL& referrer,
const base::TimeDelta& latency,
const base::Time& fetch_time,
int64_t size,
bool was_cached) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (size == 0 || url.is_empty() || !url.SchemeIsHTTPOrHTTPS()) {
// Ignore empty responses, empty URLs, or URLs that aren't HTTP or HTTPS.
return;
}
if (!history_service_)
return;
history_service_->HostRankIfAvailable(
referrer,
base::Bind(&PrecacheManager::RecordStatsForFetchInternal, AsWeakPtr(),
url, latency, fetch_time, size, was_cached));
}
void PrecacheManager::RecordStatsForFetchInternal(
const GURL& url,
const base::TimeDelta& latency,
const base::Time& fetch_time,
int64_t size,
bool was_cached,
int host_rank) {
if (is_precaching_) {
// Assume that precache is responsible for all requests made while
// precaching is currently in progress.
// TODO(sclittle): Make PrecacheFetcher explicitly mark precache-motivated
// fetches, and use that to determine whether or not a fetch was motivated
// by precaching.
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(&PrecacheDatabase::RecordURLPrefetch, precache_database_,
url, latency, 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::RecordURLNonPrefetch, precache_database_,
url, latency, fetch_time, size, was_cached, host_rank,
is_connection_cellular));
}
}
void PrecacheManager::ClearHistory() {
// PrecacheDatabase::ClearHistory must run after PrecacheDatabase::Init has
// finished. Using PostNonNestableTask guarantees this, by definition. See
// base::SequencedTaskRunner for details.
BrowserThread::PostNonNestableTask(
BrowserThread::DB, FROM_HERE,
base::Bind(&PrecacheDatabase::ClearHistory, precache_database_));
}
void PrecacheManager::Shutdown() {
CancelPrecaching();
}
void PrecacheManager::OnDone() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
precache_fetcher_.reset();
// Run completion callback if not null. It's null if the client is in the
// Control group and CancelPrecaching is called before TopHosts computation
// finishes.
if (!precache_completion_callback_.is_null()) {
precache_completion_callback_.Run(!is_precaching_);
// Uninitialize the callback so that any scoped_refptrs in it are released.
precache_completion_callback_.Reset();
}
is_precaching_ = false;
}
void PrecacheManager::OnHostsReceived(
const history::TopHostsList& host_counts) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!is_precaching_) {
// Don't start precaching if it was canceled while waiting for the list of
// hosts.
return;
}
std::vector<std::string> hosts;
for (const auto& host_count : host_counts)
hosts.push_back(host_count.first);
// Start precaching.
precache_fetcher_.reset(
new PrecacheFetcher(hosts, browser_context_->GetRequestContext(),
GURL(variations::GetVariationParamValue(
kPrecacheFieldTrialName, kConfigURLParam)),
variations::GetVariationParamValue(
kPrecacheFieldTrialName, kManifestURLPrefixParam),
this));
precache_fetcher_->Start();
}
void PrecacheManager::OnHostsReceivedThenDone(
const history::TopHostsList& host_counts) {
OnDone();
}
} // namespace precache
|