Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / cookies / cookies_api.h
blob0ee1180324b447625fa1279f55de07803c7ba033
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.
5 // Defines the Chrome Extensions Cookies API functions for accessing internet
6 // cookies, as specified in the extension API JSON.
8 #ifndef CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_
9 #define CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_
11 #include <string>
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/chrome_extension_function.h"
17 #include "chrome/browser/net/chrome_cookie_notification_details.h"
18 #include "chrome/common/extensions/api/cookies.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "extensions/browser/browser_context_keyed_api_factory.h"
22 #include "extensions/browser/event_router.h"
23 #include "net/cookies/canonical_cookie.h"
24 #include "url/gurl.h"
26 namespace net {
27 class URLRequestContextGetter;
30 namespace extensions {
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(content::BrowserContext* context);
37 ~CookiesEventRouter() override;
39 private:
40 // content::NotificationObserver implementation.
41 void Observe(int type,
42 const content::NotificationSource& source,
43 const content::NotificationDetails& details) override;
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);
49 // This method dispatches events to the extension message service.
50 void DispatchEvent(content::BrowserContext* context,
51 events::HistogramValue histogram_value,
52 const std::string& event_name,
53 scoped_ptr<base::ListValue> event_args,
54 GURL& cookie_domain);
56 // Used for tracking registrations to CookieMonster notifications.
57 content::NotificationRegistrar registrar_;
59 Profile* profile_;
61 DISALLOW_COPY_AND_ASSIGN(CookiesEventRouter);
64 // Implements the cookies.get() extension function.
65 class CookiesGetFunction : public ChromeAsyncExtensionFunction {
66 public:
67 DECLARE_EXTENSION_FUNCTION("cookies.get", COOKIES_GET)
69 CookiesGetFunction();
71 protected:
72 ~CookiesGetFunction() override;
74 // ExtensionFunction:
75 bool RunAsync() override;
77 private:
78 void GetCookieOnIOThread();
79 void RespondOnUIThread();
80 void GetCookieCallback(const net::CookieList& cookie_list);
82 GURL url_;
83 scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
84 scoped_ptr<api::cookies::Get::Params> parsed_args_;
87 // Implements the cookies.getAll() extension function.
88 class CookiesGetAllFunction : public ChromeAsyncExtensionFunction {
89 public:
90 DECLARE_EXTENSION_FUNCTION("cookies.getAll", COOKIES_GETALL)
92 CookiesGetAllFunction();
94 protected:
95 ~CookiesGetAllFunction() override;
97 // ExtensionFunction:
98 bool RunAsync() override;
100 private:
101 void GetAllCookiesOnIOThread();
102 void RespondOnUIThread();
103 void GetAllCookiesCallback(const net::CookieList& cookie_list);
105 GURL url_;
106 scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
107 scoped_ptr<api::cookies::GetAll::Params> parsed_args_;
110 // Implements the cookies.set() extension function.
111 class CookiesSetFunction : public ChromeAsyncExtensionFunction {
112 public:
113 DECLARE_EXTENSION_FUNCTION("cookies.set", COOKIES_SET)
115 CookiesSetFunction();
117 protected:
118 ~CookiesSetFunction() override;
119 bool RunAsync() override;
121 private:
122 void SetCookieOnIOThread();
123 void RespondOnUIThread();
124 void PullCookie(bool set_cookie_);
125 void PullCookieCallback(const net::CookieList& cookie_list);
127 GURL url_;
128 bool success_;
129 scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
130 scoped_ptr<api::cookies::Set::Params> parsed_args_;
133 // Implements the cookies.remove() extension function.
134 class CookiesRemoveFunction : public ChromeAsyncExtensionFunction {
135 public:
136 DECLARE_EXTENSION_FUNCTION("cookies.remove", COOKIES_REMOVE)
138 CookiesRemoveFunction();
140 protected:
141 ~CookiesRemoveFunction() override;
143 // ExtensionFunction:
144 bool RunAsync() override;
146 private:
147 void RemoveCookieOnIOThread();
148 void RespondOnUIThread();
149 void RemoveCookieCallback();
151 GURL url_;
152 scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
153 scoped_ptr<api::cookies::Remove::Params> parsed_args_;
156 // Implements the cookies.getAllCookieStores() extension function.
157 class CookiesGetAllCookieStoresFunction : public ChromeSyncExtensionFunction {
158 public:
159 DECLARE_EXTENSION_FUNCTION("cookies.getAllCookieStores",
160 COOKIES_GETALLCOOKIESTORES)
162 protected:
163 ~CookiesGetAllCookieStoresFunction() override {}
165 // ExtensionFunction:
166 bool RunSync() override;
169 class CookiesAPI : public BrowserContextKeyedAPI, public EventRouter::Observer {
170 public:
171 explicit CookiesAPI(content::BrowserContext* context);
172 ~CookiesAPI() override;
174 // KeyedService implementation.
175 void Shutdown() override;
177 // BrowserContextKeyedAPI implementation.
178 static BrowserContextKeyedAPIFactory<CookiesAPI>* GetFactoryInstance();
180 // EventRouter::Observer implementation.
181 void OnListenerAdded(const EventListenerInfo& details) override;
183 private:
184 friend class BrowserContextKeyedAPIFactory<CookiesAPI>;
186 content::BrowserContext* browser_context_;
188 // BrowserContextKeyedAPI implementation.
189 static const char* service_name() {
190 return "CookiesAPI";
192 static const bool kServiceIsNULLWhileTesting = true;
194 // Created lazily upon OnListenerAdded.
195 scoped_ptr<CookiesEventRouter> cookies_event_router_;
197 DISALLOW_COPY_AND_ASSIGN(CookiesAPI);
200 } // namespace extensions
202 #endif // CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_