OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_path.h" | 14 #include "base/file_path.h" |
15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
20 #include "base/string_util.h" | 20 #include "base/string_util.h" |
21 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
22 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
23 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
| 24 #include "base/time.h" |
24 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" | 25 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
26 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
27 #include "net/base/registry_controlled_domain.h" | 28 #include "net/base/registry_controlled_domain.h" |
28 #include "sql/meta_table.h" | 29 #include "sql/meta_table.h" |
29 #include "sql/statement.h" | 30 #include "sql/statement.h" |
30 #include "sql/transaction.h" | 31 #include "sql/transaction.h" |
31 | 32 |
32 using base::Time; | 33 using base::Time; |
33 using content::BrowserThread; | 34 using content::BrowserThread; |
34 | 35 |
35 // This class is designed to be shared between any calling threads and the | 36 // This class is designed to be shared between any calling threads and the |
36 // database thread. It batches operations and commits them on a timer. | 37 // database thread. It batches operations and commits them on a timer. |
37 // | 38 // |
38 // SQLitePersistentCookieStore::Load is called to load all cookies. It | 39 // SQLitePersistentCookieStore::Load is called to load all cookies. It |
39 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread | 40 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread |
40 // task to the DB thread. This task calls Backend::ChainLoadCookies(), which | 41 // task to the DB thread. This task calls Backend::ChainLoadCookies(), which |
41 // repeatedly posts itself to the DB thread to load each eTLD+1's cookies in | 42 // repeatedly posts itself to the DB thread to load each eTLD+1's cookies in |
42 // separate tasks. When this is complete, Backend::NotifyOnIOThread is posted | 43 // separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is |
43 // to the IO thread, which notifies the caller of SQLitePersistentCookieStore:: | 44 // posted to the IO thread, which notifies the caller of |
44 // Load that the load is complete. | 45 // SQLitePersistentCookieStore::Load that the load is complete. |
45 // | 46 // |
46 // If a priority load request is invoked via SQLitePersistentCookieStore:: | 47 // If a priority load request is invoked via SQLitePersistentCookieStore:: |
47 // LoadCookiesForKey, it is delegated to Backend::LoadCookiesForKey, which posts | 48 // LoadCookiesForKey, it is delegated to Backend::LoadCookiesForKey, which posts |
48 // Backend::LoadKeyAndNotifyOnDBThread to the DB thread. That routine loads just | 49 // Backend::LoadKeyAndNotifyOnDBThread to the DB thread. That routine loads just |
49 // that single domain key (eTLD+1)'s cookies, and posts a Backend:: | 50 // that single domain key (eTLD+1)'s cookies, and posts a Backend:: |
50 // NotifyOnIOThread to the IO thread to notify the caller of | 51 // CompleteLoadForKeyOnIOThread to the IO thread to notify the caller of |
51 // SQLitePersistentCookieStore::LoadCookiesForKey that that load is complete. | 52 // SQLitePersistentCookieStore::LoadCookiesForKey that that load is complete. |
52 // | 53 // |
53 // Subsequent to loading, mutations may be queued by any thread using | 54 // Subsequent to loading, mutations may be queued by any thread using |
54 // AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to | 55 // AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to |
55 // disk on the DB thread every 30 seconds, 512 operations, or call to Flush(), | 56 // disk on the DB thread every 30 seconds, 512 operations, or call to Flush(), |
56 // whichever occurs first. | 57 // whichever occurs first. |
57 class SQLitePersistentCookieStore::Backend | 58 class SQLitePersistentCookieStore::Backend |
58 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { | 59 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { |
59 public: | 60 public: |
60 explicit Backend(const FilePath& path) | 61 explicit Backend(const FilePath& path) |
61 : path_(path), | 62 : path_(path), |
62 db_(NULL), | 63 db_(NULL), |
63 num_pending_(0), | 64 num_pending_(0), |
64 clear_local_state_on_exit_(false), | 65 clear_local_state_on_exit_(false), |
65 initialized_(false) { | 66 initialized_(false), |
| 67 num_priority_waiting_(0), |
| 68 total_priority_requests_(0) { |
66 } | 69 } |
67 | 70 |
68 // Creates or loads the SQLite database. | 71 // Creates or loads the SQLite database. |
69 void Load(const LoadedCallback& loaded_callback); | 72 void Load(const LoadedCallback& loaded_callback); |
70 | 73 |
71 // Loads cookies for the domain key (eTLD+1). | 74 // Loads cookies for the domain key (eTLD+1). |
72 void LoadCookiesForKey(const std::string& domain, | 75 void LoadCookiesForKey(const std::string& domain, |
73 const LoadedCallback& loaded_callback); | 76 const LoadedCallback& loaded_callback); |
74 | 77 |
75 // Batch a cookie addition. | 78 // Batch a cookie addition. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // Creates or loads the SQLite database on DB thread. | 129 // Creates or loads the SQLite database on DB thread. |
127 void LoadAndNotifyOnDBThread(const LoadedCallback& loaded_callback); | 130 void LoadAndNotifyOnDBThread(const LoadedCallback& loaded_callback); |
128 | 131 |
129 // Loads cookies for the domain key (eTLD+1) on DB thread. | 132 // Loads cookies for the domain key (eTLD+1) on DB thread. |
130 void LoadKeyAndNotifyOnDBThread(const std::string& domains, | 133 void LoadKeyAndNotifyOnDBThread(const std::string& domains, |
131 const LoadedCallback& loaded_callback); | 134 const LoadedCallback& loaded_callback); |
132 | 135 |
133 // Notifies the CookieMonster when loading completes for a specific domain key | 136 // Notifies the CookieMonster when loading completes for a specific domain key |
134 // or for all domain keys. Triggers the callback and passes it all cookies | 137 // or for all domain keys. Triggers the callback and passes it all cookies |
135 // that have been loaded from DB since last IO notification. | 138 // that have been loaded from DB since last IO notification. |
136 void NotifyOnIOThread( | 139 void Notify(const LoadedCallback& loaded_callback, bool load_success); |
137 const LoadedCallback& loaded_callback, | 140 |
138 bool load_success); | 141 // Sends notification when the entire store is loaded, and reports metrics |
| 142 // for the total time to load and aggregated results from any priority loads |
| 143 // that occurred. |
| 144 void CompleteLoadOnIOThread(const LoadedCallback& loaded_callback, |
| 145 bool load_success); |
| 146 |
| 147 // Sends notification when a single priority load completes. Updates priority |
| 148 // load metric data. The data is sent only after the final load completes. |
| 149 void CompleteLoadForKeyOnIOThread(const LoadedCallback& loaded_callback, |
| 150 bool load_success); |
| 151 |
| 152 // Sends all metrics, including posting a ReportMetricsOnDBThread task. |
| 153 // Called after all priority and regular loading is complete. |
| 154 void ReportMetrics(); |
| 155 |
| 156 // Sends DB-thread owned metrics (i.e., the combined duration of all DB-thread |
| 157 // tasks). |
| 158 void ReportMetricsOnDBThread(); |
139 | 159 |
140 // Initialize the data base. | 160 // Initialize the data base. |
141 bool InitializeDatabase(); | 161 bool InitializeDatabase(); |
142 | 162 |
143 // Loads cookies for the next domain key from the DB, then either reschedules | 163 // Loads cookies for the next domain key from the DB, then either reschedules |
144 // itself or schedules the provided callback to run on the IO thread (if all | 164 // itself or schedules the provided callback to run on the IO thread (if all |
145 // domains are loaded). | 165 // domains are loaded). |
146 void ChainLoadCookies(const LoadedCallback& loaded_callback); | 166 void ChainLoadCookies(const LoadedCallback& loaded_callback); |
147 | 167 |
148 // Load all cookies for a set of domains/hosts | 168 // Load all cookies for a set of domains/hosts |
149 bool LoadCookiesForDomains(const std::set<std::string>& key); | 169 bool LoadCookiesForDomains(const std::set<std::string>& key); |
150 | 170 |
151 // Batch a cookie operation (add or delete) | 171 // Batch a cookie operation (add or delete) |
152 void BatchOperation(PendingOperation::OperationType op, | 172 void BatchOperation(PendingOperation::OperationType op, |
153 const net::CookieMonster::CanonicalCookie& cc); | 173 const net::CookieMonster::CanonicalCookie& cc); |
154 // Commit our pending operations to the database. | 174 // Commit our pending operations to the database. |
155 void Commit(); | 175 void Commit(); |
156 // Close() executed on the background thread. | 176 // Close() executed on the background thread. |
157 void InternalBackgroundClose(); | 177 void InternalBackgroundClose(); |
158 | 178 |
159 FilePath path_; | 179 FilePath path_; |
160 scoped_ptr<sql::Connection> db_; | 180 scoped_ptr<sql::Connection> db_; |
161 sql::MetaTable meta_table_; | 181 sql::MetaTable meta_table_; |
162 | 182 |
163 typedef std::list<PendingOperation*> PendingOperationsList; | 183 typedef std::list<PendingOperation*> PendingOperationsList; |
164 PendingOperationsList pending_; | 184 PendingOperationsList pending_; |
165 PendingOperationsList::size_type num_pending_; | 185 PendingOperationsList::size_type num_pending_; |
166 // True if the persistent store should be deleted upon destruction. | 186 // True if the persistent store should be deleted upon destruction. |
167 bool clear_local_state_on_exit_; | 187 bool clear_local_state_on_exit_; |
168 // Guard |cookies_|, |pending_|, |num_pending_| and | 188 // Guard |cookies_|, |pending_|, |num_pending_|, |clear_local_state_on_exit_| |
169 // |clear_local_state_on_exit_|. | |
170 base::Lock lock_; | 189 base::Lock lock_; |
171 | 190 |
172 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce | 191 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce |
173 // the number of messages sent to the IO thread. Sent back in response to | 192 // the number of messages sent to the IO thread. Sent back in response to |
174 // individual load requests for domain keys or when all loading completes. | 193 // individual load requests for domain keys or when all loading completes. |
175 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; | 194 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
176 | 195 |
177 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. | 196 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. |
178 std::map<std::string, std::set<std::string> > keys_to_load_; | 197 std::map<std::string, std::set<std::string> > keys_to_load_; |
179 | 198 |
180 // Indicates if DB has been initialized. | 199 // Indicates if DB has been initialized. |
181 bool initialized_; | 200 bool initialized_; |
182 | 201 |
| 202 // The cumulative time spent loading the cookies on the DB thread. Incremented |
| 203 // and reported from the DB thread. |
| 204 base::TimeDelta cookie_load_duration_; |
| 205 |
| 206 // Guards the following metrics-related properties (only accessed when |
| 207 // starting/completing priority loads or completing the total load). |
| 208 base::Lock metrics_lock_; |
| 209 int num_priority_waiting_; |
| 210 // The total number of priority requests. |
| 211 int total_priority_requests_; |
| 212 // The time when |num_priority_waiting_| incremented to 1. |
| 213 base::Time current_priority_wait_start_; |
| 214 // The cumulative duration of time when |num_priority_waiting_| was greater |
| 215 // than 1. |
| 216 base::TimeDelta priority_wait_duration_; |
| 217 |
183 DISALLOW_COPY_AND_ASSIGN(Backend); | 218 DISALLOW_COPY_AND_ASSIGN(Backend); |
184 }; | 219 }; |
185 | 220 |
186 // Version number of the database. In version 4, we migrated the time epoch. | 221 // Version number of the database. In version 4, we migrated the time epoch. |
187 // If you open the DB with an older version on Mac or Linux, the times will | 222 // If you open the DB with an older version on Mac or Linux, the times will |
188 // look wonky, but the file will likely be usable. On Windows version 3 and 4 | 223 // look wonky, but the file will likely be usable. On Windows version 3 and 4 |
189 // are the same. | 224 // are the same. |
190 // | 225 // |
191 // Version 3 updated the database to include the last access time, so we can | 226 // Version 3 updated the database to include the last access time, so we can |
192 // expire them in decreasing order of use when we've reached the maximum | 227 // expire them in decreasing order of use when we've reached the maximum |
193 // number of cookies. | 228 // number of cookies. |
194 static const int kCurrentVersionNumber = 4; | 229 static const int kCurrentVersionNumber = 4; |
195 static const int kCompatibleVersionNumber = 3; | 230 static const int kCompatibleVersionNumber = 3; |
196 | 231 |
197 namespace { | 232 namespace { |
198 | 233 |
| 234 // Increments a specified TimeDelta by the duration between this object's |
| 235 // constructor and destructor. Not thread safe. Multiple instances may be |
| 236 // created with the same delta instance as long as their lifetimes are nested. |
| 237 // The shortest lived instances have no impact. |
| 238 class IncrementTimeDelta { |
| 239 public: |
| 240 explicit IncrementTimeDelta(base::TimeDelta* delta) : |
| 241 delta_(delta), |
| 242 original_value_(*delta), |
| 243 start_(base::Time::Now()) {} |
| 244 |
| 245 ~IncrementTimeDelta() { |
| 246 *delta_ = original_value_ + base::Time::Now() - start_; |
| 247 } |
| 248 |
| 249 private: |
| 250 base::TimeDelta* delta_; |
| 251 base::TimeDelta original_value_; |
| 252 base::Time start_; |
| 253 |
| 254 DISALLOW_COPY_AND_ASSIGN(IncrementTimeDelta); |
| 255 }; |
| 256 |
199 // Initializes the cookies table, returning true on success. | 257 // Initializes the cookies table, returning true on success. |
200 bool InitTable(sql::Connection* db) { | 258 bool InitTable(sql::Connection* db) { |
201 if (!db->DoesTableExist("cookies")) { | 259 if (!db->DoesTableExist("cookies")) { |
202 if (!db->Execute("CREATE TABLE cookies (" | 260 if (!db->Execute("CREATE TABLE cookies (" |
203 "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY," | 261 "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY," |
204 "host_key TEXT NOT NULL," | 262 "host_key TEXT NOT NULL," |
205 "name TEXT NOT NULL," | 263 "name TEXT NOT NULL," |
206 "value TEXT NOT NULL," | 264 "value TEXT NOT NULL," |
207 "path TEXT NOT NULL," | 265 "path TEXT NOT NULL," |
208 // We only store persistent, so we know it expires | 266 // We only store persistent, so we know it expires |
(...skipping 21 matching lines...) Expand all Loading... |
230 // This function should be called only once per instance. | 288 // This function should be called only once per instance. |
231 DCHECK(!db_.get()); | 289 DCHECK(!db_.get()); |
232 BrowserThread::PostTask( | 290 BrowserThread::PostTask( |
233 BrowserThread::DB, FROM_HERE, | 291 BrowserThread::DB, FROM_HERE, |
234 base::Bind(&Backend::LoadAndNotifyOnDBThread, this, loaded_callback)); | 292 base::Bind(&Backend::LoadAndNotifyOnDBThread, this, loaded_callback)); |
235 } | 293 } |
236 | 294 |
237 void SQLitePersistentCookieStore::Backend::LoadCookiesForKey( | 295 void SQLitePersistentCookieStore::Backend::LoadCookiesForKey( |
238 const std::string& key, | 296 const std::string& key, |
239 const LoadedCallback& loaded_callback) { | 297 const LoadedCallback& loaded_callback) { |
| 298 { |
| 299 base::AutoLock locked(metrics_lock_); |
| 300 if (num_priority_waiting_ == 0) |
| 301 current_priority_wait_start_ = base::Time::Now(); |
| 302 num_priority_waiting_++; |
| 303 total_priority_requests_++; |
| 304 } |
| 305 |
240 BrowserThread::PostTask( | 306 BrowserThread::PostTask( |
241 BrowserThread::DB, FROM_HERE, | 307 BrowserThread::DB, FROM_HERE, |
242 base::Bind(&Backend::LoadKeyAndNotifyOnDBThread, this, | 308 base::Bind(&Backend::LoadKeyAndNotifyOnDBThread, this, |
243 key, | 309 key, |
244 loaded_callback)); | 310 loaded_callback)); |
245 } | 311 } |
246 | 312 |
247 void SQLitePersistentCookieStore::Backend::LoadAndNotifyOnDBThread( | 313 void SQLitePersistentCookieStore::Backend::LoadAndNotifyOnDBThread( |
248 const LoadedCallback& loaded_callback) { | 314 const LoadedCallback& loaded_callback) { |
249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 316 IncrementTimeDelta increment(&cookie_load_duration_); |
250 | 317 |
251 if (!InitializeDatabase()) { | 318 if (!InitializeDatabase()) { |
252 BrowserThread::PostTask( | 319 BrowserThread::PostTask( |
253 BrowserThread::IO, FROM_HERE, | 320 BrowserThread::IO, FROM_HERE, |
254 base::Bind(&SQLitePersistentCookieStore::Backend::NotifyOnIOThread, | 321 base::Bind(&SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread, |
255 this, loaded_callback, false)); | 322 this, loaded_callback, false)); |
256 } else { | 323 } else { |
257 ChainLoadCookies(loaded_callback); | 324 ChainLoadCookies(loaded_callback); |
258 } | 325 } |
259 } | 326 } |
260 | 327 |
261 void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyOnDBThread( | 328 void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyOnDBThread( |
262 const std::string& key, | 329 const std::string& key, |
263 const LoadedCallback& loaded_callback) { | 330 const LoadedCallback& loaded_callback) { |
264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 332 IncrementTimeDelta increment(&cookie_load_duration_); |
265 | 333 |
266 bool success = false; | 334 bool success = false; |
267 if (InitializeDatabase()) { | 335 if (InitializeDatabase()) { |
268 std::map<std::string, std::set<std::string> >::iterator | 336 std::map<std::string, std::set<std::string> >::iterator |
269 it = keys_to_load_.find(key); | 337 it = keys_to_load_.find(key); |
270 if (it != keys_to_load_.end()) { | 338 if (it != keys_to_load_.end()) { |
271 success = LoadCookiesForDomains(it->second); | 339 success = LoadCookiesForDomains(it->second); |
272 keys_to_load_.erase(it); | 340 keys_to_load_.erase(it); |
273 } else { | 341 } else { |
274 success = true; | 342 success = true; |
275 } | 343 } |
276 } | 344 } |
277 | 345 |
278 BrowserThread::PostTask( | 346 BrowserThread::PostTask( |
279 BrowserThread::IO, FROM_HERE, | 347 BrowserThread::IO, FROM_HERE, |
280 base::Bind(&SQLitePersistentCookieStore::Backend::NotifyOnIOThread, | 348 base::Bind( |
281 this, loaded_callback, success)); | 349 &SQLitePersistentCookieStore::Backend::CompleteLoadForKeyOnIOThread, |
| 350 this, loaded_callback, success)); |
282 } | 351 } |
283 | 352 |
284 void SQLitePersistentCookieStore::Backend::NotifyOnIOThread( | 353 void SQLitePersistentCookieStore::Backend::CompleteLoadForKeyOnIOThread( |
| 354 const LoadedCallback& loaded_callback, |
| 355 bool load_success) { |
| 356 Notify(loaded_callback, load_success); |
| 357 |
| 358 { |
| 359 base::AutoLock locked(metrics_lock_); |
| 360 num_priority_waiting_--; |
| 361 if (num_priority_waiting_ == 0) { |
| 362 priority_wait_duration_ += |
| 363 base::Time::Now() - current_priority_wait_start_; |
| 364 } |
| 365 } |
| 366 |
| 367 } |
| 368 |
| 369 void SQLitePersistentCookieStore::Backend::ReportMetricsOnDBThread() { |
| 370 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 371 "Cookie.TimeLoad", |
| 372 cookie_load_duration_, |
| 373 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 374 50); |
| 375 } |
| 376 |
| 377 void SQLitePersistentCookieStore::Backend::ReportMetrics() { |
| 378 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, base::Bind( |
| 379 &SQLitePersistentCookieStore::Backend::ReportMetricsOnDBThread, this)); |
| 380 |
| 381 { |
| 382 base::AutoLock locked(metrics_lock_); |
| 383 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 384 "Cookie.PriorityBlockingTime", |
| 385 priority_wait_duration_, |
| 386 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 387 50); |
| 388 |
| 389 UMA_HISTOGRAM_COUNTS_100( |
| 390 "Cookie.PriorityLoadCount", |
| 391 total_priority_requests_); |
| 392 } |
| 393 } |
| 394 |
| 395 void SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread( |
| 396 const LoadedCallback& loaded_callback, bool load_success) { |
| 397 Notify(loaded_callback, load_success); |
| 398 |
| 399 if (load_success) |
| 400 ReportMetrics(); |
| 401 } |
| 402 |
| 403 void SQLitePersistentCookieStore::Backend::Notify( |
285 const LoadedCallback& loaded_callback, | 404 const LoadedCallback& loaded_callback, |
286 bool load_success) { | 405 bool load_success) { |
287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
288 | 407 |
289 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 408 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
290 { | 409 { |
291 base::AutoLock locked(lock_); | 410 base::AutoLock locked(lock_); |
292 cookies.swap(cookies_); | 411 cookies.swap(cookies_); |
293 } | 412 } |
294 | 413 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 it->second.insert(domain); | 467 it->second.insert(domain); |
349 } | 468 } |
350 | 469 |
351 initialized_ = true; | 470 initialized_ = true; |
352 return true; | 471 return true; |
353 } | 472 } |
354 | 473 |
355 void SQLitePersistentCookieStore::Backend::ChainLoadCookies( | 474 void SQLitePersistentCookieStore::Backend::ChainLoadCookies( |
356 const LoadedCallback& loaded_callback) { | 475 const LoadedCallback& loaded_callback) { |
357 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 477 IncrementTimeDelta increment(&cookie_load_duration_); |
358 | 478 |
359 bool load_success = true; | 479 bool load_success = true; |
360 | 480 |
361 if (keys_to_load_.size() > 0) { | 481 if (keys_to_load_.size() > 0) { |
362 // Load cookies for the first domain key. | 482 // Load cookies for the first domain key. |
363 std::map<std::string, std::set<std::string> >::iterator | 483 std::map<std::string, std::set<std::string> >::iterator |
364 it = keys_to_load_.begin(); | 484 it = keys_to_load_.begin(); |
365 load_success = LoadCookiesForDomains(it->second); | 485 load_success = LoadCookiesForDomains(it->second); |
366 keys_to_load_.erase(it); | 486 keys_to_load_.erase(it); |
367 } | 487 } |
368 | 488 |
369 // If load is successful and there are more domain keys to be loaded, | 489 // If load is successful and there are more domain keys to be loaded, |
370 // then post a DB task to continue chain-load; | 490 // then post a DB task to continue chain-load; |
371 // Otherwise notify on IO thread. | 491 // Otherwise notify on IO thread. |
372 if (load_success && keys_to_load_.size() > 0) { | 492 if (load_success && keys_to_load_.size() > 0) { |
373 BrowserThread::PostTask( | 493 BrowserThread::PostTask( |
374 BrowserThread::DB, FROM_HERE, | 494 BrowserThread::DB, FROM_HERE, |
375 base::Bind(&Backend::ChainLoadCookies, this, loaded_callback)); | 495 base::Bind(&Backend::ChainLoadCookies, this, loaded_callback)); |
376 } else { | 496 } else { |
377 BrowserThread::PostTask( | 497 BrowserThread::PostTask( |
378 BrowserThread::IO, FROM_HERE, | 498 BrowserThread::IO, FROM_HERE, |
379 base::Bind(&SQLitePersistentCookieStore::Backend::NotifyOnIOThread, | 499 base::Bind(&SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread, |
380 this, loaded_callback, load_success)); | 500 this, loaded_callback, load_success)); |
381 } | 501 } |
382 } | 502 } |
383 | 503 |
384 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( | 504 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( |
385 const std::set<std::string>& domains) { | 505 const std::set<std::string>& domains) { |
386 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
387 | 507 |
388 sql::Statement smt(db_->GetCachedStatement(SQL_FROM_HERE, | 508 sql::Statement smt(db_->GetCachedStatement(SQL_FROM_HERE, |
389 "SELECT creation_utc, host_key, name, value, path, expires_utc, secure, " | 509 "SELECT creation_utc, host_key, name, value, path, expires_utc, secure, " |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 if (backend_.get()) | 847 if (backend_.get()) |
728 backend_->SetClearLocalStateOnExit(clear_local_state); | 848 backend_->SetClearLocalStateOnExit(clear_local_state); |
729 } | 849 } |
730 | 850 |
731 void SQLitePersistentCookieStore::Flush(Task* completion_task) { | 851 void SQLitePersistentCookieStore::Flush(Task* completion_task) { |
732 if (backend_.get()) | 852 if (backend_.get()) |
733 backend_->Flush(completion_task); | 853 backend_->Flush(completion_task); |
734 else if (completion_task) | 854 else if (completion_task) |
735 MessageLoop::current()->PostTask(FROM_HERE, completion_task); | 855 MessageLoop::current()->PostTask(FROM_HERE, completion_task); |
736 } | 856 } |
OLD | NEW |