OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "content/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "net/extras/sqlite/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <list> | |
8 #include <map> | 7 #include <map> |
9 #include <set> | 8 #include <set> |
10 #include <utility> | |
11 | 9 |
12 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
13 #include "base/bind.h" | 11 #include "base/bind.h" |
14 #include "base/callback.h" | 12 #include "base/callback.h" |
15 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
17 #include "base/location.h" | 15 #include "base/location.h" |
18 #include "base/logging.h" | 16 #include "base/logging.h" |
19 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
20 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
21 #include "base/metrics/field_trial.h" | 19 #include "base/metrics/field_trial.h" |
22 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
23 #include "base/profiler/scoped_tracker.h" | 21 #include "base/profiler/scoped_tracker.h" |
24 #include "base/sequenced_task_runner.h" | 22 #include "base/sequenced_task_runner.h" |
25 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
26 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
27 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
28 #include "base/threading/sequenced_worker_pool.h" | 26 #include "base/threading/sequenced_worker_pool.h" |
29 #include "base/time/time.h" | 27 #include "base/time/time.h" |
30 #include "content/public/browser/browser_thread.h" | |
31 #include "content/public/browser/cookie_store_factory.h" | |
32 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 28 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
33 #include "net/cookies/canonical_cookie.h" | 29 #include "net/cookies/canonical_cookie.h" |
34 #include "net/cookies/cookie_constants.h" | 30 #include "net/cookies/cookie_constants.h" |
35 #include "net/cookies/cookie_util.h" | 31 #include "net/cookies/cookie_util.h" |
36 #include "net/extras/sqlite/cookie_crypto_delegate.h" | 32 #include "net/extras/sqlite/cookie_crypto_delegate.h" |
37 #include "sql/error_delegate_util.h" | 33 #include "sql/error_delegate_util.h" |
38 #include "sql/meta_table.h" | 34 #include "sql/meta_table.h" |
39 #include "sql/statement.h" | 35 #include "sql/statement.h" |
40 #include "sql/transaction.h" | 36 #include "sql/transaction.h" |
41 #include "storage/browser/quota/special_storage_policy.h" | |
42 #include "third_party/sqlite/sqlite3.h" | |
43 #include "url/gurl.h" | 37 #include "url/gurl.h" |
44 | 38 |
45 using base::Time; | 39 using base::Time; |
46 | 40 |
47 namespace { | 41 namespace { |
48 | 42 |
49 // The persistent cookie store is loaded into memory on eTLD at a time. This | 43 // The persistent cookie store is loaded into memory on eTLD at a time. This |
50 // variable controls the delay between loading eTLDs, so as to not overload the | 44 // variable controls the delay between loading eTLDs, so as to not overload the |
51 // CPU or I/O with these low priority requests immediately after start up. | 45 // CPU or I/O with these low priority requests immediately after start up. |
52 const int kLoadDelayMilliseconds = 0; | 46 const int kLoadDelayMilliseconds = 0; |
53 | 47 |
54 } // namespace | 48 } // namespace |
55 | 49 |
56 namespace content { | 50 namespace net { |
57 | 51 |
58 // This class is designed to be shared between any client thread and the | 52 // This class is designed to be shared between any client thread and the |
59 // background task runner. It batches operations and commits them on a timer. | 53 // background task runner. It batches operations and commits them on a timer. |
60 // | 54 // |
61 // SQLitePersistentCookieStore::Load is called to load all cookies. It | 55 // SQLitePersistentCookieStore::Load is called to load all cookies. It |
62 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread | 56 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread |
63 // task to the background runner. This task calls Backend::ChainLoadCookies(), | 57 // task to the background runner. This task calls Backend::ChainLoadCookies(), |
64 // which repeatedly posts itself to the BG runner to load each eTLD+1's cookies | 58 // which repeatedly posts itself to the BG runner to load each eTLD+1's cookies |
65 // in separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is | 59 // in separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is |
66 // posted to the client runner, which notifies the caller of | 60 // posted to the client runner, which notifies the caller of |
(...skipping 11 matching lines...) Expand all Loading... |
78 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(), | 72 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(), |
79 // whichever occurs first. | 73 // whichever occurs first. |
80 class SQLitePersistentCookieStore::Backend | 74 class SQLitePersistentCookieStore::Backend |
81 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { | 75 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { |
82 public: | 76 public: |
83 Backend( | 77 Backend( |
84 const base::FilePath& path, | 78 const base::FilePath& path, |
85 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 79 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
86 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 80 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
87 bool restore_old_session_cookies, | 81 bool restore_old_session_cookies, |
88 storage::SpecialStoragePolicy* special_storage_policy, | 82 CookieCryptoDelegate* crypto_delegate) |
89 net::CookieCryptoDelegate* crypto_delegate) | |
90 : path_(path), | 83 : path_(path), |
91 num_pending_(0), | 84 num_pending_(0), |
92 force_keep_session_state_(false), | |
93 initialized_(false), | 85 initialized_(false), |
94 corruption_detected_(false), | 86 corruption_detected_(false), |
95 restore_old_session_cookies_(restore_old_session_cookies), | 87 restore_old_session_cookies_(restore_old_session_cookies), |
96 special_storage_policy_(special_storage_policy), | |
97 num_cookies_read_(0), | 88 num_cookies_read_(0), |
98 client_task_runner_(client_task_runner), | 89 client_task_runner_(client_task_runner), |
99 background_task_runner_(background_task_runner), | 90 background_task_runner_(background_task_runner), |
100 num_priority_waiting_(0), | 91 num_priority_waiting_(0), |
101 total_priority_requests_(0), | 92 total_priority_requests_(0), |
102 crypto_(crypto_delegate) {} | 93 crypto_(crypto_delegate) {} |
103 | 94 |
104 // Creates or loads the SQLite database. | 95 // Creates or loads the SQLite database. |
105 void Load(const LoadedCallback& loaded_callback); | 96 void Load(const LoadedCallback& loaded_callback); |
106 | 97 |
107 // Loads cookies for the domain key (eTLD+1). | 98 // Loads cookies for the domain key (eTLD+1). |
108 void LoadCookiesForKey(const std::string& domain, | 99 void LoadCookiesForKey(const std::string& domain, |
109 const LoadedCallback& loaded_callback); | 100 const LoadedCallback& loaded_callback); |
110 | 101 |
111 // Steps through all results of |smt|, makes a cookie from each, and adds the | 102 // Steps through all results of |smt|, makes a cookie from each, and adds the |
112 // cookie to |cookies|. This method also updates |cookies_per_origin_| and | 103 // cookie to |cookies|. This method also updates |num_cookies_read_|. |
113 // |num_cookies_read_|. | 104 void MakeCookiesFromSQLStatement(std::vector<CanonicalCookie*>* cookies, |
114 void MakeCookiesFromSQLStatement(std::vector<net::CanonicalCookie*>* cookies, | |
115 sql::Statement* statement); | 105 sql::Statement* statement); |
116 | 106 |
117 // Batch a cookie addition. | 107 // Batch a cookie addition. |
118 void AddCookie(const net::CanonicalCookie& cc); | 108 void AddCookie(const CanonicalCookie& cc); |
119 | 109 |
120 // Batch a cookie access time update. | 110 // Batch a cookie access time update. |
121 void UpdateCookieAccessTime(const net::CanonicalCookie& cc); | 111 void UpdateCookieAccessTime(const CanonicalCookie& cc); |
122 | 112 |
123 // Batch a cookie deletion. | 113 // Batch a cookie deletion. |
124 void DeleteCookie(const net::CanonicalCookie& cc); | 114 void DeleteCookie(const CanonicalCookie& cc); |
125 | 115 |
126 // Commit pending operations as soon as possible. | 116 // Commit pending operations as soon as possible. |
127 void Flush(const base::Closure& callback); | 117 void Flush(const base::Closure& callback); |
128 | 118 |
129 // Commit any pending operations and close the database. This must be called | 119 // Commit any pending operations and close the database. This must be called |
130 // before the object is destructed. | 120 // before the object is destructed. |
131 void Close(); | 121 void Close(); |
132 | 122 |
133 void SetForceKeepSessionState(); | 123 // Post background delete of all cookies that match |cookies|. |
| 124 void DeleteAllInList(const std::list<CookieOrigin>& cookies); |
134 | 125 |
135 private: | 126 private: |
136 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; | 127 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; |
137 | 128 |
138 // You should call Close() before destructing this object. | 129 // You should call Close() before destructing this object. |
139 ~Backend() { | 130 ~Backend() { |
140 DCHECK(!db_.get()) << "Close should have already been called."; | 131 DCHECK(!db_.get()) << "Close should have already been called."; |
141 DCHECK(num_pending_ == 0 && pending_.empty()); | 132 DCHECK_EQ(0u, num_pending_); |
| 133 DCHECK(pending_.empty()); |
142 | 134 |
143 for (net::CanonicalCookie* cookie : cookies_) { | 135 for (CanonicalCookie* cookie : cookies_) { |
144 delete cookie; | 136 delete cookie; |
145 } | 137 } |
146 } | 138 } |
147 | 139 |
148 // Database upgrade statements. | 140 // Database upgrade statements. |
149 bool EnsureDatabaseVersion(); | 141 bool EnsureDatabaseVersion(); |
150 | 142 |
151 class PendingOperation { | 143 class PendingOperation { |
152 public: | 144 public: |
153 typedef enum { | 145 enum OperationType { |
154 COOKIE_ADD, | 146 COOKIE_ADD, |
155 COOKIE_UPDATEACCESS, | 147 COOKIE_UPDATEACCESS, |
156 COOKIE_DELETE, | 148 COOKIE_DELETE, |
157 } OperationType; | 149 }; |
158 | 150 |
159 PendingOperation(OperationType op, const net::CanonicalCookie& cc) | 151 PendingOperation(OperationType op, const CanonicalCookie& cc) |
160 : op_(op), cc_(cc) { } | 152 : op_(op), cc_(cc) {} |
161 | 153 |
162 OperationType op() const { return op_; } | 154 OperationType op() const { return op_; } |
163 const net::CanonicalCookie& cc() const { return cc_; } | 155 const CanonicalCookie& cc() const { return cc_; } |
164 | 156 |
165 private: | 157 private: |
166 OperationType op_; | 158 OperationType op_; |
167 net::CanonicalCookie cc_; | 159 CanonicalCookie cc_; |
168 }; | 160 }; |
169 | 161 |
170 private: | 162 private: |
171 // Creates or loads the SQLite database on background runner. | 163 // Creates or loads the SQLite database on background runner. |
172 void LoadAndNotifyInBackground(const LoadedCallback& loaded_callback, | 164 void LoadAndNotifyInBackground(const LoadedCallback& loaded_callback, |
173 const base::Time& posted_at); | 165 const base::Time& posted_at); |
174 | 166 |
175 // Loads cookies for the domain key (eTLD+1) on background runner. | 167 // Loads cookies for the domain key (eTLD+1) on background runner. |
176 void LoadKeyAndNotifyInBackground(const std::string& domains, | 168 void LoadKeyAndNotifyInBackground(const std::string& domains, |
177 const LoadedCallback& loaded_callback, | 169 const LoadedCallback& loaded_callback, |
(...skipping 30 matching lines...) Expand all Loading... |
208 // Loads cookies for the next domain key from the DB, then either reschedules | 200 // Loads cookies for the next domain key from the DB, then either reschedules |
209 // itself or schedules the provided callback to run on the client runner (if | 201 // itself or schedules the provided callback to run on the client runner (if |
210 // all domains are loaded). | 202 // all domains are loaded). |
211 void ChainLoadCookies(const LoadedCallback& loaded_callback); | 203 void ChainLoadCookies(const LoadedCallback& loaded_callback); |
212 | 204 |
213 // Load all cookies for a set of domains/hosts | 205 // Load all cookies for a set of domains/hosts |
214 bool LoadCookiesForDomains(const std::set<std::string>& key); | 206 bool LoadCookiesForDomains(const std::set<std::string>& key); |
215 | 207 |
216 // Batch a cookie operation (add or delete) | 208 // Batch a cookie operation (add or delete) |
217 void BatchOperation(PendingOperation::OperationType op, | 209 void BatchOperation(PendingOperation::OperationType op, |
218 const net::CanonicalCookie& cc); | 210 const CanonicalCookie& cc); |
219 // Commit our pending operations to the database. | 211 // Commit our pending operations to the database. |
220 void Commit(); | 212 void Commit(); |
221 // Close() executed on the background runner. | 213 // Close() executed on the background runner. |
222 void InternalBackgroundClose(); | 214 void InternalBackgroundClose(); |
223 | 215 |
224 void DeleteSessionCookiesOnStartup(); | 216 void DeleteSessionCookiesOnStartup(); |
225 | 217 |
226 void DeleteSessionCookiesOnShutdown(); | 218 void BackgroundDeleteAllInList(const std::list<CookieOrigin>& cookies); |
227 | 219 |
228 void DatabaseErrorCallback(int error, sql::Statement* stmt); | 220 void DatabaseErrorCallback(int error, sql::Statement* stmt); |
229 void KillDatabase(); | 221 void KillDatabase(); |
230 | 222 |
231 void PostBackgroundTask(const tracked_objects::Location& origin, | 223 void PostBackgroundTask(const tracked_objects::Location& origin, |
232 const base::Closure& task); | 224 const base::Closure& task); |
233 void PostClientTask(const tracked_objects::Location& origin, | 225 void PostClientTask(const tracked_objects::Location& origin, |
234 const base::Closure& task); | 226 const base::Closure& task); |
235 | 227 |
236 // Shared code between the different load strategies to be used after all | 228 // Shared code between the different load strategies to be used after all |
237 // cookies have been loaded. | 229 // cookies have been loaded. |
238 void FinishedLoadingCookies(const LoadedCallback& loaded_callback, | 230 void FinishedLoadingCookies(const LoadedCallback& loaded_callback, |
239 bool success); | 231 bool success); |
240 | 232 |
241 base::FilePath path_; | 233 const base::FilePath path_; |
242 scoped_ptr<sql::Connection> db_; | 234 scoped_ptr<sql::Connection> db_; |
243 sql::MetaTable meta_table_; | 235 sql::MetaTable meta_table_; |
244 | 236 |
245 typedef std::list<PendingOperation*> PendingOperationsList; | 237 typedef std::list<PendingOperation*> PendingOperationsList; |
246 PendingOperationsList pending_; | 238 PendingOperationsList pending_; |
247 PendingOperationsList::size_type num_pending_; | 239 PendingOperationsList::size_type num_pending_; |
248 // True if the persistent store should skip delete on exit rules. | 240 // Guard |cookies_|, |pending_|, |num_pending_|. |
249 bool force_keep_session_state_; | |
250 // Guard |cookies_|, |pending_|, |num_pending_|, |force_keep_session_state_| | |
251 base::Lock lock_; | 241 base::Lock lock_; |
252 | 242 |
253 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce | 243 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce |
254 // the number of messages sent to the client runner. Sent back in response to | 244 // the number of messages sent to the client runner. Sent back in response to |
255 // individual load requests for domain keys or when all loading completes. | 245 // individual load requests for domain keys or when all loading completes. |
256 // Ownership of the cookies in this vector is transferred to the client in | 246 // Ownership of the cookies in this vector is transferred to the client in |
257 // response to individual load requests or when all loading completes. | 247 // response to individual load requests or when all loading completes. |
258 std::vector<net::CanonicalCookie*> cookies_; | 248 std::vector<CanonicalCookie*> cookies_; |
259 | 249 |
260 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. | 250 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. |
261 std::map<std::string, std::set<std::string> > keys_to_load_; | 251 std::map<std::string, std::set<std::string>> keys_to_load_; |
262 | |
263 // Map of (domain keys(eTLD+1), is secure cookie) to number of cookies in the | |
264 // database. | |
265 typedef std::pair<std::string, bool> CookieOrigin; | |
266 typedef std::map<CookieOrigin, int> CookiesPerOriginMap; | |
267 CookiesPerOriginMap cookies_per_origin_; | |
268 | 252 |
269 // Indicates if DB has been initialized. | 253 // Indicates if DB has been initialized. |
270 bool initialized_; | 254 bool initialized_; |
271 | 255 |
272 // Indicates if the kill-database callback has been scheduled. | 256 // Indicates if the kill-database callback has been scheduled. |
273 bool corruption_detected_; | 257 bool corruption_detected_; |
274 | 258 |
275 // If false, we should filter out session cookies when reading the DB. | 259 // If false, we should filter out session cookies when reading the DB. |
276 bool restore_old_session_cookies_; | 260 bool restore_old_session_cookies_; |
277 | 261 |
278 // Policy defining what data is deleted on shutdown. | |
279 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_; | |
280 | |
281 // The cumulative time spent loading the cookies on the background runner. | 262 // The cumulative time spent loading the cookies on the background runner. |
282 // Incremented and reported from the background runner. | 263 // Incremented and reported from the background runner. |
283 base::TimeDelta cookie_load_duration_; | 264 base::TimeDelta cookie_load_duration_; |
284 | 265 |
285 // The total number of cookies read. Incremented and reported on the | 266 // The total number of cookies read. Incremented and reported on the |
286 // background runner. | 267 // background runner. |
287 int num_cookies_read_; | 268 int num_cookies_read_; |
288 | 269 |
289 scoped_refptr<base::SequencedTaskRunner> client_task_runner_; | 270 scoped_refptr<base::SequencedTaskRunner> client_task_runner_; |
290 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | 271 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
291 | 272 |
292 // Guards the following metrics-related properties (only accessed when | 273 // Guards the following metrics-related properties (only accessed when |
293 // starting/completing priority loads or completing the total load). | 274 // starting/completing priority loads or completing the total load). |
294 base::Lock metrics_lock_; | 275 base::Lock metrics_lock_; |
295 int num_priority_waiting_; | 276 int num_priority_waiting_; |
296 // The total number of priority requests. | 277 // The total number of priority requests. |
297 int total_priority_requests_; | 278 int total_priority_requests_; |
298 // The time when |num_priority_waiting_| incremented to 1. | 279 // The time when |num_priority_waiting_| incremented to 1. |
299 base::Time current_priority_wait_start_; | 280 base::Time current_priority_wait_start_; |
300 // The cumulative duration of time when |num_priority_waiting_| was greater | 281 // The cumulative duration of time when |num_priority_waiting_| was greater |
301 // than 1. | 282 // than 1. |
302 base::TimeDelta priority_wait_duration_; | 283 base::TimeDelta priority_wait_duration_; |
303 // Class with functions that do cryptographic operations (for protecting | 284 // Class with functions that do cryptographic operations (for protecting |
304 // cookies stored persistently). | 285 // cookies stored persistently). |
305 // | 286 // |
306 // Not owned. | 287 // Not owned. |
307 net::CookieCryptoDelegate* crypto_; | 288 CookieCryptoDelegate* crypto_; |
308 | 289 |
309 DISALLOW_COPY_AND_ASSIGN(Backend); | 290 DISALLOW_COPY_AND_ASSIGN(Backend); |
310 }; | 291 }; |
311 | 292 |
312 namespace { | 293 namespace { |
313 | 294 |
314 // Version number of the database. | 295 // Version number of the database. |
315 // | 296 // |
316 // Version 9 adds a partial index to track non-persistent cookies. | 297 // Version 9 adds a partial index to track non-persistent cookies. |
317 // Non-persistent cookies sometimes need to be deleted on startup. There are | 298 // Non-persistent cookies sometimes need to be deleted on startup. There are |
(...skipping 28 matching lines...) Expand all Loading... |
346 const int kCurrentVersionNumber = 9; | 327 const int kCurrentVersionNumber = 9; |
347 const int kCompatibleVersionNumber = 5; | 328 const int kCompatibleVersionNumber = 5; |
348 | 329 |
349 // Possible values for the 'priority' column. | 330 // Possible values for the 'priority' column. |
350 enum DBCookiePriority { | 331 enum DBCookiePriority { |
351 kCookiePriorityLow = 0, | 332 kCookiePriorityLow = 0, |
352 kCookiePriorityMedium = 1, | 333 kCookiePriorityMedium = 1, |
353 kCookiePriorityHigh = 2, | 334 kCookiePriorityHigh = 2, |
354 }; | 335 }; |
355 | 336 |
356 DBCookiePriority CookiePriorityToDBCookiePriority(net::CookiePriority value) { | 337 DBCookiePriority CookiePriorityToDBCookiePriority(CookiePriority value) { |
357 switch (value) { | 338 switch (value) { |
358 case net::COOKIE_PRIORITY_LOW: | 339 case COOKIE_PRIORITY_LOW: |
359 return kCookiePriorityLow; | 340 return kCookiePriorityLow; |
360 case net::COOKIE_PRIORITY_MEDIUM: | 341 case COOKIE_PRIORITY_MEDIUM: |
361 return kCookiePriorityMedium; | 342 return kCookiePriorityMedium; |
362 case net::COOKIE_PRIORITY_HIGH: | 343 case COOKIE_PRIORITY_HIGH: |
363 return kCookiePriorityHigh; | 344 return kCookiePriorityHigh; |
364 } | 345 } |
365 | 346 |
366 NOTREACHED(); | 347 NOTREACHED(); |
367 return kCookiePriorityMedium; | 348 return kCookiePriorityMedium; |
368 } | 349 } |
369 | 350 |
370 net::CookiePriority DBCookiePriorityToCookiePriority(DBCookiePriority value) { | 351 CookiePriority DBCookiePriorityToCookiePriority(DBCookiePriority value) { |
371 switch (value) { | 352 switch (value) { |
372 case kCookiePriorityLow: | 353 case kCookiePriorityLow: |
373 return net::COOKIE_PRIORITY_LOW; | 354 return COOKIE_PRIORITY_LOW; |
374 case kCookiePriorityMedium: | 355 case kCookiePriorityMedium: |
375 return net::COOKIE_PRIORITY_MEDIUM; | 356 return COOKIE_PRIORITY_MEDIUM; |
376 case kCookiePriorityHigh: | 357 case kCookiePriorityHigh: |
377 return net::COOKIE_PRIORITY_HIGH; | 358 return COOKIE_PRIORITY_HIGH; |
378 } | 359 } |
379 | 360 |
380 NOTREACHED(); | 361 NOTREACHED(); |
381 return net::COOKIE_PRIORITY_DEFAULT; | 362 return COOKIE_PRIORITY_DEFAULT; |
382 } | 363 } |
383 | 364 |
384 // Increments a specified TimeDelta by the duration between this object's | 365 // Increments a specified TimeDelta by the duration between this object's |
385 // constructor and destructor. Not thread safe. Multiple instances may be | 366 // constructor and destructor. Not thread safe. Multiple instances may be |
386 // created with the same delta instance as long as their lifetimes are nested. | 367 // created with the same delta instance as long as their lifetimes are nested. |
387 // The shortest lived instances have no impact. | 368 // The shortest lived instances have no impact. |
388 class IncrementTimeDelta { | 369 class IncrementTimeDelta { |
389 public: | 370 public: |
390 explicit IncrementTimeDelta(base::TimeDelta* delta) : | 371 explicit IncrementTimeDelta(base::TimeDelta* delta) |
391 delta_(delta), | 372 : delta_(delta), original_value_(*delta), start_(base::Time::Now()) {} |
392 original_value_(*delta), | |
393 start_(base::Time::Now()) {} | |
394 | 373 |
395 ~IncrementTimeDelta() { | 374 ~IncrementTimeDelta() { |
396 *delta_ = original_value_ + base::Time::Now() - start_; | 375 *delta_ = original_value_ + base::Time::Now() - start_; |
397 } | 376 } |
398 | 377 |
399 private: | 378 private: |
400 base::TimeDelta* delta_; | 379 base::TimeDelta* delta_; |
401 base::TimeDelta original_value_; | 380 base::TimeDelta original_value_; |
402 base::Time start_; | 381 base::Time start_; |
403 | 382 |
(...skipping 14 matching lines...) Expand all Loading... |
418 "path TEXT NOT NULL," | 397 "path TEXT NOT NULL," |
419 "expires_utc INTEGER NOT NULL," | 398 "expires_utc INTEGER NOT NULL," |
420 "secure INTEGER NOT NULL," | 399 "secure INTEGER NOT NULL," |
421 "httponly INTEGER NOT NULL," | 400 "httponly INTEGER NOT NULL," |
422 "last_access_utc INTEGER NOT NULL, " | 401 "last_access_utc INTEGER NOT NULL, " |
423 "has_expires INTEGER NOT NULL DEFAULT 1, " | 402 "has_expires INTEGER NOT NULL DEFAULT 1, " |
424 "persistent INTEGER NOT NULL DEFAULT 1," | 403 "persistent INTEGER NOT NULL DEFAULT 1," |
425 "priority INTEGER NOT NULL DEFAULT %d," | 404 "priority INTEGER NOT NULL DEFAULT %d," |
426 "encrypted_value BLOB DEFAULT ''," | 405 "encrypted_value BLOB DEFAULT ''," |
427 "firstpartyonly INTEGER NOT NULL DEFAULT 0)", | 406 "firstpartyonly INTEGER NOT NULL DEFAULT 0)", |
428 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); | 407 CookiePriorityToDBCookiePriority(COOKIE_PRIORITY_DEFAULT))); |
429 if (!db->Execute(stmt.c_str())) | 408 if (!db->Execute(stmt.c_str())) |
430 return false; | 409 return false; |
431 | 410 |
432 if (!db->Execute("CREATE INDEX domain ON cookies(host_key)")) | 411 if (!db->Execute("CREATE INDEX domain ON cookies(host_key)")) |
433 return false; | 412 return false; |
434 | 413 |
435 #if defined(OS_IOS) | 414 #if defined(OS_IOS) |
436 // iOS 8.1 and older doesn't support partial indices. iOS 8.2 supports | 415 // iOS 8.1 and older doesn't support partial indices. iOS 8.2 supports |
437 // partial indices. | 416 // partial indices. |
438 if (!db->Execute("CREATE INDEX is_transient ON cookies(persistent)")) { | 417 if (!db->Execute("CREATE INDEX is_transient ON cookies(persistent)")) { |
439 #else | 418 #else |
440 if (!db->Execute( | 419 if (!db->Execute( |
441 "CREATE INDEX is_transient ON cookies(persistent) " | 420 "CREATE INDEX is_transient ON cookies(persistent) " |
442 "where persistent != 1")) { | 421 "where persistent != 1")) { |
443 #endif | 422 #endif |
444 return false; | 423 return false; |
445 } | 424 } |
446 | 425 |
447 return true; | 426 return true; |
448 } | 427 } |
449 | 428 |
450 } // namespace | 429 } // namespace |
451 | 430 |
452 void SQLitePersistentCookieStore::Backend::Load( | 431 void SQLitePersistentCookieStore::Backend::Load( |
453 const LoadedCallback& loaded_callback) { | 432 const LoadedCallback& loaded_callback) { |
454 PostBackgroundTask(FROM_HERE, base::Bind( | 433 PostBackgroundTask(FROM_HERE, |
455 &Backend::LoadAndNotifyInBackground, this, | 434 base::Bind(&Backend::LoadAndNotifyInBackground, this, |
456 loaded_callback, base::Time::Now())); | 435 loaded_callback, base::Time::Now())); |
457 } | 436 } |
458 | 437 |
459 void SQLitePersistentCookieStore::Backend::LoadCookiesForKey( | 438 void SQLitePersistentCookieStore::Backend::LoadCookiesForKey( |
460 const std::string& key, | 439 const std::string& key, |
461 const LoadedCallback& loaded_callback) { | 440 const LoadedCallback& loaded_callback) { |
462 { | 441 { |
463 base::AutoLock locked(metrics_lock_); | 442 base::AutoLock locked(metrics_lock_); |
464 if (num_priority_waiting_ == 0) | 443 if (num_priority_waiting_ == 0) |
465 current_priority_wait_start_ = base::Time::Now(); | 444 current_priority_wait_start_ = base::Time::Now(); |
466 num_priority_waiting_++; | 445 num_priority_waiting_++; |
467 total_priority_requests_++; | 446 total_priority_requests_++; |
468 } | 447 } |
469 | 448 |
470 PostBackgroundTask(FROM_HERE, base::Bind( | 449 PostBackgroundTask( |
471 &Backend::LoadKeyAndNotifyInBackground, | 450 FROM_HERE, base::Bind(&Backend::LoadKeyAndNotifyInBackground, this, key, |
472 this, key, loaded_callback, base::Time::Now())); | 451 loaded_callback, base::Time::Now())); |
473 } | 452 } |
474 | 453 |
475 void SQLitePersistentCookieStore::Backend::LoadAndNotifyInBackground( | 454 void SQLitePersistentCookieStore::Backend::LoadAndNotifyInBackground( |
476 const LoadedCallback& loaded_callback, const base::Time& posted_at) { | 455 const LoadedCallback& loaded_callback, |
| 456 const base::Time& posted_at) { |
477 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 457 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
478 IncrementTimeDelta increment(&cookie_load_duration_); | 458 IncrementTimeDelta increment(&cookie_load_duration_); |
479 | 459 |
480 UMA_HISTOGRAM_CUSTOM_TIMES( | 460 UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.TimeLoadDBQueueWait", |
481 "Cookie.TimeLoadDBQueueWait", | 461 base::Time::Now() - posted_at, |
482 base::Time::Now() - posted_at, | 462 base::TimeDelta::FromMilliseconds(1), |
483 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 463 base::TimeDelta::FromMinutes(1), 50); |
484 50); | |
485 | 464 |
486 if (!InitializeDatabase()) { | 465 if (!InitializeDatabase()) { |
487 PostClientTask(FROM_HERE, base::Bind( | 466 PostClientTask(FROM_HERE, base::Bind(&Backend::CompleteLoadInForeground, |
488 &Backend::CompleteLoadInForeground, this, loaded_callback, false)); | 467 this, loaded_callback, false)); |
489 } else { | 468 } else { |
490 ChainLoadCookies(loaded_callback); | 469 ChainLoadCookies(loaded_callback); |
491 } | 470 } |
492 } | 471 } |
493 | 472 |
494 void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyInBackground( | 473 void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyInBackground( |
495 const std::string& key, | 474 const std::string& key, |
496 const LoadedCallback& loaded_callback, | 475 const LoadedCallback& loaded_callback, |
497 const base::Time& posted_at) { | 476 const base::Time& posted_at) { |
498 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 477 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
499 IncrementTimeDelta increment(&cookie_load_duration_); | 478 IncrementTimeDelta increment(&cookie_load_duration_); |
500 | 479 |
501 UMA_HISTOGRAM_CUSTOM_TIMES( | 480 UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.TimeKeyLoadDBQueueWait", |
502 "Cookie.TimeKeyLoadDBQueueWait", | 481 base::Time::Now() - posted_at, |
503 base::Time::Now() - posted_at, | 482 base::TimeDelta::FromMilliseconds(1), |
504 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 483 base::TimeDelta::FromMinutes(1), 50); |
505 50); | |
506 | 484 |
507 bool success = false; | 485 bool success = false; |
508 if (InitializeDatabase()) { | 486 if (InitializeDatabase()) { |
509 std::map<std::string, std::set<std::string> >::iterator | 487 std::map<std::string, std::set<std::string>>::iterator it = |
510 it = keys_to_load_.find(key); | 488 keys_to_load_.find(key); |
511 if (it != keys_to_load_.end()) { | 489 if (it != keys_to_load_.end()) { |
512 success = LoadCookiesForDomains(it->second); | 490 success = LoadCookiesForDomains(it->second); |
513 keys_to_load_.erase(it); | 491 keys_to_load_.erase(it); |
514 } else { | 492 } else { |
515 success = true; | 493 success = true; |
516 } | 494 } |
517 } | 495 } |
518 | 496 |
519 PostClientTask(FROM_HERE, base::Bind( | 497 PostClientTask( |
520 &SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground, | 498 FROM_HERE, |
521 this, loaded_callback, success, posted_at)); | 499 base::Bind( |
| 500 &SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground, |
| 501 this, loaded_callback, success, posted_at)); |
522 } | 502 } |
523 | 503 |
524 void SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground( | 504 void SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground( |
525 const LoadedCallback& loaded_callback, | 505 const LoadedCallback& loaded_callback, |
526 bool load_success, | 506 bool load_success, |
527 const::Time& requested_at) { | 507 const ::Time& requested_at) { |
528 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); | 508 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); |
529 | 509 |
530 UMA_HISTOGRAM_CUSTOM_TIMES( | 510 UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.TimeKeyLoadTotalWait", |
531 "Cookie.TimeKeyLoadTotalWait", | 511 base::Time::Now() - requested_at, |
532 base::Time::Now() - requested_at, | 512 base::TimeDelta::FromMilliseconds(1), |
533 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 513 base::TimeDelta::FromMinutes(1), 50); |
534 50); | |
535 | 514 |
536 Notify(loaded_callback, load_success); | 515 Notify(loaded_callback, load_success); |
537 | 516 |
538 { | 517 { |
539 base::AutoLock locked(metrics_lock_); | 518 base::AutoLock locked(metrics_lock_); |
540 num_priority_waiting_--; | 519 num_priority_waiting_--; |
541 if (num_priority_waiting_ == 0) { | 520 if (num_priority_waiting_ == 0) { |
542 priority_wait_duration_ += | 521 priority_wait_duration_ += |
543 base::Time::Now() - current_priority_wait_start_; | 522 base::Time::Now() - current_priority_wait_start_; |
544 } | 523 } |
545 } | 524 } |
546 | |
547 } | 525 } |
548 | 526 |
549 void SQLitePersistentCookieStore::Backend::ReportMetricsInBackground() { | 527 void SQLitePersistentCookieStore::Backend::ReportMetricsInBackground() { |
550 UMA_HISTOGRAM_CUSTOM_TIMES( | 528 UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.TimeLoad", cookie_load_duration_, |
551 "Cookie.TimeLoad", | 529 base::TimeDelta::FromMilliseconds(1), |
552 cookie_load_duration_, | 530 base::TimeDelta::FromMinutes(1), 50); |
553 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | |
554 50); | |
555 } | 531 } |
556 | 532 |
557 void SQLitePersistentCookieStore::Backend::ReportMetrics() { | 533 void SQLitePersistentCookieStore::Backend::ReportMetrics() { |
558 PostBackgroundTask(FROM_HERE, base::Bind( | 534 PostBackgroundTask( |
559 &SQLitePersistentCookieStore::Backend::ReportMetricsInBackground, this)); | 535 FROM_HERE, |
| 536 base::Bind( |
| 537 &SQLitePersistentCookieStore::Backend::ReportMetricsInBackground, |
| 538 this)); |
560 | 539 |
561 { | 540 { |
562 base::AutoLock locked(metrics_lock_); | 541 base::AutoLock locked(metrics_lock_); |
563 UMA_HISTOGRAM_CUSTOM_TIMES( | 542 UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.PriorityBlockingTime", |
564 "Cookie.PriorityBlockingTime", | 543 priority_wait_duration_, |
565 priority_wait_duration_, | 544 base::TimeDelta::FromMilliseconds(1), |
566 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 545 base::TimeDelta::FromMinutes(1), 50); |
567 50); | |
568 | 546 |
569 UMA_HISTOGRAM_COUNTS_100( | 547 UMA_HISTOGRAM_COUNTS_100("Cookie.PriorityLoadCount", |
570 "Cookie.PriorityLoadCount", | 548 total_priority_requests_); |
571 total_priority_requests_); | |
572 | 549 |
573 UMA_HISTOGRAM_COUNTS_10000( | 550 UMA_HISTOGRAM_COUNTS_10000("Cookie.NumberOfLoadedCookies", |
574 "Cookie.NumberOfLoadedCookies", | 551 num_cookies_read_); |
575 num_cookies_read_); | |
576 } | 552 } |
577 } | 553 } |
578 | 554 |
579 void SQLitePersistentCookieStore::Backend::CompleteLoadInForeground( | 555 void SQLitePersistentCookieStore::Backend::CompleteLoadInForeground( |
580 const LoadedCallback& loaded_callback, bool load_success) { | 556 const LoadedCallback& loaded_callback, |
| 557 bool load_success) { |
581 Notify(loaded_callback, load_success); | 558 Notify(loaded_callback, load_success); |
582 | 559 |
583 if (load_success) | 560 if (load_success) |
584 ReportMetrics(); | 561 ReportMetrics(); |
585 } | 562 } |
586 | 563 |
587 void SQLitePersistentCookieStore::Backend::Notify( | 564 void SQLitePersistentCookieStore::Backend::Notify( |
588 const LoadedCallback& loaded_callback, | 565 const LoadedCallback& loaded_callback, |
589 bool load_success) { | 566 bool load_success) { |
590 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); | 567 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); |
591 | 568 |
592 std::vector<net::CanonicalCookie*> cookies; | 569 std::vector<CanonicalCookie*> cookies; |
593 { | 570 { |
594 base::AutoLock locked(lock_); | 571 base::AutoLock locked(lock_); |
595 cookies.swap(cookies_); | 572 cookies.swap(cookies_); |
596 } | 573 } |
597 | 574 |
598 loaded_callback.Run(cookies); | 575 loaded_callback.Run(cookies); |
599 } | 576 } |
600 | 577 |
601 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { | 578 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { |
602 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 579 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
603 | 580 |
604 if (initialized_ || corruption_detected_) { | 581 if (initialized_ || corruption_detected_) { |
605 // Return false if we were previously initialized but the DB has since been | 582 // Return false if we were previously initialized but the DB has since been |
606 // closed, or if corruption caused a database reset during initialization. | 583 // closed, or if corruption caused a database reset during initialization. |
607 return db_ != NULL; | 584 return db_ != NULL; |
608 } | 585 } |
609 | 586 |
610 base::Time start = base::Time::Now(); | 587 base::Time start = base::Time::Now(); |
611 | 588 |
612 const base::FilePath dir = path_.DirName(); | 589 const base::FilePath dir = path_.DirName(); |
613 if (!base::PathExists(dir) && !base::CreateDirectory(dir)) { | 590 if (!base::PathExists(dir) && !base::CreateDirectory(dir)) { |
614 return false; | 591 return false; |
615 } | 592 } |
616 | 593 |
617 int64 db_size = 0; | 594 int64 db_size = 0; |
618 if (base::GetFileSize(path_, &db_size)) | 595 if (base::GetFileSize(path_, &db_size)) |
619 UMA_HISTOGRAM_COUNTS("Cookie.DBSizeInKB", db_size / 1024 ); | 596 UMA_HISTOGRAM_COUNTS("Cookie.DBSizeInKB", db_size / 1024); |
620 | 597 |
621 db_.reset(new sql::Connection); | 598 db_.reset(new sql::Connection); |
622 db_->set_histogram_tag("Cookie"); | 599 db_->set_histogram_tag("Cookie"); |
623 | 600 |
624 // Unretained to avoid a ref loop with |db_|. | 601 // Unretained to avoid a ref loop with |db_|. |
625 db_->set_error_callback( | 602 db_->set_error_callback( |
626 base::Bind(&SQLitePersistentCookieStore::Backend::DatabaseErrorCallback, | 603 base::Bind(&SQLitePersistentCookieStore::Backend::DatabaseErrorCallback, |
627 base::Unretained(this))); | 604 base::Unretained(this))); |
628 | 605 |
629 if (!db_->Open(path_)) { | 606 if (!db_->Open(path_)) { |
630 NOTREACHED() << "Unable to open cookie DB."; | 607 NOTREACHED() << "Unable to open cookie DB."; |
631 if (corruption_detected_) | 608 if (corruption_detected_) |
632 db_->Raze(); | 609 db_->Raze(); |
633 meta_table_.Reset(); | 610 meta_table_.Reset(); |
634 db_.reset(); | 611 db_.reset(); |
635 return false; | 612 return false; |
636 } | 613 } |
637 | 614 |
638 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { | 615 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { |
639 NOTREACHED() << "Unable to open cookie DB."; | 616 NOTREACHED() << "Unable to open cookie DB."; |
640 if (corruption_detected_) | 617 if (corruption_detected_) |
641 db_->Raze(); | 618 db_->Raze(); |
642 meta_table_.Reset(); | 619 meta_table_.Reset(); |
643 db_.reset(); | 620 db_.reset(); |
644 return false; | 621 return false; |
645 } | 622 } |
646 | 623 |
647 UMA_HISTOGRAM_CUSTOM_TIMES( | 624 UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.TimeInitializeDB", |
648 "Cookie.TimeInitializeDB", | 625 base::Time::Now() - start, |
649 base::Time::Now() - start, | 626 base::TimeDelta::FromMilliseconds(1), |
650 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 627 base::TimeDelta::FromMinutes(1), 50); |
651 50); | |
652 | 628 |
653 start = base::Time::Now(); | 629 start = base::Time::Now(); |
654 | 630 |
655 // Retrieve all the domains | 631 // Retrieve all the domains |
656 sql::Statement smt(db_->GetUniqueStatement( | 632 sql::Statement smt( |
657 "SELECT DISTINCT host_key FROM cookies")); | 633 db_->GetUniqueStatement("SELECT DISTINCT host_key FROM cookies")); |
658 | 634 |
659 if (!smt.is_valid()) { | 635 if (!smt.is_valid()) { |
660 if (corruption_detected_) | 636 if (corruption_detected_) |
661 db_->Raze(); | 637 db_->Raze(); |
662 meta_table_.Reset(); | 638 meta_table_.Reset(); |
663 db_.reset(); | 639 db_.reset(); |
664 return false; | 640 return false; |
665 } | 641 } |
666 | 642 |
667 std::vector<std::string> host_keys; | 643 std::vector<std::string> host_keys; |
668 while (smt.Step()) | 644 while (smt.Step()) |
669 host_keys.push_back(smt.ColumnString(0)); | 645 host_keys.push_back(smt.ColumnString(0)); |
670 | 646 |
671 UMA_HISTOGRAM_CUSTOM_TIMES( | 647 UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.TimeLoadDomains", |
672 "Cookie.TimeLoadDomains", | 648 base::Time::Now() - start, |
673 base::Time::Now() - start, | 649 base::TimeDelta::FromMilliseconds(1), |
674 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 650 base::TimeDelta::FromMinutes(1), 50); |
675 50); | |
676 | 651 |
677 base::Time start_parse = base::Time::Now(); | 652 base::Time start_parse = base::Time::Now(); |
678 | 653 |
679 // Build a map of domain keys (always eTLD+1) to domains. | 654 // Build a map of domain keys (always eTLD+1) to domains. |
680 for (size_t idx = 0; idx < host_keys.size(); ++idx) { | 655 for (size_t idx = 0; idx < host_keys.size(); ++idx) { |
681 const std::string& domain = host_keys[idx]; | 656 const std::string& domain = host_keys[idx]; |
682 std::string key = | 657 std::string key = registry_controlled_domains::GetDomainAndRegistry( |
683 net::registry_controlled_domains::GetDomainAndRegistry( | 658 domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
684 domain, | |
685 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
686 | 659 |
687 keys_to_load_[key].insert(domain); | 660 keys_to_load_[key].insert(domain); |
688 } | 661 } |
689 | 662 |
690 UMA_HISTOGRAM_CUSTOM_TIMES( | 663 UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.TimeParseDomains", |
691 "Cookie.TimeParseDomains", | 664 base::Time::Now() - start_parse, |
692 base::Time::Now() - start_parse, | 665 base::TimeDelta::FromMilliseconds(1), |
693 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 666 base::TimeDelta::FromMinutes(1), 50); |
694 50); | |
695 | 667 |
696 UMA_HISTOGRAM_CUSTOM_TIMES( | 668 UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.TimeInitializeDomainMap", |
697 "Cookie.TimeInitializeDomainMap", | 669 base::Time::Now() - start, |
698 base::Time::Now() - start, | 670 base::TimeDelta::FromMilliseconds(1), |
699 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 671 base::TimeDelta::FromMinutes(1), 50); |
700 50); | |
701 | 672 |
702 initialized_ = true; | 673 initialized_ = true; |
703 | 674 |
704 if (!restore_old_session_cookies_) | 675 if (!restore_old_session_cookies_) |
705 DeleteSessionCookiesOnStartup(); | 676 DeleteSessionCookiesOnStartup(); |
706 return true; | 677 return true; |
707 } | 678 } |
708 | 679 |
709 void SQLitePersistentCookieStore::Backend::ChainLoadCookies( | 680 void SQLitePersistentCookieStore::Backend::ChainLoadCookies( |
710 const LoadedCallback& loaded_callback) { | 681 const LoadedCallback& loaded_callback) { |
711 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 682 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
712 IncrementTimeDelta increment(&cookie_load_duration_); | 683 IncrementTimeDelta increment(&cookie_load_duration_); |
713 | 684 |
714 bool load_success = true; | 685 bool load_success = true; |
715 | 686 |
716 if (!db_) { | 687 if (!db_) { |
717 // Close() has been called on this store. | 688 // Close() has been called on this store. |
718 load_success = false; | 689 load_success = false; |
719 } else if (keys_to_load_.size() > 0) { | 690 } else if (keys_to_load_.size() > 0) { |
720 // Load cookies for the first domain key. | 691 // Load cookies for the first domain key. |
721 std::map<std::string, std::set<std::string> >::iterator | 692 std::map<std::string, std::set<std::string>>::iterator it = |
722 it = keys_to_load_.begin(); | 693 keys_to_load_.begin(); |
723 load_success = LoadCookiesForDomains(it->second); | 694 load_success = LoadCookiesForDomains(it->second); |
724 keys_to_load_.erase(it); | 695 keys_to_load_.erase(it); |
725 } | 696 } |
726 | 697 |
727 // If load is successful and there are more domain keys to be loaded, | 698 // If load is successful and there are more domain keys to be loaded, |
728 // then post a background task to continue chain-load; | 699 // then post a background task to continue chain-load; |
729 // Otherwise notify on client runner. | 700 // Otherwise notify on client runner. |
730 if (load_success && keys_to_load_.size() > 0) { | 701 if (load_success && keys_to_load_.size() > 0) { |
731 bool success = background_task_runner_->PostDelayedTask( | 702 bool success = background_task_runner_->PostDelayedTask( |
732 FROM_HERE, | 703 FROM_HERE, |
733 base::Bind(&Backend::ChainLoadCookies, this, loaded_callback), | 704 base::Bind(&Backend::ChainLoadCookies, this, loaded_callback), |
734 base::TimeDelta::FromMilliseconds(kLoadDelayMilliseconds)); | 705 base::TimeDelta::FromMilliseconds(kLoadDelayMilliseconds)); |
735 if (!success) { | 706 if (!success) { |
736 LOG(WARNING) << "Failed to post task from " << FROM_HERE.ToString() | 707 LOG(WARNING) << "Failed to post task from " << FROM_HERE.ToString() |
737 << " to background_task_runner_."; | 708 << " to background_task_runner_."; |
738 } | 709 } |
739 } else { | 710 } else { |
740 FinishedLoadingCookies(loaded_callback, load_success); | 711 FinishedLoadingCookies(loaded_callback, load_success); |
741 } | 712 } |
742 } | 713 } |
743 | 714 |
744 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( | 715 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( |
745 const std::set<std::string>& domains) { | 716 const std::set<std::string>& domains) { |
746 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 717 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
747 | 718 |
748 sql::Statement smt; | 719 sql::Statement smt; |
749 if (restore_old_session_cookies_) { | 720 if (restore_old_session_cookies_) { |
750 smt.Assign(db_->GetCachedStatement( | 721 smt.Assign(db_->GetCachedStatement( |
751 SQL_FROM_HERE, | 722 SQL_FROM_HERE, |
752 "SELECT creation_utc, host_key, name, value, encrypted_value, path, " | 723 "SELECT creation_utc, host_key, name, value, encrypted_value, path, " |
753 "expires_utc, secure, httponly, firstpartyonly, last_access_utc, " | 724 "expires_utc, secure, httponly, firstpartyonly, last_access_utc, " |
754 "has_expires, persistent, priority FROM cookies WHERE host_key = ?")); | 725 "has_expires, persistent, priority FROM cookies WHERE host_key = ?")); |
755 } else { | 726 } else { |
756 smt.Assign(db_->GetCachedStatement( | 727 smt.Assign(db_->GetCachedStatement( |
757 SQL_FROM_HERE, | 728 SQL_FROM_HERE, |
758 "SELECT creation_utc, host_key, name, value, encrypted_value, path, " | 729 "SELECT creation_utc, host_key, name, value, encrypted_value, path, " |
759 "expires_utc, secure, httponly, firstpartyonly, last_access_utc, " | 730 "expires_utc, secure, httponly, firstpartyonly, last_access_utc, " |
760 "has_expires, persistent, priority FROM cookies WHERE host_key = ? " | 731 "has_expires, persistent, priority FROM cookies WHERE host_key = ? " |
761 "AND persistent = 1")); | 732 "AND persistent = 1")); |
762 } | 733 } |
763 if (!smt.is_valid()) { | 734 if (!smt.is_valid()) { |
764 smt.Clear(); // Disconnect smt_ref from db_. | 735 smt.Clear(); // Disconnect smt_ref from db_. |
765 meta_table_.Reset(); | 736 meta_table_.Reset(); |
766 db_.reset(); | 737 db_.reset(); |
767 return false; | 738 return false; |
768 } | 739 } |
769 | 740 |
770 std::vector<net::CanonicalCookie*> cookies; | 741 std::vector<CanonicalCookie*> cookies; |
771 std::set<std::string>::const_iterator it = domains.begin(); | 742 std::set<std::string>::const_iterator it = domains.begin(); |
772 for (; it != domains.end(); ++it) { | 743 for (; it != domains.end(); ++it) { |
773 smt.BindString(0, *it); | 744 smt.BindString(0, *it); |
774 MakeCookiesFromSQLStatement(&cookies, &smt); | 745 MakeCookiesFromSQLStatement(&cookies, &smt); |
775 smt.Reset(true); | 746 smt.Reset(true); |
776 } | 747 } |
777 { | 748 { |
778 base::AutoLock locked(lock_); | 749 base::AutoLock locked(lock_); |
779 cookies_.insert(cookies_.end(), cookies.begin(), cookies.end()); | 750 cookies_.insert(cookies_.end(), cookies.begin(), cookies.end()); |
780 } | 751 } |
781 return true; | 752 return true; |
782 } | 753 } |
783 | 754 |
784 void SQLitePersistentCookieStore::Backend::MakeCookiesFromSQLStatement( | 755 void SQLitePersistentCookieStore::Backend::MakeCookiesFromSQLStatement( |
785 std::vector<net::CanonicalCookie*>* cookies, | 756 std::vector<CanonicalCookie*>* cookies, |
786 sql::Statement* statement) { | 757 sql::Statement* statement) { |
787 sql::Statement& smt = *statement; | 758 sql::Statement& smt = *statement; |
788 while (smt.Step()) { | 759 while (smt.Step()) { |
789 std::string value; | 760 std::string value; |
790 std::string encrypted_value = smt.ColumnString(4); | 761 std::string encrypted_value = smt.ColumnString(4); |
791 if (!encrypted_value.empty() && crypto_) { | 762 if (!encrypted_value.empty() && crypto_) { |
792 crypto_->DecryptString(encrypted_value, &value); | 763 crypto_->DecryptString(encrypted_value, &value); |
793 } else { | 764 } else { |
794 DCHECK(encrypted_value.empty()); | 765 DCHECK(encrypted_value.empty()); |
795 value = smt.ColumnString(3); | 766 value = smt.ColumnString(3); |
796 } | 767 } |
797 scoped_ptr<net::CanonicalCookie> cc(new net::CanonicalCookie( | 768 scoped_ptr<CanonicalCookie> cc(new CanonicalCookie( |
798 // The "source" URL is not used with persisted cookies. | 769 // The "source" URL is not used with persisted cookies. |
799 GURL(), // Source | 770 GURL(), // Source |
800 smt.ColumnString(2), // name | 771 smt.ColumnString(2), // name |
801 value, // value | 772 value, // value |
802 smt.ColumnString(1), // domain | 773 smt.ColumnString(1), // domain |
803 smt.ColumnString(5), // path | 774 smt.ColumnString(5), // path |
804 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc | 775 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc |
805 Time::FromInternalValue(smt.ColumnInt64(6)), // expires_utc | 776 Time::FromInternalValue(smt.ColumnInt64(6)), // expires_utc |
806 Time::FromInternalValue(smt.ColumnInt64(10)), // last_access_utc | 777 Time::FromInternalValue(smt.ColumnInt64(10)), // last_access_utc |
807 smt.ColumnInt(7) != 0, // secure | 778 smt.ColumnInt(7) != 0, // secure |
808 smt.ColumnInt(8) != 0, // httponly | 779 smt.ColumnInt(8) != 0, // httponly |
809 smt.ColumnInt(9) != 0, // firstpartyonly | 780 smt.ColumnInt(9) != 0, // firstpartyonly |
810 DBCookiePriorityToCookiePriority( | 781 DBCookiePriorityToCookiePriority( |
811 static_cast<DBCookiePriority>(smt.ColumnInt(13))))); // priority | 782 static_cast<DBCookiePriority>(smt.ColumnInt(13))))); // priority |
812 DLOG_IF(WARNING, cc->CreationDate() > Time::Now()) | 783 DLOG_IF(WARNING, cc->CreationDate() > Time::Now()) |
813 << L"CreationDate too recent"; | 784 << L"CreationDate too recent"; |
814 cookies_per_origin_[CookieOrigin(cc->Domain(), cc->IsSecure())]++; | |
815 cookies->push_back(cc.release()); | 785 cookies->push_back(cc.release()); |
816 ++num_cookies_read_; | 786 ++num_cookies_read_; |
817 } | 787 } |
818 } | 788 } |
819 | 789 |
820 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { | 790 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { |
821 // Version check. | 791 // Version check. |
822 if (!meta_table_.Init( | 792 if (!meta_table_.Init(db_.get(), kCurrentVersionNumber, |
823 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { | 793 kCompatibleVersionNumber)) { |
824 return false; | 794 return false; |
825 } | 795 } |
826 | 796 |
827 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { | 797 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
828 LOG(WARNING) << "Cookie database is too new."; | 798 LOG(WARNING) << "Cookie database is too new."; |
829 return false; | 799 return false; |
830 } | 800 } |
831 | 801 |
832 int cur_version = meta_table_.GetVersionNumber(); | 802 int cur_version = meta_table_.GetVersionNumber(); |
833 if (cur_version == 2) { | 803 if (cur_version == 2) { |
834 sql::Transaction transaction(db_.get()); | 804 sql::Transaction transaction(db_.get()); |
835 if (!transaction.Begin()) | 805 if (!transaction.Begin()) |
836 return false; | 806 return false; |
837 if (!db_->Execute("ALTER TABLE cookies ADD COLUMN last_access_utc " | 807 if (!db_->Execute( |
838 "INTEGER DEFAULT 0") || | 808 "ALTER TABLE cookies ADD COLUMN last_access_utc " |
| 809 "INTEGER DEFAULT 0") || |
839 !db_->Execute("UPDATE cookies SET last_access_utc = creation_utc")) { | 810 !db_->Execute("UPDATE cookies SET last_access_utc = creation_utc")) { |
840 LOG(WARNING) << "Unable to update cookie database to version 3."; | 811 LOG(WARNING) << "Unable to update cookie database to version 3."; |
841 return false; | 812 return false; |
842 } | 813 } |
843 ++cur_version; | 814 ++cur_version; |
844 meta_table_.SetVersionNumber(cur_version); | 815 meta_table_.SetVersionNumber(cur_version); |
845 meta_table_.SetCompatibleVersionNumber( | 816 meta_table_.SetCompatibleVersionNumber( |
846 std::min(cur_version, kCompatibleVersionNumber)); | 817 std::min(cur_version, kCompatibleVersionNumber)); |
847 transaction.Commit(); | 818 transaction.Commit(); |
848 } | 819 } |
849 | 820 |
850 if (cur_version == 3) { | 821 if (cur_version == 3) { |
851 // The time epoch changed for Mac & Linux in this version to match Windows. | 822 // The time epoch changed for Mac & Linux in this version to match Windows. |
852 // This patch came after the main epoch change happened, so some | 823 // This patch came after the main epoch change happened, so some |
853 // developers have "good" times for cookies added by the more recent | 824 // developers have "good" times for cookies added by the more recent |
854 // versions. So we have to be careful to only update times that are under | 825 // versions. So we have to be careful to only update times that are under |
855 // the old system (which will appear to be from before 1970 in the new | 826 // the old system (which will appear to be from before 1970 in the new |
856 // system). The magic number used below is 1970 in our time units. | 827 // system). The magic number used below is 1970 in our time units. |
857 sql::Transaction transaction(db_.get()); | 828 sql::Transaction transaction(db_.get()); |
858 transaction.Begin(); | 829 transaction.Begin(); |
859 #if !defined(OS_WIN) | 830 #if !defined(OS_WIN) |
860 ignore_result(db_->Execute( | 831 ignore_result(db_->Execute( |
861 "UPDATE cookies " | 832 "UPDATE cookies " |
862 "SET creation_utc = creation_utc + 11644473600000000 " | 833 "SET creation_utc = creation_utc + 11644473600000000 " |
863 "WHERE rowid IN " | 834 "WHERE rowid IN " |
864 "(SELECT rowid FROM cookies WHERE " | 835 "(SELECT rowid FROM cookies WHERE " |
865 "creation_utc > 0 AND creation_utc < 11644473600000000)")); | 836 "creation_utc > 0 AND creation_utc < 11644473600000000)")); |
866 ignore_result(db_->Execute( | 837 ignore_result(db_->Execute( |
867 "UPDATE cookies " | 838 "UPDATE cookies " |
868 "SET expires_utc = expires_utc + 11644473600000000 " | 839 "SET expires_utc = expires_utc + 11644473600000000 " |
869 "WHERE rowid IN " | 840 "WHERE rowid IN " |
870 "(SELECT rowid FROM cookies WHERE " | 841 "(SELECT rowid FROM cookies WHERE " |
871 "expires_utc > 0 AND expires_utc < 11644473600000000)")); | 842 "expires_utc > 0 AND expires_utc < 11644473600000000)")); |
872 ignore_result(db_->Execute( | 843 ignore_result(db_->Execute( |
873 "UPDATE cookies " | 844 "UPDATE cookies " |
874 "SET last_access_utc = last_access_utc + 11644473600000000 " | 845 "SET last_access_utc = last_access_utc + 11644473600000000 " |
875 "WHERE rowid IN " | 846 "WHERE rowid IN " |
876 "(SELECT rowid FROM cookies WHERE " | 847 "(SELECT rowid FROM cookies WHERE " |
877 "last_access_utc > 0 AND last_access_utc < 11644473600000000)")); | 848 "last_access_utc > 0 AND last_access_utc < 11644473600000000)")); |
878 #endif | 849 #endif |
879 ++cur_version; | 850 ++cur_version; |
880 meta_table_.SetVersionNumber(cur_version); | 851 meta_table_.SetVersionNumber(cur_version); |
881 transaction.Commit(); | 852 transaction.Commit(); |
882 } | 853 } |
883 | 854 |
884 if (cur_version == 4) { | 855 if (cur_version == 4) { |
885 const base::TimeTicks start_time = base::TimeTicks::Now(); | 856 const base::TimeTicks start_time = base::TimeTicks::Now(); |
886 sql::Transaction transaction(db_.get()); | 857 sql::Transaction transaction(db_.get()); |
887 if (!transaction.Begin()) | 858 if (!transaction.Begin()) |
888 return false; | 859 return false; |
889 if (!db_->Execute("ALTER TABLE cookies " | 860 if (!db_->Execute( |
890 "ADD COLUMN has_expires INTEGER DEFAULT 1") || | 861 "ALTER TABLE cookies " |
891 !db_->Execute("ALTER TABLE cookies " | 862 "ADD COLUMN has_expires INTEGER DEFAULT 1") || |
892 "ADD COLUMN persistent INTEGER DEFAULT 1")) { | 863 !db_->Execute( |
| 864 "ALTER TABLE cookies " |
| 865 "ADD COLUMN persistent INTEGER DEFAULT 1")) { |
893 LOG(WARNING) << "Unable to update cookie database to version 5."; | 866 LOG(WARNING) << "Unable to update cookie database to version 5."; |
894 return false; | 867 return false; |
895 } | 868 } |
896 ++cur_version; | 869 ++cur_version; |
897 meta_table_.SetVersionNumber(cur_version); | 870 meta_table_.SetVersionNumber(cur_version); |
898 meta_table_.SetCompatibleVersionNumber( | 871 meta_table_.SetCompatibleVersionNumber( |
899 std::min(cur_version, kCompatibleVersionNumber)); | 872 std::min(cur_version, kCompatibleVersionNumber)); |
900 transaction.Commit(); | 873 transaction.Commit(); |
901 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV5", | 874 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV5", |
902 base::TimeTicks::Now() - start_time); | 875 base::TimeTicks::Now() - start_time); |
903 } | 876 } |
904 | 877 |
905 if (cur_version == 5) { | 878 if (cur_version == 5) { |
906 const base::TimeTicks start_time = base::TimeTicks::Now(); | 879 const base::TimeTicks start_time = base::TimeTicks::Now(); |
907 sql::Transaction transaction(db_.get()); | 880 sql::Transaction transaction(db_.get()); |
908 if (!transaction.Begin()) | 881 if (!transaction.Begin()) |
909 return false; | 882 return false; |
910 // Alter the table to add the priority column with a default value. | 883 // Alter the table to add the priority column with a default value. |
911 std::string stmt(base::StringPrintf( | 884 std::string stmt(base::StringPrintf( |
912 "ALTER TABLE cookies ADD COLUMN priority INTEGER DEFAULT %d", | 885 "ALTER TABLE cookies ADD COLUMN priority INTEGER DEFAULT %d", |
913 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); | 886 CookiePriorityToDBCookiePriority(COOKIE_PRIORITY_DEFAULT))); |
914 if (!db_->Execute(stmt.c_str())) { | 887 if (!db_->Execute(stmt.c_str())) { |
915 LOG(WARNING) << "Unable to update cookie database to version 6."; | 888 LOG(WARNING) << "Unable to update cookie database to version 6."; |
916 return false; | 889 return false; |
917 } | 890 } |
918 ++cur_version; | 891 ++cur_version; |
919 meta_table_.SetVersionNumber(cur_version); | 892 meta_table_.SetVersionNumber(cur_version); |
920 meta_table_.SetCompatibleVersionNumber( | 893 meta_table_.SetCompatibleVersionNumber( |
921 std::min(cur_version, kCompatibleVersionNumber)); | 894 std::min(cur_version, kCompatibleVersionNumber)); |
922 transaction.Commit(); | 895 transaction.Commit(); |
923 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV6", | 896 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV6", |
924 base::TimeTicks::Now() - start_time); | 897 base::TimeTicks::Now() - start_time); |
925 } | 898 } |
926 | 899 |
927 if (cur_version == 6) { | 900 if (cur_version == 6) { |
928 const base::TimeTicks start_time = base::TimeTicks::Now(); | 901 const base::TimeTicks start_time = base::TimeTicks::Now(); |
929 sql::Transaction transaction(db_.get()); | 902 sql::Transaction transaction(db_.get()); |
930 if (!transaction.Begin()) | 903 if (!transaction.Begin()) |
931 return false; | 904 return false; |
932 // Alter the table to add empty "encrypted value" column. | 905 // Alter the table to add empty "encrypted value" column. |
933 if (!db_->Execute("ALTER TABLE cookies " | 906 if (!db_->Execute( |
934 "ADD COLUMN encrypted_value BLOB DEFAULT ''")) { | 907 "ALTER TABLE cookies " |
| 908 "ADD COLUMN encrypted_value BLOB DEFAULT ''")) { |
935 LOG(WARNING) << "Unable to update cookie database to version 7."; | 909 LOG(WARNING) << "Unable to update cookie database to version 7."; |
936 return false; | 910 return false; |
937 } | 911 } |
938 ++cur_version; | 912 ++cur_version; |
939 meta_table_.SetVersionNumber(cur_version); | 913 meta_table_.SetVersionNumber(cur_version); |
940 meta_table_.SetCompatibleVersionNumber( | 914 meta_table_.SetCompatibleVersionNumber( |
941 std::min(cur_version, kCompatibleVersionNumber)); | 915 std::min(cur_version, kCompatibleVersionNumber)); |
942 transaction.Commit(); | 916 transaction.Commit(); |
943 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV7", | 917 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV7", |
944 base::TimeTicks::Now() - start_time); | 918 base::TimeTicks::Now() - start_time); |
(...skipping 26 matching lines...) Expand all Loading... |
971 if (!transaction.Begin()) | 945 if (!transaction.Begin()) |
972 return false; | 946 return false; |
973 | 947 |
974 if (!db_->Execute("DROP INDEX IF EXISTS cookie_times")) { | 948 if (!db_->Execute("DROP INDEX IF EXISTS cookie_times")) { |
975 LOG(WARNING) | 949 LOG(WARNING) |
976 << "Unable to drop table cookie_times in update to version 9."; | 950 << "Unable to drop table cookie_times in update to version 9."; |
977 return false; | 951 return false; |
978 } | 952 } |
979 | 953 |
980 if (!db_->Execute( | 954 if (!db_->Execute( |
981 "CREATE INDEX IF NOT EXISTS domain ON cookies(host_key)")) { | 955 "CREATE INDEX IF NOT EXISTS domain ON cookies(host_key)")) { |
982 LOG(WARNING) << "Unable to create index domain in update to version 9."; | 956 LOG(WARNING) << "Unable to create index domain in update to version 9."; |
983 return false; | 957 return false; |
984 } | 958 } |
985 | 959 |
986 #if defined(OS_IOS) | 960 #if defined(OS_IOS) |
987 // iOS 8.1 and older doesn't support partial indices. iOS 8.2 supports | 961 // iOS 8.1 and older doesn't support partial indices. iOS 8.2 supports |
988 // partial indices. | 962 // partial indices. |
989 if (!db_->Execute( | 963 if (!db_->Execute( |
990 "CREATE INDEX IF NOT EXISTS is_transient ON cookies(persistent)")) { | 964 "CREATE INDEX IF NOT EXISTS is_transient ON cookies(persistent)")) { |
991 #else | 965 #else |
992 if (!db_->Execute( | 966 if (!db_->Execute( |
993 "CREATE INDEX IF NOT EXISTS is_transient ON cookies(persistent) " | 967 "CREATE INDEX IF NOT EXISTS is_transient ON cookies(persistent) " |
994 "where persistent != 1")) { | 968 "where persistent != 1")) { |
995 #endif | 969 #endif |
996 LOG(WARNING) | 970 LOG(WARNING) |
997 << "Unable to create index is_transient in update to version 9."; | 971 << "Unable to create index is_transient in update to version 9."; |
998 return false; | 972 return false; |
999 } | 973 } |
1000 ++cur_version; | 974 ++cur_version; |
1001 meta_table_.SetVersionNumber(cur_version); | 975 meta_table_.SetVersionNumber(cur_version); |
1002 meta_table_.SetCompatibleVersionNumber( | 976 meta_table_.SetCompatibleVersionNumber( |
1003 std::min(cur_version, kCompatibleVersionNumber)); | 977 std::min(cur_version, kCompatibleVersionNumber)); |
1004 transaction.Commit(); | 978 transaction.Commit(); |
1005 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV9", | 979 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV9", |
1006 base::TimeTicks::Now() - start_time); | 980 base::TimeTicks::Now() - start_time); |
1007 } | 981 } |
1008 | 982 |
1009 // Put future migration cases here. | 983 // Put future migration cases here. |
1010 | 984 |
1011 if (cur_version < kCurrentVersionNumber) { | 985 if (cur_version < kCurrentVersionNumber) { |
1012 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTable", 1); | 986 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTable", 1); |
1013 | 987 |
1014 meta_table_.Reset(); | 988 meta_table_.Reset(); |
1015 db_.reset(new sql::Connection); | 989 db_.reset(new sql::Connection); |
1016 if (!sql::Connection::Delete(path_) || | 990 if (!sql::Connection::Delete(path_) || !db_->Open(path_) || |
1017 !db_->Open(path_) || | 991 !meta_table_.Init(db_.get(), kCurrentVersionNumber, |
1018 !meta_table_.Init( | 992 kCompatibleVersionNumber)) { |
1019 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { | |
1020 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTableRecoveryFailed", 1); | 993 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTableRecoveryFailed", 1); |
1021 NOTREACHED() << "Unable to reset the cookie DB."; | 994 NOTREACHED() << "Unable to reset the cookie DB."; |
1022 meta_table_.Reset(); | 995 meta_table_.Reset(); |
1023 db_.reset(); | 996 db_.reset(); |
1024 return false; | 997 return false; |
1025 } | 998 } |
1026 } | 999 } |
1027 | 1000 |
1028 return true; | 1001 return true; |
1029 } | 1002 } |
1030 | 1003 |
1031 void SQLitePersistentCookieStore::Backend::AddCookie( | 1004 void SQLitePersistentCookieStore::Backend::AddCookie( |
1032 const net::CanonicalCookie& cc) { | 1005 const CanonicalCookie& cc) { |
1033 BatchOperation(PendingOperation::COOKIE_ADD, cc); | 1006 BatchOperation(PendingOperation::COOKIE_ADD, cc); |
1034 } | 1007 } |
1035 | 1008 |
1036 void SQLitePersistentCookieStore::Backend::UpdateCookieAccessTime( | 1009 void SQLitePersistentCookieStore::Backend::UpdateCookieAccessTime( |
1037 const net::CanonicalCookie& cc) { | 1010 const CanonicalCookie& cc) { |
1038 BatchOperation(PendingOperation::COOKIE_UPDATEACCESS, cc); | 1011 BatchOperation(PendingOperation::COOKIE_UPDATEACCESS, cc); |
1039 } | 1012 } |
1040 | 1013 |
1041 void SQLitePersistentCookieStore::Backend::DeleteCookie( | 1014 void SQLitePersistentCookieStore::Backend::DeleteCookie( |
1042 const net::CanonicalCookie& cc) { | 1015 const CanonicalCookie& cc) { |
1043 BatchOperation(PendingOperation::COOKIE_DELETE, cc); | 1016 BatchOperation(PendingOperation::COOKIE_DELETE, cc); |
1044 } | 1017 } |
1045 | 1018 |
1046 void SQLitePersistentCookieStore::Backend::BatchOperation( | 1019 void SQLitePersistentCookieStore::Backend::BatchOperation( |
1047 PendingOperation::OperationType op, | 1020 PendingOperation::OperationType op, |
1048 const net::CanonicalCookie& cc) { | 1021 const CanonicalCookie& cc) { |
1049 // Commit every 30 seconds. | 1022 // Commit every 30 seconds. |
1050 static const int kCommitIntervalMs = 30 * 1000; | 1023 static const int kCommitIntervalMs = 30 * 1000; |
1051 // Commit right away if we have more than 512 outstanding operations. | 1024 // Commit right away if we have more than 512 outstanding operations. |
1052 static const size_t kCommitAfterBatchSize = 512; | 1025 static const size_t kCommitAfterBatchSize = 512; |
1053 DCHECK(!background_task_runner_->RunsTasksOnCurrentThread()); | 1026 DCHECK(!background_task_runner_->RunsTasksOnCurrentThread()); |
1054 | 1027 |
1055 // We do a full copy of the cookie here, and hopefully just here. | 1028 // We do a full copy of the cookie here, and hopefully just here. |
1056 scoped_ptr<PendingOperation> po(new PendingOperation(op, cc)); | 1029 scoped_ptr<PendingOperation> po(new PendingOperation(op, cc)); |
1057 | 1030 |
1058 PendingOperationsList::size_type num_pending; | 1031 PendingOperationsList::size_type num_pending; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1091 | 1064 |
1092 sql::Statement add_smt(db_->GetCachedStatement( | 1065 sql::Statement add_smt(db_->GetCachedStatement( |
1093 SQL_FROM_HERE, | 1066 SQL_FROM_HERE, |
1094 "INSERT INTO cookies (creation_utc, host_key, name, value, " | 1067 "INSERT INTO cookies (creation_utc, host_key, name, value, " |
1095 "encrypted_value, path, expires_utc, secure, httponly, firstpartyonly, " | 1068 "encrypted_value, path, expires_utc, secure, httponly, firstpartyonly, " |
1096 "last_access_utc, has_expires, persistent, priority) " | 1069 "last_access_utc, has_expires, persistent, priority) " |
1097 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); | 1070 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); |
1098 if (!add_smt.is_valid()) | 1071 if (!add_smt.is_valid()) |
1099 return; | 1072 return; |
1100 | 1073 |
1101 sql::Statement update_access_smt(db_->GetCachedStatement(SQL_FROM_HERE, | 1074 sql::Statement update_access_smt(db_->GetCachedStatement( |
| 1075 SQL_FROM_HERE, |
1102 "UPDATE cookies SET last_access_utc=? WHERE creation_utc=?")); | 1076 "UPDATE cookies SET last_access_utc=? WHERE creation_utc=?")); |
1103 if (!update_access_smt.is_valid()) | 1077 if (!update_access_smt.is_valid()) |
1104 return; | 1078 return; |
1105 | 1079 |
1106 sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE, | 1080 sql::Statement del_smt(db_->GetCachedStatement( |
1107 "DELETE FROM cookies WHERE creation_utc=?")); | 1081 SQL_FROM_HERE, "DELETE FROM cookies WHERE creation_utc=?")); |
1108 if (!del_smt.is_valid()) | 1082 if (!del_smt.is_valid()) |
1109 return; | 1083 return; |
1110 | 1084 |
1111 sql::Transaction transaction(db_.get()); | 1085 sql::Transaction transaction(db_.get()); |
1112 if (!transaction.Begin()) | 1086 if (!transaction.Begin()) |
1113 return; | 1087 return; |
1114 | 1088 |
1115 for (PendingOperationsList::iterator it = ops.begin(); | 1089 for (PendingOperationsList::iterator it = ops.begin(); it != ops.end(); |
1116 it != ops.end(); ++it) { | 1090 ++it) { |
1117 // Free the cookies as we commit them to the database. | 1091 // Free the cookies as we commit them to the database. |
1118 scoped_ptr<PendingOperation> po(*it); | 1092 scoped_ptr<PendingOperation> po(*it); |
1119 switch (po->op()) { | 1093 switch (po->op()) { |
1120 case PendingOperation::COOKIE_ADD: | 1094 case PendingOperation::COOKIE_ADD: |
1121 cookies_per_origin_[ | |
1122 CookieOrigin(po->cc().Domain(), po->cc().IsSecure())]++; | |
1123 add_smt.Reset(true); | 1095 add_smt.Reset(true); |
1124 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); | 1096 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
1125 add_smt.BindString(1, po->cc().Domain()); | 1097 add_smt.BindString(1, po->cc().Domain()); |
1126 add_smt.BindString(2, po->cc().Name()); | 1098 add_smt.BindString(2, po->cc().Name()); |
1127 if (crypto_) { | 1099 if (crypto_) { |
1128 std::string encrypted_value; | 1100 std::string encrypted_value; |
1129 add_smt.BindCString(3, ""); // value | 1101 add_smt.BindCString(3, ""); // value |
1130 crypto_->EncryptString(po->cc().Value(), &encrypted_value); | 1102 crypto_->EncryptString(po->cc().Value(), &encrypted_value); |
1131 // BindBlob() immediately makes an internal copy of the data. | 1103 // BindBlob() immediately makes an internal copy of the data. |
1132 add_smt.BindBlob(4, encrypted_value.data(), | 1104 add_smt.BindBlob(4, encrypted_value.data(), |
(...skipping 11 matching lines...) Expand all Loading... |
1144 add_smt.BindInt(11, po->cc().IsPersistent()); | 1116 add_smt.BindInt(11, po->cc().IsPersistent()); |
1145 add_smt.BindInt(12, po->cc().IsPersistent()); | 1117 add_smt.BindInt(12, po->cc().IsPersistent()); |
1146 add_smt.BindInt(13, | 1118 add_smt.BindInt(13, |
1147 CookiePriorityToDBCookiePriority(po->cc().Priority())); | 1119 CookiePriorityToDBCookiePriority(po->cc().Priority())); |
1148 if (!add_smt.Run()) | 1120 if (!add_smt.Run()) |
1149 NOTREACHED() << "Could not add a cookie to the DB."; | 1121 NOTREACHED() << "Could not add a cookie to the DB."; |
1150 break; | 1122 break; |
1151 | 1123 |
1152 case PendingOperation::COOKIE_UPDATEACCESS: | 1124 case PendingOperation::COOKIE_UPDATEACCESS: |
1153 update_access_smt.Reset(true); | 1125 update_access_smt.Reset(true); |
1154 update_access_smt.BindInt64(0, | 1126 update_access_smt.BindInt64( |
1155 po->cc().LastAccessDate().ToInternalValue()); | 1127 0, po->cc().LastAccessDate().ToInternalValue()); |
1156 update_access_smt.BindInt64(1, | 1128 update_access_smt.BindInt64(1, |
1157 po->cc().CreationDate().ToInternalValue()); | 1129 po->cc().CreationDate().ToInternalValue()); |
1158 if (!update_access_smt.Run()) | 1130 if (!update_access_smt.Run()) |
1159 NOTREACHED() << "Could not update cookie last access time in the DB."; | 1131 NOTREACHED() << "Could not update cookie last access time in the DB."; |
1160 break; | 1132 break; |
1161 | 1133 |
1162 case PendingOperation::COOKIE_DELETE: | 1134 case PendingOperation::COOKIE_DELETE: |
1163 cookies_per_origin_[ | |
1164 CookieOrigin(po->cc().Domain(), po->cc().IsSecure())]--; | |
1165 del_smt.Reset(true); | 1135 del_smt.Reset(true); |
1166 del_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); | 1136 del_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
1167 if (!del_smt.Run()) | 1137 if (!del_smt.Run()) |
1168 NOTREACHED() << "Could not delete a cookie from the DB."; | 1138 NOTREACHED() << "Could not delete a cookie from the DB."; |
1169 break; | 1139 break; |
1170 | 1140 |
1171 default: | 1141 default: |
1172 NOTREACHED(); | 1142 NOTREACHED(); |
1173 break; | 1143 break; |
1174 } | 1144 } |
(...skipping 27 matching lines...) Expand all Loading... |
1202 PostBackgroundTask(FROM_HERE, | 1172 PostBackgroundTask(FROM_HERE, |
1203 base::Bind(&Backend::InternalBackgroundClose, this)); | 1173 base::Bind(&Backend::InternalBackgroundClose, this)); |
1204 } | 1174 } |
1205 } | 1175 } |
1206 | 1176 |
1207 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() { | 1177 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() { |
1208 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 1178 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
1209 // Commit any pending operations | 1179 // Commit any pending operations |
1210 Commit(); | 1180 Commit(); |
1211 | 1181 |
1212 if (!force_keep_session_state_ && special_storage_policy_.get() && | |
1213 special_storage_policy_->HasSessionOnlyOrigins()) { | |
1214 DeleteSessionCookiesOnShutdown(); | |
1215 } | |
1216 | |
1217 meta_table_.Reset(); | 1182 meta_table_.Reset(); |
1218 db_.reset(); | 1183 db_.reset(); |
1219 } | 1184 } |
1220 | 1185 |
1221 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnShutdown() { | |
1222 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | |
1223 | |
1224 if (!db_) | |
1225 return; | |
1226 | |
1227 if (!special_storage_policy_.get()) | |
1228 return; | |
1229 | |
1230 sql::Statement del_smt(db_->GetCachedStatement( | |
1231 SQL_FROM_HERE, "DELETE FROM cookies WHERE host_key=? AND secure=?")); | |
1232 if (!del_smt.is_valid()) { | |
1233 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
1234 return; | |
1235 } | |
1236 | |
1237 sql::Transaction transaction(db_.get()); | |
1238 if (!transaction.Begin()) { | |
1239 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
1240 return; | |
1241 } | |
1242 | |
1243 for (CookiesPerOriginMap::iterator it = cookies_per_origin_.begin(); | |
1244 it != cookies_per_origin_.end(); ++it) { | |
1245 if (it->second <= 0) { | |
1246 DCHECK_EQ(0, it->second); | |
1247 continue; | |
1248 } | |
1249 const GURL url(net::cookie_util::CookieOriginToURL(it->first.first, | |
1250 it->first.second)); | |
1251 if (!url.is_valid() || !special_storage_policy_->IsStorageSessionOnly(url)) | |
1252 continue; | |
1253 | |
1254 del_smt.Reset(true); | |
1255 del_smt.BindString(0, it->first.first); | |
1256 del_smt.BindInt(1, it->first.second); | |
1257 if (!del_smt.Run()) | |
1258 NOTREACHED() << "Could not delete a cookie from the DB."; | |
1259 } | |
1260 | |
1261 if (!transaction.Commit()) | |
1262 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
1263 } | |
1264 | |
1265 void SQLitePersistentCookieStore::Backend::DatabaseErrorCallback( | 1186 void SQLitePersistentCookieStore::Backend::DatabaseErrorCallback( |
1266 int error, | 1187 int error, |
1267 sql::Statement* stmt) { | 1188 sql::Statement* stmt) { |
1268 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 1189 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
1269 | 1190 |
1270 if (!sql::IsErrorCatastrophic(error)) | 1191 if (!sql::IsErrorCatastrophic(error)) |
1271 return; | 1192 return; |
1272 | 1193 |
1273 // TODO(shess): Running KillDatabase() multiple times should be | 1194 // TODO(shess): Running KillDatabase() multiple times should be |
1274 // safe. | 1195 // safe. |
(...skipping 16 matching lines...) Expand all Loading... |
1291 if (db_) { | 1212 if (db_) { |
1292 // This Backend will now be in-memory only. In a future run we will recreate | 1213 // This Backend will now be in-memory only. In a future run we will recreate |
1293 // the database. Hopefully things go better then! | 1214 // the database. Hopefully things go better then! |
1294 bool success = db_->RazeAndClose(); | 1215 bool success = db_->RazeAndClose(); |
1295 UMA_HISTOGRAM_BOOLEAN("Cookie.KillDatabaseResult", success); | 1216 UMA_HISTOGRAM_BOOLEAN("Cookie.KillDatabaseResult", success); |
1296 meta_table_.Reset(); | 1217 meta_table_.Reset(); |
1297 db_.reset(); | 1218 db_.reset(); |
1298 } | 1219 } |
1299 } | 1220 } |
1300 | 1221 |
1301 void SQLitePersistentCookieStore::Backend::SetForceKeepSessionState() { | 1222 void SQLitePersistentCookieStore::Backend::DeleteAllInList( |
1302 base::AutoLock locked(lock_); | 1223 const std::list<CookieOrigin>& cookies) { |
1303 force_keep_session_state_ = true; | 1224 if (cookies.empty()) |
| 1225 return; |
| 1226 |
| 1227 // Perform deletion on background task runner. |
| 1228 background_task_runner_->PostTask( |
| 1229 FROM_HERE, |
| 1230 base::Bind(&Backend::BackgroundDeleteAllInList, this, cookies)); |
1304 } | 1231 } |
1305 | 1232 |
1306 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() { | 1233 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() { |
1307 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 1234 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
1308 base::Time start_time = base::Time::Now(); | 1235 base::Time start_time = base::Time::Now(); |
1309 if (!db_->Execute("DELETE FROM cookies WHERE persistent != 1")) | 1236 if (!db_->Execute("DELETE FROM cookies WHERE persistent != 1")) |
1310 LOG(WARNING) << "Unable to delete session cookies."; | 1237 LOG(WARNING) << "Unable to delete session cookies."; |
1311 | 1238 |
1312 UMA_HISTOGRAM_TIMES("Cookie.Startup.TimeSpentDeletingCookies", | 1239 UMA_HISTOGRAM_TIMES("Cookie.Startup.TimeSpentDeletingCookies", |
1313 base::Time::Now() - start_time); | 1240 base::Time::Now() - start_time); |
1314 UMA_HISTOGRAM_COUNTS("Cookie.Startup.NumberOfCookiesDeleted", | 1241 UMA_HISTOGRAM_COUNTS("Cookie.Startup.NumberOfCookiesDeleted", |
1315 db_->GetLastChangeCount()); | 1242 db_->GetLastChangeCount()); |
1316 } | 1243 } |
1317 | 1244 |
| 1245 void SQLitePersistentCookieStore::Backend::BackgroundDeleteAllInList( |
| 1246 const std::list<CookieOrigin>& cookies) { |
| 1247 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 1248 |
| 1249 if (!db_) |
| 1250 return; |
| 1251 |
| 1252 // Force a commit of any pending writes before issuing deletes. |
| 1253 // TODO(rohitrao): Remove the need for this Commit() by instead pruning the |
| 1254 // list of pending operations. https://crbug.com/486742. |
| 1255 Commit(); |
| 1256 |
| 1257 sql::Statement del_smt(db_->GetCachedStatement( |
| 1258 SQL_FROM_HERE, "DELETE FROM cookies WHERE host_key=? AND secure=?")); |
| 1259 if (!del_smt.is_valid()) { |
| 1260 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 1261 return; |
| 1262 } |
| 1263 |
| 1264 sql::Transaction transaction(db_.get()); |
| 1265 if (!transaction.Begin()) { |
| 1266 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 1267 return; |
| 1268 } |
| 1269 |
| 1270 for (const auto& cookie : cookies) { |
| 1271 const GURL url(cookie_util::CookieOriginToURL(cookie.first, cookie.second)); |
| 1272 if (!url.is_valid()) |
| 1273 continue; |
| 1274 |
| 1275 del_smt.Reset(true); |
| 1276 del_smt.BindString(0, cookie.first); |
| 1277 del_smt.BindInt(1, cookie.second); |
| 1278 if (!del_smt.Run()) |
| 1279 NOTREACHED() << "Could not delete a cookie from the DB."; |
| 1280 } |
| 1281 |
| 1282 if (!transaction.Commit()) |
| 1283 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 1284 } |
| 1285 |
1318 void SQLitePersistentCookieStore::Backend::PostBackgroundTask( | 1286 void SQLitePersistentCookieStore::Backend::PostBackgroundTask( |
1319 const tracked_objects::Location& origin, const base::Closure& task) { | 1287 const tracked_objects::Location& origin, |
| 1288 const base::Closure& task) { |
1320 if (!background_task_runner_->PostTask(origin, task)) { | 1289 if (!background_task_runner_->PostTask(origin, task)) { |
1321 LOG(WARNING) << "Failed to post task from " << origin.ToString() | 1290 LOG(WARNING) << "Failed to post task from " << origin.ToString() |
1322 << " to background_task_runner_."; | 1291 << " to background_task_runner_."; |
1323 } | 1292 } |
1324 } | 1293 } |
1325 | 1294 |
1326 void SQLitePersistentCookieStore::Backend::PostClientTask( | 1295 void SQLitePersistentCookieStore::Backend::PostClientTask( |
1327 const tracked_objects::Location& origin, const base::Closure& task) { | 1296 const tracked_objects::Location& origin, |
| 1297 const base::Closure& task) { |
1328 if (!client_task_runner_->PostTask(origin, task)) { | 1298 if (!client_task_runner_->PostTask(origin, task)) { |
1329 LOG(WARNING) << "Failed to post task from " << origin.ToString() | 1299 LOG(WARNING) << "Failed to post task from " << origin.ToString() |
1330 << " to client_task_runner_."; | 1300 << " to client_task_runner_."; |
1331 } | 1301 } |
1332 } | 1302 } |
1333 | 1303 |
1334 void SQLitePersistentCookieStore::Backend::FinishedLoadingCookies( | 1304 void SQLitePersistentCookieStore::Backend::FinishedLoadingCookies( |
1335 const LoadedCallback& loaded_callback, | 1305 const LoadedCallback& loaded_callback, |
1336 bool success) { | 1306 bool success) { |
1337 PostClientTask(FROM_HERE, base::Bind(&Backend::CompleteLoadInForeground, this, | 1307 PostClientTask(FROM_HERE, base::Bind(&Backend::CompleteLoadInForeground, this, |
1338 loaded_callback, success)); | 1308 loaded_callback, success)); |
1339 } | 1309 } |
1340 | 1310 |
1341 SQLitePersistentCookieStore::SQLitePersistentCookieStore( | 1311 SQLitePersistentCookieStore::SQLitePersistentCookieStore( |
1342 const base::FilePath& path, | 1312 const base::FilePath& path, |
1343 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 1313 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
1344 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 1314 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
1345 bool restore_old_session_cookies, | 1315 bool restore_old_session_cookies, |
1346 storage::SpecialStoragePolicy* special_storage_policy, | 1316 CookieCryptoDelegate* crypto_delegate) |
1347 net::CookieCryptoDelegate* crypto_delegate) | |
1348 : backend_(new Backend(path, | 1317 : backend_(new Backend(path, |
1349 client_task_runner, | 1318 client_task_runner, |
1350 background_task_runner, | 1319 background_task_runner, |
1351 restore_old_session_cookies, | 1320 restore_old_session_cookies, |
1352 special_storage_policy, | |
1353 crypto_delegate)) { | 1321 crypto_delegate)) { |
1354 } | 1322 } |
1355 | 1323 |
| 1324 void SQLitePersistentCookieStore::DeleteAllInList( |
| 1325 const std::list<CookieOrigin>& cookies) { |
| 1326 backend_->DeleteAllInList(cookies); |
| 1327 } |
| 1328 |
1356 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 1329 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
1357 backend_->Load(loaded_callback); | 1330 backend_->Load(loaded_callback); |
1358 } | 1331 } |
1359 | 1332 |
1360 void SQLitePersistentCookieStore::LoadCookiesForKey( | 1333 void SQLitePersistentCookieStore::LoadCookiesForKey( |
1361 const std::string& key, | 1334 const std::string& key, |
1362 const LoadedCallback& loaded_callback) { | 1335 const LoadedCallback& loaded_callback) { |
1363 backend_->LoadCookiesForKey(key, loaded_callback); | 1336 backend_->LoadCookiesForKey(key, loaded_callback); |
1364 } | 1337 } |
1365 | 1338 |
1366 void SQLitePersistentCookieStore::AddCookie(const net::CanonicalCookie& cc) { | 1339 void SQLitePersistentCookieStore::AddCookie(const CanonicalCookie& cc) { |
1367 backend_->AddCookie(cc); | 1340 backend_->AddCookie(cc); |
1368 } | 1341 } |
1369 | 1342 |
1370 void SQLitePersistentCookieStore::UpdateCookieAccessTime( | 1343 void SQLitePersistentCookieStore::UpdateCookieAccessTime( |
1371 const net::CanonicalCookie& cc) { | 1344 const CanonicalCookie& cc) { |
1372 backend_->UpdateCookieAccessTime(cc); | 1345 backend_->UpdateCookieAccessTime(cc); |
1373 } | 1346 } |
1374 | 1347 |
1375 void SQLitePersistentCookieStore::DeleteCookie(const net::CanonicalCookie& cc) { | 1348 void SQLitePersistentCookieStore::DeleteCookie(const CanonicalCookie& cc) { |
1376 backend_->DeleteCookie(cc); | 1349 backend_->DeleteCookie(cc); |
1377 } | 1350 } |
1378 | 1351 |
1379 void SQLitePersistentCookieStore::SetForceKeepSessionState() { | 1352 void SQLitePersistentCookieStore::SetForceKeepSessionState() { |
1380 backend_->SetForceKeepSessionState(); | 1353 // This store never discards session-only cookies, so this call has no effect. |
1381 } | 1354 } |
1382 | 1355 |
1383 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { | 1356 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { |
1384 backend_->Flush(callback); | 1357 backend_->Flush(callback); |
1385 } | 1358 } |
1386 | 1359 |
1387 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 1360 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
1388 backend_->Close(); | 1361 backend_->Close(); |
1389 // We release our reference to the Backend, though it will probably still have | 1362 // We release our reference to the Backend, though it will probably still have |
1390 // a reference if the background runner has not run Close() yet. | 1363 // a reference if the background runner has not run Close() yet. |
1391 } | 1364 } |
1392 | 1365 |
1393 CookieStoreConfig::CookieStoreConfig() | 1366 } // namespace net |
1394 : session_cookie_mode(EPHEMERAL_SESSION_COOKIES), | |
1395 crypto_delegate(NULL) { | |
1396 // Default to an in-memory cookie store. | |
1397 } | |
1398 | |
1399 CookieStoreConfig::CookieStoreConfig( | |
1400 const base::FilePath& path, | |
1401 SessionCookieMode session_cookie_mode, | |
1402 storage::SpecialStoragePolicy* storage_policy, | |
1403 net::CookieMonsterDelegate* cookie_delegate) | |
1404 : path(path), | |
1405 session_cookie_mode(session_cookie_mode), | |
1406 storage_policy(storage_policy), | |
1407 cookie_delegate(cookie_delegate), | |
1408 crypto_delegate(NULL) { | |
1409 CHECK(!path.empty() || session_cookie_mode == EPHEMERAL_SESSION_COOKIES); | |
1410 } | |
1411 | |
1412 CookieStoreConfig::~CookieStoreConfig() { | |
1413 } | |
1414 | |
1415 net::CookieStore* CreateCookieStore(const CookieStoreConfig& config) { | |
1416 // TODO(bcwhite): Remove ScopedTracker below once crbug.com/483686 is fixed. | |
1417 tracked_objects::ScopedTracker tracking_profile( | |
1418 FROM_HERE_WITH_EXPLICIT_FUNCTION("483686 content::CreateCookieStore")); | |
1419 | |
1420 net::CookieMonster* cookie_monster = NULL; | |
1421 | |
1422 if (config.path.empty()) { | |
1423 // Empty path means in-memory store. | |
1424 cookie_monster = new net::CookieMonster(NULL, config.cookie_delegate.get()); | |
1425 } else { | |
1426 scoped_refptr<base::SequencedTaskRunner> client_task_runner = | |
1427 config.client_task_runner; | |
1428 scoped_refptr<base::SequencedTaskRunner> background_task_runner = | |
1429 config.background_task_runner; | |
1430 | |
1431 if (!client_task_runner.get()) { | |
1432 client_task_runner = | |
1433 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | |
1434 } | |
1435 | |
1436 if (!background_task_runner.get()) { | |
1437 background_task_runner = | |
1438 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
1439 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
1440 } | |
1441 | |
1442 SQLitePersistentCookieStore* persistent_store = | |
1443 new SQLitePersistentCookieStore( | |
1444 config.path, | |
1445 client_task_runner, | |
1446 background_task_runner, | |
1447 (config.session_cookie_mode == | |
1448 CookieStoreConfig::RESTORED_SESSION_COOKIES), | |
1449 config.storage_policy.get(), | |
1450 config.crypto_delegate); | |
1451 | |
1452 cookie_monster = | |
1453 new net::CookieMonster(persistent_store, config.cookie_delegate.get()); | |
1454 if ((config.session_cookie_mode == | |
1455 CookieStoreConfig::PERSISTANT_SESSION_COOKIES) || | |
1456 (config.session_cookie_mode == | |
1457 CookieStoreConfig::RESTORED_SESSION_COOKIES)) { | |
1458 cookie_monster->SetPersistSessionCookies(true); | |
1459 } | |
1460 } | |
1461 | |
1462 return cookie_monster; | |
1463 } | |
1464 | |
1465 } // namespace content | |
OLD | NEW |