chromium/components/network_time/network_time_tracker.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/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"

// Time updates happen in two ways. First, other components may call
// UpdateNetworkTime() if they happen to obtain the time securely. This will
// likely be deprecated in favor of the second way, which is scheduled time
// queries issued by NetworkTimeTracker itself.
//
// On startup, the clock state may be read from a pref. (This, too, may be
// deprecated.) After that, the time is checked every |kCheckTimeInterval|. A
// "check" means the possibility, but not the certainty, of a time query. A time
// query may be issued at random, or if the network time is believed to have
// become inaccurate.
//
// After issuing a query, the next check will not happen until
// |kBackoffInterval|. This delay is doubled in the event of an error.

namespace network_time {

// Network time queries are enabled on all desktop platforms except ChromeOS,
// which uses tlsdated to set the system 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 {

// Duration between time checks. The value should be greater than zero. Note
// that a "check" is not necessarily a network time query!
constexpr base::FeatureParam<base::TimeDelta> kCheckTimeInterval{};

// Minimum number of minutes between time queries.
constexpr base::FeatureParam<base::TimeDelta> kBackoffInterval{};

// Probability that a check will randomly result in a query. Checks are made
// every |kCheckTimeInterval|. The default values are chosen with the goal of a
// high probability that a query will be issued every 24 hours. The value should
// fall between 0.0 and 1.0 (inclusive).
constexpr base::FeatureParam<double> kRandomQueryProbability{};

// The |kFetchBehavior| parameter can have three values:
//
// - "background-only": Time queries will be issued in the background as
//   needed (when the clock loses sync), but on-demand time queries will
//   not be issued (i.e. StartTimeFetch() will not start time queries.)
//
// - "on-demand-only": Time queries will not be issued except when
//   StartTimeFetch() is called. This is the default value.
//
// - "background-and-on-demand": Time queries will be issued both in the
//   background as needed and also on-demand.
constexpr base::FeatureParam<NetworkTimeTracker::FetchBehavior>::Option
    kFetchBehaviorOptions[] =;
constexpr base::FeatureParam<NetworkTimeTracker::FetchBehavior> kFetchBehavior{};

// Number of time measurements performed in a given network time calculation.
const uint32_t kNumTimeMeasurements =;

// Maximum time lapse before deserialized data are considered stale.
const uint32_t kSerializedDataMaxAgeDays =;

// Name of a pref that stores the wall clock time, via
// |InMillisecondsFSinceUnixEpoch|.
const char kPrefTime[] =;

// Name of a pref that stores the tick clock time, via |ToInternalValue|.
const char kPrefTicks[] =;

// Name of a pref that stores the time uncertainty, via |ToInternalValue|.
const char kPrefUncertainty[] =;

// Name of a pref that stores the network time via
// |InMillisecondsFSinceUnixEpoch|.
const char kPrefNetworkTime[] =;

// Time server's maximum allowable clock skew, in seconds.  (This is a property
// of the time server that we happen to know.  It's unlikely that it would ever
// be that badly wrong, but all the same it's included here to document the very
// rough nature of the time service provided by this class.)
const uint32_t kTimeServerMaxSkewSeconds =;

const char kTimeServiceURL[] =;

// This is an ECDSA prime256v1 named-curve key.
const int kKeyVersion =;
const uint8_t kKeyPubBytes[] =;

std::string GetServerProof(
    scoped_refptr<net::HttpResponseHeaders> response_headers) {}

}  // namespace

// static
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() {}

}  // namespace network_time