#include "chrome/browser/policy/messaging_layer/upload/encrypted_reporting_client.h"
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/check_is_test.h"
#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/rand_util.h"
#include "base/sequence_checker.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/bind_post_task.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/types/expected.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise/browser_management/management_service_factory.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/policy/messaging_layer/upload/record_upload_request_builder.h"
#include "chrome/browser/policy/messaging_layer/util/upload_declarations.h"
#include "chrome/browser/policy/messaging_layer/util/upload_response_parser.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/device_management_service.h"
#include "components/policy/core/common/cloud/dm_auth.h"
#include "components/policy/core/common/cloud/encrypted_reporting_job_configuration.h"
#include "components/reporting/proto/synced/record.pb.h"
#include "components/reporting/proto/synced/record_constants.pb.h"
#include "components/reporting/util/reporting_errors.h"
#include "components/reporting/util/statusor.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/backoff_entry.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace reporting {
namespace {
constexpr char kUmaRecordProcessedByServer[] = …;
constexpr char kEventsUploadCount[] = …;
constexpr char kCachedEventsCount[] = …;
bool IsIrrecoverableError(int response_code) { … }
std::unique_ptr<::net::BackoffEntry> GetBackoffEntry(Priority priority) { … }
struct UploadState { … };
UploadStateMap;
UploadStateMap* state_map() { … }
UploadState* GetState(Priority priority, int64_t generation_id) { … }
void RemoveConfirmedEventsFromCache(UploadState* state) { … }
void LogNumRecordsInUpload(uint64_t num_records) { … }
void BuildPayload(
bool is_generation_guid_required,
bool need_encryption_key,
int config_file_version,
int64_t last_sequence_id,
const base::flat_map<int64_t, EncryptedRecord>& records,
ScopedReservation scoped_reservation,
base::OnceCallback<void(std::optional<base::Value::Dict> ,
ScopedReservation ,
int64_t ,
uint64_t )> create_job_cb) { … }
class PayloadSizeUmaReporter { … };
base::Time PayloadSizeUmaReporter::last_reported_time_{ … };
class PayloadSizeComputationRateLimiterForUma { … };
static int GetPayloadSize(const base::Value::Dict& payload) { … }
}
policy::DeviceManagementService*
EncryptedReportingClient::Delegate::device_management_service() const { … }
bool EncryptedReportingClient::GenerationGuidIsRequired() { … }
void EncryptedReportingClient::PresetUploads(base::Value::Dict context,
std::string dm_token,
std::string client_id) { … }
std::unique_ptr<EncryptedReportingClient> EncryptedReportingClient::Create(
std::unique_ptr<Delegate> delegate) { … }
EncryptedReportingClient::EncryptedReportingClient(
std::unique_ptr<Delegate> delegate)
: … { … }
EncryptedReportingClient::~EncryptedReportingClient() { … }
void EncryptedReportingClient::UploadReport(
bool need_encryption_key,
int config_file_version,
std::vector<EncryptedRecord> records,
ScopedReservation scoped_reservation,
UploadEnqueuedCallback enqueued_cb,
ResponseCallback callback) { … }
void EncryptedReportingClient::MaybePerformUpload(bool need_encryption_key,
int config_file_version,
Priority priority,
int64_t generation_id,
ResponseCallback callback) { … }
void EncryptedReportingClient::CreateUploadJob(
Priority priority,
int64_t generation_id,
policy::EncryptedReportingJobConfiguration::UploadResponseCallback
response_cb,
ResponseCallback callback,
std::optional<base::Value::Dict> payload_result,
ScopedReservation scoped_reservation,
int64_t last_sequence_id,
uint64_t events_to_send) { … }
void EncryptedReportingClient::OnReportUploadCompleted(
Priority priority,
int64_t generation_id,
ScopedReservation scoped_reservation,
std::optional<int> request_payload_size,
base::WeakPtr<PayloadSizePerHourUmaReporter>
payload_size_per_hour_uma_reporter,
ResponseCallback callback,
policy::DeviceManagementService::Job* job,
policy::DeviceManagementStatus status,
int response_code,
std::optional<base::Value::Dict> response) { … }
base::TimeDelta EncryptedReportingClient::WhenIsAllowedToProceed(
Priority priority,
int64_t generation_id) { … }
void EncryptedReportingClient::AccountForAllowedJob(Priority priority,
int64_t generation_id,
int64_t last_sequence_id) { … }
void EncryptedReportingClient::AccountForUploadResponse(Priority priority,
int64_t generation_id,
int net_error,
int response_code) { … }
void EncryptedReportingClient::ResetUploadsStateForTest() { … }
int EncryptedReportingClient::PayloadSizePerHourUmaReporter::ConvertBytesToKiB(
int bytes) { … }
EncryptedReportingClient::PayloadSizePerHourUmaReporter::
PayloadSizePerHourUmaReporter() { … }
EncryptedReportingClient::PayloadSizePerHourUmaReporter::
~PayloadSizePerHourUmaReporter() { … }
void EncryptedReportingClient::PayloadSizePerHourUmaReporter::
RecordRequestPayloadSize(int payload_size) { … }
void EncryptedReportingClient::PayloadSizePerHourUmaReporter::
RecordResponsePayloadSize(int payload_size) { … }
void EncryptedReportingClient::PayloadSizePerHourUmaReporter::Report() { … }
base::WeakPtr<EncryptedReportingClient::PayloadSizePerHourUmaReporter>
EncryptedReportingClient::PayloadSizePerHourUmaReporter::GetWeakPtr() { … }
}