chromium/components/safe_browsing/core/browser/db/v4_protocol_manager_util.h

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

#ifndef COMPONENTS_SAFE_BROWSING_CORE_BROWSER_DB_V4_PROTOCOL_MANAGER_UTIL_H_
#define COMPONENTS_SAFE_BROWSING_CORE_BROWSER_DB_V4_PROTOCOL_MANAGER_UTIL_H_

// A class that implements the stateless methods used by the GetHashUpdate and
// GetFullHash stubby calls made by Chrome using the SafeBrowsing V4 protocol.

#include <functional>
#include <initializer_list>
#include <iosfwd>
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/gtest_prod_util.h"
#include "components/safe_browsing/core/browser/db/safebrowsing.pb.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "url/gurl.h"

namespace net {
class HttpRequestHeaders;
}  // namespace net

namespace safe_browsing {

// The size of the hash prefix, in bytes. It should be between 4 to 32 (full
// hash).
PrefixSize;

// The minimum expected size (in bytes) of a hash-prefix.
const PrefixSize kMinHashPrefixLength =;

// The maximum expected size (in bytes) of a hash-prefix. This represents the
// length of a SHA256 hash.
const PrefixSize kMaxHashPrefixLength =;

// A hash prefix sent by the SafeBrowsing PVer4 service.
HashPrefixStr;

// A full SHA256 hash.
FullHashStr;

ListUpdateRequest;
ListUpdateResponse;

void SetSbV4UrlPrefixForTesting(const char* url_prefix);

// Config passed to the constructor of a V4 protocol manager.
struct V4ProtocolConfig {};

// Get the v4 protocol config struct with a given client name, and ability to
// enable/disable database auto update.
V4ProtocolConfig GetV4ProtocolConfig(const std::string& client_name,
                                     bool disable_auto_update);

// Returns the URL to use for sending threat reports and other Safe Browsing
// hits back to Safe Browsing service.
std::string GetReportUrl(
    const V4ProtocolConfig& config,
    const std::string& method,
    const ExtendedReportingLevel* reporting_level = nullptr,
    const bool is_enhanced_protection = false);

// Used to specify the type of check to perform in CheckBrowseUrl function.
enum class CheckBrowseUrlType {};

// Different types of threats that SafeBrowsing protects against. This is the
// type that's returned to the clients of SafeBrowsing in Chromium.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.safe_browsing
// GENERATED_JAVA_PREFIX_TO_STRIP: SB_THREAT_TYPE_
enum class SBThreatType {};

SBThreatTypeSet;

// Return true if |set| only contains types that are valid for CheckBrowseUrl().
// Intended for use in DCHECK().
bool SBThreatTypeSetIsValidForCheckBrowseUrl(const SBThreatTypeSet& set);

// Shorthand for creating an SBThreatTypeSet from a list of SBThreatTypes. Use
// like CreateSBThreatTypeSet({SB_THREAT_TYPE_URL_PHISHING,
//                             SB_THREAT_TYPE_URL_MALWARE})
inline SBThreatTypeSet CreateSBThreatTypeSet(
    std::initializer_list<SBThreatType> set) {}

// The information required to uniquely identify each list the client is
// interested in maintaining and downloading from the SafeBrowsing servers.
// For example, for digests of Malware binaries on Windows:
// platform_type = WINDOWS,
// threat_entry_type = EXECUTABLE,
// threat_type = MALWARE
class ListIdentifier {};

std::ostream& operator<<(std::ostream& os, const ListIdentifier& id);

PlatformType GetCurrentPlatformType();
ListIdentifier GetChromeExtMalwareId();
ListIdentifier GetChromeUrlApiId();
ListIdentifier GetUrlBillingId();
ListIdentifier GetUrlCsdDownloadAllowlistId();
ListIdentifier GetUrlCsdAllowlistId();
ListIdentifier GetUrlHighConfidenceAllowlistId();
ListIdentifier GetUrlMalBinId();
ListIdentifier GetUrlMalwareId();
ListIdentifier GetUrlSocEngId();
ListIdentifier GetUrlSubresourceFilterId();
ListIdentifier GetUrlSuspiciousSiteId();
ListIdentifier GetUrlUwsId();

// Returns the basename of the store file, without the ".store" extension.
std::string GetUmaSuffixForStore(const base::FilePath& file_path);

// Represents the state of each store.
StoreStateMap;

// Sever response, parsed in vector form.
ParsedServerResponse;

// Holds the hash prefix and the store that it matched in.
struct StoreAndHashPrefix {};

// Used to track the hash prefix and the store in which a full hash's prefix
// matched.
StoreAndHashPrefixes;

// The matching hash prefixes and corresponding stores, for each full hash
// generated for a given URL.
FullHashToStoreAndHashPrefixesMap;

// Enumerate failures for histogramming purposes.  DO NOT CHANGE THE
// ORDERING OF THESE VALUES.
enum V4OperationResult {};

// A class that provides static methods related to the Pver4 protocol.
class V4ProtocolManagerUtil {};

StoresToCheck;

}  // namespace safe_browsing

namespace std {

template <>
struct hash<safe_browsing::PlatformType> {};

template <>
struct hash<safe_browsing::ThreatEntryType> {};

template <>
struct hash<safe_browsing::ThreatType> {};

template <>
struct hash<safe_browsing::ListIdentifier> {};

}  // namespace std

#endif  // COMPONENTS_SAFE_BROWSING_CORE_BROWSER_DB_V4_PROTOCOL_MANAGER_UTIL_H_