[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 5 | // Defines the Chrome Extensions Cookies API functions for accessing internet |
[email protected] | 2fc2962 | 2011-12-09 23:00:36 | [diff] [blame] | 6 | // cookies, as specified in the extension API JSON. |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 7 | |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 8 | #ifndef CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_ |
| 9 | #define CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_ |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 10 | |
| 11 | #include <string> |
| 12 | |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
[email protected] | 1794e6a | 2013-01-03 02:22:02 | [diff] [blame] | 16 | #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 17 | #include "chrome/browser/extensions/event_router.h" |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_function.h" |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 19 | #include "chrome/browser/net/chrome_cookie_notification_details.h" |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 20 | #include "chrome/common/extensions/api/cookies.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 21 | #include "content/public/browser/notification_observer.h" |
| 22 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 23 | #include "googleurl/src/gurl.h" |
[email protected] | 8da4b181 | 2012-07-25 13:54:38 | [diff] [blame] | 24 | #include "net/cookies/canonical_cookie.h" |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 25 | |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 26 | namespace net { |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 27 | class URLRequestContextGetter; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 28 | } |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 29 | |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 30 | namespace extensions { |
| 31 | |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 32 | // Observes CookieMonster notifications and routes them as events to the |
| 33 | // extension system. |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 34 | class CookiesEventRouter : public content::NotificationObserver { |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 35 | public: |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 36 | explicit CookiesEventRouter(Profile* profile); |
| 37 | virtual ~CookiesEventRouter(); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 38 | |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 39 | private: |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 40 | // content::NotificationObserver implementation. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 41 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 42 | const content::NotificationSource& source, |
| 43 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 44 | |
| 45 | // Handler for the COOKIE_CHANGED event. The method takes the details of such |
| 46 | // an event and constructs a suitable JSON formatted extension event from it. |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 47 | void CookieChanged(Profile* profile, ChromeCookieDetails* details); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 48 | |
| 49 | // This method dispatches events to the extension message service. |
| 50 | void DispatchEvent(Profile* context, |
[email protected] | ccc91c6 | 2012-09-11 01:02:43 | [diff] [blame] | 51 | const std::string& event_name, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 52 | scoped_ptr<base::ListValue> event_args, |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 53 | GURL& cookie_domain); |
| 54 | |
| 55 | // Used for tracking registrations to CookieMonster notifications. |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 56 | content::NotificationRegistrar registrar_; |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 57 | |
[email protected] | f825df2 | 2011-06-28 17:36:10 | [diff] [blame] | 58 | Profile* profile_; |
| 59 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 60 | DISALLOW_COPY_AND_ASSIGN(CookiesEventRouter); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 61 | }; |
| 62 | |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 63 | // Serves as a base class for all cookies API functions, and defines some |
| 64 | // common functionality for parsing cookies API function arguments. |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 65 | // Note that all of the functions in this file derive from |
| 66 | // AsyncExtensionFunction, and are not threadsafe, so they should not be |
| 67 | // concurrently accessed from multiple threads. They modify |result_| and other |
| 68 | // member variables directly. |
| 69 | // See chrome/browser/extensions/extension_function.h for more information. |
| 70 | class CookiesFunction : public AsyncExtensionFunction { |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 71 | protected: |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 72 | virtual ~CookiesFunction() {} |
| 73 | |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 74 | // Constructs a GURL from the given url string. Returns false and assigns the |
| 75 | // internal error_ value if the URL is invalid. If |check_host_permissions| is |
| 76 | // true, the URL is also checked against the extension's host permissions, and |
| 77 | // if there is no permission for the URL, this function returns false. |
| 78 | bool ParseUrl(const std::string& url_string, GURL* url, |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 79 | bool check_host_permissions); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 80 | |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 81 | // Gets the store identified by |store_id| and returns it in |context|. |
| 82 | // If |store_id| contains an empty string, retrieves the current execution |
| 83 | // context's store. In this case, |store_id| is populated with the found |
| 84 | // store, and |context| can be NULL if the caller only wants |store_id|. |
| 85 | bool ParseStoreContext(std::string* store_id, |
| 86 | net::URLRequestContextGetter** context); |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 87 | }; |
| 88 | |
[email protected] | 00804ca | 2010-07-16 06:38:59 | [diff] [blame] | 89 | // Implements the cookies.get() extension function. |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 90 | class GetCookieFunction : public CookiesFunction { |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 91 | public: |
[email protected] | 00804ca | 2010-07-16 06:38:59 | [diff] [blame] | 92 | DECLARE_EXTENSION_FUNCTION_NAME("cookies.get") |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 93 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 94 | GetCookieFunction(); |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 95 | |
| 96 | protected: |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 97 | virtual ~GetCookieFunction(); |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 98 | |
| 99 | // ExtensionFunction: |
| 100 | virtual bool RunImpl() OVERRIDE; |
| 101 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 102 | private: |
| 103 | void GetCookieOnIOThread(); |
| 104 | void RespondOnUIThread(); |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 105 | void GetCookieCallback(const net::CookieList& cookie_list); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 106 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 107 | GURL url_; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 108 | scoped_refptr<net::URLRequestContextGetter> store_context_; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 109 | scoped_ptr<extensions::api::cookies::Get::Params> parsed_args_; |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 110 | }; |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 111 | |
[email protected] | 00804ca | 2010-07-16 06:38:59 | [diff] [blame] | 112 | // Implements the cookies.getAll() extension function. |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 113 | class GetAllCookiesFunction : public CookiesFunction { |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 114 | public: |
[email protected] | 00804ca | 2010-07-16 06:38:59 | [diff] [blame] | 115 | DECLARE_EXTENSION_FUNCTION_NAME("cookies.getAll") |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 116 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 117 | GetAllCookiesFunction(); |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 118 | |
| 119 | protected: |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 120 | virtual ~GetAllCookiesFunction(); |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 121 | |
| 122 | // ExtensionFunction: |
| 123 | virtual bool RunImpl() OVERRIDE; |
| 124 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 125 | private: |
| 126 | void GetAllCookiesOnIOThread(); |
| 127 | void RespondOnUIThread(); |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 128 | void GetAllCookiesCallback(const net::CookieList& cookie_list); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 129 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 130 | GURL url_; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 131 | scoped_refptr<net::URLRequestContextGetter> store_context_; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 132 | scoped_ptr<extensions::api::cookies::GetAll::Params> parsed_args_; |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 133 | }; |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 134 | |
[email protected] | 00804ca | 2010-07-16 06:38:59 | [diff] [blame] | 135 | // Implements the cookies.set() extension function. |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 136 | class SetCookieFunction : public CookiesFunction { |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 137 | public: |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 138 | DECLARE_EXTENSION_FUNCTION_NAME("cookies.set") |
| 139 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 140 | SetCookieFunction(); |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 141 | |
| 142 | protected: |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 143 | virtual ~SetCookieFunction(); |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 144 | virtual bool RunImpl() OVERRIDE; |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 145 | |
| 146 | private: |
| 147 | void SetCookieOnIOThread(); |
| 148 | void RespondOnUIThread(); |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 149 | void PullCookie(bool set_cookie_); |
| 150 | void PullCookieCallback(const net::CookieList& cookie_list); |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 151 | |
| 152 | GURL url_; |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 153 | bool success_; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 154 | scoped_refptr<net::URLRequestContextGetter> store_context_; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 155 | scoped_ptr<extensions::api::cookies::Set::Params> parsed_args_; |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 156 | }; |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 157 | |
[email protected] | 00804ca | 2010-07-16 06:38:59 | [diff] [blame] | 158 | // Implements the cookies.remove() extension function. |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 159 | class RemoveCookieFunction : public CookiesFunction { |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 160 | public: |
[email protected] | 00804ca | 2010-07-16 06:38:59 | [diff] [blame] | 161 | DECLARE_EXTENSION_FUNCTION_NAME("cookies.remove") |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 162 | |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 163 | RemoveCookieFunction(); |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 164 | |
| 165 | protected: |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 166 | virtual ~RemoveCookieFunction(); |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 167 | |
| 168 | // ExtensionFunction: |
| 169 | virtual bool RunImpl() OVERRIDE; |
| 170 | |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 171 | private: |
| 172 | void RemoveCookieOnIOThread(); |
| 173 | void RespondOnUIThread(); |
[email protected] | b9e48094d | 2011-07-20 14:27:13 | [diff] [blame] | 174 | void RemoveCookieCallback(); |
[email protected] | b829045 | 2011-03-21 14:11:14 | [diff] [blame] | 175 | |
| 176 | GURL url_; |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 177 | scoped_refptr<net::URLRequestContextGetter> store_context_; |
[email protected] | 960927c0 | 2012-07-23 21:38:08 | [diff] [blame] | 178 | scoped_ptr<extensions::api::cookies::Remove::Params> parsed_args_; |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 179 | }; |
[email protected] | b7e89914 | 2010-06-02 17:03:32 | [diff] [blame] | 180 | |
[email protected] | 00804ca | 2010-07-16 06:38:59 | [diff] [blame] | 181 | // Implements the cookies.getAllCookieStores() extension function. |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 182 | class GetAllCookieStoresFunction : public CookiesFunction { |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 183 | public: |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 184 | DECLARE_EXTENSION_FUNCTION_NAME("cookies.getAllCookieStores") |
| 185 | |
| 186 | protected: |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 187 | virtual ~GetAllCookieStoresFunction() {} |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 188 | |
| 189 | // ExtensionFunction: |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 190 | // GetAllCookieStoresFunction is sync. |
[email protected] | 1cc91fe | 2011-11-21 14:48:43 | [diff] [blame] | 191 | virtual void Run() OVERRIDE; |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 192 | virtual bool RunImpl() OVERRIDE; |
[email protected] | 898bbd3 | 2010-05-18 18:52:29 | [diff] [blame] | 193 | }; |
| 194 | |
[email protected] | 1794e6a | 2013-01-03 02:22:02 | [diff] [blame] | 195 | class CookiesAPI : public ProfileKeyedAPI, |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 196 | public extensions::EventRouter::Observer { |
| 197 | public: |
| 198 | explicit CookiesAPI(Profile* profile); |
| 199 | virtual ~CookiesAPI(); |
| 200 | |
| 201 | // ProfileKeyedService implementation. |
| 202 | virtual void Shutdown() OVERRIDE; |
| 203 | |
[email protected] | c39df1a | 2013-01-08 18:22:07 | [diff] [blame] | 204 | // ProfileKeyedAPI implementation. |
| 205 | static ProfileKeyedAPIFactory<CookiesAPI>* GetFactoryInstance(); |
| 206 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 207 | // EventRouter::Observer implementation. |
| 208 | virtual void OnListenerAdded(const extensions::EventListenerInfo& details) |
| 209 | OVERRIDE; |
| 210 | |
| 211 | private: |
[email protected] | 1794e6a | 2013-01-03 02:22:02 | [diff] [blame] | 212 | friend class ProfileKeyedAPIFactory<CookiesAPI>; |
| 213 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 214 | Profile* profile_; |
| 215 | |
[email protected] | 1794e6a | 2013-01-03 02:22:02 | [diff] [blame] | 216 | // ProfileKeyedAPI implementation. |
| 217 | static const char* service_name() { |
| 218 | return "CookiesAPI"; |
| 219 | } |
| 220 | static const bool kServiceIsNULLWhileTesting = true; |
| 221 | |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 222 | // Created lazily upon OnListenerAdded. |
| 223 | scoped_ptr<CookiesEventRouter> cookies_event_router_; |
[email protected] | 1794e6a | 2013-01-03 02:22:02 | [diff] [blame] | 224 | |
| 225 | DISALLOW_COPY_AND_ASSIGN(CookiesAPI); |
[email protected] | c54115c | 2012-12-06 17:56:30 | [diff] [blame] | 226 | }; |
| 227 | |
[email protected] | 92ba7b01 | 2012-05-16 03:36:17 | [diff] [blame] | 228 | } // namespace extensions |
| 229 | |
| 230 | #endif // CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_ |