1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79

content / web_test / browser / web_test_cookie_manager.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/web_test/browser/web_test_cookie_manager.h"

#include "content/public/browser/storage_partition.h"
#include "net/cookies/canonical_cookie.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "url/gurl.h"

namespace content {

WebTestCookieManager::WebTestCookieManager(
    network::mojom::CookieManager* const cookie_manager,
    const GURL& url)
    : cookie_manager_(cookie_manager), url_(url) {
  DCHECK(url_->is_valid());
}

void WebTestCookieManager::DeleteAllCookies(
    blink::test::mojom::CookieManagerAutomation::DeleteAllCookiesCallback
        callback) {
  network::mojom::CookieDeletionFilterPtr deletion_filter =
      network::mojom::CookieDeletionFilter::New();
  deletion_filter->url = *url_;
  cookie_manager_->DeleteCookies(
      std::move(deletion_filter),
      base::BindOnce(
          [](blink::test::mojom::CookieManagerAutomation::
                 DeleteAllCookiesCallback callback,
             uint32_t) {
            // There is no ability to detect rejection here.
            std::move(callback).Run();
          },
          std::move(callback)));
}

void WebTestCookieManager::GetAllCookies(
    blink::test::mojom::CookieManagerAutomation::GetAllCookiesCallback
        callback) {
  cookie_manager_->GetCookieList(
      *url_, net::CookieOptions::MakeAllInclusive(),
      net::CookiePartitionKeyCollection(),
      base::BindOnce(
          [](blink::test::mojom::CookieManagerAutomation::GetAllCookiesCallback
                 callback,
             const net::CookieAccessResultList& cookies,
             const net::CookieAccessResultList&) {
            std::move(callback).Run(std::move(cookies));
          },
          std::move(callback)));
}

void WebTestCookieManager::GetNamedCookie(
    const std::string& name,
    blink::test::mojom::CookieManagerAutomation::GetNamedCookieCallback
        callback) {
  cookie_manager_->GetCookieList(
      *url_, net::CookieOptions::MakeAllInclusive(),
      net::CookiePartitionKeyCollection(),
      base::BindOnce(
          [](const std::string& name,
             blink::test::mojom::CookieManagerAutomation::GetNamedCookieCallback
                 callback,
             const net::CookieAccessResultList& cookies,
             const net::CookieAccessResultList&) {
            for (const auto& cookie : cookies) {
              if (cookie.cookie.Name() == name) {
                std::move(callback).Run(std::move(cookie));
                return;
              }
            }
            std::move(callback).Run(std::nullopt);
          },
          name, std::move(callback)));
}

}  // namespace content