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