// 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/update_client/request_sender.h"

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

#include "base/check.h"
#include "base/containers/span.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/strcat.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "components/client_update_protocol/features.h"
#include "components/update_client/configurator.h"
#include "components/update_client/network.h"
#include "components/update_client/update_client_errors.h"
#include "components/update_client/update_client_metrics.h"
#include "components/update_client/utils.h"
#include "url/gurl.h"

namespace update_client {

namespace {

// This is an ECDSA prime256v1 named-curve key.
constexpr int kEcdsaKeyVersion = 16;
constexpr auto kEcdsaPublicKey = std::to_array<uint8_t>({
    0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
    0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
    0x42, 0x00, 0x04, 0x51, 0x95, 0x3b, 0xf4, 0x8c, 0xd4, 0x1b, 0x71, 0x8d,
    0x23, 0xfd, 0x54, 0x56, 0xb6, 0x18, 0xf0, 0xd0, 0x65, 0x56, 0xe8, 0x56,
    0xe7, 0x8f, 0x8d, 0x15, 0x18, 0x40, 0x6b, 0xf3, 0x56, 0x5b, 0xc9, 0xfe,
    0x1e, 0xde, 0x2d, 0xa5, 0x0a, 0xdf, 0x07, 0x81, 0x86, 0x5d, 0x46, 0x46,
    0xc9, 0x48, 0x21, 0xe2, 0x5c, 0x5e, 0x60, 0x1b, 0x8a, 0x79, 0xf1, 0x9f,
    0xd3, 0xdc, 0xaf, 0xaa, 0x5a, 0xaa, 0xc4,
});

// Post-Quantum Crypto (ML-DSA44) SubjectPublicKeyInfo (1334 bytes).
constexpr int kMldsa44KeyVersion = 16;
constexpr auto kMldsa44PublicKey = std::to_array<uint8_t>({
    0x30, 0x82, 0x05, 0x32, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
    0x65, 0x03, 0x04, 0x03, 0x11, 0x03, 0x82, 0x05, 0x21, 0x00, 0xb3, 0xf5,
    0xbe, 0x7b, 0xdd, 0xa9, 0xcd, 0x0f, 0x8d, 0x71, 0x27, 0xde, 0x63, 0xdf,
    0x18, 0x9e, 0x7b, 0xd0, 0x77, 0x5d, 0xcd, 0xb9, 0xf6, 0xa5, 0x65, 0xd9,
    0x60, 0xb2, 0x7b, 0xf5, 0xe4, 0x1f, 0x40, 0xf3, 0x6c, 0xfa, 0x7e, 0x06,
    0x05, 0xb8, 0xe0, 0x43, 0xbe, 0x3a, 0x13, 0xc0, 0x41, 0x22, 0x1e, 0x70,
    0xcd, 0x8a, 0xde, 0xbc, 0xbf, 0xce, 0x4b, 0xfe, 0x70, 0xae, 0xbf, 0xf1,
    0xb1, 0x3e, 0x3f, 0x14, 0xdf, 0x6a, 0x05, 0x26, 0xcb, 0x28, 0x20, 0xae,
    0xc4, 0xac, 0xcf, 0xd3, 0x53, 0xd3, 0x99, 0xc5, 0xa8, 0x8b, 0x04, 0xd7,
    0x51, 0xed, 0xce, 0xa9, 0x21, 0x1b, 0x38, 0x98, 0xbe, 0x2e, 0x14, 0xde,
    0x6a, 0xba, 0xb2, 0xb3, 0x60, 0x2c, 0x80, 0x3f, 0x11, 0x0e, 0x04, 0x3e,
    0x79, 0x11, 0x48, 0x18, 0x27, 0x33, 0x35, 0x44, 0xef, 0x54, 0xf8, 0x43,
    0xeb, 0x52, 0xed, 0x22, 0x9c, 0x5d, 0x18, 0x28, 0xfb, 0x79, 0xae, 0xcc,
    0x90, 0xca, 0x7d, 0x81, 0x89, 0xf3, 0x2b, 0x39, 0x25, 0xab, 0xf6, 0xc0,
    0xfc, 0xaf, 0xcc, 0x99, 0xfd, 0xa6, 0x53, 0xad, 0x72, 0x8b, 0x2b, 0xbf,
    0x3b, 0x00, 0x62, 0x75, 0x73, 0xf0, 0x44, 0x3e, 0x35, 0x33, 0x42, 0x44,
    0x8b, 0xa6, 0x3c, 0xe7, 0x58, 0xac, 0xc7, 0x0c, 0xd9, 0xf4, 0xef, 0x44,
    0x69, 0xf6, 0x65, 0x6c, 0x48, 0x29, 0xfe, 0x12, 0x7e, 0xd8, 0x95, 0xcc,
    0xa8, 0x5a, 0x8b, 0xdd, 0x1a, 0xf4, 0x19, 0xf9, 0x70, 0xf7, 0x0a, 0xc0,
    0x82, 0x53, 0xe5, 0x09, 0xd4, 0xc8, 0x5c, 0x7b, 0x3e, 0x24, 0xb9, 0xbc,
    0xc0, 0xce, 0xb8, 0x06, 0x66, 0x53, 0x70, 0x9e, 0xa1, 0xc0, 0x14, 0x94,
    0x12, 0xe6, 0x9f, 0x69, 0xb6, 0x2e, 0x89, 0x0a, 0x1b, 0x62, 0xec, 0x8f,
    0x4f, 0x05, 0x11, 0x2a, 0xe1, 0x0d, 0x37, 0x5f, 0x40, 0x29, 0xaf, 0x96,
    0x72, 0xbf, 0x39, 0x2d, 0x8c, 0xc5, 0x9f, 0x0b, 0xe7, 0xee, 0x25, 0x7e,
    0x91, 0x4f, 0x23, 0x32, 0xb6, 0xb3, 0xbe, 0x4a, 0xd3, 0xe1, 0xc8, 0x59,
    0x4e, 0xf3, 0xfa, 0xb6, 0x5f, 0xad, 0x50, 0xca, 0x9c, 0x56, 0xfb, 0xa1,
    0xee, 0x60, 0x7e, 0x3f, 0x95, 0x26, 0x9b, 0x99, 0x7c, 0x81, 0x8b, 0x81,
    0x6f, 0x3e, 0xa9, 0x5c, 0x19, 0x0d, 0xb5, 0x7a, 0x95, 0xee, 0xf7, 0x76,
    0x74, 0xbe, 0x46, 0x44, 0x45, 0xa9, 0xb2, 0x87, 0xf1, 0xbf, 0x9d, 0x72,
    0xd1, 0x8f, 0xde, 0xa6, 0xaf, 0x86, 0xcf, 0x6d, 0x11, 0x4e, 0xf9, 0x2b,
    0x4b, 0x08, 0x41, 0x93, 0xa6, 0xe6, 0x33, 0x5f, 0xf6, 0x76, 0xc0, 0xf4,
    0xcb, 0xf9, 0x7b, 0xdb, 0x46, 0x41, 0x12, 0xac, 0xf6, 0xa6, 0xbc, 0x41,
    0x93, 0x37, 0x89, 0xae, 0x17, 0x70, 0x37, 0x95, 0x4e, 0x99, 0x88, 0x40,
    0xe9, 0xad, 0x0c, 0x14, 0x76, 0x24, 0xa3, 0xfb, 0xbb, 0x94, 0x99, 0x6f,
    0x59, 0xe0, 0xf2, 0xab, 0x81, 0xc3, 0x04, 0xa6, 0xda, 0xa1, 0xa3, 0x38,
    0x32, 0x84, 0x1b, 0xe9, 0x80, 0x2a, 0xdc, 0xe7, 0x30, 0xb3, 0xd7, 0x1d,
    0x87, 0x3b, 0xe6, 0x09, 0x39, 0x5a, 0xa7, 0x4a, 0x07, 0x95, 0xce, 0xe5,
    0x31, 0x07, 0x03, 0xc8, 0xa5, 0xa3, 0x3c, 0x6c, 0x85, 0x21, 0xce, 0xdb,
    0x8b, 0x5c, 0x3d, 0x56, 0x56, 0xb0, 0xc5, 0xd2, 0xa8, 0x8c, 0xf3, 0x4f,
    0xcd, 0x7d, 0xe5, 0xd5, 0xd8, 0x48, 0xe9, 0xb8, 0x17, 0xd7, 0x12, 0x76,
    0x58, 0x5a, 0x29, 0xb7, 0x0a, 0x0b, 0xa5, 0x15, 0x36, 0x1e, 0xe6, 0x2f,
    0xac, 0x42, 0x58, 0x02, 0x4b, 0xed, 0xa3, 0x3f, 0x9b, 0x6b, 0x0d, 0x24,
    0xb2, 0xe4, 0xcc, 0x0f, 0x8c, 0x03, 0x92, 0x59, 0xb0, 0xeb, 0x41, 0x09,
    0x3c, 0x61, 0xab, 0xcc, 0xd3, 0x60, 0x74, 0x9c, 0x39, 0xfb, 0xf7, 0x86,
    0xf8, 0x12, 0xf0, 0x31, 0xa7, 0x77, 0x1f, 0x8d, 0xfc, 0x23, 0xa3, 0x50,
    0x5c, 0x13, 0xf2, 0xa4, 0x93, 0x5e, 0xc7, 0x32, 0x14, 0x32, 0x3c, 0xc1,
    0xbc, 0x48, 0x85, 0x64, 0x6a, 0x46, 0xf9, 0x3a, 0x3a, 0x27, 0x3a, 0x2c,
    0xa1, 0xca, 0x56, 0x3b, 0x8d, 0x1d, 0xdf, 0x34, 0x03, 0xb7, 0x33, 0xdc,
    0xd6, 0xd8, 0xb8, 0x3b, 0x20, 0x80, 0xa7, 0xd6, 0xa8, 0xba, 0x28, 0xb7,
    0x82, 0x07, 0x57, 0x59, 0xdf, 0x9b, 0x3f, 0x51, 0x4b, 0xef, 0x8c, 0x93,
    0xf0, 0xc7, 0xd8, 0x7f, 0xb3, 0xf1, 0xb9, 0x2e, 0x8f, 0xb7, 0xf5, 0x5c,
    0x7f, 0x9c, 0x84, 0x28, 0xda, 0x0c, 0x2e, 0x28, 0x22, 0xf7, 0x53, 0xe4,
    0x9c, 0x4b, 0x8b, 0xac, 0x32, 0xe2, 0xd4, 0xef, 0x01, 0x90, 0x10, 0xa8,
    0x07, 0x7a, 0xab, 0x39, 0x09, 0x01, 0x26, 0x3a, 0x80, 0x2d, 0xcb, 0xb1,
    0x97, 0xa1, 0x0a, 0xf0, 0x31, 0x43, 0x5f, 0xd0, 0xf2, 0x25, 0x5a, 0x7a,
    0x0b, 0x6c, 0x32, 0x81, 0x5a, 0x24, 0xfe, 0x5e, 0x33, 0xa0, 0xcd, 0x1f,
    0xc6, 0x96, 0xf9, 0xe7, 0x42, 0x52, 0x9a, 0x54, 0x4c, 0x3b, 0x9f, 0xeb,
    0xde, 0xa7, 0x2d, 0xf3, 0xbe, 0xe0, 0x71, 0x32, 0x93, 0x1a, 0x8d, 0x07,
    0x02, 0x69, 0xdf, 0x25, 0xd1, 0x3c, 0x7f, 0xb5, 0x92, 0x2f, 0x0c, 0x12,
    0xb4, 0x80, 0xae, 0x32, 0x39, 0x82, 0x0f, 0x32, 0x0a, 0x72, 0x49, 0x58,
    0x26, 0x5d, 0x99, 0x32, 0xce, 0xff, 0xd8, 0x65, 0xe1, 0x74, 0x1b, 0x41,
    0x39, 0xba, 0xa1, 0x1b, 0x57, 0x77, 0x59, 0x6e, 0xcd, 0xe0, 0x69, 0x4b,
    0x51, 0x3e, 0x96, 0x9f, 0x4a, 0x85, 0xb9, 0xca, 0xcc, 0xe9, 0xfc, 0x58,
    0x08, 0x57, 0x0f, 0xdd, 0xeb, 0x7b, 0x3d, 0xde, 0xb6, 0x4b, 0xc5, 0x4a,
    0x77, 0x10, 0x1c, 0xd4, 0x64, 0x20, 0xa4, 0x57, 0x36, 0x3f, 0x18, 0x1a,
    0x47, 0xf7, 0x93, 0x41, 0x09, 0x8a, 0x2c, 0xcb, 0x5e, 0x31, 0x4b, 0x15,
    0x10, 0x60, 0x8d, 0xf7, 0xb2, 0x1e, 0xa6, 0x97, 0x0a, 0x9d, 0x81, 0x96,
    0x25, 0x86, 0x2d, 0xfe, 0x3a, 0xcf, 0x8f, 0x19, 0x2b, 0xef, 0x0e, 0xbd,
    0xa0, 0xab, 0xb5, 0x9b, 0xb8, 0x20, 0xb8, 0xb3, 0xec, 0x3c, 0x71, 0xc8,
    0x73, 0xc1, 0x10, 0x80, 0x4a, 0xc9, 0x80, 0x13, 0xff, 0xca, 0x72, 0x24,
    0x2b, 0x8f, 0xf7, 0xb4, 0x00, 0x99, 0x0a, 0xb1, 0x87, 0x8f, 0x1e, 0x6c,
    0x6e, 0xfa, 0xbf, 0x43, 0xd7, 0x92, 0x48, 0x2b, 0x56, 0xb4, 0x8a, 0xea,
    0x43, 0x2d, 0x29, 0x70, 0x8d, 0x41, 0xb0, 0x38, 0x47, 0x54, 0x99, 0xfd,
    0x38, 0x7e, 0x16, 0x30, 0x34, 0xc3, 0xc6, 0xab, 0x68, 0xe8, 0x30, 0x06,
    0x7c, 0xd4, 0x31, 0x71, 0x3a, 0xb8, 0x12, 0x4c, 0x95, 0x5a, 0xa4, 0x14,
    0x00, 0x14, 0x45, 0x97, 0xcf, 0xed, 0x5d, 0x8c, 0xc6, 0x8d, 0x34, 0xdc,
    0x8b, 0x81, 0xe9, 0x1c, 0x89, 0xb2, 0xf2, 0xc2, 0x3b, 0x4f, 0x9c, 0xba,
    0xf2, 0x65, 0x9c, 0x0e, 0x5c, 0x3f, 0x87, 0x06, 0x6b, 0xd0, 0x6e, 0xa0,
    0xbb, 0xb9, 0x4b, 0x3f, 0xd2, 0x2d, 0xf9, 0x2c, 0x97, 0xa2, 0x3f, 0xc8,
    0x81, 0x50, 0x18, 0x0e, 0x44, 0xd2, 0xb8, 0x80, 0xd1, 0xb0, 0x0a, 0xfd,
    0x8b, 0x81, 0xd8, 0x4f, 0x11, 0xe0, 0xc1, 0x6c, 0xc0, 0x3f, 0x9a, 0x54,
    0xbe, 0x49, 0xb4, 0xc7, 0xa5, 0x86, 0x5d, 0x4c, 0xfe, 0xda, 0xea, 0x1c,
    0x9a, 0x68, 0xae, 0x50, 0x87, 0xc2, 0xd4, 0xc7, 0x8d, 0x06, 0x3a, 0x84,
    0x44, 0x59, 0x37, 0xf7, 0xe2, 0x19, 0xe9, 0x62, 0x80, 0xc0, 0xf8, 0x88,
    0x15, 0xe3, 0x92, 0x49, 0xa8, 0x7c, 0x09, 0xa4, 0x34, 0xd1, 0xba, 0xa4,
    0xa9, 0x31, 0xfd, 0x6d, 0x1d, 0x13, 0xb2, 0x83, 0x53, 0x03, 0x28, 0x64,
    0xa4, 0xec, 0xed, 0x89, 0xd3, 0x1c, 0x5e, 0x74, 0xef, 0x98, 0xee, 0xfe,
    0x7f, 0xee, 0x1c, 0x4f, 0xe0, 0x4e, 0x1e, 0x60, 0x0d, 0x29, 0xb0, 0x50,
    0x02, 0x62, 0x27, 0xac, 0x82, 0x1a, 0x89, 0x53, 0xa5, 0x7e, 0xa5, 0xd7,
    0x8f, 0xba, 0xc6, 0x58, 0xe3, 0x5d, 0x22, 0xc4, 0x69, 0xd1, 0x90, 0xd5,
    0x89, 0x88, 0x93, 0x72, 0xfe, 0xf3, 0xd9, 0xba, 0xe9, 0xb1, 0xf0, 0x90,
    0x9d, 0xf0, 0xc8, 0x8a, 0xb9, 0x12, 0xba, 0x11, 0x71, 0xa2, 0x7b, 0x22,
    0x15, 0x49, 0x7d, 0x09, 0x48, 0xd8, 0xf9, 0x1c, 0xc9, 0xc9, 0x49, 0xd3,
    0x34, 0x5f, 0x53, 0x97, 0x6c, 0x5b, 0xe2, 0x45, 0xef, 0x5f, 0xb5, 0xfc,
    0xec, 0x6e, 0xac, 0x92, 0x57, 0x07, 0x0a, 0x0f, 0xed, 0xb5, 0xbd, 0xe0,
    0xbb, 0xcb, 0x4e, 0xb9, 0xd2, 0x06, 0xc2, 0x80, 0x43, 0xa7, 0xf7, 0xd3,
    0xd1, 0xac, 0x3c, 0x99, 0x99, 0x24, 0xb9, 0xc3, 0x11, 0x9f, 0x0f, 0xfa,
    0x4e, 0x68, 0x21, 0x53, 0x60, 0x9c, 0x81, 0x4f, 0x5f, 0x12, 0xa4, 0xc2,
    0xca, 0xd1, 0xa6, 0x63, 0x01, 0xd5, 0xa2, 0xa3, 0x68, 0xba, 0x8b, 0x07,
    0xb7, 0x89, 0x53, 0x4c, 0xd0, 0x0c, 0x7b, 0x8f, 0x33, 0xd7, 0xe0, 0x58,
    0xfe, 0x45, 0xde, 0x82, 0x25, 0xe7, 0x89, 0xc8, 0x64, 0x02, 0x28, 0xff,
    0xfb, 0xd0, 0xbb, 0xc9, 0x24, 0x8a, 0x03, 0xb7, 0x49, 0x91, 0x95, 0x97,
    0x5d, 0x7d, 0xac, 0x03, 0x15, 0xb9, 0xe5, 0x8b, 0xea, 0x6e, 0xcc, 0x62,
    0x54, 0x85, 0xeb, 0xcb, 0xf1, 0x72, 0xe2, 0x8a, 0xa1, 0x26, 0x40, 0x37,
    0x89, 0x08, 0x08, 0x37, 0x93, 0x5c, 0x38, 0x62, 0xee, 0xbd, 0xca, 0x0d,
    0x12, 0xa6, 0x00, 0x3d, 0xe2, 0x22, 0xa1, 0x4a, 0x2f, 0x9b, 0xf6, 0x88,
    0xaa, 0x4f, 0x0e, 0xc6, 0xf6, 0x9d, 0xff, 0x3a, 0x6f, 0x89, 0xa3, 0x7b,
    0xc0, 0xaa, 0x85, 0xea, 0x24, 0xb4, 0x96, 0xf4, 0x27, 0xab, 0xd9, 0xaf,
    0x6e, 0x7f, 0xf3, 0x37, 0xd9, 0x31, 0xb4, 0x1d, 0x00, 0xb3, 0xcf, 0xf0,
    0x4e, 0x70, 0x49, 0xed, 0x95, 0x7b, 0xe1, 0x3e, 0xba, 0xc8, 0x79, 0x36,
    0xc2, 0x3e, 0x5a, 0x86, 0x88, 0x72, 0xac, 0x33, 0x02, 0x26, 0xbf, 0x86,
    0x98, 0x3f,
});

// The content type for all protocol requests.
constexpr char kContentType[] = "application/json";

// The maximum value honored for the `X-Retry-After` response header.
// By design, HTTP response headers are not covered by CUP signing. This is
// necessary for reverse proxies which cannot sign responses with CUP to
// instruct clients to back off even if backends are unavailable. Instead,
// response headers are protected by HTTPS. While some clients (namely, the
// Component Updater) support fallback to plaintext HTTP, the `X-Retry-After`
// header is only honored for secure connections.
// To mitigate the risk of a misconfigured server injecting a huge backoff value
// that suppresses updates indefinitely, the value is clamped to a reasonable
// maximum. See: crbug.com/513396526
constexpr int kMaxRetryAfterSec = 24 * 60 * 60;  // 24 hours.

// Returns the value of |response_cup_server_proof| or the value of
// |response_etag|, if the former value is empty.
const std::string& SelectCupServerProof(
    const std::string& response_cup_server_proof,
    const std::string& response_etag) {
  if (response_cup_server_proof.empty()) {
    DVLOG(3) << "Using etag as cup server proof.";
    return response_etag;
  }
  return response_cup_server_proof;
}

client_update_protocol::Cup BuildSigner() {
  return base::FeatureList::IsEnabled(
             client_update_protocol::features::kPqcCupSigning)
             ? client_update_protocol::Cup(
                   kMldsa44KeyVersion, base::as_byte_span(kMldsa44PublicKey))
             : client_update_protocol::Cup(kEcdsaKeyVersion,
                                           base::as_byte_span(kEcdsaPublicKey));
}

}  // namespace

RequestSender::RequestSender(
    scoped_refptr<NetworkFetcherFactory> fetcher_factory)
    : fetcher_factory_(fetcher_factory), signer_(BuildSigner()) {}

RequestSender::~RequestSender() = default;

base::OnceClosure RequestSender::Send(
    const std::vector<GURL>& urls,
    const base::flat_map<std::string, std::string>& request_extra_headers,
    const std::string& request_body,
    bool use_signing,
    RequestSenderCallback request_sender_callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  urls_ = urls;
  request_extra_headers_ = request_extra_headers;
  request_body_ = request_body;
  use_signing_ = use_signing;
  request_sender_callback_ = std::move(request_sender_callback);

  if (urls_.empty()) {
    HandleSendError(static_cast<int>(ProtocolError::MISSING_URLS), 0);
    return base::DoNothing();
  }

  cur_url_ = urls_.begin();

  SendInternal();
  return base::BindOnce(&RequestSender::Cancel, this);
}

void RequestSender::SendInternal() {
  CHECK(cur_url_ != urls_.end());
  CHECK(cur_url_->is_valid());
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  GURL url(*cur_url_);
  VLOG(2) << "url: " << url.spec();

  if (use_signing_) {
    std::string request_query_string =
        signer_.PrepareRequestParameters(request_body_);
    url = BuildUpdateUrl(url, request_query_string);
  }
  VLOG_IF(2, !url.is_valid()) << "url is not valid.";

  VLOG(2) << "Sending Omaha request: " << request_body_;

  if (!fetcher_factory_) {
    // The request was cancelled.
    return;
  }

  network_fetcher_ = fetcher_factory_->Create();
  if (!network_fetcher_) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE,
        base::BindOnce(&RequestSender::SendInternalComplete, this,
                       static_cast<int>(ProtocolError::URL_FETCHER_FAILED),
                       std::string(), std::string(), std::string(),
                       std::string(), 0));
    return;
  }
  network_fetcher_->PostRequest(
      url, request_body_, kContentType, request_extra_headers_,
      base::BindRepeating(&RequestSender::OnResponseStarted, this),
      base::DoNothing(),
      base::BindOnce(&RequestSender::OnNetworkFetcherComplete, this, url));
}

void RequestSender::SendInternalComplete(
    int error,
    const std::string& response_body,
    const std::string& response_etag,
    const std::string& response_cup_server_proof,
    const std::string& response_set_cookie,
    int retry_after_sec) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  VLOG(2) << "Omaha response received: " << response_body;
  VLOG_IF(2, error) << "Omaha send error: " << error;

  if (!error) {
    if (!use_signing_) {
      base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
          FROM_HERE, base::BindOnce(TakeRequestSenderCallback(), 0,
                                    response_body, retry_after_sec));
      return;
    }

    CHECK(use_signing_);
    const base::TimeTicks start_time = base::TimeTicks::Now();
    const bool valid = signer_.ValidateResponse(
        response_body,
        SelectCupServerProof(response_cup_server_proof, response_etag));
    metrics::RecordCupValidationResult(valid);
    metrics::RecordCupValidationTime(base::TimeTicks::Now() - start_time);
    if (valid) {
      base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
          FROM_HERE, base::BindOnce(TakeRequestSenderCallback(), 0,
                                    response_body, retry_after_sec));
      return;
    }

    error = static_cast<int>(ProtocolError::RESPONSE_NOT_TRUSTED);
  }

  CHECK(error);

  // A positive |retry_after_sec| is a hint from the server that the client
  // should not send further request until the cooldown has expired.
  if (retry_after_sec <= 0 && ++cur_url_ != urls_.end() &&
      base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
          FROM_HERE, base::BindOnce(&RequestSender::SendInternal, this))) {
    return;
  }

  HandleSendError(error, retry_after_sec);
}

void RequestSender::OnResponseStarted(int response_code,
                                      int64_t /*content_length*/) {
  response_code_ = response_code;
}

void RequestSender::OnNetworkFetcherComplete(
    const GURL& original_url,
    std::optional<std::string> response_body,
    int net_error,
    const std::string& header_etag,
    const std::string& xheader_cup_server_proof,
    const std::string& header_set_cookie,
    int64_t xheader_retry_after_sec) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  VLOG(1) << "Request completed from url: " << original_url.spec();

  int error = -1;
  if (!net_error && response_code_ == 200) {
    error = 0;
  } else if (response_code_ != -1) {
    error = response_code_;
  } else {
    error = net_error;
  }

  int retry_after_sec = -1;
  if (original_url.SchemeIsCryptographic() && error >= 0) {
    retry_after_sec = std::min(
        base::saturated_cast<int>(xheader_retry_after_sec), kMaxRetryAfterSec);
  }

  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(&RequestSender::SendInternalComplete, this, error,
                     std::move(response_body).value_or(std::string()),
                     header_etag, xheader_cup_server_proof, header_set_cookie,
                     retry_after_sec));
}

void RequestSender::HandleSendError(int error, int retry_after_sec) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  VLOG(2) << __func__;

  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(TakeRequestSenderCallback(), error,
                                std::string(), retry_after_sec));
}

GURL RequestSender::BuildUpdateUrl(const GURL& url,
                                   const std::string& query_params) {
  const std::string query_string(
      url.has_query() ? base::StrCat({url.GetQuery(), "&", query_params})
                      : query_params);
  GURL::Replacements replacements;
  replacements.SetQueryStr(query_string);

  return url.ReplaceComponents(replacements);
}

void RequestSender::Cancel() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  HandleSendError(static_cast<int>(ServiceError::CANCELLED), 0);
  network_fetcher_.reset();
  fetcher_factory_.reset();
}

RequestSender::RequestSenderCallback
RequestSender::TakeRequestSenderCallback() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  VLOG(2) << __func__;

  RequestSenderCallback callback = std::move(request_sender_callback_);
  request_sender_callback_ = base::DoNothing();
  return callback;
}

}  // namespace update_client
