Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(703)

Side by Side Diff: android_webview/native/cookie_manager.cc

Issue 10913074: Add WebView implementation for CookieManager. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2011 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
5 #include "android_webview/native/cookie_manager.h"
6
7 #include "android_webview/browser/aw_cookie_access_policy.h"
8 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h"
9 #include "android_webview/native/aw_browser_dependency_factory.h"
10 #include "base/android/jni_string.h"
11 #include "base/bind.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "net/cookies/cookie_monster.h"
16 #include "net/cookies/cookie_options.h"
17 #include "net/cookies/cookie_store.h"
18 #include "net/url_request/url_request_context.h"
19 #include "net/url_request/url_request_context_getter.h"
20 #include "jni/CookieManager_jni.h"
21
22 using base::android::ConvertJavaStringToUTF8;
23 using base::android::ConvertJavaStringToUTF16;
24 using content::BrowserThread;
25 using net::CookieList;
26 using net::CookieMonster;
27 using net::URLRequestContextGetter;
28
29 // CookieManager should be refactored to not require all tasks accessing the
30 // CookieStore to be piped through the IO thread. It is currently required as
31 // the URLRequestContext provides the easiest mechanism for accessing the
32 // CookieStore, but the CookieStore is threadsafe. In the future, we may
33 // instead want to inject an explicit CookieStore dependency into this object
34 // during process initialization to avoid depending on the URLRequestContext.
35 //
36 // In addition to the IO thread being the easiest access mechanism, it is also
37 // used because the async cookie tasks need to be processed on a different
38 // thread than the caller from Java, which can be any thread. But, the calling
39 // thread will be 'dead' blocked waiting on the async task to complete, so we
40 // need it to complete on a 'live' (non-blocked) thread that is still pumping
41 // messages.
42 //
43 // We could refactor to only provide an asynchronous (but thread safe) API on
44 // the native side, and move this support for legacy synchronous blocking
45 // messages into a java-side worker thread.
46
47 namespace android_webview {
48
49 namespace {
50
51 typedef base::Callback<void(scoped_refptr<URLRequestContextGetter>,
52 base::WaitableEvent*)> CookieTask;
53
54 // Executes the |callback| task on the IO thread and |wait_for_completion|
55 // should only be true if the Java API method returns a value or is explicitly
56 // stated to be synchronous.
57 static void ExecCookieTask(const CookieTask& callback,
58 const bool wait_for_completion) {
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
60
61 content::BrowserContext* context =
62 android_webview::AwBrowserDependencyFactory::GetInstance()->
63 GetBrowserContext(false);
64 if (!context)
65 return;
66
67 scoped_refptr<URLRequestContextGetter> context_getter(
68 context->GetRequestContext());
69
70 if (wait_for_completion) {
71 base::WaitableEvent completion(false, false);
72
73 context->GetRequestContext()->GetNetworkTaskRunner()->PostTask(
74 FROM_HERE, base::Bind(callback, context_getter, &completion));
75
76 ScopedAllowWaitForLegacyWebViewApi wait;
77 completion.Wait();
78 } else {
79 base::WaitableEvent* cb = NULL;
80 context->GetRequestContext()->GetNetworkTaskRunner()->PostTask(
81 FROM_HERE, base::Bind(callback, context_getter, cb));
82 }
83 }
84
85 } // namespace
86
87 static void SetAcceptCookie(JNIEnv* env, jobject obj, jboolean accept) {
88 AwCookieAccessPolicy::GetInstance()->SetGlobalAllowAccess(accept);
89 }
90
91 static jboolean AcceptCookie(JNIEnv* env, jobject obj) {
92 return AwCookieAccessPolicy::GetInstance()->GetGlobalAllowAccess();
93 }
94
95 namespace {
96
97 // The CookieManager API does not return a value for SetCookie,
98 // so we don't need to propagate the |success| value back to the caller.
99 static void SetCookieCompleted(bool success) {
100 }
101
102 static void SetCookieAsyncHelper(
103 const GURL& host,
104 const std::string& value,
105 scoped_refptr<URLRequestContextGetter> context_getter,
106 base::WaitableEvent* completion) {
107 DCHECK(!completion);
108 net::CookieOptions options;
109 options.set_include_httponly();
110
111 context_getter->GetURLRequestContext()->cookie_store()->
112 SetCookieWithOptionsAsync(host, value, options,
113 base::Bind(&SetCookieCompleted));
114 }
115
116 } // namespace
117
118 static void SetCookie(JNIEnv* env, jobject obj, jstring url, jstring value) {
119 GURL host(ConvertJavaStringToUTF16(env, url));
120 std::string cookie_value(ConvertJavaStringToUTF8(env, value));
121
122 ExecCookieTask(base::Bind(&SetCookieAsyncHelper, host, cookie_value), false);
123 }
124
125 namespace {
126
127 static void GetCookieValueCompleted(base::WaitableEvent* completion,
128 std::string* result,
129 const std::string& value) {
130 *result = value;
131 DCHECK(completion);
132 completion->Signal();
133 }
134
135 static void GetCookieValueAsyncHelper(
136 const GURL& host,
137 std::string* result,
138 scoped_refptr<URLRequestContextGetter> context_getter,
139 base::WaitableEvent* completion) {
140
141 net::CookieOptions options;
142 options.set_include_httponly();
143
144 context_getter->GetURLRequestContext()->cookie_store()->
145 GetCookiesWithOptionsAsync(host, options,
146 base::Bind(&GetCookieValueCompleted,
147 completion,
148 result));
149 }
150
151 } // namespace
152
153 static jstring GetCookie(JNIEnv* env, jobject obj, jstring url) {
154 GURL host(ConvertJavaStringToUTF16(env, url));
155 std::string cookie_value;
156 ExecCookieTask(base::Bind(&GetCookieValueAsyncHelper, host, &cookie_value),
157 true);
158
159 return base::android::ConvertUTF8ToJavaString(env, cookie_value).Release();
160 }
161
162 namespace {
163
164 static void RemoveSessionCookieCompleted(int num_deleted) {
165 // The CookieManager API does not return a value for removeSessionCookie,
166 // so we don't need to propagate the |num_deleted| value back to the caller.
167 }
168
169 static void RemoveSessionCookieAsyncHelper(
170 scoped_refptr<URLRequestContextGetter> context_getter,
171 base::WaitableEvent* completion) {
172 DCHECK(!completion);
173 net::CookieOptions options;
174 options.set_include_httponly();
175
176 CookieMonster* monster = context_getter->GetURLRequestContext()->
177 cookie_store()->GetCookieMonster();
178 monster->DeleteSessionCookiesAsync(base::Bind(&RemoveSessionCookieCompleted));
179 }
180
181 } // namespace
182
183 static void RemoveSessionCookie(JNIEnv* env, jobject obj) {
184 ExecCookieTask(base::Bind(&RemoveSessionCookieAsyncHelper), false);
185 }
186
187 namespace {
188
189 static void RemoveAllCookieCompleted(int num_deleted) {
190 // The CookieManager API does not return a value for removeAllCookie,
191 // so we don't need to propagate the |num_deleted| value back to the caller.
192 }
193
194 static void RemoveAllCookieAsyncHelper(
195 scoped_refptr<URLRequestContextGetter> context_getter,
196 base::WaitableEvent* completion) {
197 DCHECK(!completion);
198 CookieMonster* monster = context_getter->GetURLRequestContext()->
199 cookie_store()->GetCookieMonster();
200 monster->DeleteAllAsync(base::Bind(&RemoveAllCookieCompleted));
201 }
202
203 } // namespace
204
205 static void RemoveAllCookie(JNIEnv* env, jobject obj) {
206 ExecCookieTask(base::Bind(&RemoveAllCookieAsyncHelper), false);
207 }
208
209 static void RemoveExpiredCookie(JNIEnv* env, jobject obj) {
210 // HasCookies will call GetAllCookiesAsync, which in turn will force a GC.
211 HasCookies(env, obj);
212 }
213
214 namespace {
215
216 static void HasCookiesCompleted(base::WaitableEvent* completion,
217 bool* result,
218 const CookieList& cookies) {
219 *result = cookies.size() != 0;
220 DCHECK(completion);
221 completion->Signal();
222 }
223
224 static void HasCookiesAsyncHelper(
225 bool* result,
226 scoped_refptr<URLRequestContextGetter> context_getter,
227 base::WaitableEvent* completion) {
228
229 CookieMonster* monster = context_getter->GetURLRequestContext()->
230 cookie_store()->GetCookieMonster();
231 monster->GetAllCookiesAsync(base::Bind(&HasCookiesCompleted, completion,
232 result));
233 }
234
235 } // namespace
236
237 static jboolean HasCookies(JNIEnv* env, jobject obj) {
238 bool has_cookies;
239 ExecCookieTask(base::Bind(&HasCookiesAsyncHelper, &has_cookies), true);
240 return has_cookies;
241 }
242
243 namespace {
244
245 static void AllowFileSchemeCookiesAsyncHelper(
246 bool* accept,
247 scoped_refptr<URLRequestContextGetter> context_getter,
248 base::WaitableEvent* completion) {
249
250 CookieMonster* monster = context_getter->GetURLRequestContext()->
251 cookie_store()->GetCookieMonster();
252 *accept = monster->IsCookieableScheme("file");
253
254 DCHECK(completion);
255 completion->Signal();
256 }
257
258 } // namespace
259
260 static jboolean AllowFileSchemeCookies(JNIEnv* env, jclass obj) {
261 bool accept;
262 ExecCookieTask(base::Bind(&AllowFileSchemeCookiesAsyncHelper, &accept), true);
263 return accept;
264 }
265
266 namespace {
267
268 static void SetAcceptFileSchemeCookiesAsyncHelper(
269 bool accept,
270 scoped_refptr<URLRequestContextGetter> context_getter,
271 base::WaitableEvent* completion) {
272
273 CookieMonster* monster = context_getter->GetURLRequestContext()->
274 cookie_store()->GetCookieMonster();
275 monster->SetEnableFileScheme(accept);
276
277 DCHECK(completion);
278 completion->Signal();
279 }
280
281 } // namespace
282
283 static void SetAcceptFileSchemeCookies(JNIEnv* env, jclass obj,
284 jboolean accept) {
285 ExecCookieTask(base::Bind(&SetAcceptFileSchemeCookiesAsyncHelper, accept),
286 true);
287 }
288
289 bool RegisterCookieManager(JNIEnv* env) {
290 return RegisterNativesImpl(env);
291 }
292
293 } // android_webview namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698