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 RecordURLPrecached(const GURL
& url
, const base::Time
& fetch_time
,
54 int64 size
, bool was_cached
);
56 // Report precache-related metrics in response to a URL being fetched, where
57 // the fetch was not motivated by precaching. |is_connection_cellular|
58 // indicates whether the current network connection is a cellular network.
59 void RecordURLFetched(const GURL
& url
, const base::Time
& fetch_time
,
60 int64 size
, bool was_cached
,
61 bool is_connection_cellular
);
64 friend class base::RefCountedThreadSafe
<PrecacheDatabase
>;
65 friend class PrecacheDatabaseTest
;
69 bool IsDatabaseAccessible() const;
71 // Flushes any buffered write operations. |buffered_writes_| will be empty
72 // after calling this function. To maximize performance, all the buffered
73 // writes are run in a single database transaction.
76 // Same as Flush(), but also updates the flag |is_flush_posted_| to indicate
77 // that a flush is no longer posted.
80 // Post a call to PostedFlush() on the current thread's MessageLoop, if
81 // |buffered_writes_| is non-empty and there isn't already a flush call
83 void MaybePostFlush();
85 scoped_ptr
<sql::Connection
> db_
;
87 // Table that keeps track of URLs that are in the cache because of precaching,
88 // and wouldn't be in the cache otherwise. If |buffered_writes_| is non-empty,
89 // then this table will not be up to date until the next call to Flush().
90 PrecacheURLTable precache_url_table_
;
92 // A vector of write operations to be run on the database.
93 std::vector
<base::Closure
> buffered_writes_
;
95 // Set of URLs that have been modified in |buffered_writes_|. It's a hash set
96 // of strings, and not GURLs, because there is no hash function on GURL.
97 base::hash_set
<std::string
> buffered_urls_
;
99 // Flag indicating whether or not a call to Flush() has been posted to run in
101 bool is_flush_posted_
;
103 // ThreadChecker used to ensure that all methods other than the constructor
104 // or destructor are called on the same thread.
105 base::ThreadChecker thread_checker_
;
107 DISALLOW_COPY_AND_ASSIGN(PrecacheDatabase
);
110 } // namespace precache
112 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_