Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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 chrome/common/extensions/api/extension_api.json.
      7 
      8 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_
      9 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_
     10 #pragma once
     11 
     12 #include <string>
     13 
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/singleton.h"
     16 #include "base/time.h"
     17 #include "chrome/browser/extensions/extension_function.h"
     18 #include "chrome/browser/net/chrome_cookie_notification_details.h"
     19 #include "content/common/notification_observer.h"
     20 #include "content/common/notification_registrar.h"
     21 #include "googleurl/src/gurl.h"
     22 #include "net/base/cookie_monster.h"
     23 
     24 class DictionaryValue;
     25 
     26 namespace net {
     27 class URLRequestContextGetter;
     28 }
     29 
     30 // Observes CookieMonster notifications and routes them as events to the
     31 // extension system.
     32 class ExtensionCookiesEventRouter : public NotificationObserver {
     33  public:
     34   // Single instance of the event router.
     35   static ExtensionCookiesEventRouter* GetInstance();
     36 
     37   void Init();
     38 
     39  private:
     40   friend struct DefaultSingletonTraits<ExtensionCookiesEventRouter>;
     41 
     42   ExtensionCookiesEventRouter() {}
     43   virtual ~ExtensionCookiesEventRouter() {}
     44 
     45   // NotificationObserver implementation.
     46   virtual void Observe(NotificationType type,
     47                        const NotificationSource& source,
     48                        const NotificationDetails& details);
     49 
     50   // Handler for the COOKIE_CHANGED event. The method takes the details of such
     51   // an event and constructs a suitable JSON formatted extension event from it.
     52   void CookieChanged(Profile* profile,
     53                      ChromeCookieDetails* details);
     54 
     55   // This method dispatches events to the extension message service.
     56   void DispatchEvent(Profile* context,
     57                      const char* event_name,
     58                      const std::string& json_args,
     59                      GURL& cookie_domain);
     60 
     61   // Used for tracking registrations to CookieMonster notifications.
     62   NotificationRegistrar registrar_;
     63 
     64   DISALLOW_COPY_AND_ASSIGN(ExtensionCookiesEventRouter);
     65 };
     66 
     67 // Serves as a base class for all cookies API functions, and defines some
     68 // common functionality for parsing cookies API function arguments.
     69 // Note that all of the functions in this file derive from
     70 // AsyncExtensionFunction, and are not threadsafe, so they should not be
     71 // concurrently accessed from multiple threads. They modify |result_| and other
     72 // member variables directly.
     73 // See chrome/browser/extensions/extension_function.h for more information.
     74 class CookiesFunction : public AsyncExtensionFunction {
     75  protected:
     76   // Looks for a 'url' value in the given details dictionary and constructs a
     77   // GURL from it. Returns false and assigns the internal error_ value if the
     78   // URL is invalid or isn't found in the dictionary. If check_host_permissions
     79   // is true, the URL is also checked against the extension's host permissions,
     80   // and if there is no permission for the URL, this function returns false.
     81   bool ParseUrl(const DictionaryValue* details, GURL* url,
     82                 bool check_host_permissions);
     83 
     84   // Checks the given details dictionary for a 'storeId' value, and retrieves
     85   // the cookie store context and the store ID associated with it.  If the
     86   // 'storeId' value isn't found in the dictionary, the current execution
     87   // context's cookie store context is retrieved. Returns false on error and
     88   // assigns the internal error_ value if that occurs.
     89   // At least one of the output parameters store and store_id should be
     90   // non-NULL.
     91   bool ParseStoreContext(const DictionaryValue* details,
     92                          net::URLRequestContextGetter** context,
     93                          std::string* store_id);
     94 };
     95 
     96 // Implements the cookies.get() extension function.
     97 class GetCookieFunction : public CookiesFunction {
     98  public:
     99   GetCookieFunction();
    100   virtual ~GetCookieFunction();
    101   virtual bool RunImpl();
    102   DECLARE_EXTENSION_FUNCTION_NAME("cookies.get")
    103 
    104  private:
    105   void GetCookieOnIOThread();
    106   void RespondOnUIThread();
    107 
    108   std::string name_;
    109   GURL url_;
    110   std::string store_id_;
    111   scoped_refptr<net::URLRequestContextGetter> store_context_;
    112 };
    113 
    114 // Implements the cookies.getAll() extension function.
    115 class GetAllCookiesFunction : public CookiesFunction {
    116  public:
    117   GetAllCookiesFunction();
    118   virtual ~GetAllCookiesFunction();
    119   virtual bool RunImpl();
    120   DECLARE_EXTENSION_FUNCTION_NAME("cookies.getAll")
    121 
    122  private:
    123   void GetAllCookiesOnIOThread();
    124   void RespondOnUIThread();
    125 
    126   DictionaryValue* details_;
    127   GURL url_;
    128   std::string store_id_;
    129   scoped_refptr<net::URLRequestContextGetter> store_context_;
    130 };
    131 
    132 // Implements the cookies.set() extension function.
    133 class SetCookieFunction : public CookiesFunction {
    134  public:
    135   SetCookieFunction();
    136   virtual ~SetCookieFunction();
    137   virtual bool RunImpl();
    138   DECLARE_EXTENSION_FUNCTION_NAME("cookies.set")
    139 
    140  private:
    141   void SetCookieOnIOThread();
    142   void RespondOnUIThread();
    143 
    144   GURL url_;
    145   std::string name_;
    146   std::string value_;
    147   std::string domain_;
    148   std::string path_;
    149   bool secure_;
    150   bool http_only_;
    151   base::Time expiration_time_;
    152   bool success_;
    153   std::string store_id_;
    154   scoped_refptr<net::URLRequestContextGetter> store_context_;
    155 };
    156 
    157 // Implements the cookies.remove() extension function.
    158 class RemoveCookieFunction : public CookiesFunction {
    159  public:
    160   RemoveCookieFunction();
    161   virtual ~RemoveCookieFunction();
    162   virtual bool RunImpl();
    163   DECLARE_EXTENSION_FUNCTION_NAME("cookies.remove")
    164 
    165  private:
    166   void RemoveCookieOnIOThread();
    167   void RespondOnUIThread();
    168 
    169   GURL url_;
    170   std::string name_;
    171   bool success_;
    172   std::string store_id_;
    173   scoped_refptr<net::URLRequestContextGetter> store_context_;
    174 };
    175 
    176 // Implements the cookies.getAllCookieStores() extension function.
    177 class GetAllCookieStoresFunction : public CookiesFunction {
    178  public:
    179   virtual bool RunImpl();
    180   // GetAllCookieStoresFunction is sync.
    181   virtual void Run();
    182   DECLARE_EXTENSION_FUNCTION_NAME("cookies.getAllCookieStores")
    183 };
    184 
    185 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_
    186