chromium/components/search_engines/template_url.cc

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

#include "components/search_engines/template_url.h"

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

#include "base/base64.h"
#include "base/base64url.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/format_macros.h"
#include "base/i18n/case_conversion.h"
#include "base/i18n/icu_string_conversions.h"
#include "base/i18n/rtl.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/ranges/algorithm.h"
#include "base/strings/escape.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "base/trace_event/typed_macros.h"
#include "build/build_config.h"
#include "components/google/core/common/google_util.h"
#include "components/omnibox/common/omnibox_features.h"
#include "components/search_engines/regulatory_extension_type.h"
#include "components/search_engines/search_engine_utils.h"
#include "components/search_engines/search_engines_switches.h"
#include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url_data.h"
#include "components/url_formatter/url_formatter.h"
#include "google_apis/google_api_keys.h"
#include "net/base/mime_util.h"
#include "net/base/url_util.h"
#include "template_url_starter_pack_data.h"
#include "third_party/metrics_proto/omnibox_input_type.pb.h"
#include "ui/base/device_form_factor.h"
#include "url/gurl.h"

namespace {
// The TemplateURLRef has any number of terms that need to be replaced. Each of
// the terms is enclosed in braces. If the character preceeding the final
// brace is a ?, it indicates the term is optional and can be replaced with
// an empty string.
const char kStartParameter =;
const char kEndParameter =;
const char kOptional =;

// Known parameters found in the URL.
const char kSearchTermsParameter[] =;
const char kSearchTermsParameterFull[] =;
const char kSearchTermsParameterFullEscaped[] =;

// Same as kSearchTermsParameter, with no escaping.
const char kGoogleUnescapedSearchTermsParameter[] =;
const char kGoogleUnescapedSearchTermsParameterFull[] =;

// Display value for kSearchTermsParameter.
const char kDisplaySearchTerms[] =;

// Display value for kGoogleUnescapedSearchTermsParameter.
const char kDisplayUnescapedSearchTerms[] =;

// Used if the count parameter is not optional. Indicates we want 10 search
// results.
const char kDefaultCount[] =;

// Used if the output encoding parameter is required.
const char kOutputEncodingType[] =;

const size_t kMaxStringEncodeStringLength =;

// Attempts to encode |terms| and |original_query| in |encoding| and escape
// them.  |terms| may be escaped as path or query depending on |is_in_query|;
// |original_query| is always escaped as query. If |force_encode| is true
// encoding ignores errors and function always returns true. Otherwise function
// returns whether the encoding process succeeded.
bool TryEncoding(const std::u16string& terms,
                 const std::u16string& original_query,
                 const char* encoding,
                 bool is_in_query,
                 bool force_encode,
                 std::u16string* escaped_terms,
                 std::u16string* escaped_original_query) {}

// Finds the position of the search terms' parameter in the URL component.
class SearchTermLocation {};

bool IsTemplateParameterString(const std::string& param) {}

std::string YandexSearchPathFromDeviceFormFactor() {}

// Returns true if `enterprise_engine` is strictly better than `other_engine`,
// where `enterprise_engine` is a search engine created by the
// `SiteSearchSettings` policy, and `other_engine` is a search engine not
// created by Enterprise policy.
bool IsEnterpriseSideSearchEngineBetterThanEngine(
    const TemplateURL* enterprise_engine,
    const TemplateURL* other_engine) {}

}  // namespace

// TemplateURLRef::SearchTermsArgs --------------------------------------------

TemplateURLRef::SearchTermsArgs::SearchTermsArgs() = default;

TemplateURLRef::SearchTermsArgs::SearchTermsArgs(
    const std::u16string& search_terms)
    :{}

TemplateURLRef::SearchTermsArgs::SearchTermsArgs(const SearchTermsArgs& other) =
    default;

TemplateURLRef::SearchTermsArgs::~SearchTermsArgs() {}

size_t TemplateURLRef::SearchTermsArgs::EstimateMemoryUsage() const {}

TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
    ContextualSearchParams() = default;

TemplateURLRef::SearchTermsArgs::ContextualSearchParams::ContextualSearchParams(
    int version,
    int contextual_cards_version,
    std::string home_country,
    int64_t previous_event_id,
    int previous_event_results,
    bool is_exact_search,
    std::string source_lang,
    std::string target_lang,
    std::string fluent_languages,
    std::string related_searches_stamp,
    bool apply_lang_hint)
    :{}

TemplateURLRef::SearchTermsArgs::ContextualSearchParams::ContextualSearchParams(
    const ContextualSearchParams& other) = default;

TemplateURLRef::SearchTermsArgs::ContextualSearchParams::
    ~ContextualSearchParams() {}

size_t
TemplateURLRef::SearchTermsArgs::ContextualSearchParams::EstimateMemoryUsage()
    const {}

// TemplateURLRef -------------------------------------------------------------

TemplateURLRef::TemplateURLRef(const TemplateURL* owner, Type type)
    :{}

TemplateURLRef::TemplateURLRef(const TemplateURL* owner, size_t index_in_owner)
    :{}

TemplateURLRef::~TemplateURLRef() {}

TemplateURLRef::TemplateURLRef(const TemplateURLRef& source) = default;

TemplateURLRef& TemplateURLRef::operator=(const TemplateURLRef& source) =
    default;

std::string TemplateURLRef::GetURL() const {}

std::string TemplateURLRef::GetPostParamsString() const {}

bool TemplateURLRef::UsesPOSTMethod(
    const SearchTermsData& search_terms_data) const {}

size_t TemplateURLRef::EstimateMemoryUsage() const {}

size_t TemplateURLRef::PostParam::EstimateMemoryUsage() const {}

bool TemplateURLRef::EncodeFormData(const PostParams& post_params,
                                    PostContent* post_content) const {}

bool TemplateURLRef::SupportsReplacement(
    const SearchTermsData& search_terms_data) const {}

std::string TemplateURLRef::ReplaceSearchTerms(
    const SearchTermsArgs& search_terms_args,
    const SearchTermsData& search_terms_data,
    PostContent* post_content,
    std::string url_override) const {}

bool TemplateURLRef::IsValid(const SearchTermsData& search_terms_data) const {}

std::u16string TemplateURLRef::DisplayURL(
    const SearchTermsData& search_terms_data) const {}

// static
std::string TemplateURLRef::DisplayURLToURLRef(
    const std::u16string& display_url) {}

const std::string& TemplateURLRef::GetHost(
    const SearchTermsData& search_terms_data) const {}

std::string TemplateURLRef::GetPath(
    const SearchTermsData& search_terms_data) const {}

const std::string& TemplateURLRef::GetSearchTermKey(
    const SearchTermsData& search_terms_data) const {}

url::Parsed::ComponentType TemplateURLRef::GetSearchTermKeyLocation(
    const SearchTermsData& search_terms_data) const {}

const std::string& TemplateURLRef::GetSearchTermValuePrefix(
    const SearchTermsData& search_terms_data) const {}

const std::string& TemplateURLRef::GetSearchTermValueSuffix(
    const SearchTermsData& search_terms_data) const {}

std::u16string TemplateURLRef::SearchTermToString16(
    std::string_view term) const {}

bool TemplateURLRef::HasGoogleBaseURLs(
    const SearchTermsData& search_terms_data) const {}

bool TemplateURLRef::ExtractSearchTermsFromURL(
    const GURL& url,
    std::u16string* search_terms,
    const SearchTermsData& search_terms_data,
    url::Parsed::ComponentType* search_terms_component,
    url::Component* search_terms_position) const {}

void TemplateURLRef::InvalidateCachedValues() const {}

bool TemplateURLRef::ParseParameter(size_t start,
                                    size_t end,
                                    std::string* url,
                                    Replacements* replacements) const {}

std::string TemplateURLRef::ParseURL(const std::string& url,
                                     Replacements* replacements,
                                     PostParams* post_params,
                                     bool* valid) const {}

void TemplateURLRef::ParseIfNecessary(const SearchTermsData& search_terms_data,
                                      std::string url_override) const {}

void TemplateURLRef::ParsePath(const std::string& path) const {}

bool TemplateURLRef::PathIsEqual(const GURL& url) const {}

void TemplateURLRef::ParseHostAndSearchTermKey(
    const SearchTermsData& search_terms_data) const {}

void TemplateURLRef::HandleReplacement(const std::string& name,
                                       const std::string& value,
                                       const Replacement& replacement,
                                       std::string* url) const {}

std::string TemplateURLRef::HandleReplacements(
    const SearchTermsArgs& search_terms_args,
    const SearchTermsData& search_terms_data,
    PostContent* post_content) const {}


// TemplateURL ----------------------------------------------------------------

TemplateURL::AssociatedExtensionInfo::AssociatedExtensionInfo(
    const std::string& extension_id,
    base::Time install_time,
    bool wants_to_be_default_engine)
    :{}

TemplateURL::AssociatedExtensionInfo::~AssociatedExtensionInfo() {}

size_t TemplateURL::AssociatedExtensionInfo::EstimateMemoryUsage() const {}

TemplateURL::TemplateURL(const TemplateURLData& data, Type type)
    :{}

TemplateURL::TemplateURL(const TemplateURLData& data,
                         Type type,
                         std::string extension_id,
                         base::Time install_time,
                         bool wants_to_be_default_engine)
    :{}

TemplateURL::~TemplateURL() {}

bool TemplateURL::IsBetterThanConflictingEngine(
    const TemplateURL* other) const {}

// static
std::u16string TemplateURL::GenerateKeyword(const GURL& url) {}

// static
GURL TemplateURL::GenerateFaviconURL(const GURL& url) {}

// static
bool TemplateURL::MatchesData(const TemplateURL* t_url,
                              const TemplateURLData* data,
                              const SearchTermsData& search_terms_data) {}

std::u16string TemplateURL::AdjustedShortNameForLocaleDirection() const {}

bool TemplateURL::SupportsReplacement(
    const SearchTermsData& search_terms_data) const {}

bool TemplateURL::HasGoogleBaseURLs(
    const SearchTermsData& search_terms_data) const {}

bool TemplateURL::IsGoogleSearchURLWithReplaceableKeyword(
    const SearchTermsData& search_terms_data) const {}

bool TemplateURL::HasSameKeywordAs(
    const TemplateURLData& other,
    const SearchTermsData& search_terms_data) const {}

std::string TemplateURL::GetExtensionId() const {}

SearchEngineType TemplateURL::GetEngineType(
    const SearchTermsData& search_terms_data) const {}

BuiltinEngineType TemplateURL::GetBuiltinEngineType() const {}

bool TemplateURL::ExtractSearchTermsFromURL(
    const GURL& url,
    const SearchTermsData& search_terms_data,
    std::u16string* search_terms) const {}

bool TemplateURL::IsSearchURL(const GURL& url,
                              const SearchTermsData& search_terms_data) const {}

bool TemplateURL::KeepSearchTermsInURL(const GURL& url,
                                       const SearchTermsData& search_terms_data,
                                       const bool keep_search_intent_params,
                                       const bool normalize_search_terms,
                                       GURL* out_url,
                                       std::u16string* out_search_terms) const {}

bool TemplateURL::ReplaceSearchTermsInURL(
    const GURL& url,
    const TemplateURLRef::SearchTermsArgs& search_terms_args,
    const SearchTermsData& search_terms_data,
    GURL* result) const {}

void TemplateURL::EncodeSearchTerms(
    const TemplateURLRef::SearchTermsArgs& search_terms_args,
    bool is_in_query,
    std::string* input_encoding,
    std::u16string* encoded_terms,
    std::u16string* encoded_original_query) const {}

GURL TemplateURL::GenerateSearchURL(const SearchTermsData& search_terms_data,
                                    const std::u16string& search_terms) const {}

GURL TemplateURL::GenerateSuggestionURL(
    const SearchTermsData& search_terms_data) const {}

RegulatoryExtensionType TemplateURL::GetRegulatoryExtensionType() const {}

const TemplateURLData::RegulatoryExtension* TemplateURL::GetRegulatoryExtension(
    RegulatoryExtensionType type) const {}

bool TemplateURL::IsSideSearchSupported() const {}

bool TemplateURL::IsSideImageSearchSupported() const {}

GURL TemplateURL::GenerateSideSearchURL(
    const GURL& search_url,
    const std::string& version,
    const SearchTermsData& search_terms_data) const {}

GURL TemplateURL::RemoveSideSearchParamFromURL(
    const GURL& side_search_url) const {}

GURL TemplateURL::GenerateSideImageSearchURL(const GURL& image_search_url,
                                             const std::string& version) const {}

GURL TemplateURL::RemoveSideImageSearchParamFromURL(
    const GURL& image_search_url) const {}

void TemplateURL::CopyFrom(const TemplateURL& other) {}

void TemplateURL::SetURL(const std::string& url) {}

void TemplateURL::SetPrepopulateId(int id) {}

void TemplateURL::ResetKeywordIfNecessary(
    const SearchTermsData& search_terms_data,
    bool force) {}

void TemplateURL::InvalidateCachedValues() const {}

size_t TemplateURL::EstimateMemoryUsage() const {}

void TemplateURL::ResizeURLRefVector() {}

bool TemplateURL::FindSearchTermsInURL(
    const GURL& url,
    const SearchTermsData& search_terms_data,
    std::u16string* search_terms,
    url::Parsed::ComponentType* search_term_component,
    url::Component* search_terms_position) const {}

bool TemplateURL::ContainsSideSearchParam(const GURL& url) const {}

bool TemplateURL::ContainsSideImageSearchParam(const GURL& url) const {}