1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_SYNC_PREFERENCES_CROSS_DEVICE_PREF_TRACKER_H_
#define COMPONENTS_SYNC_PREFERENCES_CROSS_DEVICE_PREF_TRACKER_H_
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "base/observer_list_types.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/sync_device_info/device_info.h"
namespace sync_preferences {
// Abstract interface for a keyed service responsible for querying the values of
// some selected non-syncing Prefs across all of a user's syncing devices. It
// allows clients to observe how a particular non-syncing Pref value differs
// across Chrome platforms and form factors.
class CrossDevicePrefTracker : public KeyedService {
public:
// Holds a Pref's `value` and the time of its last observed change.
//
// The timestamp is synced across devices. For a Pref that existed before this
// tracker was initialized, the timestamp is taken from the first recorded
// change after the user started syncing Prefs. If no change has been
// recorded, the timestamp will be unset.
struct TimestampedPrefValue {
base::Value value;
base::Time last_observed_change_time;
};
// Observer interface for remote changes.
class Observer : public base::CheckedObserver {
public:
// Called when `pref_name` is updated to `pref_value` on a remote device.
virtual void OnRemotePrefChanged(
std::string_view pref_name,
const TimestampedPrefValue& pref_value,
const syncer::DeviceInfo& remote_device_info) {}
};
// Defines criteria for querying devices.
struct DeviceFilter {
std::optional<syncer::DeviceInfo::OsType> os_type;
std::optional<syncer::DeviceInfo::FormFactor> form_factor;
};
~CrossDevicePrefTracker() override;
virtual void AddObserver(Observer* observer) = 0;
virtual void RemoveObserver(Observer* observer) = 0;
// Retrieves all values for a tracked pref matching the filter, sorted in
// descending order by timestamp (i.e., most recent first).
virtual std::vector<TimestampedPrefValue> GetValues(
std::string_view pref_name,
const DeviceFilter& filter) const = 0;
// Convenience wrapper to get the single most recent value.
//
// NOTE: In the case of a timestamp collision, we'll use the value of the
// device that's most recently updated with the sync servers (via
// `DeviceInfo::last_updated_timestamp`).
virtual std::optional<TimestampedPrefValue> GetMostRecentValue(
std::string_view pref_name,
const DeviceFilter& filter) const = 0;
protected:
CrossDevicePrefTracker() = default;
};
} // namespace sync_preferences
#endif // COMPONENTS_SYNC_PREFERENCES_CROSS_DEVICE_PREF_TRACKER_H_
|