summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_cookies_api.h
blob: 50b922b4da6cf9c4e2cd3df9b30f2bd420d5de11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Defines the Chrome Extensions Cookies API functions for accessing internet
// cookies, as specified in chrome/common/extensions/api/extension_api.json.

#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_
#pragma once

#include <string>

#include "base/ref_counted.h"
#include "base/singleton.h"
#include "base/time.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/browser/net/chrome_cookie_notification_details.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"
#include "googleurl/src/gurl.h"
#include "net/base/cookie_monster.h"

class DictionaryValue;
class URLRequestContextGetter;

// Observes CookieMonster notifications and routes them as events to the
// extension system.
class ExtensionCookiesEventRouter : public NotificationObserver {
 public:
  // Single instance of the event router.
  static ExtensionCookiesEventRouter* GetInstance();

  void Init();

 private:
  friend struct DefaultSingletonTraits<ExtensionCookiesEventRouter>;

  ExtensionCookiesEventRouter() {}
  virtual ~ExtensionCookiesEventRouter() {}

  // NotificationObserver implementation.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

  // Handler for the COOKIE_CHANGED event. The method takes the details of such
  // an event and constructs a suitable JSON formatted extension event from it.
  void CookieChanged(Profile* profile,
                     ChromeCookieDetails* details);

  // This method dispatches events to the extension message service.
  void DispatchEvent(Profile* context,
                     const char* event_name,
                     const std::string& json_args,
                     GURL& cookie_domain);

  // Used for tracking registrations to CookieMonster notifications.
  NotificationRegistrar registrar_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionCookiesEventRouter);
};

// Serves as a base class for all cookies API functions, and defines some
// common functionality for parsing cookies API function arguments.
// Note that all of the functions in this file derive from
// AsyncExtensionFunction, and are not threadsafe, so they should not be
// concurrently accessed from multiple threads. They modify |result_| and other
// member variables directly.
// See chrome/browser/extensions/extension_function.h for more information.
class CookiesFunction : public AsyncExtensionFunction {
 protected:
  // Looks for a 'url' value in the given details dictionary and constructs a
  // GURL from it. Returns false and assigns the internal error_ value if the
  // URL is invalid or isn't found in the dictionary. If check_host_permissions
  // is true, the URL is also checked against the extension's host permissions,
  // and if there is no permission for the URL, this function returns false.
  bool ParseUrl(const DictionaryValue* details, GURL* url,
                bool check_host_permissions);

  // Checks the given details dictionary for a 'storeId' value, and retrieves
  // the cookie store context and the store ID associated with it.  If the
  // 'storeId' value isn't found in the dictionary, the current execution
  // context's cookie store context is retrieved. Returns false on error and
  // assigns the internal error_ value if that occurs.
  // At least one of the output parameters store and store_id should be
  // non-NULL.
  bool ParseStoreContext(const DictionaryValue* details,
                         URLRequestContextGetter** context,
                         std::string* store_id);
};

// Implements the cookies.get() extension function.
class GetCookieFunction : public CookiesFunction {
 public:
  GetCookieFunction();
  ~GetCookieFunction();
  virtual bool RunImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("cookies.get")

 private:
  void GetCookieOnIOThread();
  void RespondOnUIThread();

  std::string name_;
  GURL url_;
  std::string store_id_;
  scoped_refptr<URLRequestContextGetter> store_context_;
  net::CookieMonster::CookieList cookie_list_;
};

// Implements the cookies.getAll() extension function.
class GetAllCookiesFunction : public CookiesFunction {
 public:
  GetAllCookiesFunction();
  ~GetAllCookiesFunction();
  virtual bool RunImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("cookies.getAll")

 private:
  void GetAllCookiesOnIOThread();
  void RespondOnUIThread();

  DictionaryValue* details_;
  GURL url_;
  std::string store_id_;
  scoped_refptr<URLRequestContextGetter> store_context_;
  net::CookieMonster::CookieList cookie_list_;
};

// Implements the cookies.set() extension function.
class SetCookieFunction : public CookiesFunction {
 public:
  SetCookieFunction();
  ~SetCookieFunction();
  virtual bool RunImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("cookies.set")

 private:
  void SetCookieOnIOThread();
  void RespondOnUIThread();

  GURL url_;
  std::string name_;
  std::string value_;
  std::string domain_;
  std::string path_;
  bool secure_;
  bool http_only_;
  base::Time expiration_time_;
  bool success_;
  scoped_refptr<URLRequestContextGetter> store_context_;
};

// Implements the cookies.remove() extension function.
class RemoveCookieFunction : public CookiesFunction {
 public:
  virtual bool RunImpl();
  // RemoveCookieFunction is sync.
  virtual void Run();
  DECLARE_EXTENSION_FUNCTION_NAME("cookies.remove")
};

// Implements the cookies.getAllCookieStores() extension function.
class GetAllCookieStoresFunction : public CookiesFunction {
 public:
  virtual bool RunImpl();
  // GetAllCookieStoresFunction is sync.
  virtual void Run();
  DECLARE_EXTENSION_FUNCTION_NAME("cookies.getAllCookieStores")
};

#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_