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.
5 #ifndef COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_
6 #define COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "components/precache/core/precache_url_table.h"
32 // Class that tracks information related to precaching. This class can be
33 // constructed or destroyed on any threads, but all other methods must be called
34 // on the same thread (e.g. the DB thread).
35 class PrecacheDatabase
: public base::RefCountedThreadSafe
<PrecacheDatabase
> {
37 // A PrecacheDatabase can be constructed on any thread.
40 // Initializes the precache database, using the specified database file path.
41 // Init must be called before any other methods.
42 bool Init(const base::FilePath
& db_path
);
44 // Deletes precache history from the precache URL table that is more than 60
45 // days older than |current_time|.
46 void DeleteExpiredPrecacheHistory(const base::Time
& current_time
);
48 // Delete all history entries from the database.
51 // Report precache-related metrics in response to a URL being fetched, where
52 // the fetch was motivated by precaching.
53 void RecordURLPrefetch(const GURL
& url
,
54 const base::TimeDelta
& latency
,
55 const base::Time
& fetch_time
,
59 // Report precache-related metrics in response to a URL being fetched, where
60 // the fetch was not motivated by precaching. |is_connection_cellular|
61 // indicates whether the current network connection is a cellular network.
62 void RecordURLNonPrefetch(const GURL
& url
,
63 const base::TimeDelta
& latency
,
64 const base::Time
& fetch_time
,
68 bool is_connection_cellular
);
71 friend class base::RefCountedThreadSafe
<PrecacheDatabase
>;
72 friend class PrecacheDatabaseTest
;
76 bool IsDatabaseAccessible() const;
78 // Flushes any buffered write operations. |buffered_writes_| will be empty
79 // after calling this function. To maximize performance, all the buffered
80 // writes are run in a single database transaction.
83 // Same as Flush(), but also updates the flag |is_flush_posted_| to indicate
84 // that a flush is no longer posted.
87 // Post a call to PostedFlush() on the current thread's MessageLoop, if
88 // |buffered_writes_| is non-empty and there isn't already a flush call
90 void MaybePostFlush();
92 scoped_ptr
<sql::Connection
> db_
;
94 // Table that keeps track of URLs that are in the cache because of precaching,
95 // and wouldn't be in the cache otherwise. If |buffered_writes_| is non-empty,
96 // then this table will not be up to date until the next call to Flush().
97 PrecacheURLTable precache_url_table_
;
99 // A vector of write operations to be run on the database.
100 std::vector
<base::Closure
> buffered_writes_
;
102 // Set of URLs that have been modified in |buffered_writes_|. It's a hash set
103 // of strings, and not GURLs, because there is no hash function on GURL.
104 base::hash_set
<std::string
> buffered_urls_
;
106 // Flag indicating whether or not a call to Flush() has been posted to run in
108 bool is_flush_posted_
;
110 // ThreadChecker used to ensure that all methods other than the constructor
111 // or destructor are called on the same thread.
112 base::ThreadChecker thread_checker_
;
114 DISALLOW_COPY_AND_ASSIGN(PrecacheDatabase
);
117 } // namespace precache
119 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_