blob: 5eddcd27cac60712c7853fff07dca80375ec9a69 [file] [log] [blame]
[email protected]5f2a4752012-04-27 22:18:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]898bbd32010-05-18 18:52:292// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b7e899142010-06-02 17:03:325// Defines the Chrome Extensions Cookies API functions for accessing internet
[email protected]2fc29622011-12-09 23:00:366// cookies, as specified in the extension API JSON.
[email protected]b7e899142010-06-02 17:03:327
[email protected]92ba7b012012-05-16 03:36:178#ifndef CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_
9#define CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_
[email protected]898bbd32010-05-18 18:52:2910
11#include <string>
12
[email protected]17902752011-08-31 22:52:5413#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
[email protected]ccc91c62012-09-11 01:02:4315#include "base/memory/scoped_ptr.h"
[email protected]1794e6a2013-01-03 02:22:0216#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
[email protected]c54115c2012-12-06 17:56:3017#include "chrome/browser/extensions/event_router.h"
[email protected]898bbd32010-05-18 18:52:2918#include "chrome/browser/extensions/extension_function.h"
[email protected]2c5e1e12010-06-10 13:14:4419#include "chrome/browser/net/chrome_cookie_notification_details.h"
[email protected]960927c02012-07-23 21:38:0820#include "chrome/common/extensions/api/cookies.h"
[email protected]6c2381d2011-10-19 02:52:5321#include "content/public/browser/notification_observer.h"
22#include "content/public/browser/notification_registrar.h"
[email protected]9eaa18e2010-06-29 20:51:0123#include "googleurl/src/gurl.h"
[email protected]8da4b1812012-07-25 13:54:3824#include "net/cookies/canonical_cookie.h"
[email protected]898bbd32010-05-18 18:52:2925
[email protected]abe2c032011-03-31 18:49:3426namespace net {
[email protected]9eaa18e2010-06-29 20:51:0127class URLRequestContextGetter;
[email protected]abe2c032011-03-31 18:49:3428}
[email protected]898bbd32010-05-18 18:52:2929
[email protected]92ba7b012012-05-16 03:36:1730namespace extensions {
31
[email protected]2c5e1e12010-06-10 13:14:4432// Observes CookieMonster notifications and routes them as events to the
33// extension system.
[email protected]c54115c2012-12-06 17:56:3034class CookiesEventRouter : public content::NotificationObserver {
[email protected]2c5e1e12010-06-10 13:14:4435 public:
[email protected]c54115c2012-12-06 17:56:3036 explicit CookiesEventRouter(Profile* profile);
37 virtual ~CookiesEventRouter();
[email protected]2c5e1e12010-06-10 13:14:4438
[email protected]2c5e1e12010-06-10 13:14:4439 private:
[email protected]6c2381d2011-10-19 02:52:5340 // content::NotificationObserver implementation.
[email protected]432115822011-07-10 15:52:2741 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:5342 const content::NotificationSource& source,
43 const content::NotificationDetails& details) OVERRIDE;
[email protected]2c5e1e12010-06-10 13:14:4444
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]ccc91c62012-09-11 01:02:4347 void CookieChanged(Profile* profile, ChromeCookieDetails* details);
[email protected]2c5e1e12010-06-10 13:14:4448
49 // This method dispatches events to the extension message service.
50 void DispatchEvent(Profile* context,
[email protected]ccc91c62012-09-11 01:02:4351 const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:1552 scoped_ptr<base::ListValue> event_args,
[email protected]2c5e1e12010-06-10 13:14:4453 GURL& cookie_domain);
54
55 // Used for tracking registrations to CookieMonster notifications.
[email protected]6c2381d2011-10-19 02:52:5356 content::NotificationRegistrar registrar_;
[email protected]2c5e1e12010-06-10 13:14:4457
[email protected]f825df22011-06-28 17:36:1058 Profile* profile_;
59
[email protected]c54115c2012-12-06 17:56:3060 DISALLOW_COPY_AND_ASSIGN(CookiesEventRouter);
[email protected]2c5e1e12010-06-10 13:14:4461};
62
[email protected]b7e899142010-06-02 17:03:3263// Serves as a base class for all cookies API functions, and defines some
64// common functionality for parsing cookies API function arguments.
[email protected]9eaa18e2010-06-29 20:51:0165// 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.
70class CookiesFunction : public AsyncExtensionFunction {
[email protected]898bbd32010-05-18 18:52:2971 protected:
[email protected]5f2a4752012-04-27 22:18:5872 virtual ~CookiesFunction() {}
73
[email protected]960927c02012-07-23 21:38:0874 // 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]b7e899142010-06-02 17:03:3279 bool check_host_permissions);
[email protected]898bbd32010-05-18 18:52:2980
[email protected]960927c02012-07-23 21:38:0881 // 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]898bbd32010-05-18 18:52:2987};
88
[email protected]00804ca2010-07-16 06:38:5989// Implements the cookies.get() extension function.
[email protected]758b0b702013-01-10 12:19:3390class GetCookieFunction : public CookiesFunction {
[email protected]898bbd32010-05-18 18:52:2991 public:
[email protected]00804ca2010-07-16 06:38:5992 DECLARE_EXTENSION_FUNCTION_NAME("cookies.get")
[email protected]9eaa18e2010-06-29 20:51:0193
[email protected]758b0b702013-01-10 12:19:3394 GetCookieFunction();
[email protected]5f2a4752012-04-27 22:18:5895
96 protected:
[email protected]758b0b702013-01-10 12:19:3397 virtual ~GetCookieFunction();
[email protected]5f2a4752012-04-27 22:18:5898
99 // ExtensionFunction:
100 virtual bool RunImpl() OVERRIDE;
101
[email protected]9eaa18e2010-06-29 20:51:01102 private:
103 void GetCookieOnIOThread();
104 void RespondOnUIThread();
[email protected]b9e48094d2011-07-20 14:27:13105 void GetCookieCallback(const net::CookieList& cookie_list);
[email protected]9eaa18e2010-06-29 20:51:01106
[email protected]9eaa18e2010-06-29 20:51:01107 GURL url_;
[email protected]abe2c032011-03-31 18:49:34108 scoped_refptr<net::URLRequestContextGetter> store_context_;
[email protected]960927c02012-07-23 21:38:08109 scoped_ptr<extensions::api::cookies::Get::Params> parsed_args_;
[email protected]898bbd32010-05-18 18:52:29110};
[email protected]b7e899142010-06-02 17:03:32111
[email protected]00804ca2010-07-16 06:38:59112// Implements the cookies.getAll() extension function.
[email protected]758b0b702013-01-10 12:19:33113class GetAllCookiesFunction : public CookiesFunction {
[email protected]898bbd32010-05-18 18:52:29114 public:
[email protected]00804ca2010-07-16 06:38:59115 DECLARE_EXTENSION_FUNCTION_NAME("cookies.getAll")
[email protected]9eaa18e2010-06-29 20:51:01116
[email protected]758b0b702013-01-10 12:19:33117 GetAllCookiesFunction();
[email protected]5f2a4752012-04-27 22:18:58118
119 protected:
[email protected]758b0b702013-01-10 12:19:33120 virtual ~GetAllCookiesFunction();
[email protected]5f2a4752012-04-27 22:18:58121
122 // ExtensionFunction:
123 virtual bool RunImpl() OVERRIDE;
124
[email protected]9eaa18e2010-06-29 20:51:01125 private:
126 void GetAllCookiesOnIOThread();
127 void RespondOnUIThread();
[email protected]b9e48094d2011-07-20 14:27:13128 void GetAllCookiesCallback(const net::CookieList& cookie_list);
[email protected]9eaa18e2010-06-29 20:51:01129
[email protected]9eaa18e2010-06-29 20:51:01130 GURL url_;
[email protected]abe2c032011-03-31 18:49:34131 scoped_refptr<net::URLRequestContextGetter> store_context_;
[email protected]960927c02012-07-23 21:38:08132 scoped_ptr<extensions::api::cookies::GetAll::Params> parsed_args_;
[email protected]898bbd32010-05-18 18:52:29133};
[email protected]b7e899142010-06-02 17:03:32134
[email protected]00804ca2010-07-16 06:38:59135// Implements the cookies.set() extension function.
[email protected]758b0b702013-01-10 12:19:33136class SetCookieFunction : public CookiesFunction {
[email protected]898bbd32010-05-18 18:52:29137 public:
[email protected]5f2a4752012-04-27 22:18:58138 DECLARE_EXTENSION_FUNCTION_NAME("cookies.set")
139
[email protected]758b0b702013-01-10 12:19:33140 SetCookieFunction();
[email protected]5f2a4752012-04-27 22:18:58141
142 protected:
[email protected]758b0b702013-01-10 12:19:33143 virtual ~SetCookieFunction();
[email protected]17902752011-08-31 22:52:54144 virtual bool RunImpl() OVERRIDE;
[email protected]9eaa18e2010-06-29 20:51:01145
146 private:
147 void SetCookieOnIOThread();
148 void RespondOnUIThread();
[email protected]b9e48094d2011-07-20 14:27:13149 void PullCookie(bool set_cookie_);
150 void PullCookieCallback(const net::CookieList& cookie_list);
[email protected]9eaa18e2010-06-29 20:51:01151
152 GURL url_;
[email protected]9eaa18e2010-06-29 20:51:01153 bool success_;
[email protected]abe2c032011-03-31 18:49:34154 scoped_refptr<net::URLRequestContextGetter> store_context_;
[email protected]960927c02012-07-23 21:38:08155 scoped_ptr<extensions::api::cookies::Set::Params> parsed_args_;
[email protected]898bbd32010-05-18 18:52:29156};
[email protected]b7e899142010-06-02 17:03:32157
[email protected]00804ca2010-07-16 06:38:59158// Implements the cookies.remove() extension function.
[email protected]758b0b702013-01-10 12:19:33159class RemoveCookieFunction : public CookiesFunction {
[email protected]898bbd32010-05-18 18:52:29160 public:
[email protected]00804ca2010-07-16 06:38:59161 DECLARE_EXTENSION_FUNCTION_NAME("cookies.remove")
[email protected]b8290452011-03-21 14:11:14162
[email protected]758b0b702013-01-10 12:19:33163 RemoveCookieFunction();
[email protected]5f2a4752012-04-27 22:18:58164
165 protected:
[email protected]758b0b702013-01-10 12:19:33166 virtual ~RemoveCookieFunction();
[email protected]5f2a4752012-04-27 22:18:58167
168 // ExtensionFunction:
169 virtual bool RunImpl() OVERRIDE;
170
[email protected]b8290452011-03-21 14:11:14171 private:
172 void RemoveCookieOnIOThread();
173 void RespondOnUIThread();
[email protected]b9e48094d2011-07-20 14:27:13174 void RemoveCookieCallback();
[email protected]b8290452011-03-21 14:11:14175
176 GURL url_;
[email protected]abe2c032011-03-31 18:49:34177 scoped_refptr<net::URLRequestContextGetter> store_context_;
[email protected]960927c02012-07-23 21:38:08178 scoped_ptr<extensions::api::cookies::Remove::Params> parsed_args_;
[email protected]898bbd32010-05-18 18:52:29179};
[email protected]b7e899142010-06-02 17:03:32180
[email protected]00804ca2010-07-16 06:38:59181// Implements the cookies.getAllCookieStores() extension function.
[email protected]758b0b702013-01-10 12:19:33182class GetAllCookieStoresFunction : public CookiesFunction {
[email protected]898bbd32010-05-18 18:52:29183 public:
[email protected]5f2a4752012-04-27 22:18:58184 DECLARE_EXTENSION_FUNCTION_NAME("cookies.getAllCookieStores")
185
186 protected:
[email protected]758b0b702013-01-10 12:19:33187 virtual ~GetAllCookieStoresFunction() {}
[email protected]5f2a4752012-04-27 22:18:58188
189 // ExtensionFunction:
[email protected]758b0b702013-01-10 12:19:33190 // GetAllCookieStoresFunction is sync.
[email protected]1cc91fe2011-11-21 14:48:43191 virtual void Run() OVERRIDE;
[email protected]5f2a4752012-04-27 22:18:58192 virtual bool RunImpl() OVERRIDE;
[email protected]898bbd32010-05-18 18:52:29193};
194
[email protected]1794e6a2013-01-03 02:22:02195class CookiesAPI : public ProfileKeyedAPI,
[email protected]c54115c2012-12-06 17:56:30196 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]c39df1a2013-01-08 18:22:07204 // ProfileKeyedAPI implementation.
205 static ProfileKeyedAPIFactory<CookiesAPI>* GetFactoryInstance();
206
[email protected]c54115c2012-12-06 17:56:30207 // EventRouter::Observer implementation.
208 virtual void OnListenerAdded(const extensions::EventListenerInfo& details)
209 OVERRIDE;
210
211 private:
[email protected]1794e6a2013-01-03 02:22:02212 friend class ProfileKeyedAPIFactory<CookiesAPI>;
213
[email protected]c54115c2012-12-06 17:56:30214 Profile* profile_;
215
[email protected]1794e6a2013-01-03 02:22:02216 // ProfileKeyedAPI implementation.
217 static const char* service_name() {
218 return "CookiesAPI";
219 }
220 static const bool kServiceIsNULLWhileTesting = true;
221
[email protected]c54115c2012-12-06 17:56:30222 // Created lazily upon OnListenerAdded.
223 scoped_ptr<CookiesEventRouter> cookies_event_router_;
[email protected]1794e6a2013-01-03 02:22:02224
225 DISALLOW_COPY_AND_ASSIGN(CookiesAPI);
[email protected]c54115c2012-12-06 17:56:30226};
227
[email protected]92ba7b012012-05-16 03:36:17228} // namespace extensions
229
230#endif // CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_