OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_ | 5 #ifndef COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_ |
6 #define COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_ | 6 #define COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <list> |
10 #include <memory> | 11 #include <memory> |
11 #include <string> | 12 #include <string> |
12 #include <vector> | 13 #include <vector> |
13 | 14 |
14 #include "base/callback.h" | 15 #include "base/callback.h" |
15 #include "base/containers/hash_tables.h" | 16 #include "base/containers/hash_tables.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ref_counted.h" |
17 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
18 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
| 21 #include "components/precache/core/precache_fetcher.h" |
| 22 #include "components/precache/core/precache_session_table.h" |
19 #include "components/precache/core/precache_url_table.h" | 23 #include "components/precache/core/precache_url_table.h" |
20 | 24 |
21 class GURL; | 25 class GURL; |
22 | 26 |
23 namespace base { | 27 namespace base { |
24 class FilePath; | 28 class FilePath; |
25 class Time; | 29 class Time; |
26 } | 30 } |
27 | 31 |
28 namespace sql { | 32 namespace sql { |
29 class Connection; | 33 class Connection; |
30 } | 34 } |
31 | 35 |
32 namespace precache { | 36 namespace precache { |
33 | 37 |
| 38 class PrecacheUnfinishedWork; |
| 39 |
34 // Class that tracks information related to precaching. This class may be | 40 // Class that tracks information related to precaching. This class may be |
35 // constructed on any thread, but all calls to, and destruction of this class | 41 // constructed on any thread, but all calls to, and destruction of this class |
36 // must be done on the the DB thread. | 42 // must be done on the the DB thread. |
37 class PrecacheDatabase { | 43 class PrecacheDatabase { |
38 public: | 44 public: |
39 // A PrecacheDatabase can be constructed on any thread. | 45 // A PrecacheDatabase can be constructed on any thread. |
40 PrecacheDatabase(); | 46 PrecacheDatabase(); |
41 | 47 |
42 ~PrecacheDatabase(); | 48 ~PrecacheDatabase(); |
43 | 49 |
(...skipping 20 matching lines...) Expand all Loading... |
64 // the fetch was not motivated by precaching. |is_connection_cellular| | 70 // the fetch was not motivated by precaching. |is_connection_cellular| |
65 // indicates whether the current network connection is a cellular network. | 71 // indicates whether the current network connection is a cellular network. |
66 void RecordURLNonPrefetch(const GURL& url, | 72 void RecordURLNonPrefetch(const GURL& url, |
67 const base::TimeDelta& latency, | 73 const base::TimeDelta& latency, |
68 const base::Time& fetch_time, | 74 const base::Time& fetch_time, |
69 int64_t size, | 75 int64_t size, |
70 bool was_cached, | 76 bool was_cached, |
71 int host_rank, | 77 int host_rank, |
72 bool is_connection_cellular); | 78 bool is_connection_cellular); |
73 | 79 |
| 80 // Gets the state required to continue a precache session. |
| 81 std::unique_ptr<PrecacheUnfinishedWork> GetUnfinishedWork(); |
| 82 |
| 83 // Stores the state required to continue a precache session so that the |
| 84 // session can be resumed later. |
| 85 void SaveUnfinishedWork( |
| 86 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work); |
| 87 |
| 88 // Deletes unfinished work from the database. |
| 89 void DeleteUnfinishedWork(); |
| 90 |
| 91 base::WeakPtr<PrecacheDatabase> GetWeakPtr(); |
| 92 |
74 private: | 93 private: |
75 friend class PrecacheDatabaseTest; | 94 friend class PrecacheDatabaseTest; |
76 | 95 |
77 bool IsDatabaseAccessible() const; | 96 bool IsDatabaseAccessible() const; |
78 | 97 |
79 // Flushes any buffered write operations. |buffered_writes_| will be empty | 98 // Flushes any buffered write operations. |buffered_writes_| will be empty |
80 // after calling this function. To maximize performance, all the buffered | 99 // after calling this function. To maximize performance, all the buffered |
81 // writes are run in a single database transaction. | 100 // writes are run in a single database transaction. |
82 void Flush(); | 101 void Flush(); |
83 | 102 |
84 // Same as Flush(), but also updates the flag |is_flush_posted_| to indicate | 103 // Same as Flush(), but also updates the flag |is_flush_posted_| to indicate |
85 // that a flush is no longer posted. | 104 // that a flush is no longer posted. |
86 void PostedFlush(); | 105 void PostedFlush(); |
87 | 106 |
88 // Post a call to PostedFlush() on the current thread's MessageLoop, if | 107 // Post a call to PostedFlush() on the current thread's MessageLoop, if |
89 // |buffered_writes_| is non-empty and there isn't already a flush call | 108 // |buffered_writes_| is non-empty and there isn't already a flush call |
90 // posted. | 109 // posted. |
91 void MaybePostFlush(); | 110 void MaybePostFlush(); |
92 | 111 |
93 std::unique_ptr<sql::Connection> db_; | 112 std::unique_ptr<sql::Connection> db_; |
94 | 113 |
95 // Table that keeps track of URLs that are in the cache because of precaching, | 114 // Table that keeps track of URLs that are in the cache because of precaching, |
96 // and wouldn't be in the cache otherwise. If |buffered_writes_| is non-empty, | 115 // and wouldn't be in the cache otherwise. If |buffered_writes_| is non-empty, |
97 // then this table will not be up to date until the next call to Flush(). | 116 // then this table will not be up to date until the next call to Flush(). |
98 PrecacheURLTable precache_url_table_; | 117 PrecacheURLTable precache_url_table_; |
99 | 118 |
| 119 // Table that persists state related to a precache session, including |
| 120 // unfinished work to be done. |
| 121 PrecacheSessionTable precache_session_table_; |
| 122 |
100 // A vector of write operations to be run on the database. | 123 // A vector of write operations to be run on the database. |
101 std::vector<base::Closure> buffered_writes_; | 124 std::vector<base::Closure> buffered_writes_; |
102 | 125 |
103 // Set of URLs that have been modified in |buffered_writes_|. It's a hash set | 126 // Set of URLs that have been modified in |buffered_writes_|. It's a hash set |
104 // of strings, and not GURLs, because there is no hash function on GURL. | 127 // of strings, and not GURLs, because there is no hash function on GURL. |
105 base::hash_set<std::string> buffered_urls_; | 128 base::hash_set<std::string> buffered_urls_; |
106 | 129 |
107 // Flag indicating whether or not a call to Flush() has been posted to run in | 130 // Flag indicating whether or not a call to Flush() has been posted to run in |
108 // the future. | 131 // the future. |
109 bool is_flush_posted_; | 132 bool is_flush_posted_; |
110 | 133 |
111 // ThreadChecker used to ensure that all methods other than the constructor | 134 // ThreadChecker used to ensure that all methods other than the constructor |
112 // or destructor are called on the same thread. | 135 // or destructor are called on the same thread. |
113 base::ThreadChecker thread_checker_; | 136 base::ThreadChecker thread_checker_; |
114 | 137 |
115 base::WeakPtrFactory<PrecacheDatabase> weak_factory_; | 138 base::WeakPtrFactory<PrecacheDatabase> weak_factory_; |
116 | 139 |
117 DISALLOW_COPY_AND_ASSIGN(PrecacheDatabase); | 140 DISALLOW_COPY_AND_ASSIGN(PrecacheDatabase); |
118 }; | 141 }; |
119 | 142 |
120 } // namespace precache | 143 } // namespace precache |
121 | 144 |
122 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_ | 145 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_ |
OLD | NEW |