Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: components/precache/core/precache_database.h

Issue 1961153003: Add pause/resume functionality to precache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_tables.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 // Represents the precache work that is left over after a precache session
39 // is interrupted.
40 class UnfinishedWork : public base::RefCountedThreadSafe<UnfinishedWork> {
sclittle 2016/05/10 00:01:26 See my comment in precache_fetcher.cc about passin
bengr 2016/05/19 01:25:44 Done.
41 public:
42 // Constructor used when saving unfinished work to storage.
43 // |manifest_urls_to_fetch| and |resource_urls_to_fetch| are the
44 // remaining manifest and resource URLs to fetch. |total_response_bytes|
45 // and |network_response_bytes| are counts of bytes receieved, the former
46 // including bytes received from local cache. |num_manifest_urls_to_fetch|
47 // is the initial number of manifest URLs that needed to be fetched. The
48 // |start_time_of_session| is the time the precache session began, before
49 // any interruptions.
50 UnfinishedWork(const std::list<GURL>& manifest_urls_to_fetch,
51 const std::list<GURL>& resource_urls_to_fetch,
52 int64_t total_response_bytes,
53 int64_t network_response_bytes,
54 int64_t num_manifest_urls_to_fetch,
55 const base::TimeTicks& start_time_of_session);
56
57 // Constructor used when retrieving unfinished work from storage. No work
58 // saved before |earliest_start_time_of_session| will be retrieved.
59 explicit UnfinishedWork(
60 const base::TimeTicks& earliest_start_time_of_session);
61
62 // Manifest URLs remaining to be fetched.
63 std::list<GURL> manifests;
sclittle 2016/05/10 00:01:26 If you just have publicly visible members, then co
bengr 2016/05/19 01:25:44 Done.
64
65 // Resource URLs remaining to be fetched.
66 std::list<GURL> resources;
67
68 // Count of the total received bytes.
69 int64_t total_bytes;
70
71 // Count of the total received bytes over the network.
72 int64_t network_bytes;
73
74 // The total number of manifest URLs that the precache session started with.
75 int64_t num_manifest_urls;
76
77 // The original time the precache session started, not the time it resumed.
78 base::TimeTicks start_time;
sclittle 2016/05/10 00:01:26 Should this be base::Time instead? I don't think i
bengr 2016/05/19 01:25:44 Done.
79
80 private:
81 friend class base::RefCountedThreadSafe<UnfinishedWork>;
82
83 ~UnfinishedWork();
84
85 DISALLOW_COPY_AND_ASSIGN(UnfinishedWork);
86 };
87
34 // Class that tracks information related to precaching. This class may be 88 // 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 89 // constructed on any thread, but all calls to, and destruction of this class
36 // must be done on the the DB thread. 90 // must be done on the the DB thread.
37 class PrecacheDatabase { 91 class PrecacheDatabase {
38 public: 92 public:
39 // A PrecacheDatabase can be constructed on any thread. 93 // A PrecacheDatabase can be constructed on any thread.
40 PrecacheDatabase(); 94 PrecacheDatabase();
41 95
42 ~PrecacheDatabase(); 96 ~PrecacheDatabase();
43 97
(...skipping 20 matching lines...) Expand all
64 // the fetch was not motivated by precaching. |is_connection_cellular| 118 // the fetch was not motivated by precaching. |is_connection_cellular|
65 // indicates whether the current network connection is a cellular network. 119 // indicates whether the current network connection is a cellular network.
66 void RecordURLNonPrefetch(const GURL& url, 120 void RecordURLNonPrefetch(const GURL& url,
67 const base::TimeDelta& latency, 121 const base::TimeDelta& latency,
68 const base::Time& fetch_time, 122 const base::Time& fetch_time,
69 int64_t size, 123 int64_t size,
70 bool was_cached, 124 bool was_cached,
71 int host_rank, 125 int host_rank,
72 bool is_connection_cellular); 126 bool is_connection_cellular);
73 127
128 // Gets the state required to continue a precache session.
129 void GetUnfinishedWork(scoped_refptr<UnfinishedWork> unfinished_work);
130
131 // Stores the state required to continue a precache session so that the
132 // session can be resumed later.
133 void SaveUnfinishedWork(scoped_refptr<UnfinishedWork> unfinished_work);
134
135 // Clears all precache session state.
136 void ClearPrecacheSessionState();
137
138 base::WeakPtr<PrecacheDatabase> GetWeakPtr();
139
74 private: 140 private:
75 friend class PrecacheDatabaseTest; 141 friend class PrecacheDatabaseTest;
76 142
77 bool IsDatabaseAccessible() const; 143 bool IsDatabaseAccessible() const;
78 144
79 // Flushes any buffered write operations. |buffered_writes_| will be empty 145 // Flushes any buffered write operations. |buffered_writes_| will be empty
80 // after calling this function. To maximize performance, all the buffered 146 // after calling this function. To maximize performance, all the buffered
81 // writes are run in a single database transaction. 147 // writes are run in a single database transaction.
82 void Flush(); 148 void Flush();
83 149
84 // Same as Flush(), but also updates the flag |is_flush_posted_| to indicate 150 // Same as Flush(), but also updates the flag |is_flush_posted_| to indicate
85 // that a flush is no longer posted. 151 // that a flush is no longer posted.
86 void PostedFlush(); 152 void PostedFlush();
87 153
88 // Post a call to PostedFlush() on the current thread's MessageLoop, if 154 // 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 155 // |buffered_writes_| is non-empty and there isn't already a flush call
90 // posted. 156 // posted.
91 void MaybePostFlush(); 157 void MaybePostFlush();
92 158
93 std::unique_ptr<sql::Connection> db_; 159 std::unique_ptr<sql::Connection> db_;
94 160
95 // Table that keeps track of URLs that are in the cache because of precaching, 161 // 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, 162 // 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(). 163 // then this table will not be up to date until the next call to Flush().
98 PrecacheURLTable precache_url_table_; 164 PrecacheURLTable precache_url_table_;
99 165
166 // Tables that persist the state required to pause and resume a precache
167 // session.
168 PrecacheSessionTables precache_session_tables_;
169
100 // A vector of write operations to be run on the database. 170 // A vector of write operations to be run on the database.
101 std::vector<base::Closure> buffered_writes_; 171 std::vector<base::Closure> buffered_writes_;
102 172
103 // Set of URLs that have been modified in |buffered_writes_|. It's a hash set 173 // 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. 174 // of strings, and not GURLs, because there is no hash function on GURL.
105 base::hash_set<std::string> buffered_urls_; 175 base::hash_set<std::string> buffered_urls_;
106 176
107 // Flag indicating whether or not a call to Flush() has been posted to run in 177 // Flag indicating whether or not a call to Flush() has been posted to run in
108 // the future. 178 // the future.
109 bool is_flush_posted_; 179 bool is_flush_posted_;
110 180
111 // ThreadChecker used to ensure that all methods other than the constructor 181 // ThreadChecker used to ensure that all methods other than the constructor
112 // or destructor are called on the same thread. 182 // or destructor are called on the same thread.
113 base::ThreadChecker thread_checker_; 183 base::ThreadChecker thread_checker_;
114 184
115 base::WeakPtrFactory<PrecacheDatabase> weak_factory_; 185 base::WeakPtrFactory<PrecacheDatabase> weak_factory_;
116 186
117 DISALLOW_COPY_AND_ASSIGN(PrecacheDatabase); 187 DISALLOW_COPY_AND_ASSIGN(PrecacheDatabase);
118 }; 188 };
119 189
120 } // namespace precache 190 } // namespace precache
121 191
122 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_ 192 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_DATABASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698