| 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/browser/cookie_manager.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "android_webview/browser/aw_browser_context.h" | 11 #include "android_webview/browser/aw_browser_context.h" |
| 12 #include "android_webview/browser/aw_cookie_access_policy.h" | 12 #include "android_webview/browser/aw_cookie_access_policy.h" |
| 13 #include "android_webview/browser/net/init_native_callback.h" | 13 #include "android_webview/browser/net/init_native_callback.h" |
| 14 #include "base/android/jni_string.h" | 14 #include "base/android/jni_string.h" |
| 15 #include "base/android/path_utils.h" | 15 #include "base/android/path_utils.h" |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 CookieManager::CookieManager() | 245 CookieManager::CookieManager() |
| 246 : accept_file_scheme_cookies_(kDefaultFileSchemeAllowed), | 246 : accept_file_scheme_cookies_(kDefaultFileSchemeAllowed), |
| 247 cookie_store_created_(false), | 247 cookie_store_created_(false), |
| 248 cookie_store_client_thread_("CookieMonsterClient"), | 248 cookie_store_client_thread_("CookieMonsterClient"), |
| 249 cookie_store_backend_thread_("CookieMonsterBackend") { | 249 cookie_store_backend_thread_("CookieMonsterBackend") { |
| 250 cookie_store_client_thread_.Start(); | 250 cookie_store_client_thread_.Start(); |
| 251 cookie_store_backend_thread_.Start(); | 251 cookie_store_backend_thread_.Start(); |
| 252 cookie_store_task_runner_ = cookie_store_client_thread_.task_runner(); | 252 cookie_store_task_runner_ = cookie_store_client_thread_.task_runner(); |
| 253 } | 253 } |
| 254 | 254 |
| 255 CookieManager::~CookieManager() { | 255 CookieManager::~CookieManager() {} |
| 256 } | |
| 257 | 256 |
| 258 // Executes the |task| on |cookie_store_task_runner_| and waits for it to | 257 // Executes the |task| on |cookie_store_task_runner_| and waits for it to |
| 259 // complete before returning. | 258 // complete before returning. |
| 260 // | 259 // |
| 261 // To execute a CookieTask synchronously you must arrange for Signal to be | 260 // To execute a CookieTask synchronously you must arrange for Signal to be |
| 262 // called on the waitable event at some point. You can call the bool or int | 261 // called on the waitable event at some point. You can call the bool or int |
| 263 // versions of ExecCookieTaskSync, these will supply the caller with a dummy | 262 // versions of ExecCookieTaskSync, these will supply the caller with a dummy |
| 264 // callback which takes an int/bool, throws it away and calls Signal. | 263 // callback which takes an int/bool, throws it away and calls Signal. |
| 265 // Alternatively you can call the version which supplies a Closure in which | 264 // Alternatively you can call the version which supplies a Closure in which |
| 266 // case you must call Run on it when you want the unblock the calling code. | 265 // case you must call Run on it when you want the unblock the calling code. |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 return AwCookieAccessPolicy::GetInstance()->GetShouldAcceptCookies(); | 358 return AwCookieAccessPolicy::GetInstance()->GetShouldAcceptCookies(); |
| 360 } | 359 } |
| 361 | 360 |
| 362 void CookieManager::SetCookie( | 361 void CookieManager::SetCookie( |
| 363 const GURL& host, | 362 const GURL& host, |
| 364 const std::string& cookie_value, | 363 const std::string& cookie_value, |
| 365 std::unique_ptr<BoolCookieCallbackHolder> callback_holder) { | 364 std::unique_ptr<BoolCookieCallbackHolder> callback_holder) { |
| 366 BoolCallback callback = | 365 BoolCallback callback = |
| 367 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); | 366 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); |
| 368 ExecCookieTask(base::Bind(&CookieManager::SetCookieHelper, | 367 ExecCookieTask(base::Bind(&CookieManager::SetCookieHelper, |
| 369 base::Unretained(this), | 368 base::Unretained(this), host, cookie_value, |
| 370 host, | |
| 371 cookie_value, | |
| 372 callback)); | 369 callback)); |
| 373 } | 370 } |
| 374 | 371 |
| 375 void CookieManager::SetCookieSync(const GURL& host, | 372 void CookieManager::SetCookieSync(const GURL& host, |
| 376 const std::string& cookie_value) { | 373 const std::string& cookie_value) { |
| 377 ExecCookieTaskSync(base::Bind(&CookieManager::SetCookieHelper, | 374 ExecCookieTaskSync(base::Bind(&CookieManager::SetCookieHelper, |
| 378 base::Unretained(this), | 375 base::Unretained(this), host, cookie_value)); |
| 379 host, | |
| 380 cookie_value)); | |
| 381 } | 376 } |
| 382 | 377 |
| 383 void CookieManager::SetCookieHelper( | 378 void CookieManager::SetCookieHelper(const GURL& host, |
| 384 const GURL& host, | 379 const std::string& value, |
| 385 const std::string& value, | 380 const BoolCallback callback) { |
| 386 const BoolCallback callback) { | |
| 387 net::CookieOptions options; | 381 net::CookieOptions options; |
| 388 options.set_include_httponly(); | 382 options.set_include_httponly(); |
| 389 | 383 |
| 390 // Log message for catching strict secure cookies related bugs. | 384 // Log message for catching strict secure cookies related bugs. |
| 391 // TODO(sgurun) temporary. Add UMA stats to monitor, and remove afterwards. | 385 // TODO(sgurun) temporary. Add UMA stats to monitor, and remove afterwards. |
| 392 if (host.is_valid() && | 386 if (host.is_valid() && |
| 393 (!host.has_scheme() || host.SchemeIs(url::kHttpScheme))) { | 387 (!host.has_scheme() || host.SchemeIs(url::kHttpScheme))) { |
| 394 net::ParsedCookie parsed_cookie(value); | 388 net::ParsedCookie parsed_cookie(value); |
| 395 if (parsed_cookie.IsValid() && parsed_cookie.IsSecure()) { | 389 if (parsed_cookie.IsValid() && parsed_cookie.IsSecure()) { |
| 396 LOG(WARNING) << "Strict Secure Cookie policy does not allow setting a " | 390 LOG(WARNING) << "Strict Secure Cookie policy does not allow setting a " |
| 397 "secure cookie for " | 391 "secure cookie for " |
| 398 << host.spec(); | 392 << host.spec(); |
| 399 GURL::Replacements replace_host; | 393 GURL::Replacements replace_host; |
| 400 replace_host.SetSchemeStr("https"); | 394 replace_host.SetSchemeStr("https"); |
| 401 GURL new_host = host.ReplaceComponents(replace_host); | 395 GURL new_host = host.ReplaceComponents(replace_host); |
| 402 GetCookieStore()->SetCookieWithOptionsAsync(new_host, value, options, | 396 GetCookieStore()->SetCookieWithOptionsAsync(new_host, value, options, |
| 403 callback); | 397 callback); |
| 404 return; | 398 return; |
| 405 } | 399 } |
| 406 } | 400 } |
| 407 | 401 |
| 408 GetCookieStore()->SetCookieWithOptionsAsync(host, value, options, callback); | 402 GetCookieStore()->SetCookieWithOptionsAsync(host, value, options, callback); |
| 409 } | 403 } |
| 410 | 404 |
| 411 std::string CookieManager::GetCookie(const GURL& host) { | 405 std::string CookieManager::GetCookie(const GURL& host) { |
| 412 std::string cookie_value; | 406 std::string cookie_value; |
| 413 ExecCookieTaskSync(base::Bind(&CookieManager::GetCookieValueAsyncHelper, | 407 ExecCookieTaskSync(base::Bind(&CookieManager::GetCookieValueAsyncHelper, |
| 414 base::Unretained(this), | 408 base::Unretained(this), host, &cookie_value)); |
| 415 host, | |
| 416 &cookie_value)); | |
| 417 return cookie_value; | 409 return cookie_value; |
| 418 } | 410 } |
| 419 | 411 |
| 420 void CookieManager::GetCookieValueAsyncHelper( | 412 void CookieManager::GetCookieValueAsyncHelper(const GURL& host, |
| 421 const GURL& host, | 413 std::string* result, |
| 422 std::string* result, | 414 base::Closure complete) { |
| 423 base::Closure complete) { | |
| 424 net::CookieOptions options; | 415 net::CookieOptions options; |
| 425 options.set_include_httponly(); | 416 options.set_include_httponly(); |
| 426 | 417 |
| 427 GetCookieStore()->GetCookiesWithOptionsAsync( | 418 GetCookieStore()->GetCookiesWithOptionsAsync( |
| 428 host, options, base::Bind(&CookieManager::GetCookieValueCompleted, | 419 host, options, |
| 429 base::Unretained(this), complete, result)); | 420 base::Bind(&CookieManager::GetCookieValueCompleted, |
| 421 base::Unretained(this), complete, result)); |
| 430 } | 422 } |
| 431 | 423 |
| 432 void CookieManager::GetCookieValueCompleted(base::Closure complete, | 424 void CookieManager::GetCookieValueCompleted(base::Closure complete, |
| 433 std::string* result, | 425 std::string* result, |
| 434 const std::string& value) { | 426 const std::string& value) { |
| 435 *result = value; | 427 *result = value; |
| 436 complete.Run(); | 428 complete.Run(); |
| 437 } | 429 } |
| 438 | 430 |
| 439 void CookieManager::RemoveSessionCookies( | 431 void CookieManager::RemoveSessionCookies( |
| 440 std::unique_ptr<BoolCookieCallbackHolder> callback_holder) { | 432 std::unique_ptr<BoolCookieCallbackHolder> callback_holder) { |
| 441 BoolCallback callback = | 433 BoolCallback callback = |
| 442 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); | 434 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); |
| 443 ExecCookieTask(base::Bind(&CookieManager::RemoveSessionCookiesHelper, | 435 ExecCookieTask(base::Bind(&CookieManager::RemoveSessionCookiesHelper, |
| 444 base::Unretained(this), | 436 base::Unretained(this), callback)); |
| 445 callback)); | |
| 446 } | 437 } |
| 447 | 438 |
| 448 void CookieManager::RemoveSessionCookiesSync() { | 439 void CookieManager::RemoveSessionCookiesSync() { |
| 449 ExecCookieTaskSync(base::Bind(&CookieManager::RemoveSessionCookiesHelper, | 440 ExecCookieTaskSync(base::Bind(&CookieManager::RemoveSessionCookiesHelper, |
| 450 base::Unretained(this))); | 441 base::Unretained(this))); |
| 451 } | 442 } |
| 452 | 443 |
| 453 void CookieManager::RemoveSessionCookiesHelper( | 444 void CookieManager::RemoveSessionCookiesHelper(BoolCallback callback) { |
| 454 BoolCallback callback) { | |
| 455 GetCookieStore()->DeleteSessionCookiesAsync( | 445 GetCookieStore()->DeleteSessionCookiesAsync( |
| 456 base::Bind(&CookieManager::RemoveCookiesCompleted, base::Unretained(this), | 446 base::Bind(&CookieManager::RemoveCookiesCompleted, base::Unretained(this), |
| 457 callback)); | 447 callback)); |
| 458 } | 448 } |
| 459 | 449 |
| 460 void CookieManager::RemoveCookiesCompleted( | 450 void CookieManager::RemoveCookiesCompleted(BoolCallback callback, |
| 461 BoolCallback callback, | 451 int num_deleted) { |
| 462 int num_deleted) { | |
| 463 callback.Run(num_deleted > 0); | 452 callback.Run(num_deleted > 0); |
| 464 } | 453 } |
| 465 | 454 |
| 466 void CookieManager::RemoveAllCookies( | 455 void CookieManager::RemoveAllCookies( |
| 467 std::unique_ptr<BoolCookieCallbackHolder> callback_holder) { | 456 std::unique_ptr<BoolCookieCallbackHolder> callback_holder) { |
| 468 BoolCallback callback = | 457 BoolCallback callback = |
| 469 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); | 458 BoolCookieCallbackHolder::ConvertToCallback(std::move(callback_holder)); |
| 470 ExecCookieTask(base::Bind(&CookieManager::RemoveAllCookiesHelper, | 459 ExecCookieTask(base::Bind(&CookieManager::RemoveAllCookiesHelper, |
| 471 base::Unretained(this), | 460 base::Unretained(this), callback)); |
| 472 callback)); | |
| 473 } | 461 } |
| 474 | 462 |
| 475 void CookieManager::RemoveAllCookiesSync() { | 463 void CookieManager::RemoveAllCookiesSync() { |
| 476 ExecCookieTaskSync(base::Bind(&CookieManager::RemoveAllCookiesHelper, | 464 ExecCookieTaskSync(base::Bind(&CookieManager::RemoveAllCookiesHelper, |
| 477 base::Unretained(this))); | 465 base::Unretained(this))); |
| 478 } | 466 } |
| 479 | 467 |
| 480 void CookieManager::RemoveAllCookiesHelper( | 468 void CookieManager::RemoveAllCookiesHelper(const BoolCallback callback) { |
| 481 const BoolCallback callback) { | |
| 482 GetCookieStore()->DeleteAllAsync( | 469 GetCookieStore()->DeleteAllAsync( |
| 483 base::Bind(&CookieManager::RemoveCookiesCompleted, base::Unretained(this), | 470 base::Bind(&CookieManager::RemoveCookiesCompleted, base::Unretained(this), |
| 484 callback)); | 471 callback)); |
| 485 } | 472 } |
| 486 | 473 |
| 487 void CookieManager::RemoveExpiredCookies() { | 474 void CookieManager::RemoveExpiredCookies() { |
| 488 // HasCookies will call GetAllCookiesAsync, which in turn will force a GC. | 475 // HasCookies will call GetAllCookiesAsync, which in turn will force a GC. |
| 489 HasCookies(); | 476 HasCookies(); |
| 490 } | 477 } |
| 491 | 478 |
| 492 void CookieManager::FlushCookieStore() { | 479 void CookieManager::FlushCookieStore() { |
| 493 ExecCookieTaskSync(base::Bind(&CookieManager::FlushCookieStoreAsyncHelper, | 480 ExecCookieTaskSync(base::Bind(&CookieManager::FlushCookieStoreAsyncHelper, |
| 494 base::Unretained(this))); | 481 base::Unretained(this))); |
| 495 } | 482 } |
| 496 | 483 |
| 497 void CookieManager::FlushCookieStoreAsyncHelper( | 484 void CookieManager::FlushCookieStoreAsyncHelper(base::Closure complete) { |
| 498 base::Closure complete) { | |
| 499 GetCookieStore()->FlushStore(complete); | 485 GetCookieStore()->FlushStore(complete); |
| 500 } | 486 } |
| 501 | 487 |
| 502 bool CookieManager::HasCookies() { | 488 bool CookieManager::HasCookies() { |
| 503 bool has_cookies; | 489 bool has_cookies; |
| 504 ExecCookieTaskSync(base::Bind(&CookieManager::HasCookiesAsyncHelper, | 490 ExecCookieTaskSync(base::Bind(&CookieManager::HasCookiesAsyncHelper, |
| 505 base::Unretained(this), | 491 base::Unretained(this), &has_cookies)); |
| 506 &has_cookies)); | |
| 507 return has_cookies; | 492 return has_cookies; |
| 508 } | 493 } |
| 509 | 494 |
| 510 // TODO(kristianm): Simplify this, copying the entire list around | 495 // TODO(kristianm): Simplify this, copying the entire list around |
| 511 // should not be needed. | 496 // should not be needed. |
| 512 void CookieManager::HasCookiesAsyncHelper(bool* result, | 497 void CookieManager::HasCookiesAsyncHelper(bool* result, |
| 513 base::Closure complete) { | 498 base::Closure complete) { |
| 514 GetCookieStore()->GetAllCookiesAsync( | 499 GetCookieStore()->GetAllCookiesAsync( |
| 515 base::Bind(&CookieManager::HasCookiesCompleted, base::Unretained(this), | 500 base::Bind(&CookieManager::HasCookiesCompleted, base::Unretained(this), |
| 516 complete, result)); | 501 complete, result)); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 static void RemoveSessionCookies(JNIEnv* env, | 566 static void RemoveSessionCookies(JNIEnv* env, |
| 582 const JavaParamRef<jobject>& obj, | 567 const JavaParamRef<jobject>& obj, |
| 583 const JavaParamRef<jobject>& java_callback) { | 568 const JavaParamRef<jobject>& java_callback) { |
| 584 std::unique_ptr<BoolCookieCallbackHolder> callback( | 569 std::unique_ptr<BoolCookieCallbackHolder> callback( |
| 585 new BoolCookieCallbackHolder(env, java_callback)); | 570 new BoolCookieCallbackHolder(env, java_callback)); |
| 586 CookieManager::GetInstance()->RemoveSessionCookies(std::move(callback)); | 571 CookieManager::GetInstance()->RemoveSessionCookies(std::move(callback)); |
| 587 } | 572 } |
| 588 | 573 |
| 589 static void RemoveSessionCookiesSync(JNIEnv* env, | 574 static void RemoveSessionCookiesSync(JNIEnv* env, |
| 590 const JavaParamRef<jobject>& obj) { | 575 const JavaParamRef<jobject>& obj) { |
| 591 CookieManager::GetInstance()->RemoveSessionCookiesSync(); | 576 CookieManager::GetInstance()->RemoveSessionCookiesSync(); |
| 592 } | 577 } |
| 593 | 578 |
| 594 static void RemoveAllCookies(JNIEnv* env, | 579 static void RemoveAllCookies(JNIEnv* env, |
| 595 const JavaParamRef<jobject>& obj, | 580 const JavaParamRef<jobject>& obj, |
| 596 const JavaParamRef<jobject>& java_callback) { | 581 const JavaParamRef<jobject>& java_callback) { |
| 597 std::unique_ptr<BoolCookieCallbackHolder> callback( | 582 std::unique_ptr<BoolCookieCallbackHolder> callback( |
| 598 new BoolCookieCallbackHolder(env, java_callback)); | 583 new BoolCookieCallbackHolder(env, java_callback)); |
| 599 CookieManager::GetInstance()->RemoveAllCookies(std::move(callback)); | 584 CookieManager::GetInstance()->RemoveAllCookies(std::move(callback)); |
| 600 } | 585 } |
| 601 | 586 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 | 623 |
| 639 net::CookieStore* GetCookieStore() { | 624 net::CookieStore* GetCookieStore() { |
| 640 return CookieManager::GetInstance()->GetCookieStore(); | 625 return CookieManager::GetInstance()->GetCookieStore(); |
| 641 } | 626 } |
| 642 | 627 |
| 643 bool RegisterCookieManager(JNIEnv* env) { | 628 bool RegisterCookieManager(JNIEnv* env) { |
| 644 return RegisterNativesImpl(env); | 629 return RegisterNativesImpl(env); |
| 645 } | 630 } |
| 646 | 631 |
| 647 } // android_webview namespace | 632 } // android_webview namespace |
| OLD | NEW |