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

Side by Side Diff: ios/net/cookies/cookie_store_ios.mm

Issue 1428673003: Injecting an NSHTTPCookieStorage dependency into CookieStoreIOS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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
« no previous file with comments | « ios/net/cookies/cookie_store_ios.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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 "ios/net/cookies/cookie_store_ios.h" 5 #include "ios/net/cookies/cookie_store_ios.h"
6 6
7 #import <Foundation/Foundation.h> 7 #import <Foundation/Foundation.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 // to cookies if one doesn't already exist. 193 // to cookies if one doesn't already exist.
194 DLOG_IF(ERROR, created_a.is_null() || created_b.is_null()) 194 DLOG_IF(ERROR, created_a.is_null() || created_b.is_null())
195 << "Cookie without creation date"; 195 << "Cookie without creation date";
196 #endif 196 #endif
197 if (created_a < created_b) 197 if (created_a < created_b)
198 return NSOrderedAscending; 198 return NSOrderedAscending;
199 return (created_a > created_b) ? NSOrderedDescending : NSOrderedSame; 199 return (created_a > created_b) ? NSOrderedDescending : NSOrderedSame;
200 } 200 }
201 201
202 // Gets the cookies for |url| from the system cookie store. 202 // Gets the cookies for |url| from the system cookie store.
203 NSArray* GetCookiesForURL(const GURL& url, CookieCreationTimeManager* manager) { 203 NSArray* GetCookiesForURL(NSHTTPCookieStorage* system_store,
204 NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] 204 const GURL& url, CookieCreationTimeManager* manager) {
205 cookiesForURL:net::NSURLWithGURL(url)]; 205 NSArray* cookies = [system_store cookiesForURL:net::NSURLWithGURL(url)];
206 206
207 // Sort cookies by decreasing path length, then creation time, as per RFC6265. 207 // Sort cookies by decreasing path length, then creation time, as per RFC6265.
208 return [cookies sortedArrayUsingFunction:CompareCookies context:manager]; 208 return [cookies sortedArrayUsingFunction:CompareCookies context:manager];
209 } 209 }
210 210
211 // Builds a cookie line (such as "key1=value1; key2=value2") from an array of 211 // Builds a cookie line (such as "key1=value1; key2=value2") from an array of
212 // cookies. 212 // cookies.
213 std::string BuildCookieLine(NSArray* cookies, 213 std::string BuildCookieLine(NSArray* cookies,
214 const net::CookieOptions& options) { 214 const net::CookieOptions& options) {
215 // The exclude_httponly() option would only be used by a javascript engine. 215 // The exclude_httponly() option would only be used by a javascript engine.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 return cookie.HasDomain(); 267 return cookie.HasDomain();
268 } 268 }
269 269
270 } // namespace 270 } // namespace
271 271
272 #pragma mark - 272 #pragma mark -
273 #pragma mark CookieStoreIOS 273 #pragma mark CookieStoreIOS
274 274
275 CookieStoreIOS::CookieStoreIOS( 275 CookieStoreIOS::CookieStoreIOS(
276 net::CookieMonster::PersistentCookieStore* persistent_store) 276 net::CookieMonster::PersistentCookieStore* persistent_store)
277 : creation_time_manager_(new CookieCreationTimeManager), 277 : CookieStoreIOS(persistent_store,
278 [NSHTTPCookieStorage sharedHTTPCookieStorage]) {
279 }
280
281 CookieStoreIOS::CookieStoreIOS(
282 net::CookieMonster::PersistentCookieStore* persistent_store,
283 NSHTTPCookieStorage* system_store)
284 : system_store_(system_store),
285 creation_time_manager_(new CookieCreationTimeManager),
278 metrics_enabled_(false), 286 metrics_enabled_(false),
279 flush_delay_(base::TimeDelta::FromSeconds(10)), 287 flush_delay_(base::TimeDelta::FromSeconds(10)),
280 synchronization_state_(NOT_SYNCHRONIZED), 288 synchronization_state_(NOT_SYNCHRONIZED),
281 cookie_cache_(new CookieCache()) { 289 cookie_cache_(new CookieCache()) {
290 DCHECK(system_store);
291
282 NotificationTrampoline::GetInstance()->AddObserver(this); 292 NotificationTrampoline::GetInstance()->AddObserver(this);
283 cookie_monster_ = new net::CookieMonster(persistent_store, nullptr); 293 cookie_monster_ = new net::CookieMonster(persistent_store, nullptr);
284 cookie_monster_->SetPersistSessionCookies(true); 294 cookie_monster_->SetPersistSessionCookies(true);
285 cookie_monster_->SetForceKeepSessionState(); 295 cookie_monster_->SetForceKeepSessionState();
286 } 296 }
287 297
288 // static 298 // static
289 void CookieStoreIOS::SetCookiePolicy(CookiePolicy setting) { 299 void CookieStoreIOS::SetCookiePolicy(CookiePolicy setting) {
290 NSHTTPCookieAcceptPolicy policy = (setting == ALLOW) 300 NSHTTPCookieAcceptPolicy policy = (setting == ALLOW)
291 ? NSHTTPCookieAcceptPolicyAlways 301 ? NSHTTPCookieAcceptPolicyAlways
292 : NSHTTPCookieAcceptPolicyNever; 302 : NSHTTPCookieAcceptPolicyNever;
293 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 303 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage];
droger 2015/10/30 17:02:51 Using sharedHTTPCookieStorage here is a bit scary.
droger 2015/10/30 17:02:51 Also, not for this CL, but you should make sure th
shreyasv1 2015/11/02 22:21:44 I don't really understand the need to pass a NSHTT
shreyasv1 2015/11/02 22:21:44 Acknowledged.
droger 2015/11/03 09:49:00 Ok, what you did works too. The important bit was
294 NSHTTPCookieAcceptPolicy current_policy = [store cookieAcceptPolicy]; 304 NSHTTPCookieAcceptPolicy current_policy = [store cookieAcceptPolicy];
295 if (current_policy == policy) 305 if (current_policy == policy)
296 return; 306 return;
297 [store setCookieAcceptPolicy:policy]; 307 [store setCookieAcceptPolicy:policy];
298 NotificationTrampoline::GetInstance()->NotifyCookiePolicyChanged(); 308 NotificationTrampoline::GetInstance()->NotifyCookiePolicyChanged();
299 } 309 }
300 310
301 CookieStoreIOS* CookieStoreIOS::CreateCookieStoreFromNSHTTPCookieStorage() { 311 // static
312 CookieStoreIOS* CookieStoreIOS::CreateCookieStore(
313 NSHTTPCookieStorage* cookie_storage) {
302 // TODO(huey): Update this when CrNet supports multiple cookie jars. 314 // TODO(huey): Update this when CrNet supports multiple cookie jars.
303 [[NSHTTPCookieStorage sharedHTTPCookieStorage] 315 [cookie_storage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
304 setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
305 316
306 // Create a cookie store with no persistent store backing. Then, populate 317 // Create a cookie store with no persistent store backing. Then, populate
307 // it from the system's cookie jar. 318 // it from the system's cookie jar.
308 CookieStoreIOS* cookie_store = new CookieStoreIOS(nullptr); 319 CookieStoreIOS* cookie_store = new CookieStoreIOS(nullptr);
309 cookie_store->synchronization_state_ = SYNCHRONIZED; 320 cookie_store->synchronization_state_ = SYNCHRONIZED;
310 cookie_store->Flush(base::Closure()); 321 cookie_store->Flush(base::Closure());
311 return cookie_store; 322 return cookie_store;
312 } 323 }
313 324
314 // static 325 // static
(...skipping 10 matching lines...) Expand all
325 void CookieStoreIOS::NotifySystemCookiesChanged() { 336 void CookieStoreIOS::NotifySystemCookiesChanged() {
326 NotificationTrampoline::GetInstance()->NotifyCookiesChanged(); 337 NotificationTrampoline::GetInstance()->NotifyCookiesChanged();
327 } 338 }
328 339
329 void CookieStoreIOS::Flush(const base::Closure& closure) { 340 void CookieStoreIOS::Flush(const base::Closure& closure) {
330 DCHECK(thread_checker_.CalledOnValidThread()); 341 DCHECK(thread_checker_.CalledOnValidThread());
331 342
332 if (SystemCookiesAllowed()) { 343 if (SystemCookiesAllowed()) {
333 // If cookies are disabled, the system store is empty, and the cookies are 344 // If cookies are disabled, the system store is empty, and the cookies are
334 // stashed on disk. Do not delete the cookies on the disk in this case. 345 // stashed on disk. Do not delete the cookies on the disk in this case.
335 WriteToCookieMonster( 346 WriteToCookieMonster([system_store_ cookies]);
336 [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]);
337 } 347 }
338 cookie_monster_->FlushStore(closure); 348 cookie_monster_->FlushStore(closure);
339 flush_closure_.Cancel(); 349 flush_closure_.Cancel();
340 } 350 }
341 351
342 void CookieStoreIOS::UnSynchronize() { 352 void CookieStoreIOS::UnSynchronize() {
343 SetSynchronizedWithSystemStore(false); 353 SetSynchronizedWithSystemStore(false);
344 } 354 }
345 355
346 void CookieStoreIOS::SetMetricsEnabled() { 356 void CookieStoreIOS::SetMetricsEnabled() {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 // b) The Domain attribute, if present, was not malformed 411 // b) The Domain attribute, if present, was not malformed
402 // c) At least one of: 412 // c) At least one of:
403 // 1) The cookie had no explicit Domain, so the Domain was inferred 413 // 1) The cookie had no explicit Domain, so the Domain was inferred
404 // from the URL, or 414 // from the URL, or
405 // 2) The cookie had an explicit Domain for which the URL is allowed 415 // 2) The cookie had an explicit Domain for which the URL is allowed
406 // to set cookies. 416 // to set cookies.
407 bool success = (cookie != nil) && !domain_string.empty() && 417 bool success = (cookie != nil) && !domain_string.empty() &&
408 (!has_explicit_domain || has_valid_domain); 418 (!has_explicit_domain || has_valid_domain);
409 419
410 if (success) { 420 if (success) {
411 [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; 421 [system_store_ setCookie:cookie];
412 creation_time_manager_->SetCreationTime( 422 creation_time_manager_->SetCreationTime(
413 cookie, 423 cookie,
414 creation_time_manager_->MakeUniqueCreationTime(base::Time::Now())); 424 creation_time_manager_->MakeUniqueCreationTime(base::Time::Now()));
415 } 425 }
416 426
417 if (!callback.is_null()) 427 if (!callback.is_null())
418 callback.Run(success); 428 callback.Run(success);
419 break; 429 break;
420 } 430 }
421 } 431 }
(...skipping 14 matching lines...) Expand all
436 options, callback)); 446 options, callback));
437 break; 447 break;
438 case SYNCHRONIZED: 448 case SYNCHRONIZED:
439 // If cookies are not allowed, they are stashed in the CookieMonster, and 449 // If cookies are not allowed, they are stashed in the CookieMonster, and
440 // should be read from there instead. 450 // should be read from there instead.
441 DCHECK(SystemCookiesAllowed()); 451 DCHECK(SystemCookiesAllowed());
442 // The exclude_httponly() option would only be used by a javascript 452 // The exclude_httponly() option would only be used by a javascript
443 // engine. 453 // engine.
444 DCHECK(!options.exclude_httponly()); 454 DCHECK(!options.exclude_httponly());
445 455
446 NSArray* cookies = GetCookiesForURL(url, creation_time_manager_.get()); 456 NSArray* cookies = GetCookiesForURL(system_store_,
457 url, creation_time_manager_.get());
447 if (!callback.is_null()) 458 if (!callback.is_null())
448 callback.Run(BuildCookieLine(cookies, options)); 459 callback.Run(BuildCookieLine(cookies, options));
449 break; 460 break;
450 } 461 }
451 } 462 }
452 463
453 void CookieStoreIOS::GetAllCookiesForURLAsync( 464 void CookieStoreIOS::GetAllCookiesForURLAsync(
454 const GURL& url, 465 const GURL& url,
455 const GetCookieListCallback& callback) { 466 const GetCookieListCallback& callback) {
456 DCHECK(thread_checker_.CalledOnValidThread()); 467 DCHECK(thread_checker_.CalledOnValidThread());
457 468
458 switch (synchronization_state_) { 469 switch (synchronization_state_) {
459 case NOT_SYNCHRONIZED: 470 case NOT_SYNCHRONIZED:
460 cookie_monster_->GetAllCookiesForURLAsync(url, callback); 471 cookie_monster_->GetAllCookiesForURLAsync(url, callback);
461 break; 472 break;
462 case SYNCHRONIZING: 473 case SYNCHRONIZING:
463 tasks_pending_synchronization_.push_back(base::Bind( 474 tasks_pending_synchronization_.push_back(base::Bind(
464 &CookieStoreIOS::GetAllCookiesForURLAsync, this, url, callback)); 475 &CookieStoreIOS::GetAllCookiesForURLAsync, this, url, callback));
465 break; 476 break;
466 case SYNCHRONIZED: 477 case SYNCHRONIZED:
467 if (!SystemCookiesAllowed()) { 478 if (!SystemCookiesAllowed()) {
468 // If cookies are not allowed, the cookies are stashed in the 479 // If cookies are not allowed, the cookies are stashed in the
469 // CookieMonster, so get them from there. 480 // CookieMonster, so get them from there.
470 cookie_monster_->GetAllCookiesForURLAsync(url, callback); 481 cookie_monster_->GetAllCookiesForURLAsync(url, callback);
471 return; 482 return;
472 } 483 }
473 484
474 NSArray* cookies = GetCookiesForURL(url, creation_time_manager_.get()); 485 NSArray* cookies = GetCookiesForURL(system_store_,
486 url, creation_time_manager_.get());
475 net::CookieList cookie_list; 487 net::CookieList cookie_list;
476 cookie_list.reserve([cookies count]); 488 cookie_list.reserve([cookies count]);
477 for (NSHTTPCookie* cookie in cookies) { 489 for (NSHTTPCookie* cookie in cookies) {
478 base::Time created = creation_time_manager_->GetCreationTime(cookie); 490 base::Time created = creation_time_manager_->GetCreationTime(cookie);
479 cookie_list.push_back(CanonicalCookieFromSystemCookie(cookie, created)); 491 cookie_list.push_back(CanonicalCookieFromSystemCookie(cookie, created));
480 } 492 }
481 if (!callback.is_null()) 493 if (!callback.is_null())
482 callback.Run(cookie_list); 494 callback.Run(cookie_list);
483 break; 495 break;
484 } 496 }
485 } 497 }
486 498
487 void CookieStoreIOS::DeleteCookieAsync(const GURL& url, 499 void CookieStoreIOS::DeleteCookieAsync(const GURL& url,
488 const std::string& cookie_name, 500 const std::string& cookie_name,
489 const base::Closure& callback) { 501 const base::Closure& callback) {
490 DCHECK(thread_checker_.CalledOnValidThread()); 502 DCHECK(thread_checker_.CalledOnValidThread());
491 503
492 switch (synchronization_state_) { 504 switch (synchronization_state_) {
493 case NOT_SYNCHRONIZED: 505 case NOT_SYNCHRONIZED:
494 cookie_monster_->DeleteCookieAsync(url, cookie_name, 506 cookie_monster_->DeleteCookieAsync(url, cookie_name,
495 WrapClosure(callback)); 507 WrapClosure(callback));
496 break; 508 break;
497 case SYNCHRONIZING: 509 case SYNCHRONIZING:
498 tasks_pending_synchronization_.push_back( 510 tasks_pending_synchronization_.push_back(
499 base::Bind(&CookieStoreIOS::DeleteCookieAsync, this, url, cookie_name, 511 base::Bind(&CookieStoreIOS::DeleteCookieAsync, this, url, cookie_name,
500 WrapClosure(callback))); 512 WrapClosure(callback)));
501 break; 513 break;
502 case SYNCHRONIZED: 514 case SYNCHRONIZED:
503 NSArray* cookies = GetCookiesForURL(url, creation_time_manager_.get()); 515 NSArray* cookies = GetCookiesForURL(system_store_,
516 url, creation_time_manager_.get());
504 for (NSHTTPCookie* cookie in cookies) { 517 for (NSHTTPCookie* cookie in cookies) {
505 if ([[cookie name] 518 if ([[cookie name]
506 isEqualToString:base::SysUTF8ToNSString(cookie_name)]) { 519 isEqualToString:base::SysUTF8ToNSString(cookie_name)]) {
507 [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; 520 [system_store_ deleteCookie:cookie];
508 creation_time_manager_->DeleteCreationTime(cookie); 521 creation_time_manager_->DeleteCreationTime(cookie);
509 } 522 }
510 } 523 }
511 if (!callback.is_null()) 524 if (!callback.is_null())
512 callback.Run(); 525 callback.Run();
513 break; 526 break;
514 } 527 }
515 } 528 }
516 529
517 // CookieStoreIOS is an implementation of CookieStore which is not a 530 // CookieStoreIOS is an implementation of CookieStore which is not a
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 CookieStoreIOS::~CookieStoreIOS() { 619 CookieStoreIOS::~CookieStoreIOS() {
607 NotificationTrampoline::GetInstance()->RemoveObserver(this); 620 NotificationTrampoline::GetInstance()->RemoveObserver(this);
608 STLDeleteContainerPairSecondPointers(hook_map_.begin(), hook_map_.end()); 621 STLDeleteContainerPairSecondPointers(hook_map_.begin(), hook_map_.end());
609 } 622 }
610 623
611 #pragma mark - 624 #pragma mark -
612 #pragma mark Private methods 625 #pragma mark Private methods
613 626
614 void CookieStoreIOS::ClearSystemStore() { 627 void CookieStoreIOS::ClearSystemStore() {
615 DCHECK(thread_checker_.CalledOnValidThread()); 628 DCHECK(thread_checker_.CalledOnValidThread());
616 NSHTTPCookieStorage* cookie_storage =
617 [NSHTTPCookieStorage sharedHTTPCookieStorage];
618 base::scoped_nsobject<NSArray> copy( 629 base::scoped_nsobject<NSArray> copy(
619 [[NSArray alloc] initWithArray:[cookie_storage cookies]]); 630 [[NSArray alloc] initWithArray:[system_store_ cookies]]);
620 for (NSHTTPCookie* cookie in copy.get()) 631 for (NSHTTPCookie* cookie in copy.get())
621 [cookie_storage deleteCookie:cookie]; 632 [system_store_ deleteCookie:cookie];
622 DCHECK_EQ(0u, [[cookie_storage cookies] count]); 633 DCHECK_EQ(0u, [[system_store_ cookies] count]);
623 creation_time_manager_->Clear(); 634 creation_time_manager_->Clear();
624 } 635 }
625 636
626 void CookieStoreIOS::OnSystemCookiePolicyChanged() { 637 void CookieStoreIOS::OnSystemCookiePolicyChanged() {
627 DCHECK(thread_checker_.CalledOnValidThread()); 638 DCHECK(thread_checker_.CalledOnValidThread());
628 639
629 if (synchronization_state_ == NOT_SYNCHRONIZED) 640 if (synchronization_state_ == NOT_SYNCHRONIZED)
630 return; 641 return;
631 642
632 NSHTTPCookieAcceptPolicy policy = 643 NSHTTPCookieAcceptPolicy policy =
633 [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy]; 644 [system_store_ cookieAcceptPolicy];
634 if (policy == NSHTTPCookieAcceptPolicyAlways) { 645 if (policy == NSHTTPCookieAcceptPolicyAlways) {
635 // If cookies are disabled, the system cookie store should be empty. 646 // If cookies are disabled, the system cookie store should be empty.
636 DCHECK(![[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] count]); 647 DCHECK(![[system_store_ cookies] count]);
637 DCHECK(synchronization_state_ != SYNCHRONIZING); 648 DCHECK(synchronization_state_ != SYNCHRONIZING);
638 synchronization_state_ = SYNCHRONIZING; 649 synchronization_state_ = SYNCHRONIZING;
639 cookie_monster_->GetAllCookiesAsync( 650 cookie_monster_->GetAllCookiesAsync(
640 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this)); 651 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this));
641 } else { 652 } else {
642 DCHECK_EQ(NSHTTPCookieAcceptPolicyNever, policy); 653 DCHECK_EQ(NSHTTPCookieAcceptPolicyNever, policy);
643 // Flush() does not write the cookies to disk when they are disabled. 654 // Flush() does not write the cookies to disk when they are disabled.
644 // Explicitly copy them. 655 // Explicitly copy them.
645 WriteToCookieMonster( 656 WriteToCookieMonster([system_store_ cookies]);
646 [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]);
647 Flush(base::Closure()); 657 Flush(base::Closure());
648 ClearSystemStore(); 658 ClearSystemStore();
649 if (synchronization_state_ == SYNCHRONIZING) { 659 if (synchronization_state_ == SYNCHRONIZING) {
650 // If synchronization was in progress, abort it and leave the cookie store 660 // If synchronization was in progress, abort it and leave the cookie store
651 // empty. 661 // empty.
652 // Temporarily toggle the synchronization state so that pending tasks are 662 // Temporarily toggle the synchronization state so that pending tasks are
653 // redirected to cookie_monster_ and can complete normally. 663 // redirected to cookie_monster_ and can complete normally.
654 synchronization_state_ = NOT_SYNCHRONIZED; 664 synchronization_state_ = NOT_SYNCHRONIZED;
655 RunAllPendingTasks(); 665 RunAllPendingTasks();
656 synchronization_state_ = SYNCHRONIZED; 666 synchronization_state_ = SYNCHRONIZED;
(...skipping 13 matching lines...) Expand all
670 << "This cookie store was not synchronized"; 680 << "This cookie store was not synchronized";
671 g_current_synchronized_store = nullptr; 681 g_current_synchronized_store = nullptr;
672 } else { 682 } else {
673 DCHECK_EQ((CookieStoreIOS*)nullptr, g_current_synchronized_store) 683 DCHECK_EQ((CookieStoreIOS*)nullptr, g_current_synchronized_store)
674 << "Un-synchronize the current cookie store first."; 684 << "Un-synchronize the current cookie store first.";
675 g_current_synchronized_store = this; 685 g_current_synchronized_store = this;
676 } 686 }
677 #endif 687 #endif
678 688
679 NSHTTPCookieAcceptPolicy policy = 689 NSHTTPCookieAcceptPolicy policy =
680 [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy]; 690 [system_store_ cookieAcceptPolicy];
681 DCHECK(policy == NSHTTPCookieAcceptPolicyAlways || 691 DCHECK(policy == NSHTTPCookieAcceptPolicyAlways ||
682 policy == NSHTTPCookieAcceptPolicyNever); 692 policy == NSHTTPCookieAcceptPolicyNever);
683 693
684 // If cookies are disabled, the system cookie store should be empty. 694 // If cookies are disabled, the system cookie store should be empty.
685 DCHECK(policy == NSHTTPCookieAcceptPolicyAlways || 695 DCHECK(policy == NSHTTPCookieAcceptPolicyAlways ||
686 ![[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] count]); 696 ![[system_store_ cookies] count]);
687 697
688 // If cookies are disabled, nothing is done now, the work will be done when 698 // If cookies are disabled, nothing is done now, the work will be done when
689 // cookies are re-enabled. 699 // cookies are re-enabled.
690 if (policy == NSHTTPCookieAcceptPolicyAlways) { 700 if (policy == NSHTTPCookieAcceptPolicyAlways) {
691 if (synchronized) { 701 if (synchronized) {
692 synchronization_state_ = SYNCHRONIZING; 702 synchronization_state_ = SYNCHRONIZING;
693 ClearSystemStore(); 703 ClearSystemStore();
694 cookie_monster_->GetAllCookiesAsync( 704 cookie_monster_->GetAllCookiesAsync(
695 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this)); 705 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this));
696 return; 706 return;
697 } else { 707 } else {
698 // Copy the cookies from the global store to |cookie_monster_|. 708 // Copy the cookies from the global store to |cookie_monster_|.
699 Flush(base::Closure()); 709 Flush(base::Closure());
700 } 710 }
701 } 711 }
702 synchronization_state_ = synchronized ? SYNCHRONIZED : NOT_SYNCHRONIZED; 712 synchronization_state_ = synchronized ? SYNCHRONIZED : NOT_SYNCHRONIZED;
703 713
704 if (synchronization_state_ == NOT_SYNCHRONIZED) { 714 if (synchronization_state_ == NOT_SYNCHRONIZED) {
705 // If there are pending tasks, then it means that the synchronization is 715 // If there are pending tasks, then it means that the synchronization is
706 // being canceled. All pending tasks can be sent to cookie_monster_. 716 // being canceled. All pending tasks can be sent to cookie_monster_.
707 RunAllPendingTasks(); 717 RunAllPendingTasks();
708 } 718 }
709 } 719 }
710 720
711 bool CookieStoreIOS::SystemCookiesAllowed() { 721 bool CookieStoreIOS::SystemCookiesAllowed() {
712 DCHECK(thread_checker_.CalledOnValidThread()); 722 DCHECK(thread_checker_.CalledOnValidThread());
713 return [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy] == 723 return [system_store_ cookieAcceptPolicy] ==
714 NSHTTPCookieAcceptPolicyAlways; 724 NSHTTPCookieAcceptPolicyAlways;
715 } 725 }
716 726
717 void CookieStoreIOS::AddCookiesToSystemStore(const net::CookieList& cookies) { 727 void CookieStoreIOS::AddCookiesToSystemStore(const net::CookieList& cookies) {
718 DCHECK(thread_checker_.CalledOnValidThread()); 728 DCHECK(thread_checker_.CalledOnValidThread());
719 if (!SystemCookiesAllowed() || synchronization_state_ != SYNCHRONIZING) { 729 if (!SystemCookiesAllowed() || synchronization_state_ != SYNCHRONIZING) {
720 // If synchronization was aborted, the pending tasks have been processed at 730 // If synchronization was aborted, the pending tasks have been processed at
721 // that time. Now is too late. 731 // that time. Now is too late.
722 DCHECK(tasks_pending_synchronization_.empty()); 732 DCHECK(tasks_pending_synchronization_.empty());
723 return; 733 return;
724 } 734 }
725 735
726 // Report metrics. 736 // Report metrics.
727 if (metrics_enabled_) { 737 if (metrics_enabled_) {
728 size_t cookie_count = cookies.size(); 738 size_t cookie_count = cookies.size();
729 UMA_HISTOGRAM_COUNTS_10000("CookieIOS.CookieReadCount", cookie_count); 739 UMA_HISTOGRAM_COUNTS_10000("CookieIOS.CookieReadCount", cookie_count);
730 CheckForCookieLoss(cookie_count, COOKIES_READ); 740 CheckForCookieLoss(cookie_count, COOKIES_READ);
731 } 741 }
732 742
733 net::CookieList::const_iterator it; 743 net::CookieList::const_iterator it;
734 for (it = cookies.begin(); it != cookies.end(); ++it) { 744 for (it = cookies.begin(); it != cookies.end(); ++it) {
735 const net::CanonicalCookie& net_cookie = *it; 745 const net::CanonicalCookie& net_cookie = *it;
736 NSHTTPCookie* system_cookie = SystemCookieFromCanonicalCookie(net_cookie); 746 NSHTTPCookie* system_cookie = SystemCookieFromCanonicalCookie(net_cookie);
737 // Canonical cookie may not be convertable into system cookie if it contains 747 // Canonical cookie may not be convertable into system cookie if it contains
738 // invalid characters. 748 // invalid characters.
739 if (!system_cookie) 749 if (!system_cookie)
740 continue; 750 continue;
741 [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:system_cookie]; 751 [system_store_ setCookie:system_cookie];
742 creation_time_manager_->SetCreationTime(system_cookie, 752 creation_time_manager_->SetCreationTime(system_cookie,
743 net_cookie.CreationDate()); 753 net_cookie.CreationDate());
744 } 754 }
745 755
746 synchronization_state_ = SYNCHRONIZED; 756 synchronization_state_ = SYNCHRONIZED;
747 RunAllPendingTasks(); 757 RunAllPendingTasks();
748 } 758 }
749 759
750 void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) { 760 void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) {
751 DCHECK(thread_checker_.CalledOnValidThread()); 761 DCHECK(thread_checker_.CalledOnValidThread());
(...skipping 24 matching lines...) Expand all
776 for (const auto& task : tasks_pending_synchronization_) { 786 for (const auto& task : tasks_pending_synchronization_) {
777 task.Run(); 787 task.Run();
778 } 788 }
779 tasks_pending_synchronization_.clear(); 789 tasks_pending_synchronization_.clear();
780 } 790 }
781 791
782 void CookieStoreIOS::DeleteCookiesWithFilter(const CookieFilterFunction& filter, 792 void CookieStoreIOS::DeleteCookiesWithFilter(const CookieFilterFunction& filter,
783 const DeleteCallback& callback) { 793 const DeleteCallback& callback) {
784 DCHECK(thread_checker_.CalledOnValidThread()); 794 DCHECK(thread_checker_.CalledOnValidThread());
785 DCHECK_EQ(SYNCHRONIZED, synchronization_state_); 795 DCHECK_EQ(SYNCHRONIZED, synchronization_state_);
786 NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; 796 NSArray* cookies = [system_store_ cookies];
787 797
788 // Collect the cookies to delete. 798 // Collect the cookies to delete.
789 base::scoped_nsobject<NSMutableArray> to_delete( 799 base::scoped_nsobject<NSMutableArray> to_delete(
790 [[NSMutableArray alloc] init]); 800 [[NSMutableArray alloc] init]);
791 for (NSHTTPCookie* cookie in cookies) { 801 for (NSHTTPCookie* cookie in cookies) {
792 base::Time creation_time = creation_time_manager_->GetCreationTime(cookie); 802 base::Time creation_time = creation_time_manager_->GetCreationTime(cookie);
793 if (filter.Run(cookie, creation_time)) 803 if (filter.Run(cookie, creation_time))
794 [to_delete addObject:cookie]; 804 [to_delete addObject:cookie];
795 } 805 }
796 806
797 // Delete them. 807 // Delete them.
798 for (NSHTTPCookie* cookie in to_delete.get()) { 808 for (NSHTTPCookie* cookie in to_delete.get()) {
799 [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; 809 [system_store_ deleteCookie:cookie];
800 creation_time_manager_->DeleteCreationTime(cookie); 810 creation_time_manager_->DeleteCreationTime(cookie);
801 } 811 }
802 812
803 if (!callback.is_null()) 813 if (!callback.is_null())
804 callback.Run([to_delete count]); 814 callback.Run([to_delete count]);
805 } 815 }
806 816
807 void CookieStoreIOS::OnSystemCookiesChanged() { 817 void CookieStoreIOS::OnSystemCookiesChanged() {
808 DCHECK(thread_checker_.CalledOnValidThread()); 818 DCHECK(thread_checker_.CalledOnValidThread());
809 819
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 callbacks->Notify(cookie, removed); 888 callbacks->Notify(cookie, removed);
879 } 889 }
880 } 890 }
881 891
882 bool CookieStoreIOS::GetSystemCookies( 892 bool CookieStoreIOS::GetSystemCookies(
883 const GURL& gurl, 893 const GURL& gurl,
884 const std::string& name, 894 const std::string& name,
885 std::vector<net::CanonicalCookie>* cookies) { 895 std::vector<net::CanonicalCookie>* cookies) {
886 DCHECK(thread_checker_.CalledOnValidThread()); 896 DCHECK(thread_checker_.CalledOnValidThread());
887 NSURL* url = net::NSURLWithGURL(gurl); 897 NSURL* url = net::NSURLWithGURL(gurl);
888 NSHTTPCookieStorage* storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 898 NSArray* nscookies = [system_store_ cookiesForURL:url];
889 NSArray* nscookies = [storage cookiesForURL:url];
890 bool found_cookies = false; 899 bool found_cookies = false;
891 for (NSHTTPCookie* nscookie in nscookies) { 900 for (NSHTTPCookie* nscookie in nscookies) {
892 if (nscookie.name.UTF8String == name) { 901 if (nscookie.name.UTF8String == name) {
893 net::CanonicalCookie canonical_cookie = CanonicalCookieFromSystemCookie( 902 net::CanonicalCookie canonical_cookie = CanonicalCookieFromSystemCookie(
894 nscookie, creation_time_manager_->GetCreationTime(nscookie)); 903 nscookie, creation_time_manager_->GetCreationTime(nscookie));
895 cookies->push_back(canonical_cookie); 904 cookies->push_back(canonical_cookie);
896 found_cookies = true; 905 found_cookies = true;
897 } 906 }
898 } 907 }
899 return found_cookies; 908 return found_cookies;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 DCHECK(thread_checker_.CalledOnValidThread()); 991 DCHECK(thread_checker_.CalledOnValidThread());
983 return base::Bind(&CookieStoreIOS::UpdateCachesAfterDelete, this, callback); 992 return base::Bind(&CookieStoreIOS::UpdateCachesAfterDelete, this, callback);
984 } 993 }
985 994
986 base::Closure CookieStoreIOS::WrapClosure(const base::Closure& callback) { 995 base::Closure CookieStoreIOS::WrapClosure(const base::Closure& callback) {
987 DCHECK(thread_checker_.CalledOnValidThread()); 996 DCHECK(thread_checker_.CalledOnValidThread());
988 return base::Bind(&CookieStoreIOS::UpdateCachesAfterClosure, this, callback); 997 return base::Bind(&CookieStoreIOS::UpdateCachesAfterClosure, this, callback);
989 } 998 }
990 999
991 } // namespace net 1000 } // namespace net
OLDNEW
« no previous file with comments | « ios/net/cookies/cookie_store_ios.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698