Home | History | Annotate | Download | only in cookies
      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
      3 // found in the LICENSE file.
      4 
      5 // Defines common functionality used by the implementation of the Chrome
      6 // Extensions Cookies API implemented in
      7 // chrome/browser/extensions/api/cookies/cookies_api.cc. This separate interface
      8 // exposes pieces of the API implementation mainly for unit testing purposes.
      9 
     10 #ifndef CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_HELPERS_H_
     11 #define CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_HELPERS_H_
     12 
     13 #include <string>
     14 #include <vector>
     15 
     16 #include "base/memory/linked_ptr.h"
     17 #include "base/memory/scoped_ptr.h"
     18 #include "chrome/common/extensions/api/cookies.h"
     19 #include "net/cookies/cookie_monster.h"
     20 #include "net/cookies/canonical_cookie.h"
     21 
     22 class Browser;
     23 class Profile;
     24 
     25 namespace base {
     26 class DictionaryValue;
     27 class ListValue;
     28 }
     29 
     30 namespace net {
     31 class CanonicalCookie;
     32 }
     33 
     34 namespace extensions {
     35 
     36 class Extension;
     37 
     38 namespace cookies_helpers {
     39 
     40 typedef std::vector<linked_ptr<extensions::api::cookies::Cookie> >
     41     LinkedCookieVec;
     42 
     43 // Returns either the original profile or the incognito profile, based on the
     44 // given store ID.  Returns NULL if the profile doesn't exist or is not allowed
     45 // (e.g. if incognito mode is not enabled for the extension).
     46 Profile* ChooseProfileFromStoreId(const std::string& store_id,
     47                                   Profile* profile,
     48                                   bool include_incognito);
     49 
     50 // Returns the store ID for a particular user profile.
     51 const char* GetStoreIdFromProfile(Profile* profile);
     52 
     53 // Allocates and construct a new Cookie object representing a cookie as defined
     54 // by the cookies API.
     55 scoped_ptr<extensions::api::cookies::Cookie> CreateCookie(
     56     const net::CanonicalCookie& cookie,
     57     const std::string& store_id);
     58 
     59 // Allocates and constructs a new CookieStore object as defined by the cookies
     60 // API.
     61 scoped_ptr<extensions::api::cookies::CookieStore> CreateCookieStore(
     62     Profile* profile,
     63     base::ListValue* tab_ids);
     64 
     65 // Retrieves all cookies from the given cookie store corresponding to the given
     66 // URL. If the URL is empty, all cookies in the cookie store are retrieved.
     67 // This can only be called on the IO thread.
     68 void GetCookieListFromStore(
     69     net::CookieStore* cookie_store, const GURL& url,
     70     const net::CookieMonster::GetCookieListCallback& callback);
     71 
     72 // Constructs a URL from a cookie's information for use in checking
     73 // a cookie against the extension's host permissions. The Secure
     74 // property of the cookie defines the URL scheme, and the cookie's
     75 // domain becomes the URL host.
     76 GURL GetURLFromCanonicalCookie(
     77     const net::CanonicalCookie& cookie);
     78 
     79 // Looks through all cookies in the given cookie store, and appends to the
     80 // match vector all the cookies that both match the given URL and cookie details
     81 // and are allowed by extension host permissions.
     82 void AppendMatchingCookiesToVector(
     83     const net::CookieList& all_cookies, const GURL& url,
     84     const extensions::api::cookies::GetAll::Params::Details* details,
     85     const Extension* extension, LinkedCookieVec* match_vector);
     86 
     87 // Appends the IDs of all tabs belonging to the given browser to the
     88 // given list.
     89 void AppendToTabIdList(Browser* browser, base::ListValue* tab_ids);
     90 
     91 // A class representing the cookie filter parameters passed into
     92 // cookies.getAll().
     93 // This class is essentially a convenience wrapper for the details dictionary
     94 // passed into the cookies.getAll() API by the user. If the dictionary contains
     95 // no filter parameters, the MatchFilter will always trivially
     96 // match all cookies.
     97 class MatchFilter {
     98  public:
     99   // Takes the details dictionary argument given by the user as input.
    100   // This class does not take ownership of the lifetime of the Details
    101   // object.
    102   explicit MatchFilter(
    103       const extensions::api::cookies::GetAll::Params::Details* details);
    104 
    105   // Returns true if the given cookie matches the properties in the match
    106   // filter.
    107   bool MatchesCookie(const net::CanonicalCookie& cookie);
    108 
    109  private:
    110   // Returns true if the given cookie domain string matches the filter's
    111   // domain. Any cookie domain which is equal to or is a subdomain of the
    112   // filter's domain will be matched; leading '.' characters indicating
    113   // host-only domains have no meaning in the match filter domain (for
    114   // instance, a match filter domain of 'foo.bar.com' will be treated the same
    115   // as '.foo.bar.com', and both will match cookies with domain values of
    116   // 'foo.bar.com', '.foo.bar.com', and 'baz.foo.bar.com'.
    117   bool MatchesDomain(const std::string& domain);
    118 
    119   const extensions::api::cookies::GetAll::Params::Details* details_;
    120 };
    121 
    122 }  // namespace cookies_helpers
    123 }  // namespace extensions
    124 
    125 #endif  // CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_HELPERS_H_
    126