OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "net/extras/sqlite/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <list> | |
8 #include <map> | 7 #include <map> |
9 #include <set> | 8 #include <set> |
10 #include <utility> | |
11 | 9 |
12 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
13 #include "base/bind.h" | 11 #include "base/bind.h" |
14 #include "base/callback.h" | 12 #include "base/callback.h" |
15 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
17 #include "base/location.h" | 15 #include "base/location.h" |
18 #include "base/logging.h" | 16 #include "base/logging.h" |
19 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
20 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
21 #include "base/metrics/field_trial.h" | 19 #include "base/metrics/field_trial.h" |
22 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
23 #include "base/profiler/scoped_tracker.h" | 21 #include "base/profiler/scoped_tracker.h" |
24 #include "base/sequenced_task_runner.h" | 22 #include "base/sequenced_task_runner.h" |
25 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
26 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
27 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
28 #include "base/threading/sequenced_worker_pool.h" | 26 #include "base/threading/sequenced_worker_pool.h" |
29 #include "base/time/time.h" | 27 #include "base/time/time.h" |
30 #include "content/public/browser/browser_thread.h" | |
31 #include "content/public/browser/cookie_store_factory.h" | |
32 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 28 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
33 #include "net/cookies/canonical_cookie.h" | 29 #include "net/cookies/canonical_cookie.h" |
34 #include "net/cookies/cookie_constants.h" | 30 #include "net/cookies/cookie_constants.h" |
35 #include "net/cookies/cookie_util.h" | 31 #include "net/cookies/cookie_util.h" |
36 #include "net/extras/sqlite/cookie_crypto_delegate.h" | 32 #include "net/extras/sqlite/cookie_crypto_delegate.h" |
37 #include "sql/error_delegate_util.h" | 33 #include "sql/error_delegate_util.h" |
38 #include "sql/meta_table.h" | 34 #include "sql/meta_table.h" |
39 #include "sql/statement.h" | 35 #include "sql/statement.h" |
40 #include "sql/transaction.h" | 36 #include "sql/transaction.h" |
41 #include "storage/browser/quota/special_storage_policy.h" | |
42 #include "third_party/sqlite/sqlite3.h" | |
43 #include "url/gurl.h" | 37 #include "url/gurl.h" |
44 | 38 |
45 using base::Time; | 39 using base::Time; |
46 | 40 |
47 namespace { | 41 namespace { |
48 | 42 |
49 // The persistent cookie store is loaded into memory on eTLD at a time. This | 43 // The persistent cookie store is loaded into memory on eTLD at a time. This |
50 // variable controls the delay between loading eTLDs, so as to not overload the | 44 // variable controls the delay between loading eTLDs, so as to not overload the |
51 // CPU or I/O with these low priority requests immediately after start up. | 45 // CPU or I/O with these low priority requests immediately after start up. |
52 const int kLoadDelayMilliseconds = 0; | 46 const int kLoadDelayMilliseconds = 0; |
53 | 47 |
54 } // namespace | 48 } // namespace |
55 | 49 |
56 namespace content { | 50 namespace net { |
57 | 51 |
58 // This class is designed to be shared between any client thread and the | 52 // This class is designed to be shared between any client thread and the |
59 // background task runner. It batches operations and commits them on a timer. | 53 // background task runner. It batches operations and commits them on a timer. |
60 // | 54 // |
61 // SQLitePersistentCookieStore::Load is called to load all cookies. It | 55 // SQLitePersistentCookieStore::Load is called to load all cookies. It |
62 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread | 56 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread |
63 // task to the background runner. This task calls Backend::ChainLoadCookies(), | 57 // task to the background runner. This task calls Backend::ChainLoadCookies(), |
64 // which repeatedly posts itself to the BG runner to load each eTLD+1's cookies | 58 // which repeatedly posts itself to the BG runner to load each eTLD+1's cookies |
65 // in separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is | 59 // in separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is |
66 // posted to the client runner, which notifies the caller of | 60 // posted to the client runner, which notifies the caller of |
(...skipping 11 matching lines...) Expand all Loading... |
78 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(), | 72 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(), |
79 // whichever occurs first. | 73 // whichever occurs first. |
80 class SQLitePersistentCookieStore::Backend | 74 class SQLitePersistentCookieStore::Backend |
81 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { | 75 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { |
82 public: | 76 public: |
83 Backend( | 77 Backend( |
84 const base::FilePath& path, | 78 const base::FilePath& path, |
85 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 79 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
86 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 80 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
87 bool restore_old_session_cookies, | 81 bool restore_old_session_cookies, |
88 storage::SpecialStoragePolicy* special_storage_policy, | 82 CookieCryptoDelegate* crypto_delegate) |
89 net::CookieCryptoDelegate* crypto_delegate) | |
90 : path_(path), | 83 : path_(path), |
91 num_pending_(0), | 84 num_pending_(0), |
92 force_keep_session_state_(false), | |
93 initialized_(false), | 85 initialized_(false), |
94 corruption_detected_(false), | 86 corruption_detected_(false), |
95 restore_old_session_cookies_(restore_old_session_cookies), | 87 restore_old_session_cookies_(restore_old_session_cookies), |
96 special_storage_policy_(special_storage_policy), | |
97 num_cookies_read_(0), | 88 num_cookies_read_(0), |
98 client_task_runner_(client_task_runner), | 89 client_task_runner_(client_task_runner), |
99 background_task_runner_(background_task_runner), | 90 background_task_runner_(background_task_runner), |
100 num_priority_waiting_(0), | 91 num_priority_waiting_(0), |
101 total_priority_requests_(0), | 92 total_priority_requests_(0), |
102 crypto_(crypto_delegate) {} | 93 crypto_(crypto_delegate) {} |
103 | 94 |
104 // Creates or loads the SQLite database. | 95 // Creates or loads the SQLite database. |
105 void Load(const LoadedCallback& loaded_callback); | 96 void Load(const LoadedCallback& loaded_callback); |
106 | 97 |
107 // Loads cookies for the domain key (eTLD+1). | 98 // Loads cookies for the domain key (eTLD+1). |
108 void LoadCookiesForKey(const std::string& domain, | 99 void LoadCookiesForKey(const std::string& domain, |
109 const LoadedCallback& loaded_callback); | 100 const LoadedCallback& loaded_callback); |
110 | 101 |
111 // Steps through all results of |smt|, makes a cookie from each, and adds the | 102 // Steps through all results of |smt|, makes a cookie from each, and adds the |
112 // cookie to |cookies|. This method also updates |cookies_per_origin_| and | 103 // cookie to |cookies|. This method also updates |num_cookies_read_|. |
113 // |num_cookies_read_|. | 104 void MakeCookiesFromSQLStatement(std::vector<CanonicalCookie*>* cookies, |
114 void MakeCookiesFromSQLStatement(std::vector<net::CanonicalCookie*>* cookies, | |
115 sql::Statement* statement); | 105 sql::Statement* statement); |
116 | 106 |
117 // Batch a cookie addition. | 107 // Batch a cookie addition. |
118 void AddCookie(const net::CanonicalCookie& cc); | 108 void AddCookie(const CanonicalCookie& cc); |
119 | 109 |
120 // Batch a cookie access time update. | 110 // Batch a cookie access time update. |
121 void UpdateCookieAccessTime(const net::CanonicalCookie& cc); | 111 void UpdateCookieAccessTime(const CanonicalCookie& cc); |
122 | 112 |
123 // Batch a cookie deletion. | 113 // Batch a cookie deletion. |
124 void DeleteCookie(const net::CanonicalCookie& cc); | 114 void DeleteCookie(const CanonicalCookie& cc); |
125 | 115 |
126 // Commit pending operations as soon as possible. | 116 // Commit pending operations as soon as possible. |
127 void Flush(const base::Closure& callback); | 117 void Flush(const base::Closure& callback); |
128 | 118 |
129 // Commit any pending operations and close the database. This must be called | 119 // Commit any pending operations and close the database. This must be called |
130 // before the object is destructed. | 120 // before the object is destructed. |
131 void Close(); | 121 void Close(); |
132 | 122 |
133 void SetForceKeepSessionState(); | 123 // Post background delete of all cookies that match |cookies|. |
| 124 void DeleteAllInList(const std::list<CookieOrigin>& cookies); |
134 | 125 |
135 private: | 126 private: |
136 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; | 127 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; |
137 | 128 |
138 // You should call Close() before destructing this object. | 129 // You should call Close() before destructing this object. |
139 ~Backend() { | 130 ~Backend() { |
140 DCHECK(!db_.get()) << "Close should have already been called."; | 131 DCHECK(!db_.get()) << "Close should have already been called."; |
141 DCHECK(num_pending_ == 0 && pending_.empty()); | 132 DCHECK_EQ(0u, num_pending_); |
| 133 DCHECK(pending_.empty()); |
142 | 134 |
143 for (net::CanonicalCookie* cookie : cookies_) { | 135 for (CanonicalCookie* cookie : cookies_) { |
144 delete cookie; | 136 delete cookie; |
145 } | 137 } |
146 } | 138 } |
147 | 139 |
148 // Database upgrade statements. | 140 // Database upgrade statements. |
149 bool EnsureDatabaseVersion(); | 141 bool EnsureDatabaseVersion(); |
150 | 142 |
151 class PendingOperation { | 143 class PendingOperation { |
152 public: | 144 public: |
153 typedef enum { | 145 enum OperationType { |
154 COOKIE_ADD, | 146 COOKIE_ADD, |
155 COOKIE_UPDATEACCESS, | 147 COOKIE_UPDATEACCESS, |
156 COOKIE_DELETE, | 148 COOKIE_DELETE, |
157 } OperationType; | 149 }; |
158 | 150 |
159 PendingOperation(OperationType op, const net::CanonicalCookie& cc) | 151 PendingOperation(OperationType op, const CanonicalCookie& cc) |
160 : op_(op), cc_(cc) { } | 152 : op_(op), cc_(cc) { } |
161 | 153 |
162 OperationType op() const { return op_; } | 154 OperationType op() const { return op_; } |
163 const net::CanonicalCookie& cc() const { return cc_; } | 155 const CanonicalCookie& cc() const { return cc_; } |
164 | 156 |
165 private: | 157 private: |
166 OperationType op_; | 158 OperationType op_; |
167 net::CanonicalCookie cc_; | 159 CanonicalCookie cc_; |
168 }; | 160 }; |
169 | 161 |
170 private: | 162 private: |
171 // Creates or loads the SQLite database on background runner. | 163 // Creates or loads the SQLite database on background runner. |
172 void LoadAndNotifyInBackground(const LoadedCallback& loaded_callback, | 164 void LoadAndNotifyInBackground(const LoadedCallback& loaded_callback, |
173 const base::Time& posted_at); | 165 const base::Time& posted_at); |
174 | 166 |
175 // Loads cookies for the domain key (eTLD+1) on background runner. | 167 // Loads cookies for the domain key (eTLD+1) on background runner. |
176 void LoadKeyAndNotifyInBackground(const std::string& domains, | 168 void LoadKeyAndNotifyInBackground(const std::string& domains, |
177 const LoadedCallback& loaded_callback, | 169 const LoadedCallback& loaded_callback, |
(...skipping 30 matching lines...) Expand all Loading... |
208 // Loads cookies for the next domain key from the DB, then either reschedules | 200 // Loads cookies for the next domain key from the DB, then either reschedules |
209 // itself or schedules the provided callback to run on the client runner (if | 201 // itself or schedules the provided callback to run on the client runner (if |
210 // all domains are loaded). | 202 // all domains are loaded). |
211 void ChainLoadCookies(const LoadedCallback& loaded_callback); | 203 void ChainLoadCookies(const LoadedCallback& loaded_callback); |
212 | 204 |
213 // Load all cookies for a set of domains/hosts | 205 // Load all cookies for a set of domains/hosts |
214 bool LoadCookiesForDomains(const std::set<std::string>& key); | 206 bool LoadCookiesForDomains(const std::set<std::string>& key); |
215 | 207 |
216 // Batch a cookie operation (add or delete) | 208 // Batch a cookie operation (add or delete) |
217 void BatchOperation(PendingOperation::OperationType op, | 209 void BatchOperation(PendingOperation::OperationType op, |
218 const net::CanonicalCookie& cc); | 210 const CanonicalCookie& cc); |
219 // Commit our pending operations to the database. | 211 // Commit our pending operations to the database. |
220 void Commit(); | 212 void Commit(); |
221 // Close() executed on the background runner. | 213 // Close() executed on the background runner. |
222 void InternalBackgroundClose(); | 214 void InternalBackgroundClose(); |
223 | 215 |
224 void DeleteSessionCookiesOnStartup(); | 216 void DeleteSessionCookiesOnStartup(); |
225 | 217 |
226 void DeleteSessionCookiesOnShutdown(); | 218 void BackgroundDeleteAllInList(const std::list<CookieOrigin>& cookies); |
227 | 219 |
228 void DatabaseErrorCallback(int error, sql::Statement* stmt); | 220 void DatabaseErrorCallback(int error, sql::Statement* stmt); |
229 void KillDatabase(); | 221 void KillDatabase(); |
230 | 222 |
231 void PostBackgroundTask(const tracked_objects::Location& origin, | 223 void PostBackgroundTask(const tracked_objects::Location& origin, |
232 const base::Closure& task); | 224 const base::Closure& task); |
233 void PostClientTask(const tracked_objects::Location& origin, | 225 void PostClientTask(const tracked_objects::Location& origin, |
234 const base::Closure& task); | 226 const base::Closure& task); |
235 | 227 |
236 // Shared code between the different load strategies to be used after all | 228 // Shared code between the different load strategies to be used after all |
237 // cookies have been loaded. | 229 // cookies have been loaded. |
238 void FinishedLoadingCookies(const LoadedCallback& loaded_callback, | 230 void FinishedLoadingCookies(const LoadedCallback& loaded_callback, |
239 bool success); | 231 bool success); |
240 | 232 |
241 base::FilePath path_; | 233 const base::FilePath path_; |
242 scoped_ptr<sql::Connection> db_; | 234 scoped_ptr<sql::Connection> db_; |
243 sql::MetaTable meta_table_; | 235 sql::MetaTable meta_table_; |
244 | 236 |
245 typedef std::list<PendingOperation*> PendingOperationsList; | 237 typedef std::list<PendingOperation*> PendingOperationsList; |
246 PendingOperationsList pending_; | 238 PendingOperationsList pending_; |
247 PendingOperationsList::size_type num_pending_; | 239 PendingOperationsList::size_type num_pending_; |
248 // True if the persistent store should skip delete on exit rules. | 240 // Guard |cookies_|, |pending_|, |num_pending_|. |
249 bool force_keep_session_state_; | |
250 // Guard |cookies_|, |pending_|, |num_pending_|, |force_keep_session_state_| | |
251 base::Lock lock_; | 241 base::Lock lock_; |
252 | 242 |
253 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce | 243 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce |
254 // the number of messages sent to the client runner. Sent back in response to | 244 // the number of messages sent to the client runner. Sent back in response to |
255 // individual load requests for domain keys or when all loading completes. | 245 // individual load requests for domain keys or when all loading completes. |
256 // Ownership of the cookies in this vector is transferred to the client in | 246 // Ownership of the cookies in this vector is transferred to the client in |
257 // response to individual load requests or when all loading completes. | 247 // response to individual load requests or when all loading completes. |
258 std::vector<net::CanonicalCookie*> cookies_; | 248 std::vector<CanonicalCookie*> cookies_; |
259 | 249 |
260 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. | 250 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. |
261 std::map<std::string, std::set<std::string> > keys_to_load_; | 251 std::map<std::string, std::set<std::string> > keys_to_load_; |
262 | 252 |
263 // Map of (domain keys(eTLD+1), is secure cookie) to number of cookies in the | |
264 // database. | |
265 typedef std::pair<std::string, bool> CookieOrigin; | |
266 typedef std::map<CookieOrigin, int> CookiesPerOriginMap; | |
267 CookiesPerOriginMap cookies_per_origin_; | |
268 | |
269 // Indicates if DB has been initialized. | 253 // Indicates if DB has been initialized. |
270 bool initialized_; | 254 bool initialized_; |
271 | 255 |
272 // Indicates if the kill-database callback has been scheduled. | 256 // Indicates if the kill-database callback has been scheduled. |
273 bool corruption_detected_; | 257 bool corruption_detected_; |
274 | 258 |
275 // If false, we should filter out session cookies when reading the DB. | 259 // If false, we should filter out session cookies when reading the DB. |
276 bool restore_old_session_cookies_; | 260 bool restore_old_session_cookies_; |
277 | 261 |
278 // Policy defining what data is deleted on shutdown. | |
279 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_; | |
280 | |
281 // The cumulative time spent loading the cookies on the background runner. | 262 // The cumulative time spent loading the cookies on the background runner. |
282 // Incremented and reported from the background runner. | 263 // Incremented and reported from the background runner. |
283 base::TimeDelta cookie_load_duration_; | 264 base::TimeDelta cookie_load_duration_; |
284 | 265 |
285 // The total number of cookies read. Incremented and reported on the | 266 // The total number of cookies read. Incremented and reported on the |
286 // background runner. | 267 // background runner. |
287 int num_cookies_read_; | 268 int num_cookies_read_; |
288 | 269 |
289 scoped_refptr<base::SequencedTaskRunner> client_task_runner_; | 270 scoped_refptr<base::SequencedTaskRunner> client_task_runner_; |
290 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | 271 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
291 | 272 |
292 // Guards the following metrics-related properties (only accessed when | 273 // Guards the following metrics-related properties (only accessed when |
293 // starting/completing priority loads or completing the total load). | 274 // starting/completing priority loads or completing the total load). |
294 base::Lock metrics_lock_; | 275 base::Lock metrics_lock_; |
295 int num_priority_waiting_; | 276 int num_priority_waiting_; |
296 // The total number of priority requests. | 277 // The total number of priority requests. |
297 int total_priority_requests_; | 278 int total_priority_requests_; |
298 // The time when |num_priority_waiting_| incremented to 1. | 279 // The time when |num_priority_waiting_| incremented to 1. |
299 base::Time current_priority_wait_start_; | 280 base::Time current_priority_wait_start_; |
300 // The cumulative duration of time when |num_priority_waiting_| was greater | 281 // The cumulative duration of time when |num_priority_waiting_| was greater |
301 // than 1. | 282 // than 1. |
302 base::TimeDelta priority_wait_duration_; | 283 base::TimeDelta priority_wait_duration_; |
303 // Class with functions that do cryptographic operations (for protecting | 284 // Class with functions that do cryptographic operations (for protecting |
304 // cookies stored persistently). | 285 // cookies stored persistently). |
305 // | 286 // |
306 // Not owned. | 287 // Not owned. |
307 net::CookieCryptoDelegate* crypto_; | 288 CookieCryptoDelegate* crypto_; |
308 | 289 |
309 DISALLOW_COPY_AND_ASSIGN(Backend); | 290 DISALLOW_COPY_AND_ASSIGN(Backend); |
310 }; | 291 }; |
311 | 292 |
312 namespace { | 293 namespace { |
313 | 294 |
314 // Version number of the database. | 295 // Version number of the database. |
315 // | 296 // |
316 // Version 9 adds a partial index to track non-persistent cookies. | 297 // Version 9 adds a partial index to track non-persistent cookies. |
317 // Non-persistent cookies sometimes need to be deleted on startup. There are | 298 // Non-persistent cookies sometimes need to be deleted on startup. There are |
(...skipping 28 matching lines...) Expand all Loading... |
346 const int kCurrentVersionNumber = 9; | 327 const int kCurrentVersionNumber = 9; |
347 const int kCompatibleVersionNumber = 5; | 328 const int kCompatibleVersionNumber = 5; |
348 | 329 |
349 // Possible values for the 'priority' column. | 330 // Possible values for the 'priority' column. |
350 enum DBCookiePriority { | 331 enum DBCookiePriority { |
351 kCookiePriorityLow = 0, | 332 kCookiePriorityLow = 0, |
352 kCookiePriorityMedium = 1, | 333 kCookiePriorityMedium = 1, |
353 kCookiePriorityHigh = 2, | 334 kCookiePriorityHigh = 2, |
354 }; | 335 }; |
355 | 336 |
356 DBCookiePriority CookiePriorityToDBCookiePriority(net::CookiePriority value) { | 337 DBCookiePriority CookiePriorityToDBCookiePriority(CookiePriority value) { |
357 switch (value) { | 338 switch (value) { |
358 case net::COOKIE_PRIORITY_LOW: | 339 case COOKIE_PRIORITY_LOW: |
359 return kCookiePriorityLow; | 340 return kCookiePriorityLow; |
360 case net::COOKIE_PRIORITY_MEDIUM: | 341 case COOKIE_PRIORITY_MEDIUM: |
361 return kCookiePriorityMedium; | 342 return kCookiePriorityMedium; |
362 case net::COOKIE_PRIORITY_HIGH: | 343 case COOKIE_PRIORITY_HIGH: |
363 return kCookiePriorityHigh; | 344 return kCookiePriorityHigh; |
364 } | 345 } |
365 | 346 |
366 NOTREACHED(); | 347 NOTREACHED(); |
367 return kCookiePriorityMedium; | 348 return kCookiePriorityMedium; |
368 } | 349 } |
369 | 350 |
370 net::CookiePriority DBCookiePriorityToCookiePriority(DBCookiePriority value) { | 351 CookiePriority DBCookiePriorityToCookiePriority(DBCookiePriority value) { |
371 switch (value) { | 352 switch (value) { |
372 case kCookiePriorityLow: | 353 case kCookiePriorityLow: |
373 return net::COOKIE_PRIORITY_LOW; | 354 return COOKIE_PRIORITY_LOW; |
374 case kCookiePriorityMedium: | 355 case kCookiePriorityMedium: |
375 return net::COOKIE_PRIORITY_MEDIUM; | 356 return COOKIE_PRIORITY_MEDIUM; |
376 case kCookiePriorityHigh: | 357 case kCookiePriorityHigh: |
377 return net::COOKIE_PRIORITY_HIGH; | 358 return COOKIE_PRIORITY_HIGH; |
378 } | 359 } |
379 | 360 |
380 NOTREACHED(); | 361 NOTREACHED(); |
381 return net::COOKIE_PRIORITY_DEFAULT; | 362 return COOKIE_PRIORITY_DEFAULT; |
382 } | 363 } |
383 | 364 |
384 // Increments a specified TimeDelta by the duration between this object's | 365 // Increments a specified TimeDelta by the duration between this object's |
385 // constructor and destructor. Not thread safe. Multiple instances may be | 366 // constructor and destructor. Not thread safe. Multiple instances may be |
386 // created with the same delta instance as long as their lifetimes are nested. | 367 // created with the same delta instance as long as their lifetimes are nested. |
387 // The shortest lived instances have no impact. | 368 // The shortest lived instances have no impact. |
388 class IncrementTimeDelta { | 369 class IncrementTimeDelta { |
389 public: | 370 public: |
390 explicit IncrementTimeDelta(base::TimeDelta* delta) : | 371 explicit IncrementTimeDelta(base::TimeDelta* delta) : |
391 delta_(delta), | 372 delta_(delta), |
(...skipping 26 matching lines...) Expand all Loading... |
418 "path TEXT NOT NULL," | 399 "path TEXT NOT NULL," |
419 "expires_utc INTEGER NOT NULL," | 400 "expires_utc INTEGER NOT NULL," |
420 "secure INTEGER NOT NULL," | 401 "secure INTEGER NOT NULL," |
421 "httponly INTEGER NOT NULL," | 402 "httponly INTEGER NOT NULL," |
422 "last_access_utc INTEGER NOT NULL, " | 403 "last_access_utc INTEGER NOT NULL, " |
423 "has_expires INTEGER NOT NULL DEFAULT 1, " | 404 "has_expires INTEGER NOT NULL DEFAULT 1, " |
424 "persistent INTEGER NOT NULL DEFAULT 1," | 405 "persistent INTEGER NOT NULL DEFAULT 1," |
425 "priority INTEGER NOT NULL DEFAULT %d," | 406 "priority INTEGER NOT NULL DEFAULT %d," |
426 "encrypted_value BLOB DEFAULT ''," | 407 "encrypted_value BLOB DEFAULT ''," |
427 "firstpartyonly INTEGER NOT NULL DEFAULT 0)", | 408 "firstpartyonly INTEGER NOT NULL DEFAULT 0)", |
428 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); | 409 CookiePriorityToDBCookiePriority(COOKIE_PRIORITY_DEFAULT))); |
429 if (!db->Execute(stmt.c_str())) | 410 if (!db->Execute(stmt.c_str())) |
430 return false; | 411 return false; |
431 | 412 |
432 if (!db->Execute("CREATE INDEX domain ON cookies(host_key)")) | 413 if (!db->Execute("CREATE INDEX domain ON cookies(host_key)")) |
433 return false; | 414 return false; |
434 | 415 |
435 #if defined(OS_IOS) | 416 #if defined(OS_IOS) |
436 // iOS 8.1 and older doesn't support partial indices. iOS 8.2 supports | 417 // iOS 8.1 and older doesn't support partial indices. iOS 8.2 supports |
437 // partial indices. | 418 // partial indices. |
438 if (!db->Execute("CREATE INDEX is_transient ON cookies(persistent)")) { | 419 if (!db->Execute("CREATE INDEX is_transient ON cookies(persistent)")) { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 | 563 |
583 if (load_success) | 564 if (load_success) |
584 ReportMetrics(); | 565 ReportMetrics(); |
585 } | 566 } |
586 | 567 |
587 void SQLitePersistentCookieStore::Backend::Notify( | 568 void SQLitePersistentCookieStore::Backend::Notify( |
588 const LoadedCallback& loaded_callback, | 569 const LoadedCallback& loaded_callback, |
589 bool load_success) { | 570 bool load_success) { |
590 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); | 571 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); |
591 | 572 |
592 std::vector<net::CanonicalCookie*> cookies; | 573 std::vector<CanonicalCookie*> cookies; |
593 { | 574 { |
594 base::AutoLock locked(lock_); | 575 base::AutoLock locked(lock_); |
595 cookies.swap(cookies_); | 576 cookies.swap(cookies_); |
596 } | 577 } |
597 | 578 |
598 loaded_callback.Run(cookies); | 579 loaded_callback.Run(cookies); |
599 } | 580 } |
600 | 581 |
601 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { | 582 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { |
602 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 583 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 base::Time::Now() - start, | 654 base::Time::Now() - start, |
674 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 655 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
675 50); | 656 50); |
676 | 657 |
677 base::Time start_parse = base::Time::Now(); | 658 base::Time start_parse = base::Time::Now(); |
678 | 659 |
679 // Build a map of domain keys (always eTLD+1) to domains. | 660 // Build a map of domain keys (always eTLD+1) to domains. |
680 for (size_t idx = 0; idx < host_keys.size(); ++idx) { | 661 for (size_t idx = 0; idx < host_keys.size(); ++idx) { |
681 const std::string& domain = host_keys[idx]; | 662 const std::string& domain = host_keys[idx]; |
682 std::string key = | 663 std::string key = |
683 net::registry_controlled_domains::GetDomainAndRegistry( | 664 registry_controlled_domains::GetDomainAndRegistry( |
684 domain, | 665 domain, |
685 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 666 registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
686 | 667 |
687 keys_to_load_[key].insert(domain); | 668 keys_to_load_[key].insert(domain); |
688 } | 669 } |
689 | 670 |
690 UMA_HISTOGRAM_CUSTOM_TIMES( | 671 UMA_HISTOGRAM_CUSTOM_TIMES( |
691 "Cookie.TimeParseDomains", | 672 "Cookie.TimeParseDomains", |
692 base::Time::Now() - start_parse, | 673 base::Time::Now() - start_parse, |
693 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), | 674 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
694 50); | 675 50); |
695 | 676 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
760 "has_expires, persistent, priority FROM cookies WHERE host_key = ? " | 741 "has_expires, persistent, priority FROM cookies WHERE host_key = ? " |
761 "AND persistent = 1")); | 742 "AND persistent = 1")); |
762 } | 743 } |
763 if (!smt.is_valid()) { | 744 if (!smt.is_valid()) { |
764 smt.Clear(); // Disconnect smt_ref from db_. | 745 smt.Clear(); // Disconnect smt_ref from db_. |
765 meta_table_.Reset(); | 746 meta_table_.Reset(); |
766 db_.reset(); | 747 db_.reset(); |
767 return false; | 748 return false; |
768 } | 749 } |
769 | 750 |
770 std::vector<net::CanonicalCookie*> cookies; | 751 std::vector<CanonicalCookie*> cookies; |
771 std::set<std::string>::const_iterator it = domains.begin(); | 752 std::set<std::string>::const_iterator it = domains.begin(); |
772 for (; it != domains.end(); ++it) { | 753 for (; it != domains.end(); ++it) { |
773 smt.BindString(0, *it); | 754 smt.BindString(0, *it); |
774 MakeCookiesFromSQLStatement(&cookies, &smt); | 755 MakeCookiesFromSQLStatement(&cookies, &smt); |
775 smt.Reset(true); | 756 smt.Reset(true); |
776 } | 757 } |
777 { | 758 { |
778 base::AutoLock locked(lock_); | 759 base::AutoLock locked(lock_); |
779 cookies_.insert(cookies_.end(), cookies.begin(), cookies.end()); | 760 cookies_.insert(cookies_.end(), cookies.begin(), cookies.end()); |
780 } | 761 } |
781 return true; | 762 return true; |
782 } | 763 } |
783 | 764 |
784 void SQLitePersistentCookieStore::Backend::MakeCookiesFromSQLStatement( | 765 void SQLitePersistentCookieStore::Backend::MakeCookiesFromSQLStatement( |
785 std::vector<net::CanonicalCookie*>* cookies, | 766 std::vector<CanonicalCookie*>* cookies, |
786 sql::Statement* statement) { | 767 sql::Statement* statement) { |
787 sql::Statement& smt = *statement; | 768 sql::Statement& smt = *statement; |
788 while (smt.Step()) { | 769 while (smt.Step()) { |
789 std::string value; | 770 std::string value; |
790 std::string encrypted_value = smt.ColumnString(4); | 771 std::string encrypted_value = smt.ColumnString(4); |
791 if (!encrypted_value.empty() && crypto_) { | 772 if (!encrypted_value.empty() && crypto_) { |
792 crypto_->DecryptString(encrypted_value, &value); | 773 crypto_->DecryptString(encrypted_value, &value); |
793 } else { | 774 } else { |
794 DCHECK(encrypted_value.empty()); | 775 DCHECK(encrypted_value.empty()); |
795 value = smt.ColumnString(3); | 776 value = smt.ColumnString(3); |
796 } | 777 } |
797 scoped_ptr<net::CanonicalCookie> cc(new net::CanonicalCookie( | 778 scoped_ptr<CanonicalCookie> cc(new CanonicalCookie( |
798 // The "source" URL is not used with persisted cookies. | 779 // The "source" URL is not used with persisted cookies. |
799 GURL(), // Source | 780 GURL(), // Source |
800 smt.ColumnString(2), // name | 781 smt.ColumnString(2), // name |
801 value, // value | 782 value, // value |
802 smt.ColumnString(1), // domain | 783 smt.ColumnString(1), // domain |
803 smt.ColumnString(5), // path | 784 smt.ColumnString(5), // path |
804 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc | 785 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc |
805 Time::FromInternalValue(smt.ColumnInt64(6)), // expires_utc | 786 Time::FromInternalValue(smt.ColumnInt64(6)), // expires_utc |
806 Time::FromInternalValue(smt.ColumnInt64(10)), // last_access_utc | 787 Time::FromInternalValue(smt.ColumnInt64(10)), // last_access_utc |
807 smt.ColumnInt(7) != 0, // secure | 788 smt.ColumnInt(7) != 0, // secure |
808 smt.ColumnInt(8) != 0, // httponly | 789 smt.ColumnInt(8) != 0, // httponly |
809 smt.ColumnInt(9) != 0, // firstpartyonly | 790 smt.ColumnInt(9) != 0, // firstpartyonly |
810 DBCookiePriorityToCookiePriority( | 791 DBCookiePriorityToCookiePriority( |
811 static_cast<DBCookiePriority>(smt.ColumnInt(13))))); // priority | 792 static_cast<DBCookiePriority>(smt.ColumnInt(13))))); // priority |
812 DLOG_IF(WARNING, cc->CreationDate() > Time::Now()) | 793 DLOG_IF(WARNING, cc->CreationDate() > Time::Now()) |
813 << L"CreationDate too recent"; | 794 << L"CreationDate too recent"; |
814 cookies_per_origin_[CookieOrigin(cc->Domain(), cc->IsSecure())]++; | |
815 cookies->push_back(cc.release()); | 795 cookies->push_back(cc.release()); |
816 ++num_cookies_read_; | 796 ++num_cookies_read_; |
817 } | 797 } |
818 } | 798 } |
819 | 799 |
820 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { | 800 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { |
821 // Version check. | 801 // Version check. |
822 if (!meta_table_.Init( | 802 if (!meta_table_.Init( |
823 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { | 803 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { |
824 return false; | 804 return false; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 } | 883 } |
904 | 884 |
905 if (cur_version == 5) { | 885 if (cur_version == 5) { |
906 const base::TimeTicks start_time = base::TimeTicks::Now(); | 886 const base::TimeTicks start_time = base::TimeTicks::Now(); |
907 sql::Transaction transaction(db_.get()); | 887 sql::Transaction transaction(db_.get()); |
908 if (!transaction.Begin()) | 888 if (!transaction.Begin()) |
909 return false; | 889 return false; |
910 // Alter the table to add the priority column with a default value. | 890 // Alter the table to add the priority column with a default value. |
911 std::string stmt(base::StringPrintf( | 891 std::string stmt(base::StringPrintf( |
912 "ALTER TABLE cookies ADD COLUMN priority INTEGER DEFAULT %d", | 892 "ALTER TABLE cookies ADD COLUMN priority INTEGER DEFAULT %d", |
913 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); | 893 CookiePriorityToDBCookiePriority(COOKIE_PRIORITY_DEFAULT))); |
914 if (!db_->Execute(stmt.c_str())) { | 894 if (!db_->Execute(stmt.c_str())) { |
915 LOG(WARNING) << "Unable to update cookie database to version 6."; | 895 LOG(WARNING) << "Unable to update cookie database to version 6."; |
916 return false; | 896 return false; |
917 } | 897 } |
918 ++cur_version; | 898 ++cur_version; |
919 meta_table_.SetVersionNumber(cur_version); | 899 meta_table_.SetVersionNumber(cur_version); |
920 meta_table_.SetCompatibleVersionNumber( | 900 meta_table_.SetCompatibleVersionNumber( |
921 std::min(cur_version, kCompatibleVersionNumber)); | 901 std::min(cur_version, kCompatibleVersionNumber)); |
922 transaction.Commit(); | 902 transaction.Commit(); |
923 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV6", | 903 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV6", |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1022 meta_table_.Reset(); | 1002 meta_table_.Reset(); |
1023 db_.reset(); | 1003 db_.reset(); |
1024 return false; | 1004 return false; |
1025 } | 1005 } |
1026 } | 1006 } |
1027 | 1007 |
1028 return true; | 1008 return true; |
1029 } | 1009 } |
1030 | 1010 |
1031 void SQLitePersistentCookieStore::Backend::AddCookie( | 1011 void SQLitePersistentCookieStore::Backend::AddCookie( |
1032 const net::CanonicalCookie& cc) { | 1012 const CanonicalCookie& cc) { |
1033 BatchOperation(PendingOperation::COOKIE_ADD, cc); | 1013 BatchOperation(PendingOperation::COOKIE_ADD, cc); |
1034 } | 1014 } |
1035 | 1015 |
1036 void SQLitePersistentCookieStore::Backend::UpdateCookieAccessTime( | 1016 void SQLitePersistentCookieStore::Backend::UpdateCookieAccessTime( |
1037 const net::CanonicalCookie& cc) { | 1017 const CanonicalCookie& cc) { |
1038 BatchOperation(PendingOperation::COOKIE_UPDATEACCESS, cc); | 1018 BatchOperation(PendingOperation::COOKIE_UPDATEACCESS, cc); |
1039 } | 1019 } |
1040 | 1020 |
1041 void SQLitePersistentCookieStore::Backend::DeleteCookie( | 1021 void SQLitePersistentCookieStore::Backend::DeleteCookie( |
1042 const net::CanonicalCookie& cc) { | 1022 const CanonicalCookie& cc) { |
1043 BatchOperation(PendingOperation::COOKIE_DELETE, cc); | 1023 BatchOperation(PendingOperation::COOKIE_DELETE, cc); |
1044 } | 1024 } |
1045 | 1025 |
1046 void SQLitePersistentCookieStore::Backend::BatchOperation( | 1026 void SQLitePersistentCookieStore::Backend::BatchOperation( |
1047 PendingOperation::OperationType op, | 1027 PendingOperation::OperationType op, |
1048 const net::CanonicalCookie& cc) { | 1028 const CanonicalCookie& cc) { |
1049 // Commit every 30 seconds. | 1029 // Commit every 30 seconds. |
1050 static const int kCommitIntervalMs = 30 * 1000; | 1030 static const int kCommitIntervalMs = 30 * 1000; |
1051 // Commit right away if we have more than 512 outstanding operations. | 1031 // Commit right away if we have more than 512 outstanding operations. |
1052 static const size_t kCommitAfterBatchSize = 512; | 1032 static const size_t kCommitAfterBatchSize = 512; |
1053 DCHECK(!background_task_runner_->RunsTasksOnCurrentThread()); | 1033 DCHECK(!background_task_runner_->RunsTasksOnCurrentThread()); |
1054 | 1034 |
1055 // We do a full copy of the cookie here, and hopefully just here. | 1035 // We do a full copy of the cookie here, and hopefully just here. |
1056 scoped_ptr<PendingOperation> po(new PendingOperation(op, cc)); | 1036 scoped_ptr<PendingOperation> po(new PendingOperation(op, cc)); |
1057 | 1037 |
1058 PendingOperationsList::size_type num_pending; | 1038 PendingOperationsList::size_type num_pending; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1111 sql::Transaction transaction(db_.get()); | 1091 sql::Transaction transaction(db_.get()); |
1112 if (!transaction.Begin()) | 1092 if (!transaction.Begin()) |
1113 return; | 1093 return; |
1114 | 1094 |
1115 for (PendingOperationsList::iterator it = ops.begin(); | 1095 for (PendingOperationsList::iterator it = ops.begin(); |
1116 it != ops.end(); ++it) { | 1096 it != ops.end(); ++it) { |
1117 // Free the cookies as we commit them to the database. | 1097 // Free the cookies as we commit them to the database. |
1118 scoped_ptr<PendingOperation> po(*it); | 1098 scoped_ptr<PendingOperation> po(*it); |
1119 switch (po->op()) { | 1099 switch (po->op()) { |
1120 case PendingOperation::COOKIE_ADD: | 1100 case PendingOperation::COOKIE_ADD: |
1121 cookies_per_origin_[ | |
1122 CookieOrigin(po->cc().Domain(), po->cc().IsSecure())]++; | |
1123 add_smt.Reset(true); | 1101 add_smt.Reset(true); |
1124 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); | 1102 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
1125 add_smt.BindString(1, po->cc().Domain()); | 1103 add_smt.BindString(1, po->cc().Domain()); |
1126 add_smt.BindString(2, po->cc().Name()); | 1104 add_smt.BindString(2, po->cc().Name()); |
1127 if (crypto_) { | 1105 if (crypto_) { |
1128 std::string encrypted_value; | 1106 std::string encrypted_value; |
1129 add_smt.BindCString(3, ""); // value | 1107 add_smt.BindCString(3, ""); // value |
1130 crypto_->EncryptString(po->cc().Value(), &encrypted_value); | 1108 crypto_->EncryptString(po->cc().Value(), &encrypted_value); |
1131 // BindBlob() immediately makes an internal copy of the data. | 1109 // BindBlob() immediately makes an internal copy of the data. |
1132 add_smt.BindBlob(4, encrypted_value.data(), | 1110 add_smt.BindBlob(4, encrypted_value.data(), |
(...skipping 20 matching lines...) Expand all Loading... |
1153 update_access_smt.Reset(true); | 1131 update_access_smt.Reset(true); |
1154 update_access_smt.BindInt64(0, | 1132 update_access_smt.BindInt64(0, |
1155 po->cc().LastAccessDate().ToInternalValue()); | 1133 po->cc().LastAccessDate().ToInternalValue()); |
1156 update_access_smt.BindInt64(1, | 1134 update_access_smt.BindInt64(1, |
1157 po->cc().CreationDate().ToInternalValue()); | 1135 po->cc().CreationDate().ToInternalValue()); |
1158 if (!update_access_smt.Run()) | 1136 if (!update_access_smt.Run()) |
1159 NOTREACHED() << "Could not update cookie last access time in the DB."; | 1137 NOTREACHED() << "Could not update cookie last access time in the DB."; |
1160 break; | 1138 break; |
1161 | 1139 |
1162 case PendingOperation::COOKIE_DELETE: | 1140 case PendingOperation::COOKIE_DELETE: |
1163 cookies_per_origin_[ | |
1164 CookieOrigin(po->cc().Domain(), po->cc().IsSecure())]--; | |
1165 del_smt.Reset(true); | 1141 del_smt.Reset(true); |
1166 del_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); | 1142 del_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
1167 if (!del_smt.Run()) | 1143 if (!del_smt.Run()) |
1168 NOTREACHED() << "Could not delete a cookie from the DB."; | 1144 NOTREACHED() << "Could not delete a cookie from the DB."; |
1169 break; | 1145 break; |
1170 | 1146 |
1171 default: | 1147 default: |
1172 NOTREACHED(); | 1148 NOTREACHED(); |
1173 break; | 1149 break; |
1174 } | 1150 } |
(...skipping 27 matching lines...) Expand all Loading... |
1202 PostBackgroundTask(FROM_HERE, | 1178 PostBackgroundTask(FROM_HERE, |
1203 base::Bind(&Backend::InternalBackgroundClose, this)); | 1179 base::Bind(&Backend::InternalBackgroundClose, this)); |
1204 } | 1180 } |
1205 } | 1181 } |
1206 | 1182 |
1207 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() { | 1183 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() { |
1208 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 1184 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
1209 // Commit any pending operations | 1185 // Commit any pending operations |
1210 Commit(); | 1186 Commit(); |
1211 | 1187 |
1212 if (!force_keep_session_state_ && special_storage_policy_.get() && | |
1213 special_storage_policy_->HasSessionOnlyOrigins()) { | |
1214 DeleteSessionCookiesOnShutdown(); | |
1215 } | |
1216 | |
1217 meta_table_.Reset(); | 1188 meta_table_.Reset(); |
1218 db_.reset(); | 1189 db_.reset(); |
1219 } | 1190 } |
1220 | 1191 |
1221 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnShutdown() { | |
1222 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | |
1223 | |
1224 if (!db_) | |
1225 return; | |
1226 | |
1227 if (!special_storage_policy_.get()) | |
1228 return; | |
1229 | |
1230 sql::Statement del_smt(db_->GetCachedStatement( | |
1231 SQL_FROM_HERE, "DELETE FROM cookies WHERE host_key=? AND secure=?")); | |
1232 if (!del_smt.is_valid()) { | |
1233 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
1234 return; | |
1235 } | |
1236 | |
1237 sql::Transaction transaction(db_.get()); | |
1238 if (!transaction.Begin()) { | |
1239 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
1240 return; | |
1241 } | |
1242 | |
1243 for (CookiesPerOriginMap::iterator it = cookies_per_origin_.begin(); | |
1244 it != cookies_per_origin_.end(); ++it) { | |
1245 if (it->second <= 0) { | |
1246 DCHECK_EQ(0, it->second); | |
1247 continue; | |
1248 } | |
1249 const GURL url(net::cookie_util::CookieOriginToURL(it->first.first, | |
1250 it->first.second)); | |
1251 if (!url.is_valid() || !special_storage_policy_->IsStorageSessionOnly(url)) | |
1252 continue; | |
1253 | |
1254 del_smt.Reset(true); | |
1255 del_smt.BindString(0, it->first.first); | |
1256 del_smt.BindInt(1, it->first.second); | |
1257 if (!del_smt.Run()) | |
1258 NOTREACHED() << "Could not delete a cookie from the DB."; | |
1259 } | |
1260 | |
1261 if (!transaction.Commit()) | |
1262 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
1263 } | |
1264 | |
1265 void SQLitePersistentCookieStore::Backend::DatabaseErrorCallback( | 1192 void SQLitePersistentCookieStore::Backend::DatabaseErrorCallback( |
1266 int error, | 1193 int error, |
1267 sql::Statement* stmt) { | 1194 sql::Statement* stmt) { |
1268 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 1195 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
1269 | 1196 |
1270 if (!sql::IsErrorCatastrophic(error)) | 1197 if (!sql::IsErrorCatastrophic(error)) |
1271 return; | 1198 return; |
1272 | 1199 |
1273 // TODO(shess): Running KillDatabase() multiple times should be | 1200 // TODO(shess): Running KillDatabase() multiple times should be |
1274 // safe. | 1201 // safe. |
(...skipping 16 matching lines...) Expand all Loading... |
1291 if (db_) { | 1218 if (db_) { |
1292 // This Backend will now be in-memory only. In a future run we will recreate | 1219 // This Backend will now be in-memory only. In a future run we will recreate |
1293 // the database. Hopefully things go better then! | 1220 // the database. Hopefully things go better then! |
1294 bool success = db_->RazeAndClose(); | 1221 bool success = db_->RazeAndClose(); |
1295 UMA_HISTOGRAM_BOOLEAN("Cookie.KillDatabaseResult", success); | 1222 UMA_HISTOGRAM_BOOLEAN("Cookie.KillDatabaseResult", success); |
1296 meta_table_.Reset(); | 1223 meta_table_.Reset(); |
1297 db_.reset(); | 1224 db_.reset(); |
1298 } | 1225 } |
1299 } | 1226 } |
1300 | 1227 |
1301 void SQLitePersistentCookieStore::Backend::SetForceKeepSessionState() { | 1228 void SQLitePersistentCookieStore::Backend::DeleteAllInList( |
1302 base::AutoLock locked(lock_); | 1229 const std::list<CookieOrigin>& cookies) { |
1303 force_keep_session_state_ = true; | 1230 if (cookies.empty()) |
| 1231 return; |
| 1232 |
| 1233 // Perform deletion on background task runner. |
| 1234 background_task_runner_->PostTask( |
| 1235 FROM_HERE, |
| 1236 base::Bind( |
| 1237 &Backend::BackgroundDeleteAllInList, this, cookies)); |
1304 } | 1238 } |
1305 | 1239 |
1306 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() { | 1240 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() { |
1307 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 1241 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
1308 if (!db_->Execute("DELETE FROM cookies WHERE persistent != 1")) | 1242 if (!db_->Execute("DELETE FROM cookies WHERE persistent != 1")) |
1309 LOG(WARNING) << "Unable to delete session cookies."; | 1243 LOG(WARNING) << "Unable to delete session cookies."; |
1310 } | 1244 } |
1311 | 1245 |
| 1246 void SQLitePersistentCookieStore::Backend::BackgroundDeleteAllInList( |
| 1247 const std::list<CookieOrigin>& cookies) { |
| 1248 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 1249 |
| 1250 if (!db_) |
| 1251 return; |
| 1252 |
| 1253 // Force a commit of any pending writes before issuing deletes. |
| 1254 // TODO(rohitrao): Remove the need for this Commit() by instead pruning the |
| 1255 // list of pending operations. https://crbug.com/486742. |
| 1256 Commit(); |
| 1257 |
| 1258 sql::Statement del_smt(db_->GetCachedStatement( |
| 1259 SQL_FROM_HERE, "DELETE FROM cookies WHERE host_key=? AND secure=?")); |
| 1260 if (!del_smt.is_valid()) { |
| 1261 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 1262 return; |
| 1263 } |
| 1264 |
| 1265 sql::Transaction transaction(db_.get()); |
| 1266 if (!transaction.Begin()) { |
| 1267 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 1268 return; |
| 1269 } |
| 1270 |
| 1271 for (const auto& cookie : cookies) { |
| 1272 const GURL url(cookie_util::CookieOriginToURL(cookie.first, cookie.second)); |
| 1273 if (!url.is_valid()) |
| 1274 continue; |
| 1275 |
| 1276 del_smt.Reset(true); |
| 1277 del_smt.BindString(0, cookie.first); |
| 1278 del_smt.BindInt(1, cookie.second); |
| 1279 if (!del_smt.Run()) |
| 1280 NOTREACHED() << "Could not delete a cookie from the DB."; |
| 1281 } |
| 1282 |
| 1283 if (!transaction.Commit()) |
| 1284 LOG(WARNING) << "Unable to delete cookies on shutdown."; |
| 1285 } |
| 1286 |
1312 void SQLitePersistentCookieStore::Backend::PostBackgroundTask( | 1287 void SQLitePersistentCookieStore::Backend::PostBackgroundTask( |
1313 const tracked_objects::Location& origin, const base::Closure& task) { | 1288 const tracked_objects::Location& origin, const base::Closure& task) { |
1314 if (!background_task_runner_->PostTask(origin, task)) { | 1289 if (!background_task_runner_->PostTask(origin, task)) { |
1315 LOG(WARNING) << "Failed to post task from " << origin.ToString() | 1290 LOG(WARNING) << "Failed to post task from " << origin.ToString() |
1316 << " to background_task_runner_."; | 1291 << " to background_task_runner_."; |
1317 } | 1292 } |
1318 } | 1293 } |
1319 | 1294 |
1320 void SQLitePersistentCookieStore::Backend::PostClientTask( | 1295 void SQLitePersistentCookieStore::Backend::PostClientTask( |
1321 const tracked_objects::Location& origin, const base::Closure& task) { | 1296 const tracked_objects::Location& origin, const base::Closure& task) { |
1322 if (!client_task_runner_->PostTask(origin, task)) { | 1297 if (!client_task_runner_->PostTask(origin, task)) { |
1323 LOG(WARNING) << "Failed to post task from " << origin.ToString() | 1298 LOG(WARNING) << "Failed to post task from " << origin.ToString() |
1324 << " to client_task_runner_."; | 1299 << " to client_task_runner_."; |
1325 } | 1300 } |
1326 } | 1301 } |
1327 | 1302 |
1328 void SQLitePersistentCookieStore::Backend::FinishedLoadingCookies( | 1303 void SQLitePersistentCookieStore::Backend::FinishedLoadingCookies( |
1329 const LoadedCallback& loaded_callback, | 1304 const LoadedCallback& loaded_callback, |
1330 bool success) { | 1305 bool success) { |
1331 PostClientTask(FROM_HERE, base::Bind(&Backend::CompleteLoadInForeground, this, | 1306 PostClientTask(FROM_HERE, base::Bind(&Backend::CompleteLoadInForeground, this, |
1332 loaded_callback, success)); | 1307 loaded_callback, success)); |
1333 } | 1308 } |
1334 | 1309 |
1335 SQLitePersistentCookieStore::SQLitePersistentCookieStore( | 1310 SQLitePersistentCookieStore::SQLitePersistentCookieStore( |
1336 const base::FilePath& path, | 1311 const base::FilePath& path, |
1337 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 1312 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
1338 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 1313 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
1339 bool restore_old_session_cookies, | 1314 bool restore_old_session_cookies, |
1340 storage::SpecialStoragePolicy* special_storage_policy, | 1315 CookieCryptoDelegate* crypto_delegate) |
1341 net::CookieCryptoDelegate* crypto_delegate) | |
1342 : backend_(new Backend(path, | 1316 : backend_(new Backend(path, |
1343 client_task_runner, | 1317 client_task_runner, |
1344 background_task_runner, | 1318 background_task_runner, |
1345 restore_old_session_cookies, | 1319 restore_old_session_cookies, |
1346 special_storage_policy, | |
1347 crypto_delegate)) { | 1320 crypto_delegate)) { |
1348 } | 1321 } |
1349 | 1322 |
| 1323 void SQLitePersistentCookieStore::DeleteAllInList( |
| 1324 const std::list<CookieOrigin>& cookies) { |
| 1325 backend_->DeleteAllInList(cookies); |
| 1326 } |
| 1327 |
1350 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 1328 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
1351 backend_->Load(loaded_callback); | 1329 backend_->Load(loaded_callback); |
1352 } | 1330 } |
1353 | 1331 |
1354 void SQLitePersistentCookieStore::LoadCookiesForKey( | 1332 void SQLitePersistentCookieStore::LoadCookiesForKey( |
1355 const std::string& key, | 1333 const std::string& key, |
1356 const LoadedCallback& loaded_callback) { | 1334 const LoadedCallback& loaded_callback) { |
1357 backend_->LoadCookiesForKey(key, loaded_callback); | 1335 backend_->LoadCookiesForKey(key, loaded_callback); |
1358 } | 1336 } |
1359 | 1337 |
1360 void SQLitePersistentCookieStore::AddCookie(const net::CanonicalCookie& cc) { | 1338 void SQLitePersistentCookieStore::AddCookie(const CanonicalCookie& cc) { |
1361 backend_->AddCookie(cc); | 1339 backend_->AddCookie(cc); |
1362 } | 1340 } |
1363 | 1341 |
1364 void SQLitePersistentCookieStore::UpdateCookieAccessTime( | 1342 void SQLitePersistentCookieStore::UpdateCookieAccessTime( |
1365 const net::CanonicalCookie& cc) { | 1343 const CanonicalCookie& cc) { |
1366 backend_->UpdateCookieAccessTime(cc); | 1344 backend_->UpdateCookieAccessTime(cc); |
1367 } | 1345 } |
1368 | 1346 |
1369 void SQLitePersistentCookieStore::DeleteCookie(const net::CanonicalCookie& cc) { | 1347 void SQLitePersistentCookieStore::DeleteCookie(const CanonicalCookie& cc) { |
1370 backend_->DeleteCookie(cc); | 1348 backend_->DeleteCookie(cc); |
1371 } | 1349 } |
1372 | 1350 |
1373 void SQLitePersistentCookieStore::SetForceKeepSessionState() { | 1351 void SQLitePersistentCookieStore::SetForceKeepSessionState() { |
1374 backend_->SetForceKeepSessionState(); | 1352 // This store never discards session-only cookies, so this call has no effect. |
1375 } | 1353 } |
1376 | 1354 |
1377 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { | 1355 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { |
1378 backend_->Flush(callback); | 1356 backend_->Flush(callback); |
1379 } | 1357 } |
1380 | 1358 |
1381 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 1359 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
1382 backend_->Close(); | 1360 backend_->Close(); |
1383 // We release our reference to the Backend, though it will probably still have | 1361 // We release our reference to the Backend, though it will probably still have |
1384 // a reference if the background runner has not run Close() yet. | 1362 // a reference if the background runner has not run Close() yet. |
1385 } | 1363 } |
1386 | 1364 |
1387 CookieStoreConfig::CookieStoreConfig() | 1365 } // namespace net |
1388 : session_cookie_mode(EPHEMERAL_SESSION_COOKIES), | |
1389 crypto_delegate(NULL) { | |
1390 // Default to an in-memory cookie store. | |
1391 } | |
1392 | |
1393 CookieStoreConfig::CookieStoreConfig( | |
1394 const base::FilePath& path, | |
1395 SessionCookieMode session_cookie_mode, | |
1396 storage::SpecialStoragePolicy* storage_policy, | |
1397 net::CookieMonsterDelegate* cookie_delegate) | |
1398 : path(path), | |
1399 session_cookie_mode(session_cookie_mode), | |
1400 storage_policy(storage_policy), | |
1401 cookie_delegate(cookie_delegate), | |
1402 crypto_delegate(NULL) { | |
1403 CHECK(!path.empty() || session_cookie_mode == EPHEMERAL_SESSION_COOKIES); | |
1404 } | |
1405 | |
1406 CookieStoreConfig::~CookieStoreConfig() { | |
1407 } | |
1408 | |
1409 net::CookieStore* CreateCookieStore(const CookieStoreConfig& config) { | |
1410 // TODO(bcwhite): Remove ScopedTracker below once crbug.com/483686 is fixed. | |
1411 tracked_objects::ScopedTracker tracking_profile( | |
1412 FROM_HERE_WITH_EXPLICIT_FUNCTION("483686 content::CreateCookieStore")); | |
1413 | |
1414 net::CookieMonster* cookie_monster = NULL; | |
1415 | |
1416 if (config.path.empty()) { | |
1417 // Empty path means in-memory store. | |
1418 cookie_monster = new net::CookieMonster(NULL, config.cookie_delegate.get()); | |
1419 } else { | |
1420 scoped_refptr<base::SequencedTaskRunner> client_task_runner = | |
1421 config.client_task_runner; | |
1422 scoped_refptr<base::SequencedTaskRunner> background_task_runner = | |
1423 config.background_task_runner; | |
1424 | |
1425 if (!client_task_runner.get()) { | |
1426 client_task_runner = | |
1427 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | |
1428 } | |
1429 | |
1430 if (!background_task_runner.get()) { | |
1431 background_task_runner = | |
1432 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
1433 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
1434 } | |
1435 | |
1436 SQLitePersistentCookieStore* persistent_store = | |
1437 new SQLitePersistentCookieStore( | |
1438 config.path, | |
1439 client_task_runner, | |
1440 background_task_runner, | |
1441 (config.session_cookie_mode == | |
1442 CookieStoreConfig::RESTORED_SESSION_COOKIES), | |
1443 config.storage_policy.get(), | |
1444 config.crypto_delegate); | |
1445 | |
1446 cookie_monster = | |
1447 new net::CookieMonster(persistent_store, config.cookie_delegate.get()); | |
1448 if ((config.session_cookie_mode == | |
1449 CookieStoreConfig::PERSISTANT_SESSION_COOKIES) || | |
1450 (config.session_cookie_mode == | |
1451 CookieStoreConfig::RESTORED_SESSION_COOKIES)) { | |
1452 cookie_monster->SetPersistSessionCookies(true); | |
1453 } | |
1454 } | |
1455 | |
1456 return cookie_monster; | |
1457 } | |
1458 | |
1459 } // namespace content | |
OLD | NEW |