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