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) { | |
Ryan Sleevi
2015/03/19 03:37:51
Doesn't this vend a mutable |it| due to auto bindi
rohitrao (ping after 24h)
2015/03/19 14:55:42
Did not know that was possible, but I like it way
| |
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 special_storage_policy_ = NULL; | |
106 } | |
107 | |
108 void QuotaPolicyCookieStore::Flush(const base::Closure& callback) { | |
109 persistent_store_->Flush(callback); | |
110 } | |
111 | |
112 void QuotaPolicyCookieStore::OnLoad( | |
113 const LoadedCallback& loaded_callback, | |
114 const std::vector<net::CanonicalCookie*>& cookies) { | |
115 for (auto it = cookies.begin(); it != cookies.end(); ++it) { | |
Ryan Sleevi
2015/03/19 03:37:51
ditto const auto& + range
rohitrao (ping after 24h)
2015/03/19 14:55:42
Done.
| |
116 cookies_per_origin_[ | |
117 net::SQLitePersistentCookieStore::CookieOrigin((*it)->Domain(), | |
118 (*it)->IsSecure())]++; | |
119 } | |
120 | |
121 loaded_callback.Run(cookies); | |
122 } | |
123 | |
124 CookieStoreConfig::CookieStoreConfig() | |
125 : session_cookie_mode(EPHEMERAL_SESSION_COOKIES), | |
126 crypto_delegate(NULL) { | |
127 // Default to an in-memory cookie store. | |
128 } | |
129 | |
130 CookieStoreConfig::CookieStoreConfig( | |
131 const base::FilePath& path, | |
132 SessionCookieMode session_cookie_mode, | |
133 storage::SpecialStoragePolicy* storage_policy, | |
134 net::CookieMonsterDelegate* cookie_delegate) | |
135 : path(path), | |
136 session_cookie_mode(session_cookie_mode), | |
137 storage_policy(storage_policy), | |
138 cookie_delegate(cookie_delegate), | |
139 crypto_delegate(NULL) { | |
140 CHECK(!path.empty() || session_cookie_mode == EPHEMERAL_SESSION_COOKIES); | |
141 } | |
142 | |
143 CookieStoreConfig::~CookieStoreConfig() { | |
144 } | |
145 | |
146 net::CookieStore* CreateCookieStore(const CookieStoreConfig& config) { | |
147 net::CookieMonster* cookie_monster = NULL; | |
148 | |
149 if (config.path.empty()) { | |
150 // Empty path means in-memory store. | |
151 cookie_monster = new net::CookieMonster(NULL, config.cookie_delegate.get()); | |
152 } else { | |
153 scoped_refptr<base::SequencedTaskRunner> client_task_runner = | |
154 config.client_task_runner; | |
155 scoped_refptr<base::SequencedTaskRunner> background_task_runner = | |
156 config.background_task_runner; | |
157 | |
158 if (!client_task_runner.get()) { | |
159 client_task_runner = | |
160 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | |
161 } | |
162 | |
163 if (!background_task_runner.get()) { | |
164 background_task_runner = | |
165 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
166 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
Ryan Sleevi
2015/03/19 03:37:51
Is it right that every cookie store should be give
rohitrao (ping after 24h)
2015/03/19 14:55:42
This entire block was copied verbatim from the old
| |
167 } | |
168 | |
169 QuotaPolicyCookieStore* persistent_store = | |
170 new QuotaPolicyCookieStore( | |
171 config.path, | |
172 client_task_runner, | |
173 background_task_runner, | |
174 (config.session_cookie_mode == | |
175 CookieStoreConfig::RESTORED_SESSION_COOKIES), | |
176 config.storage_policy.get(), | |
177 config.crypto_delegate); | |
178 | |
179 cookie_monster = | |
180 new net::CookieMonster(persistent_store, config.cookie_delegate.get()); | |
181 if ((config.session_cookie_mode == | |
182 CookieStoreConfig::PERSISTANT_SESSION_COOKIES) || | |
183 (config.session_cookie_mode == | |
184 CookieStoreConfig::RESTORED_SESSION_COOKIES)) { | |
185 cookie_monster->SetPersistSessionCookies(true); | |
186 } | |
187 } | |
188 | |
189 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
190 switches::kEnableFileCookies)) { | |
191 cookie_monster->SetEnableFileScheme(true); | |
192 } | |
193 | |
194 return cookie_monster; | |
195 } | |
196 | |
197 } // namespace content | |
OLD | NEW |