| 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 "chrome/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/location.h" |
| 17 #include "base/logging.h" | 18 #include "base/logging.h" |
| 18 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/metrics/histogram.h" | 21 #include "base/metrics/histogram.h" |
| 22 #include "base/sequenced_task_runner.h" |
| 21 #include "base/string_util.h" | 23 #include "base/string_util.h" |
| 22 #include "base/synchronization/lock.h" | 24 #include "base/synchronization/lock.h" |
| 23 #include "base/threading/thread.h" | |
| 24 #include "base/time.h" | 25 #include "base/time.h" |
| 25 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" | 26 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
| 26 #include "chrome/browser/net/clear_on_exit_policy.h" | 27 #include "chrome/browser/net/clear_on_exit_policy.h" |
| 27 #include "content/public/browser/browser_thread.h" | |
| 28 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
| 29 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 29 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 30 #include "net/cookies/canonical_cookie.h" | 30 #include "net/cookies/canonical_cookie.h" |
| 31 #include "sql/error_delegate_util.h" | 31 #include "sql/error_delegate_util.h" |
| 32 #include "sql/meta_table.h" | 32 #include "sql/meta_table.h" |
| 33 #include "sql/statement.h" | 33 #include "sql/statement.h" |
| 34 #include "sql/transaction.h" | 34 #include "sql/transaction.h" |
| 35 #include "third_party/sqlite/sqlite3.h" | 35 #include "third_party/sqlite/sqlite3.h" |
| 36 | 36 |
| 37 using base::Time; | 37 using base::Time; |
| 38 using content::BrowserThread; | |
| 39 | 38 |
| 40 // This class is designed to be shared between any calling threads and the | 39 // This class is designed to be shared between any client thread and the |
| 41 // database thread. It batches operations and commits them on a timer. | 40 // background task runner. It batches operations and commits them on a timer. |
| 42 // | 41 // |
| 43 // SQLitePersistentCookieStore::Load is called to load all cookies. It | 42 // SQLitePersistentCookieStore::Load is called to load all cookies. It |
| 44 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread | 43 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread |
| 45 // task to the DB thread. This task calls Backend::ChainLoadCookies(), which | 44 // task to the background runner. This task calls Backend::ChainLoadCookies(), |
| 46 // repeatedly posts itself to the DB thread to load each eTLD+1's cookies in | 45 // which repeatedly posts itself to the BG runner to load each eTLD+1's cookies |
| 47 // separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is | 46 // in separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is |
| 48 // posted to the IO thread, which notifies the caller of | 47 // posted to the client runner, which notifies the caller of |
| 49 // SQLitePersistentCookieStore::Load that the load is complete. | 48 // SQLitePersistentCookieStore::Load that the load is complete. |
| 50 // | 49 // |
| 51 // If a priority load request is invoked via SQLitePersistentCookieStore:: | 50 // If a priority load request is invoked via SQLitePersistentCookieStore:: |
| 52 // LoadCookiesForKey, it is delegated to Backend::LoadCookiesForKey, which posts | 51 // LoadCookiesForKey, it is delegated to Backend::LoadCookiesForKey, which posts |
| 53 // Backend::LoadKeyAndNotifyOnDBThread to the DB thread. That routine loads just | 52 // Backend::LoadKeyAndNotifyOnDBThread to the BG runner. That routine loads just |
| 54 // that single domain key (eTLD+1)'s cookies, and posts a Backend:: | 53 // that single domain key (eTLD+1)'s cookies, and posts a Backend:: |
| 55 // CompleteLoadForKeyOnIOThread to the IO thread to notify the caller of | 54 // CompleteLoadForKeyOnIOThread to the client runner to notify the caller of |
| 56 // SQLitePersistentCookieStore::LoadCookiesForKey that that load is complete. | 55 // SQLitePersistentCookieStore::LoadCookiesForKey that that load is complete. |
| 57 // | 56 // |
| 58 // Subsequent to loading, mutations may be queued by any thread using | 57 // Subsequent to loading, mutations may be queued by any thread using |
| 59 // AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to | 58 // AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to |
| 60 // disk on the DB thread every 30 seconds, 512 operations, or call to Flush(), | 59 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(), |
| 61 // whichever occurs first. | 60 // whichever occurs first. |
| 62 class SQLitePersistentCookieStore::Backend | 61 class SQLitePersistentCookieStore::Backend |
| 63 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { | 62 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { |
| 64 public: | 63 public: |
| 65 Backend(const base::FilePath& path, | 64 Backend( |
| 66 bool restore_old_session_cookies, | 65 const base::FilePath& path, |
| 67 ClearOnExitPolicy* clear_on_exit_policy) | 66 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
| 67 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
| 68 bool restore_old_session_cookies, |
| 69 ClearOnExitPolicy* clear_on_exit_policy) |
| 68 : path_(path), | 70 : path_(path), |
| 69 db_(NULL), | 71 db_(NULL), |
| 70 num_pending_(0), | 72 num_pending_(0), |
| 71 force_keep_session_state_(false), | 73 force_keep_session_state_(false), |
| 72 initialized_(false), | 74 initialized_(false), |
| 73 corruption_detected_(false), | 75 corruption_detected_(false), |
| 74 restore_old_session_cookies_(restore_old_session_cookies), | 76 restore_old_session_cookies_(restore_old_session_cookies), |
| 75 clear_on_exit_policy_(clear_on_exit_policy), | 77 clear_on_exit_policy_(clear_on_exit_policy), |
| 76 num_cookies_read_(0), | 78 num_cookies_read_(0), |
| 79 client_task_runner_(client_task_runner), |
| 80 background_task_runner_(background_task_runner), |
| 77 num_priority_waiting_(0), | 81 num_priority_waiting_(0), |
| 78 total_priority_requests_(0) { | 82 total_priority_requests_(0) { |
| 79 } | 83 } |
| 80 | 84 |
| 81 // Creates or loads the SQLite database. | 85 // Creates or loads the SQLite database. |
| 82 void Load(const LoadedCallback& loaded_callback); | 86 void Load(const LoadedCallback& loaded_callback); |
| 83 | 87 |
| 84 // Loads cookies for the domain key (eTLD+1). | 88 // Loads cookies for the domain key (eTLD+1). |
| 85 void LoadCookiesForKey(const std::string& domain, | 89 void LoadCookiesForKey(const std::string& domain, |
| 86 const LoadedCallback& loaded_callback); | 90 const LoadedCallback& loaded_callback); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 154 |
| 151 OperationType op() const { return op_; } | 155 OperationType op() const { return op_; } |
| 152 const net::CanonicalCookie& cc() const { return cc_; } | 156 const net::CanonicalCookie& cc() const { return cc_; } |
| 153 | 157 |
| 154 private: | 158 private: |
| 155 OperationType op_; | 159 OperationType op_; |
| 156 net::CanonicalCookie cc_; | 160 net::CanonicalCookie cc_; |
| 157 }; | 161 }; |
| 158 | 162 |
| 159 private: | 163 private: |
| 160 // Creates or loads the SQLite database on DB thread. | 164 // Creates or loads the SQLite database on background runner. |
| 161 void LoadAndNotifyOnDBThread(const LoadedCallback& loaded_callback, | 165 void LoadAndNotifyInBackground(const LoadedCallback& loaded_callback, |
| 162 const base::Time& posted_at); | 166 const base::Time& posted_at); |
| 163 | 167 |
| 164 // Loads cookies for the domain key (eTLD+1) on DB thread. | 168 // Loads cookies for the domain key (eTLD+1) on background runner. |
| 165 void LoadKeyAndNotifyOnDBThread(const std::string& domains, | 169 void LoadKeyAndNotifyInBackground(const std::string& domains, |
| 166 const LoadedCallback& loaded_callback, | 170 const LoadedCallback& loaded_callback, |
| 167 const base::Time& posted_at); | 171 const base::Time& posted_at); |
| 168 | 172 |
| 169 // Notifies the CookieMonster when loading completes for a specific domain key | 173 // Notifies the CookieMonster when loading completes for a specific domain key |
| 170 // or for all domain keys. Triggers the callback and passes it all cookies | 174 // or for all domain keys. Triggers the callback and passes it all cookies |
| 171 // that have been loaded from DB since last IO notification. | 175 // that have been loaded from DB since last IO notification. |
| 172 void Notify(const LoadedCallback& loaded_callback, bool load_success); | 176 void Notify(const LoadedCallback& loaded_callback, bool load_success); |
| 173 | 177 |
| 174 // Sends notification when the entire store is loaded, and reports metrics | 178 // Sends notification when the entire store is loaded, and reports metrics |
| 175 // for the total time to load and aggregated results from any priority loads | 179 // for the total time to load and aggregated results from any priority loads |
| 176 // that occurred. | 180 // that occurred. |
| 177 void CompleteLoadOnIOThread(const LoadedCallback& loaded_callback, | 181 void CompleteLoadInForeground(const LoadedCallback& loaded_callback, |
| 178 bool load_success); | 182 bool load_success); |
| 179 | 183 |
| 180 // Sends notification when a single priority load completes. Updates priority | 184 // Sends notification when a single priority load completes. Updates priority |
| 181 // load metric data. The data is sent only after the final load completes. | 185 // load metric data. The data is sent only after the final load completes. |
| 182 void CompleteLoadForKeyOnIOThread(const LoadedCallback& loaded_callback, | 186 void CompleteLoadForKeyInForeground(const LoadedCallback& loaded_callback, |
| 183 bool load_success); | 187 bool load_success); |
| 184 | 188 |
| 185 // Sends all metrics, including posting a ReportMetricsOnDBThread task. | 189 // Sends all metrics, including posting a ReportMetricsInBackground task. |
| 186 // Called after all priority and regular loading is complete. | 190 // Called after all priority and regular loading is complete. |
| 187 void ReportMetrics(); | 191 void ReportMetrics(); |
| 188 | 192 |
| 189 // Sends DB-thread owned metrics (i.e., the combined duration of all DB-thread | 193 // Sends background-runner owned metrics (i.e., the combined duration of all |
| 190 // tasks). | 194 // BG-runner tasks). |
| 191 void ReportMetricsOnDBThread(); | 195 void ReportMetricsInBackground(); |
| 192 | 196 |
| 193 // Initialize the data base. | 197 // Initialize the data base. |
| 194 bool InitializeDatabase(); | 198 bool InitializeDatabase(); |
| 195 | 199 |
| 196 // 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 |
| 197 // itself or schedules the provided callback to run on the IO thread (if all | 201 // itself or schedules the provided callback to run on the client runner (if |
| 198 // domains are loaded). | 202 // all domains are loaded). |
| 199 void ChainLoadCookies(const LoadedCallback& loaded_callback); | 203 void ChainLoadCookies(const LoadedCallback& loaded_callback); |
| 200 | 204 |
| 201 // Load all cookies for a set of domains/hosts | 205 // Load all cookies for a set of domains/hosts |
| 202 bool LoadCookiesForDomains(const std::set<std::string>& key); | 206 bool LoadCookiesForDomains(const std::set<std::string>& key); |
| 203 | 207 |
| 204 // Batch a cookie operation (add or delete) | 208 // Batch a cookie operation (add or delete) |
| 205 void BatchOperation(PendingOperation::OperationType op, | 209 void BatchOperation(PendingOperation::OperationType op, |
| 206 const net::CanonicalCookie& cc); | 210 const net::CanonicalCookie& cc); |
| 207 // Commit our pending operations to the database. | 211 // Commit our pending operations to the database. |
| 208 void Commit(); | 212 void Commit(); |
| 209 // Close() executed on the background thread. | 213 // Close() executed on the background runner. |
| 210 void InternalBackgroundClose(); | 214 void InternalBackgroundClose(); |
| 211 | 215 |
| 212 void DeleteSessionCookiesOnStartup(); | 216 void DeleteSessionCookiesOnStartup(); |
| 213 | 217 |
| 214 void DeleteSessionCookiesOnShutdown(); | 218 void DeleteSessionCookiesOnShutdown(); |
| 215 | 219 |
| 216 void KillDatabase(); | 220 void KillDatabase(); |
| 217 void ScheduleKillDatabase(); | 221 void ScheduleKillDatabase(); |
| 218 | 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 |
| 219 base::FilePath path_; | 228 base::FilePath path_; |
| 220 scoped_ptr<sql::Connection> db_; | 229 scoped_ptr<sql::Connection> db_; |
| 221 sql::MetaTable meta_table_; | 230 sql::MetaTable meta_table_; |
| 222 | 231 |
| 223 typedef std::list<PendingOperation*> PendingOperationsList; | 232 typedef std::list<PendingOperation*> PendingOperationsList; |
| 224 PendingOperationsList pending_; | 233 PendingOperationsList pending_; |
| 225 PendingOperationsList::size_type num_pending_; | 234 PendingOperationsList::size_type num_pending_; |
| 226 // True if the persistent store should skip delete on exit rules. | 235 // True if the persistent store should skip delete on exit rules. |
| 227 bool force_keep_session_state_; | 236 bool force_keep_session_state_; |
| 228 // Guard |cookies_|, |pending_|, |num_pending_|, |force_keep_session_state_| | 237 // Guard |cookies_|, |pending_|, |num_pending_|, |force_keep_session_state_| |
| 229 base::Lock lock_; | 238 base::Lock lock_; |
| 230 | 239 |
| 231 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce | 240 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce |
| 232 // the number of messages sent to the IO thread. Sent back in response to | 241 // the number of messages sent to the client runner. Sent back in response to |
| 233 // individual load requests for domain keys or when all loading completes. | 242 // individual load requests for domain keys or when all loading completes. |
| 234 std::vector<net::CanonicalCookie*> cookies_; | 243 std::vector<net::CanonicalCookie*> cookies_; |
| 235 | 244 |
| 236 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. | 245 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. |
| 237 std::map<std::string, std::set<std::string> > keys_to_load_; | 246 std::map<std::string, std::set<std::string> > keys_to_load_; |
| 238 | 247 |
| 239 // Map of (domain keys(eTLD+1), is secure cookie) to number of cookies in the | 248 // Map of (domain keys(eTLD+1), is secure cookie) to number of cookies in the |
| 240 // database. | 249 // database. |
| 241 typedef std::pair<std::string, bool> CookieOrigin; | 250 typedef std::pair<std::string, bool> CookieOrigin; |
| 242 typedef std::map<CookieOrigin, int> CookiesPerOriginMap; | 251 typedef std::map<CookieOrigin, int> CookiesPerOriginMap; |
| 243 CookiesPerOriginMap cookies_per_origin_; | 252 CookiesPerOriginMap cookies_per_origin_; |
| 244 | 253 |
| 245 // Indicates if DB has been initialized. | 254 // Indicates if DB has been initialized. |
| 246 bool initialized_; | 255 bool initialized_; |
| 247 | 256 |
| 248 // Indicates if the kill-database callback has been scheduled. | 257 // Indicates if the kill-database callback has been scheduled. |
| 249 bool corruption_detected_; | 258 bool corruption_detected_; |
| 250 | 259 |
| 251 // If false, we should filter out session cookies when reading the DB. | 260 // If false, we should filter out session cookies when reading the DB. |
| 252 bool restore_old_session_cookies_; | 261 bool restore_old_session_cookies_; |
| 253 | 262 |
| 254 // Policy defining what data is deleted on shutdown. | 263 // Policy defining what data is deleted on shutdown. |
| 255 scoped_refptr<ClearOnExitPolicy> clear_on_exit_policy_; | 264 scoped_refptr<ClearOnExitPolicy> clear_on_exit_policy_; |
| 256 | 265 |
| 257 // The cumulative time spent loading the cookies on the DB thread. Incremented | 266 // The cumulative time spent loading the cookies on the background runner. |
| 258 // and reported from the DB thread. | 267 // Incremented and reported from the background runner. |
| 259 base::TimeDelta cookie_load_duration_; | 268 base::TimeDelta cookie_load_duration_; |
| 260 | 269 |
| 261 // The total number of cookies read. Incremented and reported on the DB | 270 // The total number of cookies read. Incremented and reported on the |
| 262 // thread. | 271 // background runner. |
| 263 int num_cookies_read_; | 272 int num_cookies_read_; |
| 264 | 273 |
| 274 scoped_refptr<base::SequencedTaskRunner> client_task_runner_; |
| 275 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 276 |
| 265 // Guards the following metrics-related properties (only accessed when | 277 // Guards the following metrics-related properties (only accessed when |
| 266 // starting/completing priority loads or completing the total load). | 278 // starting/completing priority loads or completing the total load). |
| 267 base::Lock metrics_lock_; | 279 base::Lock metrics_lock_; |
| 268 int num_priority_waiting_; | 280 int num_priority_waiting_; |
| 269 // The total number of priority requests. | 281 // The total number of priority requests. |
| 270 int total_priority_requests_; | 282 int total_priority_requests_; |
| 271 // The time when |num_priority_waiting_| incremented to 1. | 283 // The time when |num_priority_waiting_| incremented to 1. |
| 272 base::Time current_priority_wait_start_; | 284 base::Time current_priority_wait_start_; |
| 273 // The cumulative duration of time when |num_priority_waiting_| was greater | 285 // The cumulative duration of time when |num_priority_waiting_| was greater |
| 274 // than 1. | 286 // than 1. |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 | 379 |
| 368 return true; | 380 return true; |
| 369 } | 381 } |
| 370 | 382 |
| 371 } // namespace | 383 } // namespace |
| 372 | 384 |
| 373 void SQLitePersistentCookieStore::Backend::Load( | 385 void SQLitePersistentCookieStore::Backend::Load( |
| 374 const LoadedCallback& loaded_callback) { | 386 const LoadedCallback& loaded_callback) { |
| 375 // This function should be called only once per instance. | 387 // This function should be called only once per instance. |
| 376 DCHECK(!db_.get()); | 388 DCHECK(!db_.get()); |
| 377 BrowserThread::PostTask( | 389 PostBackgroundTask(FROM_HERE, base::Bind( |
| 378 BrowserThread::DB, FROM_HERE, | 390 &Backend::LoadAndNotifyInBackground, this, |
| 379 base::Bind(&Backend::LoadAndNotifyOnDBThread, this, loaded_callback, | 391 loaded_callback, base::Time::Now())); |
| 380 base::Time::Now())); | |
| 381 } | 392 } |
| 382 | 393 |
| 383 void SQLitePersistentCookieStore::Backend::LoadCookiesForKey( | 394 void SQLitePersistentCookieStore::Backend::LoadCookiesForKey( |
| 384 const std::string& key, | 395 const std::string& key, |
| 385 const LoadedCallback& loaded_callback) { | 396 const LoadedCallback& loaded_callback) { |
| 386 { | 397 { |
| 387 base::AutoLock locked(metrics_lock_); | 398 base::AutoLock locked(metrics_lock_); |
| 388 if (num_priority_waiting_ == 0) | 399 if (num_priority_waiting_ == 0) |
| 389 current_priority_wait_start_ = base::Time::Now(); | 400 current_priority_wait_start_ = base::Time::Now(); |
| 390 num_priority_waiting_++; | 401 num_priority_waiting_++; |
| 391 total_priority_requests_++; | 402 total_priority_requests_++; |
| 392 } | 403 } |
| 393 | 404 |
| 394 BrowserThread::PostTask( | 405 PostBackgroundTask(FROM_HERE, base::Bind( |
| 395 BrowserThread::DB, FROM_HERE, | 406 &Backend::LoadKeyAndNotifyInBackground, |
| 396 base::Bind(&Backend::LoadKeyAndNotifyOnDBThread, this, | 407 this, key, loaded_callback, base::Time::Now())); |
| 397 key, | |
| 398 loaded_callback, | |
| 399 base::Time::Now())); | |
| 400 } | 408 } |
| 401 | 409 |
| 402 void SQLitePersistentCookieStore::Backend::LoadAndNotifyOnDBThread( | 410 void SQLitePersistentCookieStore::Backend::LoadAndNotifyInBackground( |
| 403 const LoadedCallback& loaded_callback, const base::Time& posted_at) { | 411 const LoadedCallback& loaded_callback, const base::Time& posted_at) { |
| 404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 412 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 405 IncrementTimeDelta increment(&cookie_load_duration_); | 413 IncrementTimeDelta increment(&cookie_load_duration_); |
| 406 | 414 |
| 407 UMA_HISTOGRAM_CUSTOM_TIMES( | 415 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 408 "Cookie.TimeLoadDBQueueWait", | 416 "Cookie.TimeLoadDBQueueWait", |
| 409 base::Time::Now() - posted_at, | 417 base::Time::Now() - posted_at, |
| 410 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 418 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 411 50); | 419 50); |
| 412 | 420 |
| 413 if (!InitializeDatabase()) { | 421 if (!InitializeDatabase()) { |
| 414 BrowserThread::PostTask( | 422 PostClientTask(FROM_HERE, base::Bind( |
| 415 BrowserThread::IO, FROM_HERE, | 423 &Backend::CompleteLoadInForeground, this, loaded_callback, false)); |
| 416 base::Bind(&SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread, | |
| 417 this, loaded_callback, false)); | |
| 418 } else { | 424 } else { |
| 419 ChainLoadCookies(loaded_callback); | 425 ChainLoadCookies(loaded_callback); |
| 420 } | 426 } |
| 421 } | 427 } |
| 422 | 428 |
| 423 void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyOnDBThread( | 429 void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyInBackground( |
| 424 const std::string& key, | 430 const std::string& key, |
| 425 const LoadedCallback& loaded_callback, | 431 const LoadedCallback& loaded_callback, |
| 426 const base::Time& posted_at) { | 432 const base::Time& posted_at) { |
| 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 433 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 428 IncrementTimeDelta increment(&cookie_load_duration_); | 434 IncrementTimeDelta increment(&cookie_load_duration_); |
| 429 | 435 |
| 430 UMA_HISTOGRAM_CUSTOM_TIMES( | 436 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 431 "Cookie.TimeKeyLoadDBQueueWait", | 437 "Cookie.TimeKeyLoadDBQueueWait", |
| 432 base::Time::Now() - posted_at, | 438 base::Time::Now() - posted_at, |
| 433 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 439 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 434 50); | 440 50); |
| 435 | 441 |
| 436 bool success = false; | 442 bool success = false; |
| 437 if (InitializeDatabase()) { | 443 if (InitializeDatabase()) { |
| 438 std::map<std::string, std::set<std::string> >::iterator | 444 std::map<std::string, std::set<std::string> >::iterator |
| 439 it = keys_to_load_.find(key); | 445 it = keys_to_load_.find(key); |
| 440 if (it != keys_to_load_.end()) { | 446 if (it != keys_to_load_.end()) { |
| 441 success = LoadCookiesForDomains(it->second); | 447 success = LoadCookiesForDomains(it->second); |
| 442 keys_to_load_.erase(it); | 448 keys_to_load_.erase(it); |
| 443 } else { | 449 } else { |
| 444 success = true; | 450 success = true; |
| 445 } | 451 } |
| 446 } | 452 } |
| 447 | 453 |
| 448 BrowserThread::PostTask( | 454 PostClientTask(FROM_HERE, base::Bind( |
| 449 BrowserThread::IO, FROM_HERE, | 455 &SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground, |
| 450 base::Bind( | 456 this, loaded_callback, success)); |
| 451 &SQLitePersistentCookieStore::Backend::CompleteLoadForKeyOnIOThread, | |
| 452 this, loaded_callback, success)); | |
| 453 } | 457 } |
| 454 | 458 |
| 455 void SQLitePersistentCookieStore::Backend::CompleteLoadForKeyOnIOThread( | 459 void SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground( |
| 456 const LoadedCallback& loaded_callback, | 460 const LoadedCallback& loaded_callback, |
| 457 bool load_success) { | 461 bool load_success) { |
| 462 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); |
| 463 |
| 458 Notify(loaded_callback, load_success); | 464 Notify(loaded_callback, load_success); |
| 459 | 465 |
| 460 { | 466 { |
| 461 base::AutoLock locked(metrics_lock_); | 467 base::AutoLock locked(metrics_lock_); |
| 462 num_priority_waiting_--; | 468 num_priority_waiting_--; |
| 463 if (num_priority_waiting_ == 0) { | 469 if (num_priority_waiting_ == 0) { |
| 464 priority_wait_duration_ += | 470 priority_wait_duration_ += |
| 465 base::Time::Now() - current_priority_wait_start_; | 471 base::Time::Now() - current_priority_wait_start_; |
| 466 } | 472 } |
| 467 } | 473 } |
| 468 | 474 |
| 469 } | 475 } |
| 470 | 476 |
| 471 void SQLitePersistentCookieStore::Backend::ReportMetricsOnDBThread() { | 477 void SQLitePersistentCookieStore::Backend::ReportMetricsInBackground() { |
| 472 UMA_HISTOGRAM_CUSTOM_TIMES( | 478 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 473 "Cookie.TimeLoad", | 479 "Cookie.TimeLoad", |
| 474 cookie_load_duration_, | 480 cookie_load_duration_, |
| 475 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 481 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 476 50); | 482 50); |
| 477 } | 483 } |
| 478 | 484 |
| 479 void SQLitePersistentCookieStore::Backend::ReportMetrics() { | 485 void SQLitePersistentCookieStore::Backend::ReportMetrics() { |
| 480 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, base::Bind( | 486 PostBackgroundTask(FROM_HERE, base::Bind( |
| 481 &SQLitePersistentCookieStore::Backend::ReportMetricsOnDBThread, this)); | 487 &SQLitePersistentCookieStore::Backend::ReportMetricsInBackground, this)); |
| 482 | 488 |
| 483 { | 489 { |
| 484 base::AutoLock locked(metrics_lock_); | 490 base::AutoLock locked(metrics_lock_); |
| 485 UMA_HISTOGRAM_CUSTOM_TIMES( | 491 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 486 "Cookie.PriorityBlockingTime", | 492 "Cookie.PriorityBlockingTime", |
| 487 priority_wait_duration_, | 493 priority_wait_duration_, |
| 488 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 494 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 489 50); | 495 50); |
| 490 | 496 |
| 491 UMA_HISTOGRAM_COUNTS_100( | 497 UMA_HISTOGRAM_COUNTS_100( |
| 492 "Cookie.PriorityLoadCount", | 498 "Cookie.PriorityLoadCount", |
| 493 total_priority_requests_); | 499 total_priority_requests_); |
| 494 | 500 |
| 495 UMA_HISTOGRAM_COUNTS_10000( | 501 UMA_HISTOGRAM_COUNTS_10000( |
| 496 "Cookie.NumberOfLoadedCookies", | 502 "Cookie.NumberOfLoadedCookies", |
| 497 num_cookies_read_); | 503 num_cookies_read_); |
| 498 } | 504 } |
| 499 } | 505 } |
| 500 | 506 |
| 501 void SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread( | 507 void SQLitePersistentCookieStore::Backend::CompleteLoadInForeground( |
| 502 const LoadedCallback& loaded_callback, bool load_success) { | 508 const LoadedCallback& loaded_callback, bool load_success) { |
| 503 Notify(loaded_callback, load_success); | 509 Notify(loaded_callback, load_success); |
| 504 | 510 |
| 505 if (load_success) | 511 if (load_success) |
| 506 ReportMetrics(); | 512 ReportMetrics(); |
| 507 } | 513 } |
| 508 | 514 |
| 509 void SQLitePersistentCookieStore::Backend::Notify( | 515 void SQLitePersistentCookieStore::Backend::Notify( |
| 510 const LoadedCallback& loaded_callback, | 516 const LoadedCallback& loaded_callback, |
| 511 bool load_success) { | 517 bool load_success) { |
| 512 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 518 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); |
| 513 | 519 |
| 514 std::vector<net::CanonicalCookie*> cookies; | 520 std::vector<net::CanonicalCookie*> cookies; |
| 515 { | 521 { |
| 516 base::AutoLock locked(lock_); | 522 base::AutoLock locked(lock_); |
| 517 cookies.swap(cookies_); | 523 cookies.swap(cookies_); |
| 518 } | 524 } |
| 519 | 525 |
| 520 loaded_callback.Run(cookies); | 526 loaded_callback.Run(cookies); |
| 521 } | 527 } |
| 522 | 528 |
| 523 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { | 529 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { |
| 524 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 530 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 525 | 531 |
| 526 if (initialized_ || corruption_detected_) { | 532 if (initialized_ || corruption_detected_) { |
| 527 // Return false if we were previously initialized but the DB has since been | 533 // Return false if we were previously initialized but the DB has since been |
| 528 // closed, or if corruption caused a database reset during initialization. | 534 // closed, or if corruption caused a database reset during initialization. |
| 529 return db_ != NULL; | 535 return db_ != NULL; |
| 530 } | 536 } |
| 531 | 537 |
| 532 base::Time start = base::Time::Now(); | 538 base::Time start = base::Time::Now(); |
| 533 | 539 |
| 534 const base::FilePath dir = path_.DirName(); | 540 const base::FilePath dir = path_.DirName(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 base::Time::Now() - start, | 607 base::Time::Now() - start, |
| 602 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 608 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 603 50); | 609 50); |
| 604 | 610 |
| 605 initialized_ = true; | 611 initialized_ = true; |
| 606 return true; | 612 return true; |
| 607 } | 613 } |
| 608 | 614 |
| 609 void SQLitePersistentCookieStore::Backend::ChainLoadCookies( | 615 void SQLitePersistentCookieStore::Backend::ChainLoadCookies( |
| 610 const LoadedCallback& loaded_callback) { | 616 const LoadedCallback& loaded_callback) { |
| 611 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 617 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 612 IncrementTimeDelta increment(&cookie_load_duration_); | 618 IncrementTimeDelta increment(&cookie_load_duration_); |
| 613 | 619 |
| 614 bool load_success = true; | 620 bool load_success = true; |
| 615 | 621 |
| 616 if (!db_.get()) { | 622 if (!db_.get()) { |
| 617 // Close() has been called on this store. | 623 // Close() has been called on this store. |
| 618 load_success = false; | 624 load_success = false; |
| 619 } else if (keys_to_load_.size() > 0) { | 625 } else if (keys_to_load_.size() > 0) { |
| 620 // Load cookies for the first domain key. | 626 // Load cookies for the first domain key. |
| 621 std::map<std::string, std::set<std::string> >::iterator | 627 std::map<std::string, std::set<std::string> >::iterator |
| 622 it = keys_to_load_.begin(); | 628 it = keys_to_load_.begin(); |
| 623 load_success = LoadCookiesForDomains(it->second); | 629 load_success = LoadCookiesForDomains(it->second); |
| 624 keys_to_load_.erase(it); | 630 keys_to_load_.erase(it); |
| 625 } | 631 } |
| 626 | 632 |
| 627 // If load is successful and there are more domain keys to be loaded, | 633 // If load is successful and there are more domain keys to be loaded, |
| 628 // then post a DB task to continue chain-load; | 634 // then post a background task to continue chain-load; |
| 629 // Otherwise notify on IO thread. | 635 // Otherwise notify on client runner. |
| 630 if (load_success && keys_to_load_.size() > 0) { | 636 if (load_success && keys_to_load_.size() > 0) { |
| 631 BrowserThread::PostTask( | 637 PostBackgroundTask(FROM_HERE, base::Bind( |
| 632 BrowserThread::DB, FROM_HERE, | 638 &Backend::ChainLoadCookies, this, loaded_callback)); |
| 633 base::Bind(&Backend::ChainLoadCookies, this, loaded_callback)); | |
| 634 } else { | 639 } else { |
| 635 BrowserThread::PostTask( | 640 PostClientTask(FROM_HERE, base::Bind( |
| 636 BrowserThread::IO, FROM_HERE, | 641 &Backend::CompleteLoadInForeground, this, |
| 637 base::Bind(&SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread, | 642 loaded_callback, load_success)); |
| 638 this, loaded_callback, load_success)); | |
| 639 if (load_success && !restore_old_session_cookies_) | 643 if (load_success && !restore_old_session_cookies_) |
| 640 DeleteSessionCookiesOnStartup(); | 644 DeleteSessionCookiesOnStartup(); |
| 641 } | 645 } |
| 642 } | 646 } |
| 643 | 647 |
| 644 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( | 648 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( |
| 645 const std::set<std::string>& domains) { | 649 const std::set<std::string>& domains) { |
| 646 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 650 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 647 | 651 |
| 648 sql::Statement smt; | 652 sql::Statement smt; |
| 649 if (restore_old_session_cookies_) { | 653 if (restore_old_session_cookies_) { |
| 650 smt.Assign(db_->GetCachedStatement( | 654 smt.Assign(db_->GetCachedStatement( |
| 651 SQL_FROM_HERE, | 655 SQL_FROM_HERE, |
| 652 "SELECT creation_utc, host_key, name, value, path, expires_utc, " | 656 "SELECT creation_utc, host_key, name, value, path, expires_utc, " |
| 653 "secure, httponly, last_access_utc, has_expires, persistent " | 657 "secure, httponly, last_access_utc, has_expires, persistent " |
| 654 "FROM cookies WHERE host_key = ?")); | 658 "FROM cookies WHERE host_key = ?")); |
| 655 } else { | 659 } else { |
| 656 smt.Assign(db_->GetCachedStatement( | 660 smt.Assign(db_->GetCachedStatement( |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 823 BatchOperation(PendingOperation::COOKIE_DELETE, cc); | 827 BatchOperation(PendingOperation::COOKIE_DELETE, cc); |
| 824 } | 828 } |
| 825 | 829 |
| 826 void SQLitePersistentCookieStore::Backend::BatchOperation( | 830 void SQLitePersistentCookieStore::Backend::BatchOperation( |
| 827 PendingOperation::OperationType op, | 831 PendingOperation::OperationType op, |
| 828 const net::CanonicalCookie& cc) { | 832 const net::CanonicalCookie& cc) { |
| 829 // Commit every 30 seconds. | 833 // Commit every 30 seconds. |
| 830 static const int kCommitIntervalMs = 30 * 1000; | 834 static const int kCommitIntervalMs = 30 * 1000; |
| 831 // Commit right away if we have more than 512 outstanding operations. | 835 // Commit right away if we have more than 512 outstanding operations. |
| 832 static const size_t kCommitAfterBatchSize = 512; | 836 static const size_t kCommitAfterBatchSize = 512; |
| 833 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB)); | 837 DCHECK(!background_task_runner_->RunsTasksOnCurrentThread()); |
| 834 | 838 |
| 835 // We do a full copy of the cookie here, and hopefully just here. | 839 // We do a full copy of the cookie here, and hopefully just here. |
| 836 scoped_ptr<PendingOperation> po(new PendingOperation(op, cc)); | 840 scoped_ptr<PendingOperation> po(new PendingOperation(op, cc)); |
| 837 | 841 |
| 838 PendingOperationsList::size_type num_pending; | 842 PendingOperationsList::size_type num_pending; |
| 839 { | 843 { |
| 840 base::AutoLock locked(lock_); | 844 base::AutoLock locked(lock_); |
| 841 pending_.push_back(po.release()); | 845 pending_.push_back(po.release()); |
| 842 num_pending = ++num_pending_; | 846 num_pending = ++num_pending_; |
| 843 } | 847 } |
| 844 | 848 |
| 845 if (num_pending == 1) { | 849 if (num_pending == 1) { |
| 846 // We've gotten our first entry for this batch, fire off the timer. | 850 // We've gotten our first entry for this batch, fire off the timer. |
| 847 BrowserThread::PostDelayedTask( | 851 if (!background_task_runner_->PostDelayedTask( |
| 848 BrowserThread::DB, FROM_HERE, | 852 FROM_HERE, base::Bind(&Backend::Commit, this), |
| 849 base::Bind(&Backend::Commit, this), | 853 base::TimeDelta::FromMilliseconds(kCommitIntervalMs))) { |
| 850 base::TimeDelta::FromMilliseconds(kCommitIntervalMs)); | 854 NOTREACHED() << "background_task_runner_ is not running."; |
| 855 } |
| 851 } else if (num_pending == kCommitAfterBatchSize) { | 856 } else if (num_pending == kCommitAfterBatchSize) { |
| 852 // We've reached a big enough batch, fire off a commit now. | 857 // We've reached a big enough batch, fire off a commit now. |
| 853 BrowserThread::PostTask( | 858 PostBackgroundTask(FROM_HERE, base::Bind(&Backend::Commit, this)); |
| 854 BrowserThread::DB, FROM_HERE, | |
| 855 base::Bind(&Backend::Commit, this)); | |
| 856 } | 859 } |
| 857 } | 860 } |
| 858 | 861 |
| 859 void SQLitePersistentCookieStore::Backend::Commit() { | 862 void SQLitePersistentCookieStore::Backend::Commit() { |
| 860 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 863 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 861 | 864 |
| 862 PendingOperationsList ops; | 865 PendingOperationsList ops; |
| 863 { | 866 { |
| 864 base::AutoLock locked(lock_); | 867 base::AutoLock locked(lock_); |
| 865 pending_.swap(ops); | 868 pending_.swap(ops); |
| 866 num_pending_ = 0; | 869 num_pending_ = 0; |
| 867 } | 870 } |
| 868 | 871 |
| 869 // Maybe an old timer fired or we are already Close()'ed. | 872 // Maybe an old timer fired or we are already Close()'ed. |
| 870 if (!db_.get() || ops.empty()) | 873 if (!db_.get() || ops.empty()) |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 940 break; | 943 break; |
| 941 } | 944 } |
| 942 } | 945 } |
| 943 bool succeeded = transaction.Commit(); | 946 bool succeeded = transaction.Commit(); |
| 944 UMA_HISTOGRAM_ENUMERATION("Cookie.BackingStoreUpdateResults", | 947 UMA_HISTOGRAM_ENUMERATION("Cookie.BackingStoreUpdateResults", |
| 945 succeeded ? 0 : 1, 2); | 948 succeeded ? 0 : 1, 2); |
| 946 } | 949 } |
| 947 | 950 |
| 948 void SQLitePersistentCookieStore::Backend::Flush( | 951 void SQLitePersistentCookieStore::Backend::Flush( |
| 949 const base::Closure& callback) { | 952 const base::Closure& callback) { |
| 950 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB)); | 953 DCHECK(!background_task_runner_->RunsTasksOnCurrentThread()); |
| 951 BrowserThread::PostTask( | 954 PostBackgroundTask(FROM_HERE, base::Bind(&Backend::Commit, this)); |
| 952 BrowserThread::DB, FROM_HERE, base::Bind(&Backend::Commit, this)); | 955 |
| 953 if (!callback.is_null()) { | 956 if (!callback.is_null()) { |
| 954 // We want the completion task to run immediately after Commit() returns. | 957 // We want the completion task to run immediately after Commit() returns. |
| 955 // Posting it from here means there is less chance of another task getting | 958 // Posting it from here means there is less chance of another task getting |
| 956 // onto the message queue first, than if we posted it from Commit() itself. | 959 // onto the message queue first, than if we posted it from Commit() itself. |
| 957 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, callback); | 960 PostBackgroundTask(FROM_HERE, callback); |
| 958 } | 961 } |
| 959 } | 962 } |
| 960 | 963 |
| 961 // Fire off a close message to the background thread. We could still have a | 964 // Fire off a close message to the background runner. We could still have a |
| 962 // pending commit timer or Load operations holding references on us, but if/when | 965 // pending commit timer or Load operations holding references on us, but if/when |
| 963 // this fires we will already have been cleaned up and it will be ignored. | 966 // this fires we will already have been cleaned up and it will be ignored. |
| 964 void SQLitePersistentCookieStore::Backend::Close() { | 967 void SQLitePersistentCookieStore::Backend::Close() { |
| 965 if (BrowserThread::CurrentlyOn(BrowserThread::DB)) { | 968 if (background_task_runner_->RunsTasksOnCurrentThread()) { |
| 966 InternalBackgroundClose(); | 969 InternalBackgroundClose(); |
| 967 } else { | 970 } else { |
| 968 // Must close the backend on the background thread. | 971 // Must close the backend on the background runner. |
| 969 BrowserThread::PostTask( | 972 PostBackgroundTask(FROM_HERE, |
| 970 BrowserThread::DB, FROM_HERE, | 973 base::Bind(&Backend::InternalBackgroundClose, this)); |
| 971 base::Bind(&Backend::InternalBackgroundClose, this)); | |
| 972 } | 974 } |
| 973 } | 975 } |
| 974 | 976 |
| 975 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() { | 977 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() { |
| 976 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 978 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 977 // Commit any pending operations | 979 // Commit any pending operations |
| 978 Commit(); | 980 Commit(); |
| 979 | 981 |
| 980 if (!force_keep_session_state_ && clear_on_exit_policy_.get() && | 982 if (!force_keep_session_state_ && clear_on_exit_policy_.get() && |
| 981 clear_on_exit_policy_->HasClearOnExitOrigins()) { | 983 clear_on_exit_policy_->HasClearOnExitOrigins()) { |
| 982 DeleteSessionCookiesOnShutdown(); | 984 DeleteSessionCookiesOnShutdown(); |
| 983 } | 985 } |
| 984 | 986 |
| 985 meta_table_.Reset(); | 987 meta_table_.Reset(); |
| 986 db_.reset(); | 988 db_.reset(); |
| 987 } | 989 } |
| 988 | 990 |
| 989 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnShutdown() { | 991 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnShutdown() { |
| 990 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 992 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 991 | 993 |
| 992 if (!db_.get()) | 994 if (!db_.get()) |
| 993 return; | 995 return; |
| 994 | 996 |
| 995 sql::Statement del_smt(db_->GetCachedStatement( | 997 sql::Statement del_smt(db_->GetCachedStatement( |
| 996 SQL_FROM_HERE, "DELETE FROM cookies WHERE host_key=? AND secure=?")); | 998 SQL_FROM_HERE, "DELETE FROM cookies WHERE host_key=? AND secure=?")); |
| 997 if (!del_smt.is_valid()) { | 999 if (!del_smt.is_valid()) { |
| 998 LOG(WARNING) << "Unable to delete cookies on shutdown."; | 1000 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 999 return; | 1001 return; |
| 1000 } | 1002 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1021 del_smt.BindInt(1, it->first.second); | 1023 del_smt.BindInt(1, it->first.second); |
| 1022 if (!del_smt.Run()) | 1024 if (!del_smt.Run()) |
| 1023 NOTREACHED() << "Could not delete a cookie from the DB."; | 1025 NOTREACHED() << "Could not delete a cookie from the DB."; |
| 1024 } | 1026 } |
| 1025 | 1027 |
| 1026 if (!transaction.Commit()) | 1028 if (!transaction.Commit()) |
| 1027 LOG(WARNING) << "Unable to delete cookies on shutdown."; | 1029 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 1028 } | 1030 } |
| 1029 | 1031 |
| 1030 void SQLitePersistentCookieStore::Backend::ScheduleKillDatabase() { | 1032 void SQLitePersistentCookieStore::Backend::ScheduleKillDatabase() { |
| 1031 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 1033 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 1032 | 1034 |
| 1033 corruption_detected_ = true; | 1035 corruption_detected_ = true; |
| 1034 | 1036 |
| 1035 // Don't just do the close/delete here, as we are being called by |db| and | 1037 // Don't just do the close/delete here, as we are being called by |db| and |
| 1036 // that seems dangerous. | 1038 // that seems dangerous. |
| 1037 MessageLoop::current()->PostTask( | 1039 PostBackgroundTask(FROM_HERE, base::Bind(&Backend::KillDatabase, this)); |
| 1038 FROM_HERE, base::Bind(&Backend::KillDatabase, this)); | |
| 1039 } | 1040 } |
| 1040 | 1041 |
| 1041 void SQLitePersistentCookieStore::Backend::KillDatabase() { | 1042 void SQLitePersistentCookieStore::Backend::KillDatabase() { |
| 1042 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 1043 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 1043 | 1044 |
| 1044 if (db_.get()) { | 1045 if (db_.get()) { |
| 1045 // This Backend will now be in-memory only. In a future run we will recreate | 1046 // This Backend will now be in-memory only. In a future run we will recreate |
| 1046 // the database. Hopefully things go better then! | 1047 // the database. Hopefully things go better then! |
| 1047 bool success = db_->RazeAndClose(); | 1048 bool success = db_->RazeAndClose(); |
| 1048 UMA_HISTOGRAM_BOOLEAN("Cookie.KillDatabaseResult", success); | 1049 UMA_HISTOGRAM_BOOLEAN("Cookie.KillDatabaseResult", success); |
| 1049 meta_table_.Reset(); | 1050 meta_table_.Reset(); |
| 1050 db_.reset(); | 1051 db_.reset(); |
| 1051 } | 1052 } |
| 1052 } | 1053 } |
| 1053 | 1054 |
| 1054 void SQLitePersistentCookieStore::Backend::SetForceKeepSessionState() { | 1055 void SQLitePersistentCookieStore::Backend::SetForceKeepSessionState() { |
| 1055 base::AutoLock locked(lock_); | 1056 base::AutoLock locked(lock_); |
| 1056 force_keep_session_state_ = true; | 1057 force_keep_session_state_ = true; |
| 1057 } | 1058 } |
| 1058 | 1059 |
| 1059 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() { | 1060 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() { |
| 1060 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 1061 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 1061 if (!db_->Execute("DELETE FROM cookies WHERE persistent == 0")) | 1062 if (!db_->Execute("DELETE FROM cookies WHERE persistent == 0")) |
| 1062 LOG(WARNING) << "Unable to delete session cookies."; | 1063 LOG(WARNING) << "Unable to delete session cookies."; |
| 1063 } | 1064 } |
| 1064 | 1065 |
| 1066 void SQLitePersistentCookieStore::Backend::PostBackgroundTask( |
| 1067 const tracked_objects::Location& origin, const base::Closure& task) { |
| 1068 if (!background_task_runner_->PostTask(origin, task)) { |
| 1069 LOG(WARNING) << "Failed to post task from " << origin.ToString() |
| 1070 << " to background_task_runner_."; |
| 1071 } |
| 1072 } |
| 1073 |
| 1074 void SQLitePersistentCookieStore::Backend::PostClientTask( |
| 1075 const tracked_objects::Location& origin, const base::Closure& task) { |
| 1076 if (!client_task_runner_->PostTask(origin, task)) { |
| 1077 LOG(WARNING) << "Failed to post task from " << origin.ToString() |
| 1078 << " to client_task_runner_."; |
| 1079 } |
| 1080 } |
| 1081 |
| 1065 SQLitePersistentCookieStore::SQLitePersistentCookieStore( | 1082 SQLitePersistentCookieStore::SQLitePersistentCookieStore( |
| 1066 const base::FilePath& path, | 1083 const base::FilePath& path, |
| 1084 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
| 1085 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
| 1067 bool restore_old_session_cookies, | 1086 bool restore_old_session_cookies, |
| 1068 ClearOnExitPolicy* clear_on_exit_policy) | 1087 ClearOnExitPolicy* clear_on_exit_policy) |
| 1069 : backend_( | 1088 : backend_(new Backend(path, |
| 1070 new Backend(path, restore_old_session_cookies, clear_on_exit_policy)) { | 1089 client_task_runner, |
| 1090 background_task_runner, |
| 1091 restore_old_session_cookies, |
| 1092 clear_on_exit_policy)) { |
| 1071 } | 1093 } |
| 1072 | 1094 |
| 1073 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 1095 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
| 1074 backend_->Load(loaded_callback); | 1096 backend_->Load(loaded_callback); |
| 1075 } | 1097 } |
| 1076 | 1098 |
| 1077 void SQLitePersistentCookieStore::LoadCookiesForKey( | 1099 void SQLitePersistentCookieStore::LoadCookiesForKey( |
| 1078 const std::string& key, | 1100 const std::string& key, |
| 1079 const LoadedCallback& loaded_callback) { | 1101 const LoadedCallback& loaded_callback) { |
| 1080 backend_->LoadCookiesForKey(key, loaded_callback); | 1102 backend_->LoadCookiesForKey(key, loaded_callback); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1097 backend_->SetForceKeepSessionState(); | 1119 backend_->SetForceKeepSessionState(); |
| 1098 } | 1120 } |
| 1099 | 1121 |
| 1100 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { | 1122 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { |
| 1101 backend_->Flush(callback); | 1123 backend_->Flush(callback); |
| 1102 } | 1124 } |
| 1103 | 1125 |
| 1104 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 1126 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
| 1105 backend_->Close(); | 1127 backend_->Close(); |
| 1106 // We release our reference to the Backend, though it will probably still have | 1128 // We release our reference to the Backend, though it will probably still have |
| 1107 // a reference if the background thread has not run Close() yet. | 1129 // a reference if the background runner has not run Close() yet. |
| 1108 } | 1130 } |
| OLD | NEW |