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 <memory> |
7 #include <utility> | 8 #include <utility> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "android_webview/browser/aw_browser_context.h" | 11 #include "android_webview/browser/aw_browser_context.h" |
11 #include "android_webview/browser/aw_cookie_access_policy.h" | 12 #include "android_webview/browser/aw_cookie_access_policy.h" |
12 #include "android_webview/browser/net/init_native_callback.h" | 13 #include "android_webview/browser/net/init_native_callback.h" |
13 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h" | 14 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h" |
14 #include "android_webview/native/aw_browser_dependency_factory.h" | 15 #include "android_webview/native/aw_browser_dependency_factory.h" |
15 #include "base/android/jni_string.h" | 16 #include "base/android/jni_string.h" |
16 #include "base/android/path_utils.h" | 17 #include "base/android/path_utils.h" |
17 #include "base/bind.h" | 18 #include "base/bind.h" |
18 #include "base/bind_helpers.h" | 19 #include "base/bind_helpers.h" |
19 #include "base/files/file_path.h" | 20 #include "base/files/file_path.h" |
20 #include "base/files/file_util.h" | 21 #include "base/files/file_util.h" |
21 #include "base/lazy_instance.h" | 22 #include "base/lazy_instance.h" |
22 #include "base/location.h" | 23 #include "base/location.h" |
23 #include "base/logging.h" | 24 #include "base/logging.h" |
24 #include "base/memory/ref_counted.h" | 25 #include "base/memory/ref_counted.h" |
25 #include "base/memory/scoped_ptr.h" | |
26 #include "base/path_service.h" | 26 #include "base/path_service.h" |
27 #include "base/single_thread_task_runner.h" | 27 #include "base/single_thread_task_runner.h" |
28 #include "base/synchronization/lock.h" | 28 #include "base/synchronization/lock.h" |
29 #include "base/synchronization/waitable_event.h" | 29 #include "base/synchronization/waitable_event.h" |
30 #include "base/threading/sequenced_worker_pool.h" | 30 #include "base/threading/sequenced_worker_pool.h" |
31 #include "base/threading/thread.h" | 31 #include "base/threading/thread.h" |
32 #include "base/threading/thread_restrictions.h" | 32 #include "base/threading/thread_restrictions.h" |
33 #include "content/public/browser/browser_context.h" | 33 #include "content/public/browser/browser_context.h" |
34 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
35 #include "content/public/browser/cookie_store_factory.h" | 35 #include "content/public/browser/cookie_store_factory.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 | 76 |
77 void Invoke(bool result) { | 77 void Invoke(bool result) { |
78 if (!callback_.is_null()) { | 78 if (!callback_.is_null()) { |
79 JNIEnv* env = base::android::AttachCurrentThread(); | 79 JNIEnv* env = base::android::AttachCurrentThread(); |
80 Java_AwCookieManager_invokeBooleanCookieCallback(env, callback_.obj(), | 80 Java_AwCookieManager_invokeBooleanCookieCallback(env, callback_.obj(), |
81 result); | 81 result); |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
85 static BoolCallback ConvertToCallback( | 85 static BoolCallback ConvertToCallback( |
86 scoped_ptr<BoolCookieCallbackHolder> me) { | 86 std::unique_ptr<BoolCookieCallbackHolder> me) { |
87 return base::Bind(&BoolCookieCallbackHolder::Invoke, | 87 return base::Bind(&BoolCookieCallbackHolder::Invoke, |
88 base::Owned(me.release())); | 88 base::Owned(me.release())); |
89 } | 89 } |
90 | 90 |
91 private: | 91 private: |
92 ScopedJavaGlobalRef<jobject> callback_; | 92 ScopedJavaGlobalRef<jobject> callback_; |
93 DISALLOW_COPY_AND_ASSIGN(BoolCookieCallbackHolder); | 93 DISALLOW_COPY_AND_ASSIGN(BoolCookieCallbackHolder); |
94 }; | 94 }; |
95 | 95 |
96 // Construct a closure which signals a waitable event if and when the closure | 96 // Construct a closure which signals a waitable event if and when the closure |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 // Returns the TaskRunner on which the CookieStore lives. | 160 // Returns the TaskRunner on which the CookieStore lives. |
161 base::SingleThreadTaskRunner* GetCookieStoreTaskRunner(); | 161 base::SingleThreadTaskRunner* GetCookieStoreTaskRunner(); |
162 // Returns the CookieStore, creating it if necessary. This must only be called | 162 // Returns the CookieStore, creating it if necessary. This must only be called |
163 // on the CookieStore TaskRunner. | 163 // on the CookieStore TaskRunner. |
164 net::CookieStore* GetCookieStore(); | 164 net::CookieStore* GetCookieStore(); |
165 | 165 |
166 void SetShouldAcceptCookies(bool accept); | 166 void SetShouldAcceptCookies(bool accept); |
167 bool GetShouldAcceptCookies(); | 167 bool GetShouldAcceptCookies(); |
168 void SetCookie(const GURL& host, | 168 void SetCookie(const GURL& host, |
169 const std::string& cookie_value, | 169 const std::string& cookie_value, |
170 scoped_ptr<BoolCookieCallbackHolder> callback); | 170 std::unique_ptr<BoolCookieCallbackHolder> callback); |
171 void SetCookieSync(const GURL& host, const std::string& cookie_value); | 171 void SetCookieSync(const GURL& host, const std::string& cookie_value); |
172 std::string GetCookie(const GURL& host); | 172 std::string GetCookie(const GURL& host); |
173 void RemoveSessionCookies(scoped_ptr<BoolCookieCallbackHolder> callback); | 173 void RemoveSessionCookies(std::unique_ptr<BoolCookieCallbackHolder> callback); |
174 void RemoveAllCookies(scoped_ptr<BoolCookieCallbackHolder> callback); | 174 void RemoveAllCookies(std::unique_ptr<BoolCookieCallbackHolder> callback); |
175 void RemoveAllCookiesSync(); | 175 void RemoveAllCookiesSync(); |
176 void RemoveSessionCookiesSync(); | 176 void RemoveSessionCookiesSync(); |
177 void RemoveExpiredCookies(); | 177 void RemoveExpiredCookies(); |
178 void FlushCookieStore(); | 178 void FlushCookieStore(); |
179 bool HasCookies(); | 179 bool HasCookies(); |
180 bool AllowFileSchemeCookies(); | 180 bool AllowFileSchemeCookies(); |
181 void SetAcceptFileSchemeCookies(bool accept); | 181 void SetAcceptFileSchemeCookies(bool accept); |
182 | 182 |
183 private: | 183 private: |
184 friend struct base::DefaultLazyInstanceTraits<CookieManager>; | 184 friend struct base::DefaultLazyInstanceTraits<CookieManager>; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 // to creating the CookieStore. | 219 // to creating the CookieStore. |
220 bool accept_file_scheme_cookies_; | 220 bool accept_file_scheme_cookies_; |
221 // True once the cookie store has been created. Just used to track when | 221 // True once the cookie store has been created. Just used to track when |
222 // |accept_file_scheme_cookies_| can no longer be modified. | 222 // |accept_file_scheme_cookies_| can no longer be modified. |
223 bool cookie_store_created_; | 223 bool cookie_store_created_; |
224 | 224 |
225 base::Thread cookie_store_client_thread_; | 225 base::Thread cookie_store_client_thread_; |
226 base::Thread cookie_store_backend_thread_; | 226 base::Thread cookie_store_backend_thread_; |
227 | 227 |
228 scoped_refptr<base::SingleThreadTaskRunner> cookie_store_task_runner_; | 228 scoped_refptr<base::SingleThreadTaskRunner> cookie_store_task_runner_; |
229 scoped_ptr<net::CookieStore> cookie_store_; | 229 std::unique_ptr<net::CookieStore> cookie_store_; |
230 | 230 |
231 DISALLOW_COPY_AND_ASSIGN(CookieManager); | 231 DISALLOW_COPY_AND_ASSIGN(CookieManager); |
232 }; | 232 }; |
233 | 233 |
234 base::LazyInstance<CookieManager>::Leaky g_lazy_instance; | 234 base::LazyInstance<CookieManager>::Leaky g_lazy_instance; |
235 | 235 |
236 } // namespace | 236 } // namespace |
237 | 237 |
238 // static | 238 // static |
239 CookieManager* CookieManager::GetInstance() { | 239 CookieManager* CookieManager::GetInstance() { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 AwCookieAccessPolicy::GetInstance()->SetShouldAcceptCookies(accept); | 350 AwCookieAccessPolicy::GetInstance()->SetShouldAcceptCookies(accept); |
351 } | 351 } |
352 | 352 |
353 bool CookieManager::GetShouldAcceptCookies() { | 353 bool CookieManager::GetShouldAcceptCookies() { |
354 return AwCookieAccessPolicy::GetInstance()->GetShouldAcceptCookies(); | 354 return AwCookieAccessPolicy::GetInstance()->GetShouldAcceptCookies(); |
355 } | 355 } |
356 | 356 |
357 void CookieManager::SetCookie( | 357 void CookieManager::SetCookie( |
358 const GURL& host, | 358 const GURL& host, |
359 const std::string& cookie_value, | 359 const std::string& cookie_value, |
360 scoped_ptr<BoolCookieCallbackHolder> callback_holder) { | 360 std::unique_ptr<BoolCookieCallbackHolder> callback_holder) { |
361 BoolCallback callback = | 361 BoolCallback callback = |
362 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); | 362 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); |
363 ExecCookieTask(base::Bind(&CookieManager::SetCookieHelper, | 363 ExecCookieTask(base::Bind(&CookieManager::SetCookieHelper, |
364 base::Unretained(this), | 364 base::Unretained(this), |
365 host, | 365 host, |
366 cookie_value, | 366 cookie_value, |
367 callback)); | 367 callback)); |
368 } | 368 } |
369 | 369 |
370 void CookieManager::SetCookieSync(const GURL& host, | 370 void CookieManager::SetCookieSync(const GURL& host, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 } | 407 } |
408 | 408 |
409 void CookieManager::GetCookieValueCompleted(base::Closure complete, | 409 void CookieManager::GetCookieValueCompleted(base::Closure complete, |
410 std::string* result, | 410 std::string* result, |
411 const std::string& value) { | 411 const std::string& value) { |
412 *result = value; | 412 *result = value; |
413 complete.Run(); | 413 complete.Run(); |
414 } | 414 } |
415 | 415 |
416 void CookieManager::RemoveSessionCookies( | 416 void CookieManager::RemoveSessionCookies( |
417 scoped_ptr<BoolCookieCallbackHolder> callback_holder) { | 417 std::unique_ptr<BoolCookieCallbackHolder> callback_holder) { |
418 BoolCallback callback = | 418 BoolCallback callback = |
419 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); | 419 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); |
420 ExecCookieTask(base::Bind(&CookieManager::RemoveSessionCookiesHelper, | 420 ExecCookieTask(base::Bind(&CookieManager::RemoveSessionCookiesHelper, |
421 base::Unretained(this), | 421 base::Unretained(this), |
422 callback)); | 422 callback)); |
423 } | 423 } |
424 | 424 |
425 void CookieManager::RemoveSessionCookiesSync() { | 425 void CookieManager::RemoveSessionCookiesSync() { |
426 ExecCookieTaskSync(base::Bind(&CookieManager::RemoveSessionCookiesHelper, | 426 ExecCookieTaskSync(base::Bind(&CookieManager::RemoveSessionCookiesHelper, |
427 base::Unretained(this))); | 427 base::Unretained(this))); |
428 } | 428 } |
429 | 429 |
430 void CookieManager::RemoveSessionCookiesHelper( | 430 void CookieManager::RemoveSessionCookiesHelper( |
431 BoolCallback callback) { | 431 BoolCallback callback) { |
432 GetCookieStore()->DeleteSessionCookiesAsync( | 432 GetCookieStore()->DeleteSessionCookiesAsync( |
433 base::Bind(&CookieManager::RemoveCookiesCompleted, base::Unretained(this), | 433 base::Bind(&CookieManager::RemoveCookiesCompleted, base::Unretained(this), |
434 callback)); | 434 callback)); |
435 } | 435 } |
436 | 436 |
437 void CookieManager::RemoveCookiesCompleted( | 437 void CookieManager::RemoveCookiesCompleted( |
438 BoolCallback callback, | 438 BoolCallback callback, |
439 int num_deleted) { | 439 int num_deleted) { |
440 callback.Run(num_deleted > 0); | 440 callback.Run(num_deleted > 0); |
441 } | 441 } |
442 | 442 |
443 void CookieManager::RemoveAllCookies( | 443 void CookieManager::RemoveAllCookies( |
444 scoped_ptr<BoolCookieCallbackHolder> callback_holder) { | 444 std::unique_ptr<BoolCookieCallbackHolder> callback_holder) { |
445 BoolCallback callback = | 445 BoolCallback callback = |
446 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); | 446 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); |
447 ExecCookieTask(base::Bind(&CookieManager::RemoveAllCookiesHelper, | 447 ExecCookieTask(base::Bind(&CookieManager::RemoveAllCookiesHelper, |
448 base::Unretained(this), | 448 base::Unretained(this), |
449 callback)); | 449 callback)); |
450 } | 450 } |
451 | 451 |
452 void CookieManager::RemoveAllCookiesSync() { | 452 void CookieManager::RemoveAllCookiesSync() { |
453 ExecCookieTaskSync(base::Bind(&CookieManager::RemoveAllCookiesHelper, | 453 ExecCookieTaskSync(base::Bind(&CookieManager::RemoveAllCookiesHelper, |
454 base::Unretained(this))); | 454 base::Unretained(this))); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 return CookieManager::GetInstance()->GetShouldAcceptCookies(); | 523 return CookieManager::GetInstance()->GetShouldAcceptCookies(); |
524 } | 524 } |
525 | 525 |
526 static void SetCookie(JNIEnv* env, | 526 static void SetCookie(JNIEnv* env, |
527 const JavaParamRef<jobject>& obj, | 527 const JavaParamRef<jobject>& obj, |
528 const JavaParamRef<jstring>& url, | 528 const JavaParamRef<jstring>& url, |
529 const JavaParamRef<jstring>& value, | 529 const JavaParamRef<jstring>& value, |
530 const JavaParamRef<jobject>& java_callback) { | 530 const JavaParamRef<jobject>& java_callback) { |
531 GURL host(ConvertJavaStringToUTF16(env, url)); | 531 GURL host(ConvertJavaStringToUTF16(env, url)); |
532 std::string cookie_value(ConvertJavaStringToUTF8(env, value)); | 532 std::string cookie_value(ConvertJavaStringToUTF8(env, value)); |
533 scoped_ptr<BoolCookieCallbackHolder> callback( | 533 std::unique_ptr<BoolCookieCallbackHolder> callback( |
534 new BoolCookieCallbackHolder(env, java_callback)); | 534 new BoolCookieCallbackHolder(env, java_callback)); |
535 CookieManager::GetInstance()->SetCookie(host, cookie_value, | 535 CookieManager::GetInstance()->SetCookie(host, cookie_value, |
536 std::move(callback)); | 536 std::move(callback)); |
537 } | 537 } |
538 | 538 |
539 static void SetCookieSync(JNIEnv* env, | 539 static void SetCookieSync(JNIEnv* env, |
540 const JavaParamRef<jobject>& obj, | 540 const JavaParamRef<jobject>& obj, |
541 const JavaParamRef<jstring>& url, | 541 const JavaParamRef<jstring>& url, |
542 const JavaParamRef<jstring>& value) { | 542 const JavaParamRef<jstring>& value) { |
543 GURL host(ConvertJavaStringToUTF16(env, url)); | 543 GURL host(ConvertJavaStringToUTF16(env, url)); |
544 std::string cookie_value(ConvertJavaStringToUTF8(env, value)); | 544 std::string cookie_value(ConvertJavaStringToUTF8(env, value)); |
545 | 545 |
546 CookieManager::GetInstance()->SetCookieSync(host, cookie_value); | 546 CookieManager::GetInstance()->SetCookieSync(host, cookie_value); |
547 } | 547 } |
548 | 548 |
549 static ScopedJavaLocalRef<jstring> GetCookie(JNIEnv* env, | 549 static ScopedJavaLocalRef<jstring> GetCookie(JNIEnv* env, |
550 const JavaParamRef<jobject>& obj, | 550 const JavaParamRef<jobject>& obj, |
551 const JavaParamRef<jstring>& url) { | 551 const JavaParamRef<jstring>& url) { |
552 GURL host(ConvertJavaStringToUTF16(env, url)); | 552 GURL host(ConvertJavaStringToUTF16(env, url)); |
553 | 553 |
554 return base::android::ConvertUTF8ToJavaString( | 554 return base::android::ConvertUTF8ToJavaString( |
555 env, CookieManager::GetInstance()->GetCookie(host)); | 555 env, CookieManager::GetInstance()->GetCookie(host)); |
556 } | 556 } |
557 | 557 |
558 static void RemoveSessionCookies(JNIEnv* env, | 558 static void RemoveSessionCookies(JNIEnv* env, |
559 const JavaParamRef<jobject>& obj, | 559 const JavaParamRef<jobject>& obj, |
560 const JavaParamRef<jobject>& java_callback) { | 560 const JavaParamRef<jobject>& java_callback) { |
561 scoped_ptr<BoolCookieCallbackHolder> callback( | 561 std::unique_ptr<BoolCookieCallbackHolder> callback( |
562 new BoolCookieCallbackHolder(env, java_callback)); | 562 new BoolCookieCallbackHolder(env, java_callback)); |
563 CookieManager::GetInstance()->RemoveSessionCookies(std::move(callback)); | 563 CookieManager::GetInstance()->RemoveSessionCookies(std::move(callback)); |
564 } | 564 } |
565 | 565 |
566 static void RemoveSessionCookiesSync(JNIEnv* env, | 566 static void RemoveSessionCookiesSync(JNIEnv* env, |
567 const JavaParamRef<jobject>& obj) { | 567 const JavaParamRef<jobject>& obj) { |
568 CookieManager::GetInstance()->RemoveSessionCookiesSync(); | 568 CookieManager::GetInstance()->RemoveSessionCookiesSync(); |
569 } | 569 } |
570 | 570 |
571 static void RemoveAllCookies(JNIEnv* env, | 571 static void RemoveAllCookies(JNIEnv* env, |
572 const JavaParamRef<jobject>& obj, | 572 const JavaParamRef<jobject>& obj, |
573 const JavaParamRef<jobject>& java_callback) { | 573 const JavaParamRef<jobject>& java_callback) { |
574 scoped_ptr<BoolCookieCallbackHolder> callback( | 574 std::unique_ptr<BoolCookieCallbackHolder> callback( |
575 new BoolCookieCallbackHolder(env, java_callback)); | 575 new BoolCookieCallbackHolder(env, java_callback)); |
576 CookieManager::GetInstance()->RemoveAllCookies(std::move(callback)); | 576 CookieManager::GetInstance()->RemoveAllCookies(std::move(callback)); |
577 } | 577 } |
578 | 578 |
579 static void RemoveAllCookiesSync(JNIEnv* env, | 579 static void RemoveAllCookiesSync(JNIEnv* env, |
580 const JavaParamRef<jobject>& obj) { | 580 const JavaParamRef<jobject>& obj) { |
581 CookieManager::GetInstance()->RemoveAllCookiesSync(); | 581 CookieManager::GetInstance()->RemoveAllCookiesSync(); |
582 } | 582 } |
583 | 583 |
584 static void RemoveExpiredCookies(JNIEnv* env, | 584 static void RemoveExpiredCookies(JNIEnv* env, |
(...skipping 30 matching lines...) Expand all Loading... |
615 | 615 |
616 net::CookieStore* GetCookieStore() { | 616 net::CookieStore* GetCookieStore() { |
617 return CookieManager::GetInstance()->GetCookieStore(); | 617 return CookieManager::GetInstance()->GetCookieStore(); |
618 } | 618 } |
619 | 619 |
620 bool RegisterCookieManager(JNIEnv* env) { | 620 bool RegisterCookieManager(JNIEnv* env) { |
621 return RegisterNativesImpl(env); | 621 return RegisterNativesImpl(env); |
622 } | 622 } |
623 | 623 |
624 } // android_webview namespace | 624 } // android_webview namespace |
OLD | NEW |