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 "content/browser/net/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // whichever occurs first. | 70 // whichever occurs first. |
71 class SQLitePersistentCookieStore::Backend | 71 class SQLitePersistentCookieStore::Backend |
72 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { | 72 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { |
73 public: | 73 public: |
74 Backend( | 74 Backend( |
75 const base::FilePath& path, | 75 const base::FilePath& path, |
76 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 76 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
77 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 77 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
78 bool restore_old_session_cookies, | 78 bool restore_old_session_cookies, |
79 quota::SpecialStoragePolicy* special_storage_policy, | 79 quota::SpecialStoragePolicy* special_storage_policy, |
80 scoped_ptr<CookieCryptoDelegate> crypto_delegate) | 80 CookieCryptoDelegate* crypto_delegate) |
81 : path_(path), | 81 : path_(path), |
82 num_pending_(0), | 82 num_pending_(0), |
83 force_keep_session_state_(false), | 83 force_keep_session_state_(false), |
84 initialized_(false), | 84 initialized_(false), |
85 corruption_detected_(false), | 85 corruption_detected_(false), |
86 restore_old_session_cookies_(restore_old_session_cookies), | 86 restore_old_session_cookies_(restore_old_session_cookies), |
87 special_storage_policy_(special_storage_policy), | 87 special_storage_policy_(special_storage_policy), |
88 num_cookies_read_(0), | 88 num_cookies_read_(0), |
89 client_task_runner_(client_task_runner), | 89 client_task_runner_(client_task_runner), |
90 background_task_runner_(background_task_runner), | 90 background_task_runner_(background_task_runner), |
91 num_priority_waiting_(0), | 91 num_priority_waiting_(0), |
92 total_priority_requests_(0), | 92 total_priority_requests_(0), |
93 crypto_(crypto_delegate.Pass()) {} | 93 crypto_(crypto_delegate) {} |
94 | 94 |
95 // Creates or loads the SQLite database. | 95 // Creates or loads the SQLite database. |
96 void Load(const LoadedCallback& loaded_callback); | 96 void Load(const LoadedCallback& loaded_callback); |
97 | 97 |
98 // Loads cookies for the domain key (eTLD+1). | 98 // Loads cookies for the domain key (eTLD+1). |
99 void LoadCookiesForKey(const std::string& domain, | 99 void LoadCookiesForKey(const std::string& domain, |
100 const LoadedCallback& loaded_callback); | 100 const LoadedCallback& loaded_callback); |
101 | 101 |
102 // Batch a cookie addition. | 102 // Batch a cookie addition. |
103 void AddCookie(const net::CanonicalCookie& cc); | 103 void AddCookie(const net::CanonicalCookie& cc); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 int num_priority_waiting_; | 268 int num_priority_waiting_; |
269 // The total number of priority requests. | 269 // The total number of priority requests. |
270 int total_priority_requests_; | 270 int total_priority_requests_; |
271 // The time when |num_priority_waiting_| incremented to 1. | 271 // The time when |num_priority_waiting_| incremented to 1. |
272 base::Time current_priority_wait_start_; | 272 base::Time current_priority_wait_start_; |
273 // The cumulative duration of time when |num_priority_waiting_| was greater | 273 // The cumulative duration of time when |num_priority_waiting_| was greater |
274 // than 1. | 274 // than 1. |
275 base::TimeDelta priority_wait_duration_; | 275 base::TimeDelta priority_wait_duration_; |
276 // Class with functions that do cryptographic operations (for protecting | 276 // Class with functions that do cryptographic operations (for protecting |
277 // cookies stored persistently). | 277 // cookies stored persistently). |
278 scoped_ptr<CookieCryptoDelegate> crypto_; | 278 // |
| 279 // Not owned. |
| 280 CookieCryptoDelegate* crypto_; |
279 | 281 |
280 DISALLOW_COPY_AND_ASSIGN(Backend); | 282 DISALLOW_COPY_AND_ASSIGN(Backend); |
281 }; | 283 }; |
282 | 284 |
283 namespace { | 285 namespace { |
284 | 286 |
285 // Version number of the database. | 287 // Version number of the database. |
286 // | 288 // |
287 // Version 7 adds encrypted values. Old values will continue to be used but | 289 // Version 7 adds encrypted values. Old values will continue to be used but |
288 // all new values written will be encrypted on selected operating systems. New | 290 // all new values written will be encrypted on selected operating systems. New |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 return false; | 714 return false; |
713 } | 715 } |
714 | 716 |
715 std::vector<net::CanonicalCookie*> cookies; | 717 std::vector<net::CanonicalCookie*> cookies; |
716 std::set<std::string>::const_iterator it = domains.begin(); | 718 std::set<std::string>::const_iterator it = domains.begin(); |
717 for (; it != domains.end(); ++it) { | 719 for (; it != domains.end(); ++it) { |
718 smt.BindString(0, *it); | 720 smt.BindString(0, *it); |
719 while (smt.Step()) { | 721 while (smt.Step()) { |
720 std::string value; | 722 std::string value; |
721 std::string encrypted_value = smt.ColumnString(4); | 723 std::string encrypted_value = smt.ColumnString(4); |
722 if (!encrypted_value.empty() && crypto_.get()) { | 724 if (!encrypted_value.empty() && crypto_) { |
723 crypto_->DecryptString(encrypted_value, &value); | 725 crypto_->DecryptString(encrypted_value, &value); |
724 } else { | 726 } else { |
725 DCHECK(encrypted_value.empty()); | 727 DCHECK(encrypted_value.empty()); |
726 value = smt.ColumnString(3); | 728 value = smt.ColumnString(3); |
727 } | 729 } |
728 scoped_ptr<net::CanonicalCookie> cc(new net::CanonicalCookie( | 730 scoped_ptr<net::CanonicalCookie> cc(new net::CanonicalCookie( |
729 // The "source" URL is not used with persisted cookies. | 731 // The "source" URL is not used with persisted cookies. |
730 GURL(), // Source | 732 GURL(), // Source |
731 smt.ColumnString(2), // name | 733 smt.ColumnString(2), // name |
732 value, // value | 734 value, // value |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 // Free the cookies as we commit them to the database. | 993 // Free the cookies as we commit them to the database. |
992 scoped_ptr<PendingOperation> po(*it); | 994 scoped_ptr<PendingOperation> po(*it); |
993 switch (po->op()) { | 995 switch (po->op()) { |
994 case PendingOperation::COOKIE_ADD: | 996 case PendingOperation::COOKIE_ADD: |
995 cookies_per_origin_[ | 997 cookies_per_origin_[ |
996 CookieOrigin(po->cc().Domain(), po->cc().IsSecure())]++; | 998 CookieOrigin(po->cc().Domain(), po->cc().IsSecure())]++; |
997 add_smt.Reset(true); | 999 add_smt.Reset(true); |
998 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); | 1000 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
999 add_smt.BindString(1, po->cc().Domain()); | 1001 add_smt.BindString(1, po->cc().Domain()); |
1000 add_smt.BindString(2, po->cc().Name()); | 1002 add_smt.BindString(2, po->cc().Name()); |
1001 if (crypto_.get()) { | 1003 if (crypto_) { |
1002 std::string encrypted_value; | 1004 std::string encrypted_value; |
1003 add_smt.BindCString(3, ""); // value | 1005 add_smt.BindCString(3, ""); // value |
1004 crypto_->EncryptString(po->cc().Value(), &encrypted_value); | 1006 crypto_->EncryptString(po->cc().Value(), &encrypted_value); |
1005 // BindBlob() immediately makes an internal copy of the data. | 1007 // BindBlob() immediately makes an internal copy of the data. |
1006 add_smt.BindBlob(4, encrypted_value.data(), | 1008 add_smt.BindBlob(4, encrypted_value.data(), |
1007 static_cast<int>(encrypted_value.length())); | 1009 static_cast<int>(encrypted_value.length())); |
1008 } else { | 1010 } else { |
1009 add_smt.BindString(3, po->cc().Value()); | 1011 add_smt.BindString(3, po->cc().Value()); |
1010 add_smt.BindBlob(4, "", 0); // encrypted_value | 1012 add_smt.BindBlob(4, "", 0); // encrypted_value |
1011 } | 1013 } |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1197 << " to client_task_runner_."; | 1199 << " to client_task_runner_."; |
1198 } | 1200 } |
1199 } | 1201 } |
1200 | 1202 |
1201 SQLitePersistentCookieStore::SQLitePersistentCookieStore( | 1203 SQLitePersistentCookieStore::SQLitePersistentCookieStore( |
1202 const base::FilePath& path, | 1204 const base::FilePath& path, |
1203 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 1205 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
1204 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 1206 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
1205 bool restore_old_session_cookies, | 1207 bool restore_old_session_cookies, |
1206 quota::SpecialStoragePolicy* special_storage_policy, | 1208 quota::SpecialStoragePolicy* special_storage_policy, |
1207 scoped_ptr<CookieCryptoDelegate> crypto_delegate) | 1209 CookieCryptoDelegate* crypto_delegate) |
1208 : backend_(new Backend(path, | 1210 : backend_(new Backend(path, |
1209 client_task_runner, | 1211 client_task_runner, |
1210 background_task_runner, | 1212 background_task_runner, |
1211 restore_old_session_cookies, | 1213 restore_old_session_cookies, |
1212 special_storage_policy, | 1214 special_storage_policy, |
1213 crypto_delegate.Pass())) { | 1215 crypto_delegate)) { |
1214 } | 1216 } |
1215 | 1217 |
1216 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 1218 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
1217 backend_->Load(loaded_callback); | 1219 backend_->Load(loaded_callback); |
1218 } | 1220 } |
1219 | 1221 |
1220 void SQLitePersistentCookieStore::LoadCookiesForKey( | 1222 void SQLitePersistentCookieStore::LoadCookiesForKey( |
1221 const std::string& key, | 1223 const std::string& key, |
1222 const LoadedCallback& loaded_callback) { | 1224 const LoadedCallback& loaded_callback) { |
1223 backend_->LoadCookiesForKey(key, loaded_callback); | 1225 backend_->LoadCookiesForKey(key, loaded_callback); |
(...skipping 19 matching lines...) Expand all Loading... |
1243 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { | 1245 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { |
1244 backend_->Flush(callback); | 1246 backend_->Flush(callback); |
1245 } | 1247 } |
1246 | 1248 |
1247 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 1249 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
1248 backend_->Close(); | 1250 backend_->Close(); |
1249 // We release our reference to the Backend, though it will probably still have | 1251 // We release our reference to the Backend, though it will probably still have |
1250 // a reference if the background runner has not run Close() yet. | 1252 // a reference if the background runner has not run Close() yet. |
1251 } | 1253 } |
1252 | 1254 |
1253 net::CookieStore* CreatePersistentCookieStore( | 1255 CookieStoreConfig::CookieStoreConfig() |
1254 const base::FilePath& path, | 1256 : session_cookie_mode(EPHEMERAL_SESSION_COOKIES), |
1255 bool restore_old_session_cookies, | 1257 crypto_delegate(NULL) { |
1256 quota::SpecialStoragePolicy* storage_policy, | 1258 // Default to an in-memory cookie store. |
1257 net::CookieMonster::Delegate* cookie_monster_delegate, | 1259 } |
1258 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | |
1259 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | |
1260 scoped_ptr<CookieCryptoDelegate> crypto_delegate) { | |
1261 SQLitePersistentCookieStore* persistent_store = | |
1262 new SQLitePersistentCookieStore( | |
1263 path, | |
1264 client_task_runner, | |
1265 background_task_runner, | |
1266 restore_old_session_cookies, | |
1267 storage_policy, | |
1268 crypto_delegate.Pass()); | |
1269 | 1260 |
1270 net::CookieMonster* cookie_monster = | 1261 CookieStoreConfig::CookieStoreConfig( |
1271 new net::CookieMonster(persistent_store, cookie_monster_delegate); | 1262 const base::FilePath& path, |
| 1263 SessionCookieMode session_cookie_mode, |
| 1264 quota::SpecialStoragePolicy* storage_policy, |
| 1265 net::CookieMonsterDelegate* cookie_delegate) |
| 1266 : path(path), |
| 1267 session_cookie_mode(session_cookie_mode), |
| 1268 storage_policy(storage_policy), |
| 1269 cookie_delegate(cookie_delegate), |
| 1270 crypto_delegate(NULL) { |
| 1271 CHECK(!path.empty() || session_cookie_mode == EPHEMERAL_SESSION_COOKIES); |
| 1272 } |
| 1273 |
| 1274 CookieStoreConfig::~CookieStoreConfig() { |
| 1275 } |
| 1276 |
| 1277 net::CookieStore* CreateCookieStore(const CookieStoreConfig& config) { |
| 1278 net::CookieMonster* cookie_monster = NULL; |
| 1279 |
| 1280 if (config.path.empty()) { |
| 1281 // Empty path means in-memory store. |
| 1282 cookie_monster = new net::CookieMonster(NULL, config.cookie_delegate); |
| 1283 } else { |
| 1284 scoped_refptr<base::SequencedTaskRunner> client_task_runner = |
| 1285 config.client_task_runner; |
| 1286 scoped_refptr<base::SequencedTaskRunner> background_task_runner = |
| 1287 config.background_task_runner; |
| 1288 |
| 1289 if (!client_task_runner) { |
| 1290 client_task_runner = |
| 1291 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
| 1292 } |
| 1293 |
| 1294 if (!background_task_runner) { |
| 1295 background_task_runner = |
| 1296 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( |
| 1297 BrowserThread::GetBlockingPool()->GetSequenceToken()); |
| 1298 } |
| 1299 |
| 1300 SQLitePersistentCookieStore* persistent_store = |
| 1301 new SQLitePersistentCookieStore( |
| 1302 config.path, |
| 1303 client_task_runner, |
| 1304 background_task_runner, |
| 1305 (config.session_cookie_mode == |
| 1306 CookieStoreConfig::RESTORED_SESSION_COOKIES), |
| 1307 config.storage_policy, |
| 1308 config.crypto_delegate); |
| 1309 |
| 1310 cookie_monster = |
| 1311 new net::CookieMonster(persistent_store, config.cookie_delegate); |
| 1312 if ((config.session_cookie_mode == |
| 1313 CookieStoreConfig::PERSISTANT_SESSION_COOKIES) || |
| 1314 (config.session_cookie_mode == |
| 1315 CookieStoreConfig::RESTORED_SESSION_COOKIES)) { |
| 1316 cookie_monster->SetPersistSessionCookies(true); |
| 1317 } |
| 1318 } |
1272 | 1319 |
1273 // In the case of Android WebView, the cookie store may be created | 1320 // In the case of Android WebView, the cookie store may be created |
1274 // before the browser process fully initializes -- certainly before | 1321 // before the browser process fully initializes -- certainly before |
1275 // the main loop ever runs. In this situation, the CommandLine singleton | 1322 // the main loop ever runs. In this situation, the CommandLine singleton |
1276 // will not have been set up. Android tests do not need file cookies | 1323 // will not have been set up. Android tests do not need file cookies |
1277 // so always ignore them here. | 1324 // so always ignore them here. |
1278 // | 1325 // |
1279 // TODO(ajwong): Remove the InitializedForCurrentProcess() check | 1326 // TODO(ajwong): Remove the InitializedForCurrentProcess() check |
1280 // once http://crbug.com/331424 is resolved. | 1327 // once http://crbug.com/331424 is resolved. |
1281 if (CommandLine::InitializedForCurrentProcess() && | 1328 if (CommandLine::InitializedForCurrentProcess() && |
1282 CommandLine::ForCurrentProcess()->HasSwitch( | 1329 CommandLine::ForCurrentProcess()->HasSwitch( |
1283 switches::kEnableFileCookies)) { | 1330 switches::kEnableFileCookies)) { |
1284 cookie_monster->SetEnableFileScheme(true); | 1331 cookie_monster->SetEnableFileScheme(true); |
1285 } | 1332 } |
1286 | 1333 |
1287 return cookie_monster; | 1334 return cookie_monster; |
1288 } | 1335 } |
1289 | 1336 |
1290 net::CookieStore* CreatePersistentCookieStore( | |
1291 const base::FilePath& path, | |
1292 bool restore_old_session_cookies, | |
1293 quota::SpecialStoragePolicy* storage_policy, | |
1294 net::CookieMonster::Delegate* cookie_monster_delegate, | |
1295 scoped_ptr<CookieCryptoDelegate> crypto_delegate) { | |
1296 return CreatePersistentCookieStore( | |
1297 path, | |
1298 restore_old_session_cookies, | |
1299 storage_policy, | |
1300 cookie_monster_delegate, | |
1301 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), | |
1302 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
1303 BrowserThread::GetBlockingPool()->GetSequenceToken()), | |
1304 crypto_delegate.Pass()); | |
1305 } | |
1306 | |
1307 net::CookieStore* CreateInMemoryCookieStore( | |
1308 net::CookieMonster::Delegate* cookie_monster_delegate) { | |
1309 net::CookieMonster* cookie_monster = | |
1310 new net::CookieMonster(NULL, cookie_monster_delegate); | |
1311 if (CommandLine::InitializedForCurrentProcess() && | |
1312 CommandLine::ForCurrentProcess()->HasSwitch( | |
1313 switches::kEnableFileCookies)) { | |
1314 cookie_monster->SetEnableFileScheme(true); | |
1315 } | |
1316 return cookie_monster; | |
1317 } | |
1318 | |
1319 } // namespace content | 1337 } // namespace content |
OLD | NEW |