OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // Brought to you by the letter D and the number 2. | 5 // Brought to you by the letter D and the number 2. |
6 | 6 |
7 #ifndef NET_BASE_COOKIE_MONSTER_H_ | 7 #ifndef NET_BASE_COOKIE_MONSTER_H_ |
8 #define NET_BASE_COOKIE_MONSTER_H_ | 8 #define NET_BASE_COOKIE_MONSTER_H_ |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 | 64 |
65 // The store passed in should not have had Init() called on it yet. This class | 65 // The store passed in should not have had Init() called on it yet. This class |
66 // will take care of initializing it. The backing store is NOT owned by this | 66 // will take care of initializing it. The backing store is NOT owned by this |
67 // class, but it must remain valid for the duration of the cookie monster's | 67 // class, but it must remain valid for the duration of the cookie monster's |
68 // existence. | 68 // existence. |
69 CookieMonster(PersistentCookieStore* store); | 69 CookieMonster(PersistentCookieStore* store); |
70 | 70 |
71 ~CookieMonster(); | 71 ~CookieMonster(); |
72 | 72 |
73 // Parse the string with the cookie time (very forgivingly). | 73 // Parse the string with the cookie time (very forgivingly). |
74 static Time ParseCookieTime(const std::string& time_string); | 74 static base::Time ParseCookieTime(const std::string& time_string); |
75 | 75 |
76 // Set a single cookie. Expects a cookie line, like "a=1; domain=b.com". | 76 // Set a single cookie. Expects a cookie line, like "a=1; domain=b.com". |
77 bool SetCookie(const GURL& url, const std::string& cookie_line); | 77 bool SetCookie(const GURL& url, const std::string& cookie_line); |
78 // Sets a single cookie with a specific creation date. To set a cookie with | 78 // Sets a single cookie with a specific creation date. To set a cookie with |
79 // a creation date of Now() use SetCookie() instead (it calls this function | 79 // a creation date of Now() use SetCookie() instead (it calls this function |
80 // internally). | 80 // internally). |
81 bool SetCookieWithCreationTime(const GURL& url, | 81 bool SetCookieWithCreationTime(const GURL& url, |
82 const std::string& cookie_line, | 82 const std::string& cookie_line, |
83 const Time& creation_time); | 83 const base::Time& creation_time); |
84 // Set a vector of response cookie values for the same URL. | 84 // Set a vector of response cookie values for the same URL. |
85 void SetCookies(const GURL& url, const std::vector<std::string>& cookies); | 85 void SetCookies(const GURL& url, const std::vector<std::string>& cookies); |
86 | 86 |
87 // TODO what if the total size of all the cookies >4k, can we have a header | 87 // TODO what if the total size of all the cookies >4k, can we have a header |
88 // that big or do we need multiple Cookie: headers? | 88 // that big or do we need multiple Cookie: headers? |
89 // Simple interface, get a cookie string "a=b; c=d" for the given URL. | 89 // Simple interface, get a cookie string "a=b; c=d" for the given URL. |
90 // It will _not_ return httponly cookies, see GetCookiesWithOptions | 90 // It will _not_ return httponly cookies, see GetCookiesWithOptions |
91 std::string GetCookies(const GURL& url); | 91 std::string GetCookies(const GURL& url); |
92 std::string GetCookiesWithOptions(const GURL& url, CookieOptions options); | 92 std::string GetCookiesWithOptions(const GURL& url, CookieOptions options); |
93 // Returns all the cookies, for use in management UI, etc. | 93 // Returns all the cookies, for use in management UI, etc. |
94 CookieList GetAllCookies(); | 94 CookieList GetAllCookies(); |
95 | 95 |
96 // Delete all of the cookies. | 96 // Delete all of the cookies. |
97 int DeleteAll(bool sync_to_store); | 97 int DeleteAll(bool sync_to_store); |
98 // Delete all of the cookies that have a creation_date greater than or equal | 98 // Delete all of the cookies that have a creation_date greater than or equal |
99 // to |delete_begin| and less than |delete_end| | 99 // to |delete_begin| and less than |delete_end| |
100 int DeleteAllCreatedBetween(const Time& delete_begin, | 100 int DeleteAllCreatedBetween(const base::Time& delete_begin, |
101 const Time& delete_end, | 101 const base::Time& delete_end, |
102 bool sync_to_store); | 102 bool sync_to_store); |
103 // Delete all of the cookies that have a creation_date more recent than the | 103 // Delete all of the cookies that have a creation_date more recent than the |
104 // one passed into the function via |delete_after|. | 104 // one passed into the function via |delete_after|. |
105 int DeleteAllCreatedAfter(const Time& delete_begin, bool sync_to_store); | 105 int DeleteAllCreatedAfter(const base::Time& delete_begin, bool sync_to_store); |
106 | 106 |
107 // Delete one specific cookie. | 107 // Delete one specific cookie. |
108 bool DeleteCookie(const std::string& domain, | 108 bool DeleteCookie(const std::string& domain, |
109 const CanonicalCookie& cookie, | 109 const CanonicalCookie& cookie, |
110 bool sync_to_store); | 110 bool sync_to_store); |
111 | 111 |
112 // There are some unknowns about how to correctly handle file:// cookies, | 112 // There are some unknowns about how to correctly handle file:// cookies, |
113 // and our implementation for this is not robust enough. This allows you | 113 // and our implementation for this is not robust enough. This allows you |
114 // to enable support, but it should only be used for testing. Bug 1157243. | 114 // to enable support, but it should only be used for testing. Bug 1157243. |
115 static void EnableFileScheme(); | 115 static void EnableFileScheme(); |
(...skipping 16 matching lines...) Expand all Loading... |
132 // Should only be called by InitIfNecessary(). | 132 // Should only be called by InitIfNecessary(). |
133 void InitStore(); | 133 void InitStore(); |
134 | 134 |
135 void FindCookiesForHostAndDomain(const GURL& url, | 135 void FindCookiesForHostAndDomain(const GURL& url, |
136 CookieOptions options, | 136 CookieOptions options, |
137 std::vector<CanonicalCookie*>* cookies); | 137 std::vector<CanonicalCookie*>* cookies); |
138 | 138 |
139 void FindCookiesForKey(const std::string& key, | 139 void FindCookiesForKey(const std::string& key, |
140 const GURL& url, | 140 const GURL& url, |
141 CookieOptions options, | 141 CookieOptions options, |
142 const Time& current, | 142 const base::Time& current, |
143 std::vector<CanonicalCookie*>* cookies); | 143 std::vector<CanonicalCookie*>* cookies); |
144 | 144 |
145 int DeleteEquivalentCookies(const std::string& key, | 145 int DeleteEquivalentCookies(const std::string& key, |
146 const CanonicalCookie& ecc); | 146 const CanonicalCookie& ecc); |
147 | 147 |
148 void InternalInsertCookie(const std::string& key, | 148 void InternalInsertCookie(const std::string& key, |
149 CanonicalCookie* cc, | 149 CanonicalCookie* cc, |
150 bool sync_to_store); | 150 bool sync_to_store); |
151 | 151 |
152 void InternalDeleteCookie(CookieMap::iterator it, bool sync_to_store); | 152 void InternalDeleteCookie(CookieMap::iterator it, bool sync_to_store); |
153 | 153 |
154 // Enforce cookie maximum limits, purging expired and old cookies if needed | 154 // Enforce cookie maximum limits, purging expired and old cookies if needed |
155 int GarbageCollect(const Time& current, const std::string& key); | 155 int GarbageCollect(const base::Time& current, const std::string& key); |
156 int GarbageCollectRange(const Time& current, | 156 int GarbageCollectRange(const base::Time& current, |
157 const CookieMapItPair& itpair, | 157 const CookieMapItPair& itpair, |
158 size_t num_max, | 158 size_t num_max, |
159 size_t num_purge); | 159 size_t num_purge); |
160 | 160 |
161 CookieMap cookies_; | 161 CookieMap cookies_; |
162 | 162 |
163 // Indicates whether the cookie store has been initialized. This happens | 163 // Indicates whether the cookie store has been initialized. This happens |
164 // lazily in InitStoreIfNecessary(). | 164 // lazily in InitStoreIfNecessary(). |
165 bool initialized_; | 165 bool initialized_; |
166 | 166 |
167 PersistentCookieStore* store_; | 167 PersistentCookieStore* store_; |
168 | 168 |
169 // The resolution of our time isn't enough, so we do something | 169 // The resolution of our time isn't enough, so we do something |
170 // ugly and increment when we've seen the same time twice. | 170 // ugly and increment when we've seen the same time twice. |
171 Time CurrentTime(); | 171 base::Time CurrentTime(); |
172 Time last_time_seen_; | 172 base::Time last_time_seen_; |
173 | 173 |
174 // Lock for thread-safety | 174 // Lock for thread-safety |
175 Lock lock_; | 175 Lock lock_; |
176 | 176 |
177 DISALLOW_COPY_AND_ASSIGN(CookieMonster); | 177 DISALLOW_COPY_AND_ASSIGN(CookieMonster); |
178 }; | 178 }; |
179 | 179 |
180 class CookieMonster::ParsedCookie { | 180 class CookieMonster::ParsedCookie { |
181 public: | 181 public: |
182 typedef std::pair<std::string, std::string> TokenValuePair; | 182 typedef std::pair<std::string, std::string> TokenValuePair; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 size_t httponly_index_; | 230 size_t httponly_index_; |
231 | 231 |
232 DISALLOW_COPY_AND_ASSIGN(ParsedCookie); | 232 DISALLOW_COPY_AND_ASSIGN(ParsedCookie); |
233 }; | 233 }; |
234 | 234 |
235 | 235 |
236 class CookieMonster::CanonicalCookie { | 236 class CookieMonster::CanonicalCookie { |
237 public: | 237 public: |
238 CanonicalCookie(const std::string& name, const std::string& value, | 238 CanonicalCookie(const std::string& name, const std::string& value, |
239 const std::string& path, bool secure, | 239 const std::string& path, bool secure, |
240 bool httponly, const Time& creation, | 240 bool httponly, const base::Time& creation, |
241 bool has_expires, const Time& expires) | 241 bool has_expires, const base::Time& expires) |
242 : name_(name), | 242 : name_(name), |
243 value_(value), | 243 value_(value), |
244 path_(path), | 244 path_(path), |
245 creation_date_(creation), | 245 creation_date_(creation), |
246 expiry_date_(expires), | 246 expiry_date_(expires), |
247 has_expires_(has_expires), | 247 has_expires_(has_expires), |
248 secure_(secure), | 248 secure_(secure), |
249 httponly_(httponly) { | 249 httponly_(httponly) { |
250 } | 250 } |
251 | 251 |
252 #if defined(_MSC_VER) && _CPPLIB_VER == 505 | 252 #if defined(_MSC_VER) && _CPPLIB_VER == 505 |
253 // On Visual Studio 2008 Service Pack 1, std::vector<> do an early | 253 // On Visual Studio 2008 Service Pack 1, std::vector<> do an early |
254 // optimization in a way that requires the availability of a default | 254 // optimization in a way that requires the availability of a default |
255 // constructor. It is because it sees std::pair<> as "swappable", so creates a | 255 // constructor. It is because it sees std::pair<> as "swappable", so creates a |
256 // dummy to swap with, which requires an empty constructor for any entry in | 256 // dummy to swap with, which requires an empty constructor for any entry in |
257 // the std::pair. | 257 // the std::pair. |
258 CanonicalCookie() { } | 258 CanonicalCookie() { } |
259 #endif | 259 #endif |
260 | 260 |
261 // Supports the default copy constructor. | 261 // Supports the default copy constructor. |
262 | 262 |
263 const std::string& Name() const { return name_; } | 263 const std::string& Name() const { return name_; } |
264 const std::string& Value() const { return value_; } | 264 const std::string& Value() const { return value_; } |
265 const std::string& Path() const { return path_; } | 265 const std::string& Path() const { return path_; } |
266 const Time& CreationDate() const { return creation_date_; } | 266 const base::Time& CreationDate() const { return creation_date_; } |
267 bool DoesExpire() const { return has_expires_; } | 267 bool DoesExpire() const { return has_expires_; } |
268 bool IsPersistent() const { return DoesExpire(); } | 268 bool IsPersistent() const { return DoesExpire(); } |
269 const Time& ExpiryDate() const { return expiry_date_; } | 269 const base::Time& ExpiryDate() const { return expiry_date_; } |
270 bool IsSecure() const { return secure_; } | 270 bool IsSecure() const { return secure_; } |
271 bool IsHttpOnly() const { return httponly_; } | 271 bool IsHttpOnly() const { return httponly_; } |
272 | 272 |
273 bool IsExpired(const Time& current) { | 273 bool IsExpired(const base::Time& current) { |
274 return has_expires_ && current >= expiry_date_; | 274 return has_expires_ && current >= expiry_date_; |
275 } | 275 } |
276 | 276 |
277 // Are the cookies considered equivalent in the eyes of the RFC. | 277 // Are the cookies considered equivalent in the eyes of the RFC. |
278 // This says that the domain and path should string match identically. | 278 // This says that the domain and path should string match identically. |
279 bool IsEquivalent(const CanonicalCookie& ecc) const { | 279 bool IsEquivalent(const CanonicalCookie& ecc) const { |
280 // It seems like it would make sense to take secure and httponly into | 280 // It seems like it would make sense to take secure and httponly into |
281 // account, but the RFC doesn't specify this. | 281 // account, but the RFC doesn't specify this. |
282 return name_ == ecc.Name() && path_ == ecc.Path(); | 282 return name_ == ecc.Name() && path_ == ecc.Path(); |
283 } | 283 } |
284 | 284 |
285 bool IsOnPath(const std::string& url_path) const; | 285 bool IsOnPath(const std::string& url_path) const; |
286 | 286 |
287 std::string DebugString() const; | 287 std::string DebugString() const; |
288 private: | 288 private: |
289 std::string name_; | 289 std::string name_; |
290 std::string value_; | 290 std::string value_; |
291 std::string path_; | 291 std::string path_; |
292 Time creation_date_; | 292 base::Time creation_date_; |
293 Time expiry_date_; | 293 base::Time expiry_date_; |
294 bool has_expires_; | 294 bool has_expires_; |
295 bool secure_; | 295 bool secure_; |
296 bool httponly_; | 296 bool httponly_; |
297 }; | 297 }; |
298 | 298 |
299 class CookieMonster::PersistentCookieStore { | 299 class CookieMonster::PersistentCookieStore { |
300 public: | 300 public: |
301 virtual ~PersistentCookieStore() { } | 301 virtual ~PersistentCookieStore() { } |
302 | 302 |
303 // Initializes the store and retrieves the existing cookies. This will be | 303 // Initializes the store and retrieves the existing cookies. This will be |
304 // called only once at startup. | 304 // called only once at startup. |
305 virtual bool Load(std::vector<CookieMonster::KeyedCanonicalCookie>*) = 0; | 305 virtual bool Load(std::vector<CookieMonster::KeyedCanonicalCookie>*) = 0; |
306 | 306 |
307 virtual void AddCookie(const std::string&, const CanonicalCookie&) = 0; | 307 virtual void AddCookie(const std::string&, const CanonicalCookie&) = 0; |
308 virtual void DeleteCookie(const CanonicalCookie&) = 0; | 308 virtual void DeleteCookie(const CanonicalCookie&) = 0; |
309 | 309 |
310 protected: | 310 protected: |
311 PersistentCookieStore() { } | 311 PersistentCookieStore() { } |
312 | 312 |
313 private: | 313 private: |
314 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); | 314 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); |
315 }; | 315 }; |
316 | 316 |
317 } // namespace net | 317 } // namespace net |
318 | 318 |
319 #endif // NET_BASE_COOKIE_MONSTER_H_ | 319 #endif // NET_BASE_COOKIE_MONSTER_H_ |
320 | 320 |
OLD | NEW |