Home | History | Annotate | Download | only in cookies
      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
      3 // found in the LICENSE file.
      4 
      5 // Defines the Chrome Extensions Cookies API functions for accessing internet
      6 // cookies, as specified in the extension API JSON.
      7 
      8 #ifndef CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_
      9 #define CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_
     10 
     11 #include <string>
     12 
     13 #include "base/compiler_specific.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
     17 #include "chrome/browser/extensions/event_router.h"
     18 #include "chrome/browser/extensions/extension_function.h"
     19 #include "chrome/browser/net/chrome_cookie_notification_details.h"
     20 #include "chrome/common/extensions/api/cookies.h"
     21 #include "content/public/browser/notification_observer.h"
     22 #include "content/public/browser/notification_registrar.h"
     23 #include "net/cookies/canonical_cookie.h"
     24 #include "url/gurl.h"
     25 
     26 namespace net {
     27 class URLRequestContextGetter;
     28 }
     29 
     30 namespace extensions {
     31 
     32 // Observes CookieMonster notifications and routes them as events to the
     33 // extension system.
     34 class CookiesEventRouter : public content::NotificationObserver {
     35  public:
     36   explicit CookiesEventRouter(Profile* profile);
     37   virtual ~CookiesEventRouter();
     38 
     39  private:
     40   // content::NotificationObserver implementation.
     41   virtual void Observe(int type,
     42                        const content::NotificationSource& source,
     43                        const content::NotificationDetails& details) OVERRIDE;
     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.
     47   void CookieChanged(Profile* profile, ChromeCookieDetails* details);
     48 
     49   // This method dispatches events to the extension message service.
     50   void DispatchEvent(Profile* context,
     51                      const std::string& event_name,
     52                      scoped_ptr<base::ListValue> event_args,
     53                      GURL& cookie_domain);
     54 
     55   // Used for tracking registrations to CookieMonster notifications.
     56   content::NotificationRegistrar registrar_;
     57 
     58   Profile* profile_;
     59 
     60   DISALLOW_COPY_AND_ASSIGN(CookiesEventRouter);
     61 };
     62 
     63 // Serves as a base class for all cookies API functions, and defines some
     64 // common functionality for parsing cookies API function arguments.
     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 {
     71  protected:
     72   virtual ~CookiesFunction() {}
     73 
     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,
     79                 bool check_host_permissions);
     80 
     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);
     87 };
     88 
     89 // Implements the cookies.get() extension function.
     90 class CookiesGetFunction : public CookiesFunction {
     91  public:
     92   DECLARE_EXTENSION_FUNCTION("cookies.get", COOKIES_GET)
     93 
     94   CookiesGetFunction();
     95 
     96  protected:
     97   virtual ~CookiesGetFunction();
     98 
     99   // ExtensionFunction:
    100   virtual bool RunImpl() OVERRIDE;
    101 
    102  private:
    103   void GetCookieOnIOThread();
    104   void RespondOnUIThread();
    105   void GetCookieCallback(const net::CookieList& cookie_list);
    106 
    107   GURL url_;
    108   scoped_refptr<net::URLRequestContextGetter> store_context_;
    109   scoped_ptr<extensions::api::cookies::Get::Params> parsed_args_;
    110 };
    111 
    112 // Implements the cookies.getAll() extension function.
    113 class CookiesGetAllFunction : public CookiesFunction {
    114  public:
    115   DECLARE_EXTENSION_FUNCTION("cookies.getAll", COOKIES_GETALL)
    116 
    117   CookiesGetAllFunction();
    118 
    119  protected:
    120   virtual ~CookiesGetAllFunction();
    121 
    122   // ExtensionFunction:
    123   virtual bool RunImpl() OVERRIDE;
    124 
    125  private:
    126   void GetAllCookiesOnIOThread();
    127   void RespondOnUIThread();
    128   void GetAllCookiesCallback(const net::CookieList& cookie_list);
    129 
    130   GURL url_;
    131   scoped_refptr<net::URLRequestContextGetter> store_context_;
    132   scoped_ptr<extensions::api::cookies::GetAll::Params> parsed_args_;
    133 };
    134 
    135 // Implements the cookies.set() extension function.
    136 class CookiesSetFunction : public CookiesFunction {
    137  public:
    138   DECLARE_EXTENSION_FUNCTION("cookies.set", COOKIES_SET)
    139 
    140   CookiesSetFunction();
    141 
    142  protected:
    143   virtual ~CookiesSetFunction();
    144   virtual bool RunImpl() OVERRIDE;
    145 
    146  private:
    147   void SetCookieOnIOThread();
    148   void RespondOnUIThread();
    149   void PullCookie(bool set_cookie_);
    150   void PullCookieCallback(const net::CookieList& cookie_list);
    151 
    152   GURL url_;
    153   bool success_;
    154   scoped_refptr<net::URLRequestContextGetter> store_context_;
    155   scoped_ptr<extensions::api::cookies::Set::Params> parsed_args_;
    156 };
    157 
    158 // Implements the cookies.remove() extension function.
    159 class CookiesRemoveFunction : public CookiesFunction {
    160  public:
    161   DECLARE_EXTENSION_FUNCTION("cookies.remove", COOKIES_REMOVE)
    162 
    163   CookiesRemoveFunction();
    164 
    165  protected:
    166   virtual ~CookiesRemoveFunction();
    167 
    168   // ExtensionFunction:
    169   virtual bool RunImpl() OVERRIDE;
    170 
    171  private:
    172   void RemoveCookieOnIOThread();
    173   void RespondOnUIThread();
    174   void RemoveCookieCallback();
    175 
    176   GURL url_;
    177   scoped_refptr<net::URLRequestContextGetter> store_context_;
    178   scoped_ptr<extensions::api::cookies::Remove::Params> parsed_args_;
    179 };
    180 
    181 // Implements the cookies.getAllCookieStores() extension function.
    182 class CookiesGetAllCookieStoresFunction : public CookiesFunction {
    183  public:
    184   DECLARE_EXTENSION_FUNCTION("cookies.getAllCookieStores",
    185                              COOKIES_GETALLCOOKIESTORES)
    186 
    187  protected:
    188   virtual ~CookiesGetAllCookieStoresFunction() {}
    189 
    190   // ExtensionFunction:
    191   // CookiesGetAllCookieStoresFunction is sync.
    192   virtual void Run() OVERRIDE;
    193   virtual bool RunImpl() OVERRIDE;
    194 };
    195 
    196 class CookiesAPI : public ProfileKeyedAPI,
    197                    public extensions::EventRouter::Observer {
    198  public:
    199   explicit CookiesAPI(Profile* profile);
    200   virtual ~CookiesAPI();
    201 
    202   // BrowserContextKeyedService implementation.
    203   virtual void Shutdown() OVERRIDE;
    204 
    205   // ProfileKeyedAPI implementation.
    206   static ProfileKeyedAPIFactory<CookiesAPI>* GetFactoryInstance();
    207 
    208   // EventRouter::Observer implementation.
    209   virtual void OnListenerAdded(const extensions::EventListenerInfo& details)
    210       OVERRIDE;
    211 
    212  private:
    213   friend class ProfileKeyedAPIFactory<CookiesAPI>;
    214 
    215   Profile* profile_;
    216 
    217   // ProfileKeyedAPI implementation.
    218   static const char* service_name() {
    219     return "CookiesAPI";
    220   }
    221   static const bool kServiceIsNULLWhileTesting = true;
    222 
    223   // Created lazily upon OnListenerAdded.
    224   scoped_ptr<CookiesEventRouter> cookies_event_router_;
    225 
    226   DISALLOW_COPY_AND_ASSIGN(CookiesAPI);
    227 };
    228 
    229 }  // namespace extensions
    230 
    231 #endif  // CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_
    232