Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / precache / core / precache_database.h
blob6aa341c0738757d474a73cb15999d27e03f795dd
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_
8 #include <string>
9 #include <vector>
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"
19 class GURL;
21 namespace base {
22 class FilePath;
23 class Time;
26 namespace sql {
27 class Connection;
30 namespace precache {
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> {
36 public:
37 // A PrecacheDatabase can be constructed on any thread.
38 PrecacheDatabase();
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.
49 void ClearHistory();
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,
56 int64 size,
57 bool was_cached);
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,
65 int64 size,
66 bool was_cached,
67 int host_rank,
68 bool is_connection_cellular);
70 private:
71 friend class base::RefCountedThreadSafe<PrecacheDatabase>;
72 friend class PrecacheDatabaseTest;
74 ~PrecacheDatabase();
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.
81 void Flush();
83 // Same as Flush(), but also updates the flag |is_flush_posted_| to indicate
84 // that a flush is no longer posted.
85 void PostedFlush();
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
89 // posted.
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
107 // the future.
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_