Enable Enterprise enrollment on desktop builds.
[chromium-blink-merge.git] / chrome / browser / extensions / api / cookies / cookies_api.h
blob1389ffcda6f749ec95f47f7c40fbb4241ece25d8
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 virtual ~CookiesEventRouter();
39 private:
40 // content::NotificationObserver implementation.
41 virtual 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 const std::string& event_name,
52 scoped_ptr<base::ListValue> event_args,
53 GURL& cookie_domain);
55 // Used for tracking registrations to CookieMonster notifications.
56 content::NotificationRegistrar registrar_;
58 Profile* profile_;
60 DISALLOW_COPY_AND_ASSIGN(CookiesEventRouter);
63 // Implements the cookies.get() extension function.
64 class CookiesGetFunction : public ChromeAsyncExtensionFunction {
65 public:
66 DECLARE_EXTENSION_FUNCTION("cookies.get", COOKIES_GET)
68 CookiesGetFunction();
70 protected:
71 virtual ~CookiesGetFunction();
73 // ExtensionFunction:
74 virtual bool RunImpl() OVERRIDE;
76 private:
77 void GetCookieOnIOThread();
78 void RespondOnUIThread();
79 void GetCookieCallback(const net::CookieList& cookie_list);
81 GURL url_;
82 scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
83 scoped_ptr<extensions::api::cookies::Get::Params> parsed_args_;
86 // Implements the cookies.getAll() extension function.
87 class CookiesGetAllFunction : public ChromeAsyncExtensionFunction {
88 public:
89 DECLARE_EXTENSION_FUNCTION("cookies.getAll", COOKIES_GETALL)
91 CookiesGetAllFunction();
93 protected:
94 virtual ~CookiesGetAllFunction();
96 // ExtensionFunction:
97 virtual bool RunImpl() OVERRIDE;
99 private:
100 void GetAllCookiesOnIOThread();
101 void RespondOnUIThread();
102 void GetAllCookiesCallback(const net::CookieList& cookie_list);
104 GURL url_;
105 scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
106 scoped_ptr<extensions::api::cookies::GetAll::Params> parsed_args_;
109 // Implements the cookies.set() extension function.
110 class CookiesSetFunction : public ChromeAsyncExtensionFunction {
111 public:
112 DECLARE_EXTENSION_FUNCTION("cookies.set", COOKIES_SET)
114 CookiesSetFunction();
116 protected:
117 virtual ~CookiesSetFunction();
118 virtual bool RunImpl() OVERRIDE;
120 private:
121 void SetCookieOnIOThread();
122 void RespondOnUIThread();
123 void PullCookie(bool set_cookie_);
124 void PullCookieCallback(const net::CookieList& cookie_list);
126 GURL url_;
127 bool success_;
128 scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
129 scoped_ptr<extensions::api::cookies::Set::Params> parsed_args_;
132 // Implements the cookies.remove() extension function.
133 class CookiesRemoveFunction : public ChromeAsyncExtensionFunction {
134 public:
135 DECLARE_EXTENSION_FUNCTION("cookies.remove", COOKIES_REMOVE)
137 CookiesRemoveFunction();
139 protected:
140 virtual ~CookiesRemoveFunction();
142 // ExtensionFunction:
143 virtual bool RunImpl() OVERRIDE;
145 private:
146 void RemoveCookieOnIOThread();
147 void RespondOnUIThread();
148 void RemoveCookieCallback();
150 GURL url_;
151 scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
152 scoped_ptr<extensions::api::cookies::Remove::Params> parsed_args_;
155 // Implements the cookies.getAllCookieStores() extension function.
156 class CookiesGetAllCookieStoresFunction : public ChromeSyncExtensionFunction {
157 public:
158 DECLARE_EXTENSION_FUNCTION("cookies.getAllCookieStores",
159 COOKIES_GETALLCOOKIESTORES)
161 protected:
162 virtual ~CookiesGetAllCookieStoresFunction() {}
164 // ExtensionFunction:
165 virtual bool RunSync() OVERRIDE;
168 class CookiesAPI : public BrowserContextKeyedAPI,
169 public extensions::EventRouter::Observer {
170 public:
171 explicit CookiesAPI(content::BrowserContext* context);
172 virtual ~CookiesAPI();
174 // KeyedService implementation.
175 virtual void Shutdown() OVERRIDE;
177 // BrowserContextKeyedAPI implementation.
178 static BrowserContextKeyedAPIFactory<CookiesAPI>* GetFactoryInstance();
180 // EventRouter::Observer implementation.
181 virtual void OnListenerAdded(const extensions::EventListenerInfo& details)
182 OVERRIDE;
184 private:
185 friend class BrowserContextKeyedAPIFactory<CookiesAPI>;
187 content::BrowserContext* browser_context_;
189 // BrowserContextKeyedAPI implementation.
190 static const char* service_name() {
191 return "CookiesAPI";
193 static const bool kServiceIsNULLWhileTesting = true;
195 // Created lazily upon OnListenerAdded.
196 scoped_ptr<CookiesEventRouter> cookies_event_router_;
198 DISALLOW_COPY_AND_ASSIGN(CookiesAPI);
201 } // namespace extensions
203 #endif // CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_