| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SERVICE_H_ | |
| 6 #define COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SERVICE_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "base/observer_list.h" | |
| 18 #include "base/time/time.h" | |
| 19 #include "base/timer/timer.h" | |
| 20 #include "components/sync/device_info/device_info_tracker.h" | |
| 21 #include "components/sync/device_info/local_device_info_provider.h" | |
| 22 #include "components/sync/model/model_type_store.h" | |
| 23 #include "components/sync/model/model_type_sync_bridge.h" | |
| 24 #include "components/sync/model/simple_metadata_change_list.h" | |
| 25 | |
| 26 namespace sync_pb { | |
| 27 class DeviceInfoSpecifics; | |
| 28 } // namespace sync_pb | |
| 29 | |
| 30 namespace syncer { | |
| 31 | |
| 32 class ModelTypeChangeProcessor; | |
| 33 class SyncError; | |
| 34 | |
| 35 // USS service implementation for DEVICE_INFO model type. Handles storage of | |
| 36 // device info and associated sync metadata, applying/merging foreign changes, | |
| 37 // and allows public read access. | |
| 38 class DeviceInfoService : public ModelTypeSyncBridge, public DeviceInfoTracker { | |
| 39 public: | |
| 40 typedef base::Callback<void(const ModelTypeStore::InitCallback& callback)> | |
| 41 StoreFactoryFunction; | |
| 42 | |
| 43 DeviceInfoService(LocalDeviceInfoProvider* local_device_info_provider, | |
| 44 const StoreFactoryFunction& callback, | |
| 45 const ChangeProcessorFactory& change_processor_factory); | |
| 46 ~DeviceInfoService() override; | |
| 47 | |
| 48 // ModelTypeSyncBridge implementation. | |
| 49 std::unique_ptr<MetadataChangeList> CreateMetadataChangeList() override; | |
| 50 SyncError MergeSyncData( | |
| 51 std::unique_ptr<MetadataChangeList> metadata_change_list, | |
| 52 EntityDataMap entity_data_map) override; | |
| 53 SyncError ApplySyncChanges( | |
| 54 std::unique_ptr<MetadataChangeList> metadata_change_list, | |
| 55 EntityChangeList entity_changes) override; | |
| 56 void GetData(StorageKeyList storage_keys, DataCallback callback) override; | |
| 57 void GetAllData(DataCallback callback) override; | |
| 58 std::string GetClientTag(const EntityData& entity_data) override; | |
| 59 std::string GetStorageKey(const EntityData& entity_data) override; | |
| 60 void DisableSync() override; | |
| 61 | |
| 62 // DeviceInfoTracker implementation. | |
| 63 bool IsSyncing() const override; | |
| 64 std::unique_ptr<DeviceInfo> GetDeviceInfo( | |
| 65 const std::string& client_id) const override; | |
| 66 std::vector<std::unique_ptr<DeviceInfo>> GetAllDeviceInfo() const override; | |
| 67 void AddObserver(Observer* observer) override; | |
| 68 void RemoveObserver(Observer* observer) override; | |
| 69 int CountActiveDevices() const override; | |
| 70 | |
| 71 private: | |
| 72 friend class DeviceInfoServiceTest; | |
| 73 | |
| 74 // Cache of all syncable and local data, stored by device cache guid. | |
| 75 using ClientIdToSpecifics = | |
| 76 std::map<std::string, std::unique_ptr<sync_pb::DeviceInfoSpecifics>>; | |
| 77 | |
| 78 // Store SyncData in the cache and durable storage. | |
| 79 void StoreSpecifics(std::unique_ptr<sync_pb::DeviceInfoSpecifics> specifics, | |
| 80 ModelTypeStore::WriteBatch* batch); | |
| 81 // Delete SyncData from the cache and durable storage, returns true if there | |
| 82 // was actually anything at the given tag. | |
| 83 bool DeleteSpecifics(const std::string& tag, | |
| 84 ModelTypeStore::WriteBatch* batch); | |
| 85 | |
| 86 // Notify all registered observers. | |
| 87 void NotifyObservers(); | |
| 88 | |
| 89 // Used as callback given to LocalDeviceInfoProvider. | |
| 90 void OnProviderInitialized(); | |
| 91 | |
| 92 // Methods used as callbacks given to DataTypeStore. | |
| 93 void OnStoreCreated(ModelTypeStore::Result result, | |
| 94 std::unique_ptr<ModelTypeStore> store); | |
| 95 void OnReadAllData(ModelTypeStore::Result result, | |
| 96 std::unique_ptr<ModelTypeStore::RecordList> record_list); | |
| 97 void OnReadAllMetadata( | |
| 98 ModelTypeStore::Result result, | |
| 99 std::unique_ptr<ModelTypeStore::RecordList> metadata_records, | |
| 100 const std::string& global_metadata); | |
| 101 void OnCommit(ModelTypeStore::Result result); | |
| 102 | |
| 103 // Load metadata if the data is loaded and the provider is initialized. | |
| 104 void LoadMetadataIfReady(); | |
| 105 | |
| 106 // Performs reconciliation between the locally provided device info and the | |
| 107 // stored device info data. If the sets of data differ, then we consider this | |
| 108 // a local change and we send it to the processor. | |
| 109 void ReconcileLocalAndStored(); | |
| 110 | |
| 111 // Stores the updated version of the local copy of device info in durable | |
| 112 // storage, in memory, and informs sync of the change. Should not be called | |
| 113 // before the provider and processor have initialized. | |
| 114 void SendLocalData(); | |
| 115 | |
| 116 // Persists the changes in the given aggregators and notifies observers if | |
| 117 // indicated to do as such. | |
| 118 void CommitAndNotify(std::unique_ptr<ModelTypeStore::WriteBatch> batch, | |
| 119 std::unique_ptr<MetadataChangeList> metadata_change_list, | |
| 120 bool should_notify); | |
| 121 | |
| 122 // Counts the number of active devices relative to |now|. The activeness of a | |
| 123 // device depends on the amount of time since it was updated, which means | |
| 124 // comparing it against the current time. |now| is passed into this method to | |
| 125 // allow unit tests to control expected results. | |
| 126 int CountActiveDevices(const base::Time now) const; | |
| 127 | |
| 128 // Report an error starting up to sync if it tries to connect to this | |
| 129 // datatype, since these errors prevent us from knowing if sync is enabled. | |
| 130 void ReportStartupErrorToSync(const std::string& msg); | |
| 131 | |
| 132 // |local_device_info_provider_| isn't owned. | |
| 133 const LocalDeviceInfoProvider* const local_device_info_provider_; | |
| 134 | |
| 135 ClientIdToSpecifics all_data_; | |
| 136 | |
| 137 // Registered observers, not owned. | |
| 138 base::ObserverList<Observer, true> observers_; | |
| 139 | |
| 140 // Used to listen for provider initialization. If the provider is already | |
| 141 // initialized during our constructor then the subscription is never used. | |
| 142 std::unique_ptr<LocalDeviceInfoProvider::Subscription> subscription_; | |
| 143 | |
| 144 // In charge of actually persiting changes to disk, or loading previous data. | |
| 145 std::unique_ptr<ModelTypeStore> store_; | |
| 146 | |
| 147 // If |local_device_info_provider_| has initialized. | |
| 148 bool has_provider_initialized_ = false; | |
| 149 // If data has been loaded from the store. | |
| 150 bool has_data_loaded_ = false; | |
| 151 | |
| 152 // Used to update our local device info once every pulse interval. | |
| 153 base::OneShotTimer pulse_timer_; | |
| 154 | |
| 155 DISALLOW_COPY_AND_ASSIGN(DeviceInfoService); | |
| 156 }; | |
| 157 | |
| 158 } // namespace syncer | |
| 159 | |
| 160 #endif // COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SERVICE_H_ | |
| OLD | NEW |