Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2009 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 // Brought to you by number 42.
      6 
      7 #ifndef NET_BASE_COOKIE_STORE_H_
      8 #define NET_BASE_COOKIE_STORE_H_
      9 
     10 #include <string>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/ref_counted.h"
     14 #include "base/time.h"
     15 #include "net/base/cookie_options.h"
     16 
     17 class GURL;
     18 
     19 namespace net {
     20 
     21 class CookieMonster;
     22 
     23 // An interface for storing and retrieving cookies. Implementations need to
     24 // be thread safe as its methods can be accessed from IO as well as UI threads.
     25 class CookieStore : public base::RefCountedThreadSafe<CookieStore> {
     26  public:
     27   // Sets a single cookie.  Expects a cookie line, like "a=1; domain=b.com".
     28   virtual bool SetCookieWithOptions(const GURL& url,
     29                                     const std::string& cookie_line,
     30                                     const CookieOptions& options) = 0;
     31 
     32   // TODO what if the total size of all the cookies >4k, can we have a header
     33   // that big or do we need multiple Cookie: headers?
     34   // Simple interface, gets a cookie string "a=b; c=d" for the given URL.
     35   // Use options to access httponly cookies.
     36   virtual std::string GetCookiesWithOptions(const GURL& url,
     37                                             const CookieOptions& options) = 0;
     38 
     39   // Deletes the passed in cookie for the specified URL.
     40   virtual void DeleteCookie(const GURL& url,
     41                             const std::string& cookie_name) = 0;
     42 
     43   // Returns the underlying CookieMonster.
     44   virtual CookieMonster* GetCookieMonster() = 0;
     45 
     46 
     47   // --------------------------------------------------------------------------
     48   // Helpers to make the above interface simpler for some cases.
     49 
     50   // Sets a cookie for the given URL using default options.
     51   bool SetCookie(const GURL& url, const std::string& cookie_line) {
     52     return SetCookieWithOptions(url, cookie_line, CookieOptions());
     53   }
     54 
     55   // Gets cookies for the given URL using default options.
     56   std::string GetCookies(const GURL& url) {
     57     return GetCookiesWithOptions(url, CookieOptions());
     58   }
     59 
     60   // Sets a vector of response cookie values for the same URL.
     61   void SetCookiesWithOptions(const GURL& url,
     62                              const std::vector<std::string>& cookie_lines,
     63                              const CookieOptions& options) {
     64     for (size_t i = 0; i < cookie_lines.size(); ++i)
     65       SetCookieWithOptions(url, cookie_lines[i], options);
     66   }
     67   void SetCookies(const GURL& url,
     68                   const std::vector<std::string>& cookie_lines) {
     69     SetCookiesWithOptions(url, cookie_lines, CookieOptions());
     70   }
     71 
     72  protected:
     73   friend class base::RefCountedThreadSafe<CookieStore>;
     74   virtual ~CookieStore() {}
     75 };
     76 
     77 }  // namespace net
     78 
     79 #endif  // NET_BASE_COOKIE_STORE_H_
     80