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