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
|
// 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.
#ifndef COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_
#define COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_
#include <stdint.h>
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/containers/hash_tables.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "components/precache/core/precache_url_table.h"
class GURL;
namespace base {
class FilePath;
class Time;
}
namespace sql {
class Connection;
}
namespace precache {
// Class that tracks information related to precaching. This class may be
// constructed on any thread, but all calls to, and destruction of this class
// must be done on the the DB thread.
class PrecacheDatabase {
public:
// A PrecacheDatabase can be constructed on any thread.
PrecacheDatabase();
~PrecacheDatabase();
// Initializes the precache database, using the specified database file path.
// Init must be called before any other methods.
bool Init(const base::FilePath& db_path);
// Deletes precache history from the precache URL table that is more than 60
// days older than |current_time|.
void DeleteExpiredPrecacheHistory(const base::Time& current_time);
// Delete all history entries from the database.
void ClearHistory();
// Report precache-related metrics in response to a URL being fetched, where
// the fetch was motivated by precaching.
void RecordURLPrefetch(const GURL& url,
const base::TimeDelta& latency,
const base::Time& fetch_time,
int64_t size,
bool was_cached);
// Report precache-related metrics in response to a URL being fetched, where
// the fetch was not motivated by precaching. |is_connection_cellular|
// indicates whether the current network connection is a cellular network.
void RecordURLNonPrefetch(const GURL& url,
const base::TimeDelta& latency,
const base::Time& fetch_time,
int64_t size,
bool was_cached,
int host_rank,
bool is_connection_cellular);
private:
friend class PrecacheDatabaseTest;
bool IsDatabaseAccessible() const;
// Flushes any buffered write operations. |buffered_writes_| will be empty
// after calling this function. To maximize performance, all the buffered
// writes are run in a single database transaction.
void Flush();
// Same as Flush(), but also updates the flag |is_flush_posted_| to indicate
// that a flush is no longer posted.
void PostedFlush();
// Post a call to PostedFlush() on the current thread's MessageLoop, if
// |buffered_writes_| is non-empty and there isn't already a flush call
// posted.
void MaybePostFlush();
scoped_ptr<sql::Connection> db_;
// Table that keeps track of URLs that are in the cache because of precaching,
// and wouldn't be in the cache otherwise. If |buffered_writes_| is non-empty,
// then this table will not be up to date until the next call to Flush().
PrecacheURLTable precache_url_table_;
// A vector of write operations to be run on the database.
std::vector<base::Closure> buffered_writes_;
// Set of URLs that have been modified in |buffered_writes_|. It's a hash set
// of strings, and not GURLs, because there is no hash function on GURL.
base::hash_set<std::string> buffered_urls_;
// Flag indicating whether or not a call to Flush() has been posted to run in
// the future.
bool is_flush_posted_;
// ThreadChecker used to ensure that all methods other than the constructor
// or destructor are called on the same thread.
base::ThreadChecker thread_checker_;
base::WeakPtrFactory<PrecacheDatabase> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(PrecacheDatabase);
};
} // namespace precache
#endif // COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_
|