#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 {
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[] = …;
constexpr char kGoogEncodeResponseIfExecutable[] = …;
const base::FeatureParam<int> kAutofillUploadThrottlingPeriodInDays(
&features::test::kAutofillUploadThrottling,
switches::kAutofillUploadThrottlingPeriodInDays,
28);
const base::FeatureParam<int> kAutofillMaxServerAttempts(
&features::test::kAutofillServerCommunication,
"max-attempts",
5);
enum class RequestType { … };
enum class UploadType { … };
GURL GetAutofillServerURL() { … }
base::TimeDelta GetThrottleResetPeriod() { … }
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) { … }
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) { … }
bool IsHttpSuccess(int response_code) { … }
std::optional<std::string> GetUploadPayloadForApi(
const AutofillUploadContents& upload) { … }
std::string GetAPIMethodUrl(RequestType type,
std::string_view resource_id,
std::string_view method) { … }
std::optional<std::string> GetAPIBodyPayload(std::string payload,
RequestType type) { … }
std::optional<std::string> GetAPIQueryPayload(
const AutofillPageQueryRequest& query) { … }
std::string GetAPIKeyForUrl(version_info::Channel channel) { … }
std::optional<std::vector<variations::VariationID>>& GetActiveExperiments() { … }
void InitActiveExperiments() { … }
}
template <typename Signature>
class ScopedCallbackRunner;
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 { … }
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) { … }
}