chromium/components/autofill/core/browser/crowdsourcing/autofill_crowdsourcing_manager.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/autofill/core/browser/crowdsourcing/autofill_crowdsourcing_manager.h"

#include <algorithm>
#include <functional>
#include <optional>
#include <utility>
#include <vector>

#include "base/base64url.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/numerics/safe_conversions.h"
#include "base/rand_util.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "components/autofill/core/browser/autofill_client.h"
#include "components/autofill/core/browser/crowdsourcing/autofill_crowdsourcing_encoding.h"
#include "components/autofill/core/browser/logging/log_manager.h"
#include "components/autofill/core/browser/logging/log_protobufs.h"
#include "components/autofill/core/browser/metrics/autofill_metrics.h"
#include "components/autofill/core/browser/proto/api_v1.pb.h"
#include "components/autofill/core/browser/proto/server.pb.h"
#include "components/autofill/core/common/autofill_clock.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_internals/log_message.h"
#include "components/autofill/core/common/autofill_internals/logging_scope.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/logging/log_buffer.h"
#include "components/autofill/core/common/mojom/autofill_types.mojom-shared.h"
#include "components/autofill/core/common/mojom/autofill_types.mojom.h"
#include "components/autofill/core/common/signatures.h"
#include "components/google/core/common/google_util.h"
#include "components/history/core/browser/history_service.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/variations/net/variations_http_headers.h"
#include "components/variations/variations_ids_provider.h"
#include "google_apis/common/api_key_request_util.h"
#include "google_apis/google_api_keys.h"
#include "net/base/load_flags.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "net/http/http_util.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "url/gurl.h"

namespace autofill {

namespace {

// The reserved identifier ranges for autofill server experiments.
constexpr std::pair<int, int> kAutofillExperimentRanges[] =;

constexpr size_t kAutofillCrowdsourcingManagerMaxFormCacheSize =;
constexpr size_t kMaxFieldsPerQueryRequest =;

constexpr base::TimeDelta kFetchTimeout(base::Seconds(10));

constexpr net::BackoffEntry::Policy kAutofillBackoffPolicy =;

constexpr char kDefaultAutofillServerURL[] =;

// Header to get base64 encoded serialized proto from API for safety.
constexpr char kGoogEncodeResponseIfExecutable[] =;

// The default number of days after which to reset the registry of autofill
// events for which an upload has been sent.
const base::FeatureParam<int> kAutofillUploadThrottlingPeriodInDays(
    &features::test::kAutofillUploadThrottling,
    switches::kAutofillUploadThrottlingPeriodInDays,
    28);

// The maximum number of attempts for a given autofill request.
const base::FeatureParam<int> kAutofillMaxServerAttempts(
    &features::test::kAutofillServerCommunication,
    "max-attempts",
    5);

enum class RequestType {};

// Used in `ShouldThrottleUpload` to specify which part of the upload is
// checked for throttling.
enum class UploadType {};

// Returns the base URL for the autofill server.
GURL GetAutofillServerURL() {}

base::TimeDelta GetThrottleResetPeriod() {}

// Returns true if `id` is within `kAutofillExperimentRanges`.
bool IsAutofillExperimentId(int id) {}

std::string GetMetricName(RequestType request_type, std::string_view suffix) {}

net::NetworkTrafficAnnotationTag GetNetworkTrafficAnnotation(
    RequestType request_type) {}

size_t CountActiveFieldsInForms(
    const std::vector<raw_ptr<FormStructure, VectorExperimental>>& forms) {}

std::string FieldTypeToString(uint32_t type) {}

LogBuffer& operator<<(LogBuffer& out, const AutofillPageQueryRequest& query) {}

LogBuffer& operator<<(LogBuffer& out, const AutofillUploadContents& upload) {}

// Returns true if part of upload of a form with `form_signature`, triggered by
// `form_submission_source` should be throttled/suppressed. This is true if
// `pref_service` indicates that this upload has already happened within the
// last update window. Updates `pref_service` account for the upload of a form
// with `form_signature`.
// If `upload_type` equals `UploadType::kVote`, the check is done on the vote
// part of the upload. Vote throttling is only used on the Autofill side.
// If `upload_type` equals `UploadType::kMetadata` the check is done on the
// metadata part of the upload. Metadata throttling is shared by Autofill and
// the Password Manager, ensuring that together they don't upload metadata more
// frequently than desired.
bool ShouldThrottleUpload(FormSignature form_signature,
                          UploadType upload_type,
                          base::TimeDelta throttle_reset_period,
                          PrefService* pref_service,
                          std::optional<mojom::SubmissionSource>
                              form_submission_source_for_vote_upload) {}

// Determines whether a HTTP request was successful based on its response code.
bool IsHttpSuccess(int response_code) {}

std::optional<std::string> GetUploadPayloadForApi(
    const AutofillUploadContents& upload) {}

// Gets an API method URL given its type (query or upload), an optional
// resource ID, and the HTTP method to be used.
// Example usage:
// * GetAPIMethodUrl(RequestType::kRequestQuery, "1234", "GET") will return
//   "/v1/pages/1234".
// * GetAPIMethodUrl(RequestType::kRequestQuery, "1234", "POST") will return
//   "/v1/pages:get".
// * GetAPIMethodUrl(RequestType::kRequestUpload, "", "POST") will return
//   "/v1/forms:vote".
std::string GetAPIMethodUrl(RequestType type,
                            std::string_view resource_id,
                            std::string_view method) {}

// Gets HTTP body payload for API POST request.
std::optional<std::string> GetAPIBodyPayload(std::string payload,
                                             RequestType type) {}

// Gets the data payload for API Query (POST and GET).
std::optional<std::string> GetAPIQueryPayload(
    const AutofillPageQueryRequest& query) {}

std::string GetAPIKeyForUrl(version_info::Channel channel) {}

std::optional<std::vector<variations::VariationID>>& GetActiveExperiments() {}

// Populates `GetActiveExperiments()` with the set of active autofill server
// experiments.
void InitActiveExperiments() {}

}  // namespace

template <typename Signature>
class ScopedCallbackRunner;

// A variant of `base::ScopedClosureRunner` that encapsulates a callback and
// default arguments.
ScopedCallbackRunner<R (Args...)>;

struct AutofillCrowdsourcingManager::FormRequestData {};

ScopedActiveAutofillExperiments::ScopedActiveAutofillExperiments() {}

ScopedActiveAutofillExperiments::~ScopedActiveAutofillExperiments() {}

AutofillCrowdsourcingManager::QueryResponse::QueryResponse(
    std::string response,
    std::vector<FormSignature> queried_form_signatures)
    :{}

AutofillCrowdsourcingManager::QueryResponse::QueryResponse(QueryResponse&&) =
    default;
AutofillCrowdsourcingManager::QueryResponse&
AutofillCrowdsourcingManager::QueryResponse::operator=(QueryResponse&&) =
    default;

AutofillCrowdsourcingManager::QueryResponse::~QueryResponse() = default;

AutofillCrowdsourcingManager::AutofillCrowdsourcingManager(AutofillClient* client,
                                                 version_info::Channel channel,
                                                 LogManager* log_manager)
    :{}

AutofillCrowdsourcingManager::AutofillCrowdsourcingManager(AutofillClient* client,
                                                 const std::string& api_key,
                                                 LogManager* log_manager)
    :{}

AutofillCrowdsourcingManager::~AutofillCrowdsourcingManager() = default;

bool AutofillCrowdsourcingManager::IsEnabled() const {}

bool AutofillCrowdsourcingManager::StartQueryRequest(
    const std::vector<raw_ptr<FormStructure, VectorExperimental>>& forms,
    std::optional<net::IsolationInfo> isolation_info,
    base::OnceCallback<void(std::optional<QueryResponse>)> callback) {}

bool AutofillCrowdsourcingManager::StartUploadRequest(
    std::vector<AutofillUploadContents> upload_contents,
    mojom::SubmissionSource form_submission_source,
    bool is_password_manager_upload) {}

void AutofillCrowdsourcingManager::ClearUploadHistory(PrefService* pref_service) {}

size_t AutofillCrowdsourcingManager::GetPayloadLength(
    std::string_view payload) const {}

std::tuple<GURL, std::string> AutofillCrowdsourcingManager::GetRequestURLAndMethod(
    const FormRequestData& request_data) const {}

bool AutofillCrowdsourcingManager::StartRequest(FormRequestData request_data) {}

void AutofillCrowdsourcingManager::CacheQueryRequest(
    const std::vector<FormSignature>& forms_in_query,
    const std::string& query_data) {}

bool AutofillCrowdsourcingManager::CheckCacheForQueryRequest(
    const std::vector<FormSignature>& forms_in_query,
    std::string* query_data) const {}

// static
int AutofillCrowdsourcingManager::GetMaxServerAttempts() {}

void AutofillCrowdsourcingManager::OnSimpleLoaderComplete(
    std::list<std::unique_ptr<network::SimpleURLLoader>>::iterator it,
    FormRequestData request_data,
    base::TimeTicks request_start,
    std::unique_ptr<std::string> response_body) {}

}  // namespace autofill