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