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