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