chromium/chrome/browser/extensions/api/cookies/cookies_helpers.h

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

// Defines common functionality used by the implementation of the Chrome
// Extensions Cookies API implemented in
// chrome/browser/extensions/api/cookies/cookies_api.cc. This separate interface
// exposes pieces of the API implementation mainly for unit testing purposes.

#ifndef CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_HELPERS_H_
#define CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_HELPERS_H_

#include <memory>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/values.h"
#include "chrome/common/extensions/api/cookies.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_monster.h"
#include "net/cookies/cookie_options.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"

class Browser;
class Profile;

namespace net {
class CanonicalCookie;
}

namespace extensions {

class Extension;

namespace cookies_helpers {

// Returns either the original profile or the incognito profile, based on the
// given store ID.  Returns NULL if the profile doesn't exist or is not allowed
// (e.g. if incognito mode is not enabled for the extension).
Profile* ChooseProfileFromStoreId(const std::string& store_id,
                                  Profile* profile,
                                  bool include_incognito);

// Returns the store ID for a particular user profile.
const char* GetStoreIdFromProfile(Profile* profile);

// Constructs a new Cookie object representing a cookie as defined by the
// cookies API.
api::cookies::Cookie CreateCookie(const net::CanonicalCookie& cookie,
                                  const std::string& store_id);

// Constructs a new CookieStore object as defined by the cookies API.
api::cookies::CookieStore CreateCookieStore(Profile* profile,
                                            base::Value::List tab_ids);

// Dispatch a request to the CookieManager for cookies associated with
// |url| and |partition_key_collection|.
void GetCookieListFromManager(
    network::mojom::CookieManager* manager,
    const GURL& url,
    const net::CookiePartitionKeyCollection& partition_key_collection,
    network::mojom::CookieManager::GetCookieListCallback callback);

// Dispatch a request to the CookieManager for all cookies.
void GetAllCookiesFromManager(
    network::mojom::CookieManager* manager,
    network::mojom::CookieManager::GetAllCookiesCallback callback);

// Constructs a URL from a cookie's information for use in checking
// a cookie against the extension's host permissions. The Secure
// property of the cookie defines the URL scheme, and the cookie's
// domain becomes the URL host.
GURL GetURLFromCanonicalCookie(
    const net::CanonicalCookie& cookie);

// Looks through all cookies in the given cookie store, and appends to the
// match vector all the cookies that both match the given URL and cookie details
// and are allowed by extension host permissions.
void AppendMatchingCookiesFromCookieListToVector(
    const net::CookieList& all_cookies,
    api::cookies::GetAll::Params::Details* details,
    const Extension* extension,
    std::vector<api::cookies::Cookie>* match_vector,
    const net::CookiePartitionKeyCollection& cookie_partition_key_collection);

// Same as above except takes a CookieAccessResultList (and ignores the access
// results).
void AppendMatchingCookiesFromCookieAccessResultListToVector(
    const net::CookieAccessResultList& all_cookies_with_access_result,
    api::cookies::GetAll::Params::Details* details,
    const Extension* extension,
    std::vector<api::cookies::Cookie>* match_vector);

// Appends the IDs of all tabs belonging to the given browser to the
// given list.
void AppendToTabIdList(Browser* browser, base::Value::List& tab_ids);

// Checks if the partition_key provided, which may unknown user input,
// can be used to deserialize into the net_partition_key.
// Returns false and populates error_message string if deserialization
// fails.
bool ValidateCookieApiPartitionKey(
    const std::optional<extensions::api::cookies::CookiePartitionKey>&
        partition_key,
    std::optional<net::CookiePartitionKey>& net_partition_key,
    std::string& error_message);

// Returns empty collection if no partition_key.
// Returns CookiePartitionKeyCollection::ContainsAll() if top_level_site has no
// value. Returns CookiePartitionKeyCollection::FromOptional() if partition_key
// and top_level_site are both present.
net::CookiePartitionKeyCollection
CookiePartitionKeyCollectionFromApiPartitionKey(
    const std::optional<extensions::api::cookies::CookiePartitionKey>&
        partition_key);

// Returns true for unpartitioned cookies if the collection is empty.
// Otherwise returns true if the collection contains the cookie's partition key.
bool CookieMatchesPartitionKeyCollection(
    const net::CookiePartitionKeyCollection& cookie_partition_key_collection,
    const net::CanonicalCookie& cookie);

// Returns true if the top_level_site values match or both optionals do not
// contain a value. For match to occur both partition keys must be serializable
// if they are present.
bool CanonicalCookiePartitionKeyMatchesApiCookiePartitionKey(
    const std::optional<extensions::api::cookies::CookiePartitionKey>&
        api_partition_key,
    const std::optional<net::CookiePartitionKey>& net_partition_key);

// A class representing the cookie filter parameters passed into
// cookies.getAll().
// This class is essentially a convenience wrapper for the details dictionary
// passed into the cookies.getAll() API by the user. If the dictionary contains
// no filter parameters, the MatchFilter will always trivially
// match all cookies.
class MatchFilter {};

}  // namespace cookies_helpers
}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_COOKIES_COOKIES_HELPERS_H_