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