OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "android_webview/native/cookie_manager.h" | 5 #include "android_webview/native/cookie_manager.h" |
6 | 6 |
7 #include "android_webview/browser/aw_browser_context.h" | 7 #include "android_webview/browser/aw_browser_context.h" |
8 #include "android_webview/browser/aw_cookie_access_policy.h" | 8 #include "android_webview/browser/aw_cookie_access_policy.h" |
9 #include "android_webview/browser/net/init_native_callback.h" | 9 #include "android_webview/browser/net/init_native_callback.h" |
10 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h" | 10 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
29 #include "content/public/browser/cookie_crypto_delegate.h" | 29 #include "content/public/browser/cookie_crypto_delegate.h" |
30 #include "content/public/browser/cookie_store_factory.h" | 30 #include "content/public/browser/cookie_store_factory.h" |
31 #include "content/public/common/url_constants.h" | 31 #include "content/public/common/url_constants.h" |
32 #include "jni/AwCookieManager_jni.h" | 32 #include "jni/AwCookieManager_jni.h" |
33 #include "net/cookies/cookie_monster.h" | 33 #include "net/cookies/cookie_monster.h" |
34 #include "net/cookies/cookie_options.h" | 34 #include "net/cookies/cookie_options.h" |
35 #include "net/url_request/url_request_context.h" | 35 #include "net/url_request/url_request_context.h" |
36 | 36 |
37 using base::FilePath; | 37 using base::FilePath; |
| 38 using base::WaitableEvent; |
38 using base::android::ConvertJavaStringToUTF8; | 39 using base::android::ConvertJavaStringToUTF8; |
39 using base::android::ConvertJavaStringToUTF16; | 40 using base::android::ConvertJavaStringToUTF16; |
| 41 using base::android::ScopedJavaGlobalRef; |
40 using content::BrowserThread; | 42 using content::BrowserThread; |
41 using net::CookieList; | 43 using net::CookieList; |
42 using net::CookieMonster; | 44 using net::CookieMonster; |
43 | 45 |
44 // In the future, we may instead want to inject an explicit CookieStore | 46 // In the future, we may instead want to inject an explicit CookieStore |
45 // dependency into this object during process initialization to avoid | 47 // dependency into this object during process initialization to avoid |
46 // depending on the URLRequestContext. | 48 // depending on the URLRequestContext. |
47 // See issue http://crbug.com/157683 | 49 // See issue http://crbug.com/157683 |
48 | 50 |
49 // All functions on the CookieManager can be called from any thread, including | 51 // On the CookieManager methods without a callback and methods with a callback |
50 // threads without a message loop. BrowserThread::IO is used to call methods | 52 // when that callback is null can be called from any thread, including threads |
51 // on CookieMonster that needs to be called, and called back, on a chrome | 53 // without a message loop. Methods with a non-null callback must be called on |
52 // thread. | 54 // a thread with a running message loop. |
53 | 55 |
54 namespace android_webview { | 56 namespace android_webview { |
55 | 57 |
56 namespace { | 58 namespace { |
57 | 59 |
| 60 typedef base::Callback<void(bool)> BoolCallback; |
| 61 typedef base::Callback<void(int)> IntCallback; |
| 62 |
| 63 // Holds a Java BooleanCookieCallback, knows how to invoke it and turn it |
| 64 // into a base callback. |
| 65 class BoolCookieCallbackHolder { |
| 66 public: |
| 67 BoolCookieCallbackHolder(JNIEnv* env, jobject callback) { |
| 68 callback_.Reset(env, callback); |
| 69 } |
| 70 |
| 71 void Invoke(bool result) { |
| 72 if (!callback_.is_null()) { |
| 73 JNIEnv* env = base::android::AttachCurrentThread(); |
| 74 Java_AwCookieManager_invokeBooleanCookieCallback( |
| 75 env, callback_.obj(), result); |
| 76 } |
| 77 } |
| 78 |
| 79 static BoolCallback ConvertToCallback( |
| 80 scoped_ptr<BoolCookieCallbackHolder> me) { |
| 81 return base::Bind(&BoolCookieCallbackHolder::Invoke, |
| 82 base::Owned(me.release())); |
| 83 } |
| 84 |
| 85 private: |
| 86 ScopedJavaGlobalRef<jobject> callback_; |
| 87 DISALLOW_COPY_AND_ASSIGN(BoolCookieCallbackHolder); |
| 88 }; |
| 89 |
| 90 // Construct a closure which signals a waitable event if and when the closure |
| 91 // is called the waitable event must still exist. |
| 92 static base::Closure SignalEventClosure(WaitableEvent* completion) { |
| 93 return base::Bind(&WaitableEvent::Signal, base::Unretained(completion)); |
| 94 } |
| 95 |
| 96 static void DiscardBool(const base::Closure& f, bool b) { |
| 97 f.Run(); |
| 98 } |
| 99 |
| 100 static BoolCallback BoolCallbackAdapter(const base::Closure& f) { |
| 101 return base::Bind(&DiscardBool, f); |
| 102 } |
| 103 |
| 104 static void DiscardInt(const base::Closure& f, int i) { |
| 105 f.Run(); |
| 106 } |
| 107 |
| 108 static IntCallback IntCallbackAdapter(const base::Closure& f) { |
| 109 return base::Bind(&DiscardInt, f); |
| 110 } |
| 111 |
58 // Are cookies allowed for file:// URLs by default? | 112 // Are cookies allowed for file:// URLs by default? |
59 const bool kDefaultFileSchemeAllowed = false; | 113 const bool kDefaultFileSchemeAllowed = false; |
60 | 114 |
61 void ImportLegacyCookieStore(const FilePath& cookie_store_path) { | 115 void ImportLegacyCookieStore(const FilePath& cookie_store_path) { |
62 // We use the old cookie store to create the new cookie store only if the | 116 // We use the old cookie store to create the new cookie store only if the |
63 // new cookie store does not exist. | 117 // new cookie store does not exist. |
64 if (base::PathExists(cookie_store_path)) | 118 if (base::PathExists(cookie_store_path)) |
65 return; | 119 return; |
66 | 120 |
67 // WebViewClassic gets the database path from Context and appends a | 121 // WebViewClassic gets the database path from Context and appends a |
(...skipping 23 matching lines...) Expand all Loading... |
91 public: | 145 public: |
92 static CookieManager* GetInstance(); | 146 static CookieManager* GetInstance(); |
93 | 147 |
94 scoped_refptr<net::CookieStore> CreateBrowserThreadCookieStore( | 148 scoped_refptr<net::CookieStore> CreateBrowserThreadCookieStore( |
95 AwBrowserContext* browser_context); | 149 AwBrowserContext* browser_context); |
96 | 150 |
97 void SetAcceptCookie(bool accept); | 151 void SetAcceptCookie(bool accept); |
98 bool AcceptCookie(); | 152 bool AcceptCookie(); |
99 void SetAcceptThirdPartyCookie(bool accept); | 153 void SetAcceptThirdPartyCookie(bool accept); |
100 bool AcceptThirdPartyCookie(); | 154 bool AcceptThirdPartyCookie(); |
101 void SetCookie(const GURL& host, const std::string& cookie_value); | 155 void SetCookie(const GURL& host, |
| 156 const std::string& cookie_value, |
| 157 scoped_ptr<BoolCookieCallbackHolder> callback); |
| 158 void SetCookieSync(const GURL& host, |
| 159 const std::string& cookie_value); |
102 std::string GetCookie(const GURL& host); | 160 std::string GetCookie(const GURL& host); |
103 void RemoveSessionCookie(); | 161 void RemoveSessionCookies(scoped_ptr<BoolCookieCallbackHolder> callback); |
104 void RemoveAllCookie(); | 162 void RemoveAllCookies(scoped_ptr<BoolCookieCallbackHolder> callback); |
105 void RemoveExpiredCookie(); | 163 void RemoveAllCookiesSync(); |
| 164 void RemoveSessionCookiesSync(); |
| 165 void RemoveExpiredCookies(); |
106 void FlushCookieStore(); | 166 void FlushCookieStore(); |
107 bool HasCookies(); | 167 bool HasCookies(); |
108 bool AllowFileSchemeCookies(); | 168 bool AllowFileSchemeCookies(); |
109 void SetAcceptFileSchemeCookies(bool accept); | 169 void SetAcceptFileSchemeCookies(bool accept); |
110 | 170 |
111 private: | 171 private: |
112 friend struct base::DefaultLazyInstanceTraits<CookieManager>; | 172 friend struct base::DefaultLazyInstanceTraits<CookieManager>; |
113 | 173 |
114 CookieManager(); | 174 CookieManager(); |
115 ~CookieManager(); | 175 ~CookieManager(); |
116 | 176 |
117 typedef base::Callback<void(base::WaitableEvent*)> CookieTask; | 177 void ExecCookieTaskSync(const base::Callback<void(BoolCallback)>& task); |
118 void ExecCookieTask(const CookieTask& task); | 178 void ExecCookieTaskSync(const base::Callback<void(IntCallback)>& task); |
| 179 void ExecCookieTaskSync(const base::Callback<void(base::Closure)>& task); |
| 180 void ExecCookieTask(const base::Closure& task); |
119 | 181 |
120 void SetCookieAsyncHelper( | 182 void SetCookieHelper( |
121 const GURL& host, | 183 const GURL& host, |
122 const std::string& value, | 184 const std::string& value, |
123 base::WaitableEvent* completion); | 185 BoolCallback callback); |
124 void SetCookieCompleted(base::WaitableEvent* completion, bool success); | |
125 | 186 |
126 void GetCookieValueAsyncHelper( | 187 void GetCookieValueAsyncHelper(const GURL& host, |
127 const GURL& host, | 188 std::string* result, |
128 std::string* result, | 189 base::Closure complete); |
129 base::WaitableEvent* completion); | 190 void GetCookieValueCompleted(base::Closure complete, |
130 void GetCookieValueCompleted(base::WaitableEvent* completion, | |
131 std::string* result, | 191 std::string* result, |
132 const std::string& value); | 192 const std::string& value); |
133 | 193 |
134 void RemoveSessionCookieAsyncHelper(base::WaitableEvent* completion); | 194 void RemoveSessionCookiesHelper(BoolCallback callback); |
135 void RemoveAllCookieAsyncHelper(base::WaitableEvent* completion); | 195 void RemoveAllCookiesHelper(BoolCallback callback); |
136 void RemoveCookiesCompleted(base::WaitableEvent* completion, int num_deleted); | 196 void RemoveCookiesCompleted(BoolCallback callback, int num_deleted); |
137 | 197 |
138 void FlushCookieStoreAsyncHelper(base::WaitableEvent* completion); | 198 void FlushCookieStoreAsyncHelper(base::Closure complete); |
139 | 199 |
140 void HasCookiesAsyncHelper(bool* result, | 200 void HasCookiesAsyncHelper(bool* result, base::Closure complete); |
141 base::WaitableEvent* completion); | 201 void HasCookiesCompleted(base::Closure complete, |
142 void HasCookiesCompleted(base::WaitableEvent* completion, | |
143 bool* result, | 202 bool* result, |
144 const CookieList& cookies); | 203 const CookieList& cookies); |
145 | 204 |
146 void CreateCookieMonster( | 205 void CreateCookieMonster( |
147 const FilePath& user_data_dir, | 206 const FilePath& user_data_dir, |
148 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 207 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
149 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner); | 208 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner); |
150 void EnsureCookieMonsterExistsLocked(); | 209 void EnsureCookieMonsterExistsLocked(); |
151 bool AllowFileSchemeCookiesLocked(); | 210 bool AllowFileSchemeCookiesLocked(); |
152 void SetAcceptFileSchemeCookiesLocked(bool accept); | 211 void SetAcceptFileSchemeCookiesLocked(bool accept); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 cookie_monster_proxy_ = cookie_monster_client_thread_->message_loop_proxy(); | 273 cookie_monster_proxy_ = cookie_monster_client_thread_->message_loop_proxy(); |
215 cookie_monster_backend_thread_.reset( | 274 cookie_monster_backend_thread_.reset( |
216 new base::Thread("CookieMonsterBackend")); | 275 new base::Thread("CookieMonsterBackend")); |
217 cookie_monster_backend_thread_->Start(); | 276 cookie_monster_backend_thread_->Start(); |
218 | 277 |
219 CreateCookieMonster(user_data_dir, | 278 CreateCookieMonster(user_data_dir, |
220 cookie_monster_proxy_, | 279 cookie_monster_proxy_, |
221 cookie_monster_backend_thread_->message_loop_proxy()); | 280 cookie_monster_backend_thread_->message_loop_proxy()); |
222 } | 281 } |
223 | 282 |
224 // Executes the |task| on the |cookie_monster_proxy_| message loop. | 283 // Executes the |task| on the |cookie_monster_proxy_| message loop and |
225 void CookieManager::ExecCookieTask(const CookieTask& task) { | 284 // waits for it to complete before returning. |
226 base::WaitableEvent completion(false, false); | |
227 base::AutoLock lock(cookie_monster_lock_); | |
228 | 285 |
229 EnsureCookieMonsterExistsLocked(); | 286 // To execute a CookieTask synchronously you must arrange for Signal to be |
| 287 // called on the waitable event at some point. You can call the bool or int |
| 288 // versions of ExecCookieTaskSync, these will supply the caller with a dummy |
| 289 // callback which takes an int/bool, throws it away and calls Signal. |
| 290 // Alternatively you can call the version which supplies a Closure in which |
| 291 // case you must call Run on it when you want the unblock the calling code. |
230 | 292 |
231 cookie_monster_proxy_->PostTask(FROM_HERE, base::Bind(task, &completion)); | 293 // Ignore a bool callback. |
232 | 294 void CookieManager::ExecCookieTaskSync( |
233 // We always wait for the posted task to complete, even when it doesn't return | 295 const base::Callback<void(BoolCallback)>& task) { |
234 // a value, because previous versions of the CookieManager API were | 296 WaitableEvent completion(false, false); |
235 // synchronous in most/all cases and the caller may be relying on this. | 297 ExecCookieTask( |
| 298 base::Bind(task, BoolCallbackAdapter(SignalEventClosure(&completion)))); |
236 ScopedAllowWaitForLegacyWebViewApi wait; | 299 ScopedAllowWaitForLegacyWebViewApi wait; |
237 completion.Wait(); | 300 completion.Wait(); |
238 } | 301 } |
239 | 302 |
| 303 // Ignore an int callback. |
| 304 void CookieManager::ExecCookieTaskSync( |
| 305 const base::Callback<void(IntCallback)>& task) { |
| 306 WaitableEvent completion(false, false); |
| 307 ExecCookieTask( |
| 308 base::Bind(task, IntCallbackAdapter(SignalEventClosure(&completion)))); |
| 309 ScopedAllowWaitForLegacyWebViewApi wait; |
| 310 completion.Wait(); |
| 311 } |
| 312 |
| 313 // Call the supplied closure when you want to signal that the blocked code can |
| 314 // continue. |
| 315 void CookieManager::ExecCookieTaskSync( |
| 316 const base::Callback<void(base::Closure)>& task) { |
| 317 WaitableEvent completion(false, false); |
| 318 ExecCookieTask(base::Bind(task, SignalEventClosure(&completion))); |
| 319 ScopedAllowWaitForLegacyWebViewApi wait; |
| 320 completion.Wait(); |
| 321 } |
| 322 |
| 323 // Executes the |task| on the |cookie_monster_proxy_| message loop. |
| 324 void CookieManager::ExecCookieTask(const base::Closure& task) { |
| 325 base::AutoLock lock(cookie_monster_lock_); |
| 326 EnsureCookieMonsterExistsLocked(); |
| 327 cookie_monster_proxy_->PostTask(FROM_HERE, task); |
| 328 } |
| 329 |
240 scoped_refptr<net::CookieStore> CookieManager::CreateBrowserThreadCookieStore( | 330 scoped_refptr<net::CookieStore> CookieManager::CreateBrowserThreadCookieStore( |
241 AwBrowserContext* browser_context) { | 331 AwBrowserContext* browser_context) { |
242 base::AutoLock lock(cookie_monster_lock_); | 332 base::AutoLock lock(cookie_monster_lock_); |
243 | 333 |
244 if (cookie_monster_client_thread_) { | 334 if (cookie_monster_client_thread_) { |
245 // We created a cookie monster already on its own threads; we'll just keep | 335 // We created a cookie monster already on its own threads; we'll just keep |
246 // using it rather than creating one on the normal Chromium threads. | 336 // using it rather than creating one on the normal Chromium threads. |
247 // CookieMonster is threadsafe, so this is fine. | 337 // CookieMonster is threadsafe, so this is fine. |
248 return cookie_monster_; | 338 return cookie_monster_; |
249 } | 339 } |
(...skipping 26 matching lines...) Expand all Loading... |
276 } | 366 } |
277 | 367 |
278 void CookieManager::SetAcceptThirdPartyCookie(bool accept) { | 368 void CookieManager::SetAcceptThirdPartyCookie(bool accept) { |
279 AwCookieAccessPolicy::GetInstance()->SetThirdPartyAllowAccess(accept); | 369 AwCookieAccessPolicy::GetInstance()->SetThirdPartyAllowAccess(accept); |
280 } | 370 } |
281 | 371 |
282 bool CookieManager::AcceptThirdPartyCookie() { | 372 bool CookieManager::AcceptThirdPartyCookie() { |
283 return AwCookieAccessPolicy::GetInstance()->GetThirdPartyAllowAccess(); | 373 return AwCookieAccessPolicy::GetInstance()->GetThirdPartyAllowAccess(); |
284 } | 374 } |
285 | 375 |
286 void CookieManager::SetCookie(const GURL& host, | 376 void CookieManager::SetCookie( |
| 377 const GURL& host, |
| 378 const std::string& cookie_value, |
| 379 scoped_ptr<BoolCookieCallbackHolder> callback_holder) { |
| 380 BoolCallback callback = |
| 381 BoolCookieCallbackHolder::ConvertToCallback(callback_holder.Pass()); |
| 382 ExecCookieTask(base::Bind(&CookieManager::SetCookieHelper, |
| 383 base::Unretained(this), |
| 384 host, |
| 385 cookie_value, |
| 386 callback)); |
| 387 } |
| 388 |
| 389 void CookieManager::SetCookieSync(const GURL& host, |
287 const std::string& cookie_value) { | 390 const std::string& cookie_value) { |
288 ExecCookieTask(base::Bind(&CookieManager::SetCookieAsyncHelper, | 391 ExecCookieTaskSync(base::Bind(&CookieManager::SetCookieHelper, |
289 base::Unretained(this), | 392 base::Unretained(this), |
290 host, | 393 host, |
291 cookie_value)); | 394 cookie_value)); |
292 } | 395 } |
293 | 396 |
294 void CookieManager::SetCookieAsyncHelper( | 397 void CookieManager::SetCookieHelper( |
295 const GURL& host, | 398 const GURL& host, |
296 const std::string& value, | 399 const std::string& value, |
297 base::WaitableEvent* completion) { | 400 const BoolCallback callback) { |
298 net::CookieOptions options; | 401 net::CookieOptions options; |
299 options.set_include_httponly(); | 402 options.set_include_httponly(); |
300 | 403 |
301 cookie_monster_->SetCookieWithOptionsAsync( | 404 cookie_monster_->SetCookieWithOptionsAsync( |
302 host, value, options, | 405 host, value, options, callback); |
303 base::Bind(&CookieManager::SetCookieCompleted, | |
304 base::Unretained(this), | |
305 completion)); | |
306 } | |
307 | |
308 void CookieManager::SetCookieCompleted(base::WaitableEvent* completion, | |
309 bool success) { | |
310 // The CookieManager API does not return a value for SetCookie, | |
311 // so we don't need to propagate the |success| value back to the caller. | |
312 completion->Signal(); | |
313 } | 406 } |
314 | 407 |
315 std::string CookieManager::GetCookie(const GURL& host) { | 408 std::string CookieManager::GetCookie(const GURL& host) { |
316 std::string cookie_value; | 409 std::string cookie_value; |
317 ExecCookieTask(base::Bind(&CookieManager::GetCookieValueAsyncHelper, | 410 ExecCookieTaskSync(base::Bind(&CookieManager::GetCookieValueAsyncHelper, |
318 base::Unretained(this), | 411 base::Unretained(this), |
319 host, | 412 host, |
320 &cookie_value)); | 413 &cookie_value)); |
321 | |
322 return cookie_value; | 414 return cookie_value; |
323 } | 415 } |
324 | 416 |
325 void CookieManager::GetCookieValueAsyncHelper( | 417 void CookieManager::GetCookieValueAsyncHelper( |
326 const GURL& host, | 418 const GURL& host, |
327 std::string* result, | 419 std::string* result, |
328 base::WaitableEvent* completion) { | 420 base::Closure complete) { |
329 net::CookieOptions options; | 421 net::CookieOptions options; |
330 options.set_include_httponly(); | 422 options.set_include_httponly(); |
331 | 423 |
332 cookie_monster_->GetCookiesWithOptionsAsync( | 424 cookie_monster_->GetCookiesWithOptionsAsync( |
333 host, | 425 host, |
334 options, | 426 options, |
335 base::Bind(&CookieManager::GetCookieValueCompleted, | 427 base::Bind(&CookieManager::GetCookieValueCompleted, |
336 base::Unretained(this), | 428 base::Unretained(this), |
337 completion, | 429 complete, |
338 result)); | 430 result)); |
339 } | 431 } |
340 | 432 |
341 void CookieManager::GetCookieValueCompleted(base::WaitableEvent* completion, | 433 void CookieManager::GetCookieValueCompleted(base::Closure complete, |
342 std::string* result, | 434 std::string* result, |
343 const std::string& value) { | 435 const std::string& value) { |
344 *result = value; | 436 *result = value; |
345 completion->Signal(); | 437 complete.Run(); |
346 } | 438 } |
347 | 439 |
348 void CookieManager::RemoveSessionCookie() { | 440 void CookieManager::RemoveSessionCookies( |
349 ExecCookieTask(base::Bind(&CookieManager::RemoveSessionCookieAsyncHelper, | 441 scoped_ptr<BoolCookieCallbackHolder> callback_holder) { |
| 442 BoolCallback callback = |
| 443 BoolCookieCallbackHolder::ConvertToCallback(callback_holder.Pass()); |
| 444 ExecCookieTask(base::Bind(&CookieManager::RemoveSessionCookiesHelper, |
| 445 base::Unretained(this), |
| 446 callback)); |
| 447 } |
| 448 |
| 449 void CookieManager::RemoveSessionCookiesSync() { |
| 450 ExecCookieTaskSync(base::Bind(&CookieManager::RemoveSessionCookiesHelper, |
350 base::Unretained(this))); | 451 base::Unretained(this))); |
351 } | 452 } |
352 | 453 |
353 void CookieManager::RemoveSessionCookieAsyncHelper( | 454 void CookieManager::RemoveSessionCookiesHelper( |
354 base::WaitableEvent* completion) { | 455 BoolCallback callback) { |
355 cookie_monster_->DeleteSessionCookiesAsync( | 456 cookie_monster_->DeleteSessionCookiesAsync( |
356 base::Bind(&CookieManager::RemoveCookiesCompleted, | 457 base::Bind(&CookieManager::RemoveCookiesCompleted, |
357 base::Unretained(this), | 458 base::Unretained(this), |
358 completion)); | 459 callback)); |
359 } | 460 } |
360 | 461 |
361 void CookieManager::RemoveCookiesCompleted(base::WaitableEvent* completion, | 462 void CookieManager::RemoveCookiesCompleted( |
362 int num_deleted) { | 463 BoolCallback callback, |
363 // The CookieManager API does not return a value for removeSessionCookie or | 464 int num_deleted) { |
364 // removeAllCookie, so we don't need to propagate the |num_deleted| value back | 465 callback.Run(num_deleted > 0); |
365 // to the caller. | |
366 completion->Signal(); | |
367 } | 466 } |
368 | 467 |
369 void CookieManager::RemoveAllCookie() { | 468 void CookieManager::RemoveAllCookies( |
370 ExecCookieTask(base::Bind(&CookieManager::RemoveAllCookieAsyncHelper, | 469 scoped_ptr<BoolCookieCallbackHolder> callback_holder) { |
| 470 BoolCallback callback = |
| 471 BoolCookieCallbackHolder::ConvertToCallback(callback_holder.Pass()); |
| 472 ExecCookieTask(base::Bind(&CookieManager::RemoveAllCookiesHelper, |
| 473 base::Unretained(this), |
| 474 callback)); |
| 475 } |
| 476 |
| 477 void CookieManager::RemoveAllCookiesSync() { |
| 478 ExecCookieTaskSync(base::Bind(&CookieManager::RemoveAllCookiesHelper, |
371 base::Unretained(this))); | 479 base::Unretained(this))); |
372 } | 480 } |
373 | 481 |
374 void CookieManager::RemoveAllCookieAsyncHelper( | 482 void CookieManager::RemoveAllCookiesHelper( |
375 base::WaitableEvent* completion) { | 483 const BoolCallback callback) { |
376 cookie_monster_->DeleteAllAsync( | 484 cookie_monster_->DeleteAllAsync( |
377 base::Bind(&CookieManager::RemoveCookiesCompleted, | 485 base::Bind(&CookieManager::RemoveCookiesCompleted, |
378 base::Unretained(this), | 486 base::Unretained(this), |
379 completion)); | 487 callback)); |
380 } | 488 } |
381 | 489 |
382 void CookieManager::RemoveExpiredCookie() { | 490 void CookieManager::RemoveExpiredCookies() { |
383 // HasCookies will call GetAllCookiesAsync, which in turn will force a GC. | 491 // HasCookies will call GetAllCookiesAsync, which in turn will force a GC. |
384 HasCookies(); | 492 HasCookies(); |
385 } | 493 } |
386 | 494 |
387 void CookieManager::FlushCookieStoreAsyncHelper( | 495 void CookieManager::FlushCookieStore() { |
388 base::WaitableEvent* completion) { | 496 ExecCookieTaskSync(base::Bind(&CookieManager::FlushCookieStoreAsyncHelper, |
389 cookie_monster_->FlushStore(base::Bind(&base::WaitableEvent::Signal, | 497 base::Unretained(this))); |
390 base::Unretained(completion))); | |
391 } | 498 } |
392 | 499 |
393 void CookieManager::FlushCookieStore() { | 500 void CookieManager::FlushCookieStoreAsyncHelper( |
394 ExecCookieTask(base::Bind(&CookieManager::FlushCookieStoreAsyncHelper, | 501 base::Closure complete) { |
395 base::Unretained(this))); | 502 cookie_monster_->FlushStore(complete); |
396 } | 503 } |
397 | 504 |
398 bool CookieManager::HasCookies() { | 505 bool CookieManager::HasCookies() { |
399 bool has_cookies; | 506 bool has_cookies; |
400 ExecCookieTask(base::Bind(&CookieManager::HasCookiesAsyncHelper, | 507 ExecCookieTaskSync(base::Bind(&CookieManager::HasCookiesAsyncHelper, |
401 base::Unretained(this), | 508 base::Unretained(this), |
402 &has_cookies)); | 509 &has_cookies)); |
403 return has_cookies; | 510 return has_cookies; |
404 } | 511 } |
405 | 512 |
406 // TODO(kristianm): Simplify this, copying the entire list around | 513 // TODO(kristianm): Simplify this, copying the entire list around |
407 // should not be needed. | 514 // should not be needed. |
408 void CookieManager::HasCookiesAsyncHelper(bool* result, | 515 void CookieManager::HasCookiesAsyncHelper(bool* result, |
409 base::WaitableEvent* completion) { | 516 base::Closure complete) { |
410 cookie_monster_->GetAllCookiesAsync( | 517 cookie_monster_->GetAllCookiesAsync( |
411 base::Bind(&CookieManager::HasCookiesCompleted, | 518 base::Bind(&CookieManager::HasCookiesCompleted, |
412 base::Unretained(this), | 519 base::Unretained(this), |
413 completion, | 520 complete, |
414 result)); | 521 result)); |
415 } | 522 } |
416 | 523 |
417 void CookieManager::HasCookiesCompleted(base::WaitableEvent* completion, | 524 void CookieManager::HasCookiesCompleted(base::Closure complete, |
418 bool* result, | 525 bool* result, |
419 const CookieList& cookies) { | 526 const CookieList& cookies) { |
420 *result = cookies.size() != 0; | 527 *result = cookies.size() != 0; |
421 completion->Signal(); | 528 complete.Run(); |
422 } | 529 } |
423 | 530 |
424 bool CookieManager::AllowFileSchemeCookies() { | 531 bool CookieManager::AllowFileSchemeCookies() { |
425 base::AutoLock lock(cookie_monster_lock_); | 532 base::AutoLock lock(cookie_monster_lock_); |
426 EnsureCookieMonsterExistsLocked(); | 533 EnsureCookieMonsterExistsLocked(); |
427 return AllowFileSchemeCookiesLocked(); | 534 return AllowFileSchemeCookiesLocked(); |
428 } | 535 } |
429 | 536 |
430 bool CookieManager::AllowFileSchemeCookiesLocked() { | 537 bool CookieManager::AllowFileSchemeCookiesLocked() { |
431 return cookie_monster_->IsCookieableScheme(content::kFileScheme); | 538 return cookie_monster_->IsCookieableScheme(content::kFileScheme); |
(...skipping 28 matching lines...) Expand all Loading... |
460 static void SetAcceptThirdPartyCookie(JNIEnv* env, | 567 static void SetAcceptThirdPartyCookie(JNIEnv* env, |
461 jobject obj, | 568 jobject obj, |
462 jboolean accept) { | 569 jboolean accept) { |
463 CookieManager::GetInstance()->SetAcceptThirdPartyCookie(accept); | 570 CookieManager::GetInstance()->SetAcceptThirdPartyCookie(accept); |
464 } | 571 } |
465 | 572 |
466 static jboolean AcceptThirdPartyCookie(JNIEnv* env, jobject obj) { | 573 static jboolean AcceptThirdPartyCookie(JNIEnv* env, jobject obj) { |
467 return CookieManager::GetInstance()->AcceptThirdPartyCookie(); | 574 return CookieManager::GetInstance()->AcceptThirdPartyCookie(); |
468 } | 575 } |
469 | 576 |
470 static void SetCookie(JNIEnv* env, jobject obj, jstring url, jstring value) { | 577 static void SetCookie(JNIEnv* env, |
| 578 jobject obj, |
| 579 jstring url, |
| 580 jstring value, |
| 581 jobject java_callback) { |
| 582 GURL host(ConvertJavaStringToUTF16(env, url)); |
| 583 std::string cookie_value(ConvertJavaStringToUTF8(env, value)); |
| 584 scoped_ptr<BoolCookieCallbackHolder> callback( |
| 585 new BoolCookieCallbackHolder(env, java_callback)); |
| 586 CookieManager::GetInstance()->SetCookie(host, cookie_value, callback.Pass()); |
| 587 } |
| 588 |
| 589 static void SetCookieSync(JNIEnv* env, |
| 590 jobject obj, |
| 591 jstring url, |
| 592 jstring value) { |
471 GURL host(ConvertJavaStringToUTF16(env, url)); | 593 GURL host(ConvertJavaStringToUTF16(env, url)); |
472 std::string cookie_value(ConvertJavaStringToUTF8(env, value)); | 594 std::string cookie_value(ConvertJavaStringToUTF8(env, value)); |
473 | 595 |
474 CookieManager::GetInstance()->SetCookie(host, cookie_value); | 596 CookieManager::GetInstance()->SetCookieSync(host, cookie_value); |
475 } | 597 } |
476 | 598 |
477 static jstring GetCookie(JNIEnv* env, jobject obj, jstring url) { | 599 static jstring GetCookie(JNIEnv* env, jobject obj, jstring url) { |
478 GURL host(ConvertJavaStringToUTF16(env, url)); | 600 GURL host(ConvertJavaStringToUTF16(env, url)); |
479 | 601 |
480 return base::android::ConvertUTF8ToJavaString( | 602 return base::android::ConvertUTF8ToJavaString( |
481 env, | 603 env, |
482 CookieManager::GetInstance()->GetCookie(host)).Release(); | 604 CookieManager::GetInstance()->GetCookie(host)).Release(); |
483 } | 605 } |
484 | 606 |
485 static void RemoveSessionCookie(JNIEnv* env, jobject obj) { | 607 static void RemoveSessionCookies(JNIEnv* env, |
486 CookieManager::GetInstance()->RemoveSessionCookie(); | 608 jobject obj, |
| 609 jobject java_callback) { |
| 610 scoped_ptr<BoolCookieCallbackHolder> callback( |
| 611 new BoolCookieCallbackHolder(env, java_callback)); |
| 612 CookieManager::GetInstance()->RemoveSessionCookies(callback.Pass()); |
487 } | 613 } |
488 | 614 |
489 static void RemoveAllCookie(JNIEnv* env, jobject obj) { | 615 static void RemoveSessionCookiesSync(JNIEnv* env, jobject obj) { |
490 CookieManager::GetInstance()->RemoveAllCookie(); | 616 CookieManager::GetInstance()->RemoveSessionCookiesSync(); |
491 } | 617 } |
492 | 618 |
493 static void RemoveExpiredCookie(JNIEnv* env, jobject obj) { | 619 static void RemoveAllCookies(JNIEnv* env, jobject obj, jobject java_callback) { |
494 CookieManager::GetInstance()->RemoveExpiredCookie(); | 620 scoped_ptr<BoolCookieCallbackHolder> callback( |
| 621 new BoolCookieCallbackHolder(env, java_callback)); |
| 622 CookieManager::GetInstance()->RemoveAllCookies(callback.Pass()); |
| 623 } |
| 624 |
| 625 static void RemoveAllCookiesSync(JNIEnv* env, jobject obj) { |
| 626 CookieManager::GetInstance()->RemoveAllCookiesSync(); |
| 627 } |
| 628 |
| 629 static void RemoveExpiredCookies(JNIEnv* env, jobject obj) { |
| 630 CookieManager::GetInstance()->RemoveExpiredCookies(); |
495 } | 631 } |
496 | 632 |
497 static void FlushCookieStore(JNIEnv* env, jobject obj) { | 633 static void FlushCookieStore(JNIEnv* env, jobject obj) { |
498 CookieManager::GetInstance()->FlushCookieStore(); | 634 CookieManager::GetInstance()->FlushCookieStore(); |
499 } | 635 } |
500 | 636 |
501 static jboolean HasCookies(JNIEnv* env, jobject obj) { | 637 static jboolean HasCookies(JNIEnv* env, jobject obj) { |
502 return CookieManager::GetInstance()->HasCookies(); | 638 return CookieManager::GetInstance()->HasCookies(); |
503 } | 639 } |
504 | 640 |
(...skipping 10 matching lines...) Expand all Loading... |
515 AwBrowserContext* browser_context) { | 651 AwBrowserContext* browser_context) { |
516 return CookieManager::GetInstance()->CreateBrowserThreadCookieStore( | 652 return CookieManager::GetInstance()->CreateBrowserThreadCookieStore( |
517 browser_context); | 653 browser_context); |
518 } | 654 } |
519 | 655 |
520 bool RegisterCookieManager(JNIEnv* env) { | 656 bool RegisterCookieManager(JNIEnv* env) { |
521 return RegisterNativesImpl(env); | 657 return RegisterNativesImpl(env); |
522 } | 658 } |
523 | 659 |
524 } // android_webview namespace | 660 } // android_webview namespace |
OLD | NEW |