blob: 1931363cfe15f1429c30484863913ffe4eec42c9 [file] [log] [blame]
// Copyright (c) 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/net/cookies/system_cookie_store.h"
#include <memory>
#import "ios/net/cookies/cookie_creation_time_manager.h"
#include "ios/net/ios_net_buildflags.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
namespace net {
SystemCookieStore::~SystemCookieStore() = default;
: creation_time_manager_(std::make_unique<CookieCreationTimeManager>()),
weak_factory_(this) {}
void SystemCookieStore::SetCookieAsync(NSHTTPCookie* cookie,
SystemCookieCallback callback) {
SetCookieAsync(cookie, /*optional_creation_time=*/nullptr,
base::Time SystemCookieStore::GetCookieCreationTime(NSHTTPCookie* cookie) {
return creation_time_manager_->GetCreationTime(cookie);
base::WeakPtr<SystemCookieStore> SystemCookieStore::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
// protected static
NSInteger SystemCookieStore::CompareCookies(id a, id b, void* context) {
NSHTTPCookie* cookie_a = static_cast<NSHTTPCookie*>(a);
NSHTTPCookie* cookie_b = static_cast<NSHTTPCookie*>(b);
// Compare path lengths first.
NSUInteger path_length_a = cookie_a.path.length;
NSUInteger path_length_b = cookie_b.path.length;
if (path_length_a < path_length_b)
return NSOrderedDescending;
if (path_length_b < path_length_a)
return NSOrderedAscending;
// Compare creation times.
CookieCreationTimeManager* manager =
base::Time created_a = manager->GetCreationTime(cookie_a);
base::Time created_b = manager->GetCreationTime(cookie_b);
// CookieCreationTimeManager is returning creation times that are null.
// Since in Cronet, the cookie store is recreated on startup, let's suppress
// this warning for now.
DLOG_IF(ERROR, created_a.is_null() || created_b.is_null())
<< "Cookie without creation date";
if (created_a < created_b)
return NSOrderedAscending;
if (created_a > created_b)
return NSOrderedDescending;
return NSOrderedSame;
} // namespace net