OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Defines the Chrome Extensions Cookies API functions for accessing internet | 5 // Defines the Chrome Extensions Cookies API functions for accessing internet |
6 // cookies, as specified in chrome/common/extensions/api/extension_api.json. | 6 // cookies, as specified in chrome/common/extensions/api/extension_api.json. |
7 | 7 |
8 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_ | 8 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_ |
9 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_ | 9 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_ |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
| 13 #include "base/ref_counted.h" |
13 #include "base/singleton.h" | 14 #include "base/singleton.h" |
| 15 #include "base/time.h" |
14 #include "chrome/browser/extensions/extension_function.h" | 16 #include "chrome/browser/extensions/extension_function.h" |
15 #include "chrome/browser/net/chrome_cookie_notification_details.h" | 17 #include "chrome/browser/net/chrome_cookie_notification_details.h" |
16 #include "chrome/common/notification_registrar.h" | 18 #include "chrome/common/notification_registrar.h" |
| 19 #include "googleurl/src/gurl.h" |
| 20 #include "net/base/cookie_monster.h" |
17 | 21 |
18 namespace net { | 22 class URLRequestContextGetter; |
19 class CookieStore; | |
20 } // namespace net | |
21 | 23 |
22 // Observes CookieMonster notifications and routes them as events to the | 24 // Observes CookieMonster notifications and routes them as events to the |
23 // extension system. | 25 // extension system. |
24 class ExtensionCookiesEventRouter : public NotificationObserver { | 26 class ExtensionCookiesEventRouter : public NotificationObserver { |
25 public: | 27 public: |
26 // Single instance of the event router. | 28 // Single instance of the event router. |
27 static ExtensionCookiesEventRouter* GetInstance(); | 29 static ExtensionCookiesEventRouter* GetInstance(); |
28 | 30 |
29 void Init(); | 31 void Init(); |
30 | 32 |
(...skipping 20 matching lines...) Expand all Loading... |
51 GURL& cookie_domain); | 53 GURL& cookie_domain); |
52 | 54 |
53 // Used for tracking registrations to CookieMonster notifications. | 55 // Used for tracking registrations to CookieMonster notifications. |
54 NotificationRegistrar registrar_; | 56 NotificationRegistrar registrar_; |
55 | 57 |
56 DISALLOW_COPY_AND_ASSIGN(ExtensionCookiesEventRouter); | 58 DISALLOW_COPY_AND_ASSIGN(ExtensionCookiesEventRouter); |
57 }; | 59 }; |
58 | 60 |
59 // Serves as a base class for all cookies API functions, and defines some | 61 // Serves as a base class for all cookies API functions, and defines some |
60 // common functionality for parsing cookies API function arguments. | 62 // common functionality for parsing cookies API function arguments. |
61 // Note that all of the functions in this file derive from ExtensionFunction, | 63 // Note that all of the functions in this file derive from |
62 // and are not threadsafe. They modify the result_ member variable directly; | 64 // AsyncExtensionFunction, and are not threadsafe, so they should not be |
63 // see chrome/browser/extensions/extension_function.h for more information. | 65 // concurrently accessed from multiple threads. They modify |result_| and other |
64 class CookiesFunction : public SyncExtensionFunction { | 66 // member variables directly. |
| 67 // See chrome/browser/extensions/extension_function.h for more information. |
| 68 class CookiesFunction : public AsyncExtensionFunction { |
65 protected: | 69 protected: |
66 // Looks for a 'url' value in the given details dictionary and constructs a | 70 // Looks for a 'url' value in the given details dictionary and constructs a |
67 // GURL from it. Returns false and assigns the internal error_ value if the | 71 // GURL from it. Returns false and assigns the internal error_ value if the |
68 // URL is invalid or isn't found in the dictionary. If check_host_permissions | 72 // URL is invalid or isn't found in the dictionary. If check_host_permissions |
69 // is true, the URL is also checked against the extension's host permissions, | 73 // is true, the URL is also checked against the extension's host permissions, |
70 // and if there is no permission for the URL, this function returns false. | 74 // and if there is no permission for the URL, this function returns false. |
71 bool ParseUrl(const DictionaryValue* details, GURL* url, | 75 bool ParseUrl(const DictionaryValue* details, GURL* url, |
72 bool check_host_permissions); | 76 bool check_host_permissions); |
73 | 77 |
74 // Checks the given details dictionary for a 'storeId' value, and retrieves | 78 // Checks the given details dictionary for a 'storeId' value, and retrieves |
75 // the cookie store and the store ID associated with it. If the 'storeId' | 79 // the cookie store context and the store ID associated with it. If the |
76 // value isn't found in the dictionary, the current execution context's | 80 // 'storeId' value isn't found in the dictionary, the current execution |
77 // cookie store is retrieved. Returns false on error and assigns the | 81 // context's cookie store context is retrieved. Returns false on error and |
78 // internal error_ value if that occurs. | 82 // assigns the internal error_ value if that occurs. |
79 // At least one of the output parameters store and store_id should be | 83 // At least one of the output parameters store and store_id should be |
80 // non-null. | 84 // non-NULL. |
81 bool ParseCookieStore(const DictionaryValue* details, | 85 bool ParseStoreContext(const DictionaryValue* details, |
82 net::CookieStore** store, std::string* store_id); | 86 URLRequestContextGetter** context, |
| 87 std::string* store_id); |
83 }; | 88 }; |
84 | 89 |
85 // Implements the experimental.cookies.get() extension function. | 90 // Implements the experimental.cookies.get() extension function. |
86 class GetCookieFunction : public CookiesFunction { | 91 class GetCookieFunction : public CookiesFunction { |
87 public: | 92 public: |
| 93 GetCookieFunction(); |
88 virtual bool RunImpl(); | 94 virtual bool RunImpl(); |
89 DECLARE_EXTENSION_FUNCTION_NAME("experimental.cookies.get") | 95 DECLARE_EXTENSION_FUNCTION_NAME("experimental.cookies.get") |
| 96 |
| 97 private: |
| 98 void GetCookieOnIOThread(); |
| 99 void RespondOnUIThread(); |
| 100 |
| 101 std::string name_; |
| 102 GURL url_; |
| 103 std::string store_id_; |
| 104 scoped_refptr<URLRequestContextGetter> store_context_; |
| 105 net::CookieMonster::CookieList cookie_list_; |
90 }; | 106 }; |
91 | 107 |
92 // Implements the experimental.cookies.getAll() extension function. | 108 // Implements the experimental.cookies.getAll() extension function. |
93 class GetAllCookiesFunction : public CookiesFunction { | 109 class GetAllCookiesFunction : public CookiesFunction { |
94 public: | 110 public: |
| 111 GetAllCookiesFunction(); |
95 virtual bool RunImpl(); | 112 virtual bool RunImpl(); |
96 DECLARE_EXTENSION_FUNCTION_NAME("experimental.cookies.getAll") | 113 DECLARE_EXTENSION_FUNCTION_NAME("experimental.cookies.getAll") |
| 114 |
| 115 private: |
| 116 void GetAllCookiesOnIOThread(); |
| 117 void RespondOnUIThread(); |
| 118 |
| 119 DictionaryValue* details_; |
| 120 GURL url_; |
| 121 std::string store_id_; |
| 122 scoped_refptr<URLRequestContextGetter> store_context_; |
| 123 net::CookieMonster::CookieList cookie_list_; |
97 }; | 124 }; |
98 | 125 |
99 // Implements the experimental.cookies.set() extension function. | 126 // Implements the experimental.cookies.set() extension function. |
100 class SetCookieFunction : public CookiesFunction { | 127 class SetCookieFunction : public CookiesFunction { |
101 public: | 128 public: |
| 129 SetCookieFunction(); |
102 virtual bool RunImpl(); | 130 virtual bool RunImpl(); |
103 DECLARE_EXTENSION_FUNCTION_NAME("experimental.cookies.set") | 131 DECLARE_EXTENSION_FUNCTION_NAME("experimental.cookies.set") |
| 132 |
| 133 private: |
| 134 void SetCookieOnIOThread(); |
| 135 void RespondOnUIThread(); |
| 136 |
| 137 GURL url_; |
| 138 std::string name_; |
| 139 std::string value_; |
| 140 std::string domain_; |
| 141 std::string path_; |
| 142 bool secure_; |
| 143 bool http_only_; |
| 144 base::Time expiration_time_; |
| 145 bool success_; |
| 146 scoped_refptr<URLRequestContextGetter> store_context_; |
104 }; | 147 }; |
105 | 148 |
106 // Implements the experimental.cookies.remove() extension function. | 149 // Implements the experimental.cookies.remove() extension function. |
107 class RemoveCookieFunction : public CookiesFunction { | 150 class RemoveCookieFunction : public CookiesFunction { |
108 public: | 151 public: |
109 virtual bool RunImpl(); | 152 virtual bool RunImpl(); |
| 153 // RemoveCookieFunction is sync. |
| 154 virtual void Run() { |
| 155 SendResponse(RunImpl()); |
| 156 } |
110 DECLARE_EXTENSION_FUNCTION_NAME("experimental.cookies.remove") | 157 DECLARE_EXTENSION_FUNCTION_NAME("experimental.cookies.remove") |
111 }; | 158 }; |
112 | 159 |
113 // Implements the experimental.cookies.getAllCookieStores() extension function. | 160 // Implements the experimental.cookies.getAllCookieStores() extension function. |
114 class GetAllCookieStoresFunction : public CookiesFunction { | 161 class GetAllCookieStoresFunction : public CookiesFunction { |
115 public: | 162 public: |
116 virtual bool RunImpl(); | 163 virtual bool RunImpl(); |
| 164 // GetAllCookieStoresFunction is sync. |
| 165 virtual void Run() { |
| 166 SendResponse(RunImpl()); |
| 167 } |
117 DECLARE_EXTENSION_FUNCTION_NAME("experimental.cookies.getAllCookieStores") | 168 DECLARE_EXTENSION_FUNCTION_NAME("experimental.cookies.getAllCookieStores") |
118 }; | 169 }; |
119 | 170 |
120 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_ | 171 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_COOKIES_API_H_ |
OLD | NEW |