OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/net/quota_policy_cookie_store.h" |
| 6 |
| 7 #include <list> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/cookie_store_factory.h" |
| 19 #include "content/public/common/content_switches.h" |
| 20 #include "net/cookies/canonical_cookie.h" |
| 21 #include "net/cookies/cookie_constants.h" |
| 22 #include "net/cookies/cookie_util.h" |
| 23 #include "net/extras/sqlite/cookie_crypto_delegate.h" |
| 24 #include "storage/browser/quota/special_storage_policy.h" |
| 25 #include "url/gurl.h" |
| 26 |
| 27 namespace content { |
| 28 |
| 29 QuotaPolicyCookieStore::QuotaPolicyCookieStore( |
| 30 const base::FilePath& path, |
| 31 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
| 32 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
| 33 bool restore_old_session_cookies, |
| 34 storage::SpecialStoragePolicy* special_storage_policy, |
| 35 net::CookieCryptoDelegate* crypto_delegate) |
| 36 : special_storage_policy_(special_storage_policy), |
| 37 persistent_store_( |
| 38 new net::SQLitePersistentCookieStore(path, |
| 39 client_task_runner, |
| 40 background_task_runner, |
| 41 restore_old_session_cookies, |
| 42 crypto_delegate)) { |
| 43 DCHECK(client_task_runner); |
| 44 DCHECK(background_task_runner); |
| 45 } |
| 46 |
| 47 QuotaPolicyCookieStore::~QuotaPolicyCookieStore() { |
| 48 if (!special_storage_policy_.get() || |
| 49 !special_storage_policy_->HasSessionOnlyOrigins()) { |
| 50 return; |
| 51 } |
| 52 |
| 53 std::list<net::SQLitePersistentCookieStore::CookieOrigin> |
| 54 session_only_cookies; |
| 55 for (auto it = cookies_per_origin_.begin(); |
| 56 it != cookies_per_origin_.end(); ++it) { |
| 57 if (it->second <= 0) { |
| 58 DCHECK_EQ(0, it->second); |
| 59 continue; |
| 60 } |
| 61 const GURL url(net::cookie_util::CookieOriginToURL(it->first.first, |
| 62 it->first.second)); |
| 63 if (!url.is_valid() || !special_storage_policy_->IsStorageSessionOnly(url)) |
| 64 continue; |
| 65 |
| 66 session_only_cookies.push_back(it->first); |
| 67 } |
| 68 |
| 69 persistent_store_->DeleteAllInList(session_only_cookies); |
| 70 } |
| 71 |
| 72 void QuotaPolicyCookieStore::Load(const LoadedCallback& loaded_callback) { |
| 73 persistent_store_->Load( |
| 74 base::Bind(&QuotaPolicyCookieStore::OnLoad, this, loaded_callback)); |
| 75 } |
| 76 |
| 77 void QuotaPolicyCookieStore::LoadCookiesForKey( |
| 78 const std::string& key, |
| 79 const LoadedCallback& loaded_callback) { |
| 80 persistent_store_->LoadCookiesForKey( |
| 81 key, |
| 82 base::Bind(&QuotaPolicyCookieStore::OnLoad, this, loaded_callback)); |
| 83 } |
| 84 |
| 85 void QuotaPolicyCookieStore::AddCookie(const net::CanonicalCookie& cc) { |
| 86 cookies_per_origin_[ |
| 87 net::SQLitePersistentCookieStore::CookieOrigin(cc.Domain(), |
| 88 cc.IsSecure())]++; |
| 89 persistent_store_->AddCookie(cc); |
| 90 } |
| 91 |
| 92 void QuotaPolicyCookieStore::UpdateCookieAccessTime( |
| 93 const net::CanonicalCookie& cc) { |
| 94 persistent_store_->UpdateCookieAccessTime(cc); |
| 95 } |
| 96 |
| 97 void QuotaPolicyCookieStore::DeleteCookie(const net::CanonicalCookie& cc) { |
| 98 cookies_per_origin_[ |
| 99 net::SQLitePersistentCookieStore::CookieOrigin(cc.Domain(), |
| 100 cc.IsSecure())]--; |
| 101 persistent_store_->DeleteCookie(cc); |
| 102 } |
| 103 |
| 104 void QuotaPolicyCookieStore::SetForceKeepSessionState() { |
| 105 // TODO(rohitrao): Just delete the storage policy here instead? |
| 106 persistent_store_->SetForceKeepSessionState(); |
| 107 } |
| 108 |
| 109 void QuotaPolicyCookieStore::Flush(const base::Closure& callback) { |
| 110 persistent_store_->Flush(callback); |
| 111 } |
| 112 |
| 113 void QuotaPolicyCookieStore::OnLoad( |
| 114 const LoadedCallback& loaded_callback, |
| 115 const std::vector<net::CanonicalCookie*>& cookies) { |
| 116 for (auto it = cookies.begin(); it != cookies.end(); ++it) { |
| 117 cookies_per_origin_[ |
| 118 net::SQLitePersistentCookieStore::CookieOrigin((*it)->Domain(), |
| 119 (*it)->IsSecure())]++; |
| 120 } |
| 121 |
| 122 loaded_callback.Run(cookies); |
| 123 } |
| 124 |
| 125 CookieStoreConfig::CookieStoreConfig() |
| 126 : session_cookie_mode(EPHEMERAL_SESSION_COOKIES), |
| 127 crypto_delegate(NULL) { |
| 128 // Default to an in-memory cookie store. |
| 129 } |
| 130 |
| 131 CookieStoreConfig::CookieStoreConfig( |
| 132 const base::FilePath& path, |
| 133 SessionCookieMode session_cookie_mode, |
| 134 storage::SpecialStoragePolicy* storage_policy, |
| 135 net::CookieMonsterDelegate* cookie_delegate) |
| 136 : path(path), |
| 137 session_cookie_mode(session_cookie_mode), |
| 138 storage_policy(storage_policy), |
| 139 cookie_delegate(cookie_delegate), |
| 140 crypto_delegate(NULL) { |
| 141 CHECK(!path.empty() || session_cookie_mode == EPHEMERAL_SESSION_COOKIES); |
| 142 } |
| 143 |
| 144 CookieStoreConfig::~CookieStoreConfig() { |
| 145 } |
| 146 |
| 147 net::CookieStore* CreateCookieStore(const CookieStoreConfig& config) { |
| 148 net::CookieMonster* cookie_monster = NULL; |
| 149 |
| 150 if (config.path.empty()) { |
| 151 // Empty path means in-memory store. |
| 152 cookie_monster = new net::CookieMonster(NULL, config.cookie_delegate.get()); |
| 153 } else { |
| 154 scoped_refptr<base::SequencedTaskRunner> client_task_runner = |
| 155 config.client_task_runner; |
| 156 scoped_refptr<base::SequencedTaskRunner> background_task_runner = |
| 157 config.background_task_runner; |
| 158 |
| 159 if (!client_task_runner.get()) { |
| 160 client_task_runner = |
| 161 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
| 162 } |
| 163 |
| 164 if (!background_task_runner.get()) { |
| 165 background_task_runner = |
| 166 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( |
| 167 BrowserThread::GetBlockingPool()->GetSequenceToken()); |
| 168 } |
| 169 |
| 170 QuotaPolicyCookieStore* persistent_store = |
| 171 new QuotaPolicyCookieStore( |
| 172 config.path, |
| 173 client_task_runner, |
| 174 background_task_runner, |
| 175 (config.session_cookie_mode == |
| 176 CookieStoreConfig::RESTORED_SESSION_COOKIES), |
| 177 config.storage_policy.get(), |
| 178 config.crypto_delegate); |
| 179 |
| 180 cookie_monster = |
| 181 new net::CookieMonster(persistent_store, config.cookie_delegate.get()); |
| 182 if ((config.session_cookie_mode == |
| 183 CookieStoreConfig::PERSISTANT_SESSION_COOKIES) || |
| 184 (config.session_cookie_mode == |
| 185 CookieStoreConfig::RESTORED_SESSION_COOKIES)) { |
| 186 cookie_monster->SetPersistSessionCookies(true); |
| 187 } |
| 188 } |
| 189 |
| 190 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 191 switches::kEnableFileCookies)) { |
| 192 cookie_monster->SetEnableFileScheme(true); |
| 193 } |
| 194 |
| 195 return cookie_monster; |
| 196 } |
| 197 |
| 198 } // namespace content |
OLD | NEW |