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