#include "components/network_time/network_time_tracker.h"
#include <stdint.h>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include "base/check.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/i18n/time_formatting.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/metrics/field_trial_params.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/client_update_protocol/ecdsa.h"
#include "components/network_time/network_time_pref_names.h"
#include "components/network_time/time_tracker/time_tracker.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.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/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
namespace network_time {
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_IOS)
BASE_FEATURE(kNetworkTimeServiceQuerying,
"NetworkTimeServiceQuerying",
base::FEATURE_DISABLED_BY_DEFAULT);
#else
BASE_FEATURE(…);
#endif
namespace {
constexpr base::FeatureParam<base::TimeDelta> kCheckTimeInterval{ … };
constexpr base::FeatureParam<base::TimeDelta> kBackoffInterval{ … };
constexpr base::FeatureParam<double> kRandomQueryProbability{ … };
constexpr base::FeatureParam<NetworkTimeTracker::FetchBehavior>::Option
kFetchBehaviorOptions[] = …;
constexpr base::FeatureParam<NetworkTimeTracker::FetchBehavior> kFetchBehavior{ … };
const uint32_t kNumTimeMeasurements = …;
const uint32_t kSerializedDataMaxAgeDays = …;
const char kPrefTime[] = …;
const char kPrefTicks[] = …;
const char kPrefUncertainty[] = …;
const char kPrefNetworkTime[] = …;
const uint32_t kTimeServerMaxSkewSeconds = …;
const char kTimeServiceURL[] = …;
const int kKeyVersion = …;
const uint8_t kKeyPubBytes[] = …;
std::string GetServerProof(
scoped_refptr<net::HttpResponseHeaders> response_headers) { … }
}
void NetworkTimeTracker::RegisterPrefs(PrefRegistrySimple* registry) { … }
NetworkTimeTracker::NetworkTimeTracker(
std::unique_ptr<base::Clock> clock,
std::unique_ptr<const base::TickClock> tick_clock,
PrefService* pref_service,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
std::optional<FetchBehavior> fetch_behavior)
: … { … }
NetworkTimeTracker::~NetworkTimeTracker() { … }
void NetworkTimeTracker::UpdateNetworkTime(base::Time network_time,
base::TimeDelta resolution,
base::TimeDelta latency,
base::TimeTicks post_time) { … }
bool NetworkTimeTracker::AreTimeFetchesEnabled() const { … }
NetworkTimeTracker::FetchBehavior NetworkTimeTracker::GetFetchBehavior() const { … }
void NetworkTimeTracker::SetTimeServerURLForTesting(const GURL& url) { … }
GURL NetworkTimeTracker::GetTimeServerURLForTesting() const { … }
void NetworkTimeTracker::SetMaxResponseSizeForTesting(size_t limit) { … }
void NetworkTimeTracker::SetPublicKeyForTesting(std::string_view key) { … }
bool NetworkTimeTracker::QueryTimeServiceForTesting() { … }
void NetworkTimeTracker::WaitForFetch() { … }
void NetworkTimeTracker::AddObserver(NetworkTimeObserver* obs) { … }
void NetworkTimeTracker::RemoveObserver(NetworkTimeObserver* obs) { … }
bool NetworkTimeTracker::GetTrackerState(
TimeTracker::TimeTrackerState* state) const { … }
void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) { … }
void NetworkTimeTracker::OverrideNonceForTesting(uint32_t nonce) { … }
base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { … }
void NetworkTimeTracker::ClearNetworkTimeForTesting() { … }
NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime(
base::Time* network_time,
base::TimeDelta* uncertainty) const { … }
bool NetworkTimeTracker::StartTimeFetch(base::OnceClosure closure) { … }
void NetworkTimeTracker::CheckTime() { … }
bool NetworkTimeTracker::UpdateTimeFromResponse(
std::unique_ptr<std::string> response_body) { … }
void NetworkTimeTracker::OnURLLoaderComplete(
std::unique_ptr<std::string> response_body) { … }
void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { … }
bool NetworkTimeTracker::ShouldIssueTimeQuery() { … }
void NetworkTimeTracker::NotifyObservers() { … }
}