Remove now obsolete checkdeps copy
[chromium-blink-merge.git] / components / precache / core / precache_database.h
blob47fd2dace346c0df8847b70525d7c56b945b84da
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 // Report precache-related metrics in response to a URL being fetched, where
49 // the fetch was motivated by precaching.
50 void RecordURLPrecached(const GURL& url, const base::Time& fetch_time,
51 int64 size, bool was_cached);
53 // Report precache-related metrics in response to a URL being fetched, where
54 // the fetch was not motivated by precaching. |is_connection_cellular|
55 // indicates whether the current network connection is a cellular network.
56 void RecordURLFetched(const GURL& url, const base::Time& fetch_time,
57 int64 size, bool was_cached,
58 bool is_connection_cellular);
60 private:
61 friend class base::RefCountedThreadSafe<PrecacheDatabase>;
62 friend class PrecacheDatabaseTest;
64 ~PrecacheDatabase();
66 bool IsDatabaseAccessible() const;
68 // Flushes any buffered write operations. |buffered_writes_| will be empty
69 // after calling this function. To maximize performance, all the buffered
70 // writes are run in a single database transaction.
71 void Flush();
73 // Same as Flush(), but also updates the flag |is_flush_posted_| to indicate
74 // that a flush is no longer posted.
75 void PostedFlush();
77 // Post a call to PostedFlush() on the current thread's MessageLoop, if
78 // |buffered_writes_| is non-empty and there isn't already a flush call
79 // posted.
80 void MaybePostFlush();
82 scoped_ptr<sql::Connection> db_;
84 // Table that keeps track of URLs that are in the cache because of precaching,
85 // and wouldn't be in the cache otherwise. If |buffered_writes_| is non-empty,
86 // then this table will not be up to date until the next call to Flush().
87 PrecacheURLTable precache_url_table_;
89 // A vector of write operations to be run on the database.
90 std::vector<base::Closure> buffered_writes_;
92 // Set of URLs that have been modified in |buffered_writes_|. It's a hash set
93 // of strings, and not GURLs, because there is no hash function on GURL.
94 base::hash_set<std::string> buffered_urls_;
96 // Flag indicating whether or not a call to Flush() has been posted to run in
97 // the future.
98 bool is_flush_posted_;
100 // ThreadChecker used to ensure that all methods other than the constructor
101 // or destructor are called on the same thread.
102 base::ThreadChecker thread_checker_;
104 DISALLOW_COPY_AND_ASSIGN(PrecacheDatabase);
107 } // namespace precache
109 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_