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