/chrome/browser/extensions/api/cookies/cookies_api.h

https://gitlab.com/jonnialva90/iridium-browser · C Header · 202 lines · 126 code · 50 blank · 26 comment · 0 complexity · 9478b5b07e853808372ca934f101e0c1 MD5 · raw file

  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.
  4. // Defines the Chrome Extensions Cookies API functions for accessing internet
  5. // cookies, as specified in the extension API JSON.
  6. #ifndef CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_
  7. #define CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_
  8. #include <string>
  9. #include "base/compiler_specific.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/scoped_ptr.h"
  12. #include "chrome/browser/extensions/chrome_extension_function.h"
  13. #include "chrome/browser/net/chrome_cookie_notification_details.h"
  14. #include "chrome/common/extensions/api/cookies.h"
  15. #include "content/public/browser/notification_observer.h"
  16. #include "content/public/browser/notification_registrar.h"
  17. #include "extensions/browser/browser_context_keyed_api_factory.h"
  18. #include "extensions/browser/event_router.h"
  19. #include "net/cookies/canonical_cookie.h"
  20. #include "url/gurl.h"
  21. namespace net {
  22. class URLRequestContextGetter;
  23. }
  24. namespace extensions {
  25. // Observes CookieMonster notifications and routes them as events to the
  26. // extension system.
  27. class CookiesEventRouter : public content::NotificationObserver {
  28. public:
  29. explicit CookiesEventRouter(content::BrowserContext* context);
  30. ~CookiesEventRouter() override;
  31. private:
  32. // content::NotificationObserver implementation.
  33. void Observe(int type,
  34. const content::NotificationSource& source,
  35. const content::NotificationDetails& details) override;
  36. // Handler for the COOKIE_CHANGED event. The method takes the details of such
  37. // an event and constructs a suitable JSON formatted extension event from it.
  38. void CookieChanged(Profile* profile, ChromeCookieDetails* details);
  39. // This method dispatches events to the extension message service.
  40. void DispatchEvent(content::BrowserContext* context,
  41. events::HistogramValue histogram_value,
  42. const std::string& event_name,
  43. scoped_ptr<base::ListValue> event_args,
  44. GURL& cookie_domain);
  45. // Used for tracking registrations to CookieMonster notifications.
  46. content::NotificationRegistrar registrar_;
  47. Profile* profile_;
  48. DISALLOW_COPY_AND_ASSIGN(CookiesEventRouter);
  49. };
  50. // Implements the cookies.get() extension function.
  51. class CookiesGetFunction : public ChromeAsyncExtensionFunction {
  52. public:
  53. DECLARE_EXTENSION_FUNCTION("cookies.get", COOKIES_GET)
  54. CookiesGetFunction();
  55. protected:
  56. ~CookiesGetFunction() override;
  57. // ExtensionFunction:
  58. bool RunAsync() override;
  59. private:
  60. void GetCookieOnIOThread();
  61. void RespondOnUIThread();
  62. void GetCookieCallback(const net::CookieList& cookie_list);
  63. GURL url_;
  64. scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
  65. scoped_ptr<api::cookies::Get::Params> parsed_args_;
  66. };
  67. // Implements the cookies.getAll() extension function.
  68. class CookiesGetAllFunction : public ChromeAsyncExtensionFunction {
  69. public:
  70. DECLARE_EXTENSION_FUNCTION("cookies.getAll", COOKIES_GETALL)
  71. CookiesGetAllFunction();
  72. protected:
  73. ~CookiesGetAllFunction() override;
  74. // ExtensionFunction:
  75. bool RunAsync() override;
  76. private:
  77. void GetAllCookiesOnIOThread();
  78. void RespondOnUIThread();
  79. void GetAllCookiesCallback(const net::CookieList& cookie_list);
  80. GURL url_;
  81. scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
  82. scoped_ptr<api::cookies::GetAll::Params> parsed_args_;
  83. };
  84. // Implements the cookies.set() extension function.
  85. class CookiesSetFunction : public ChromeAsyncExtensionFunction {
  86. public:
  87. DECLARE_EXTENSION_FUNCTION("cookies.set", COOKIES_SET)
  88. CookiesSetFunction();
  89. protected:
  90. ~CookiesSetFunction() override;
  91. bool RunAsync() override;
  92. private:
  93. void SetCookieOnIOThread();
  94. void RespondOnUIThread();
  95. void PullCookie(bool set_cookie_);
  96. void PullCookieCallback(const net::CookieList& cookie_list);
  97. GURL url_;
  98. bool success_;
  99. scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
  100. scoped_ptr<api::cookies::Set::Params> parsed_args_;
  101. };
  102. // Implements the cookies.remove() extension function.
  103. class CookiesRemoveFunction : public ChromeAsyncExtensionFunction {
  104. public:
  105. DECLARE_EXTENSION_FUNCTION("cookies.remove", COOKIES_REMOVE)
  106. CookiesRemoveFunction();
  107. protected:
  108. ~CookiesRemoveFunction() override;
  109. // ExtensionFunction:
  110. bool RunAsync() override;
  111. private:
  112. void RemoveCookieOnIOThread();
  113. void RespondOnUIThread();
  114. void RemoveCookieCallback();
  115. GURL url_;
  116. scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
  117. scoped_ptr<api::cookies::Remove::Params> parsed_args_;
  118. };
  119. // Implements the cookies.getAllCookieStores() extension function.
  120. class CookiesGetAllCookieStoresFunction : public ChromeSyncExtensionFunction {
  121. public:
  122. DECLARE_EXTENSION_FUNCTION("cookies.getAllCookieStores",
  123. COOKIES_GETALLCOOKIESTORES)
  124. protected:
  125. ~CookiesGetAllCookieStoresFunction() override {}
  126. // ExtensionFunction:
  127. bool RunSync() override;
  128. };
  129. class CookiesAPI : public BrowserContextKeyedAPI, public EventRouter::Observer {
  130. public:
  131. explicit CookiesAPI(content::BrowserContext* context);
  132. ~CookiesAPI() override;
  133. // KeyedService implementation.
  134. void Shutdown() override;
  135. // BrowserContextKeyedAPI implementation.
  136. static BrowserContextKeyedAPIFactory<CookiesAPI>* GetFactoryInstance();
  137. // EventRouter::Observer implementation.
  138. void OnListenerAdded(const EventListenerInfo& details) override;
  139. private:
  140. friend class BrowserContextKeyedAPIFactory<CookiesAPI>;
  141. content::BrowserContext* browser_context_;
  142. // BrowserContextKeyedAPI implementation.
  143. static const char* service_name() {
  144. return "CookiesAPI";
  145. }
  146. static const bool kServiceIsNULLWhileTesting = true;
  147. // Created lazily upon OnListenerAdded.
  148. scoped_ptr<CookiesEventRouter> cookies_event_router_;
  149. DISALLOW_COPY_AND_ASSIGN(CookiesAPI);
  150. };
  151. } // namespace extensions
  152. #endif // CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_API_H_