chromium/components/url_pattern_index/url_rule_test_support.h

// Copyright 2017 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_URL_PATTERN_INDEX_URL_RULE_TEST_SUPPORT_H_
#define COMPONENTS_URL_PATTERN_INDEX_URL_RULE_TEST_SUPPORT_H_

#include <string>
#include <string_view>
#include <vector>

#include "components/url_pattern_index/proto/rules.pb.h"
#include "components/url_pattern_index/url_pattern.h"

class GURL;

namespace url {
class Origin;
}

namespace url_pattern_index {
namespace testing {

// Constants -------------------------------------------------------------------

constexpr proto::UrlPatternType kSubstring =;

constexpr proto::AnchorType kAnchorNone =;
constexpr proto::AnchorType kBoundary =;
constexpr proto::AnchorType kSubdomain =;

constexpr proto::ElementType kNoElement =;
constexpr proto::ElementType kOther =;
constexpr proto::ElementType kScript =;
constexpr proto::ElementType kImage =;
constexpr proto::ElementType kSubdocument =;
constexpr proto::ElementType kFont =;
constexpr proto::ElementType kPopup =;
constexpr proto::ElementType kWebSocket =;
constexpr proto::ElementType kAllElementTypes =;

constexpr proto::ActivationType kNoActivation =;
constexpr proto::ActivationType kDocument =;
constexpr proto::ActivationType kGenericBlock =;

constexpr proto::SourceType kAnyParty =;
constexpr proto::SourceType kThirdParty =;
constexpr proto::SourceType kFirstParty =;

// Helpers ---------------------------------------------------------------------

// Creates a UrlRule with the given |url_pattern|, and all necessary fields
// initialized to defaults.
proto::UrlRule MakeUrlRule(const UrlPattern& url_pattern = UrlPattern());

// Parses `initiator_domains` and adds them to the initiator domain list of the
// `rule`.
//
// The `initiator_domains` vector should contain non-empty strings. If a string
// starts with '~' then the following part of the string is an exception domain.
void AddInitiatorDomains(const std::vector<std::string>& initiator_domains,
                         proto::UrlRule* rule);

// Parses `request_domains` and adds them to the request domain list of the
// `rule`. See `AddInitiatorDomains`.
void AddRequestDomains(const std::vector<std::string>& request_domains,
                       proto::UrlRule* rule);

// Returns the url::Origin parsed from |origin_string|, or the unique origin if
// the string is empty.
url::Origin GetOrigin(std::string_view origin_string);

// Returns whether |url| is third-party resource w.r.t. |first_party_origin|.
bool IsThirdParty(const GURL& url, const url::Origin& first_party_origin);

}  // namespace testing
}  // namespace url_pattern_index

#endif  // COMPONENTS_URL_PATTERN_INDEX_URL_RULE_TEST_SUPPORT_H_