| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_SYNC_DRIVER_DEVICE_INFO_SERVICE_H_ | 5 #ifndef COMPONENTS_SYNC_DRIVER_DEVICE_INFO_SERVICE_H_ |
| 6 #define COMPONENTS_SYNC_DRIVER_DEVICE_INFO_SERVICE_H_ | 6 #define COMPONENTS_SYNC_DRIVER_DEVICE_INFO_SERVICE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> |
| 11 #include <string> | 12 #include <string> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 18 #include "components/sync_driver/device_info_tracker.h" | 18 #include "components/sync_driver/device_info_tracker.h" |
| 19 #include "components/sync_driver/local_device_info_provider.h" | 19 #include "components/sync_driver/local_device_info_provider.h" |
| 20 #include "sync/api/model_type_service.h" | 20 #include "sync/api/model_type_service.h" |
| 21 #include "sync/api/model_type_store.h" | 21 #include "sync/api/model_type_store.h" |
| 22 #include "sync/internal_api/public/simple_metadata_change_list.h" | 22 #include "sync/internal_api/public/simple_metadata_change_list.h" |
| 23 | 23 |
| 24 namespace syncer { | 24 namespace syncer { |
| 25 class SyncError; | 25 class SyncError; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 45 const syncer_v2::ModelTypeStore::InitCallback& callback)> | 45 const syncer_v2::ModelTypeStore::InitCallback& callback)> |
| 46 StoreFactoryFunction; | 46 StoreFactoryFunction; |
| 47 | 47 |
| 48 DeviceInfoService( | 48 DeviceInfoService( |
| 49 sync_driver::LocalDeviceInfoProvider* local_device_info_provider, | 49 sync_driver::LocalDeviceInfoProvider* local_device_info_provider, |
| 50 const StoreFactoryFunction& callback, | 50 const StoreFactoryFunction& callback, |
| 51 const ChangeProcessorFactory& change_processor_factory); | 51 const ChangeProcessorFactory& change_processor_factory); |
| 52 ~DeviceInfoService() override; | 52 ~DeviceInfoService() override; |
| 53 | 53 |
| 54 // ModelTypeService implementation. | 54 // ModelTypeService implementation. |
| 55 scoped_ptr<syncer_v2::MetadataChangeList> CreateMetadataChangeList() override; | 55 std::unique_ptr<syncer_v2::MetadataChangeList> CreateMetadataChangeList() |
| 56 override; |
| 56 syncer::SyncError MergeSyncData( | 57 syncer::SyncError MergeSyncData( |
| 57 scoped_ptr<syncer_v2::MetadataChangeList> metadata_change_list, | 58 std::unique_ptr<syncer_v2::MetadataChangeList> metadata_change_list, |
| 58 syncer_v2::EntityDataMap entity_data_map) override; | 59 syncer_v2::EntityDataMap entity_data_map) override; |
| 59 syncer::SyncError ApplySyncChanges( | 60 syncer::SyncError ApplySyncChanges( |
| 60 scoped_ptr<syncer_v2::MetadataChangeList> metadata_change_list, | 61 std::unique_ptr<syncer_v2::MetadataChangeList> metadata_change_list, |
| 61 syncer_v2::EntityChangeList entity_changes) override; | 62 syncer_v2::EntityChangeList entity_changes) override; |
| 62 void GetData(ClientTagList client_tags, DataCallback callback) override; | 63 void GetData(ClientTagList client_tags, DataCallback callback) override; |
| 63 void GetAllData(DataCallback callback) override; | 64 void GetAllData(DataCallback callback) override; |
| 64 std::string GetClientTag(const syncer_v2::EntityData& entity_data) override; | 65 std::string GetClientTag(const syncer_v2::EntityData& entity_data) override; |
| 65 void OnChangeProcessorSet() override; | 66 void OnChangeProcessorSet() override; |
| 66 | 67 |
| 67 // DeviceInfoTracker implementation. | 68 // DeviceInfoTracker implementation. |
| 68 bool IsSyncing() const override; | 69 bool IsSyncing() const override; |
| 69 scoped_ptr<sync_driver::DeviceInfo> GetDeviceInfo( | 70 std::unique_ptr<sync_driver::DeviceInfo> GetDeviceInfo( |
| 70 const std::string& client_id) const override; | 71 const std::string& client_id) const override; |
| 71 ScopedVector<sync_driver::DeviceInfo> GetAllDeviceInfo() const override; | 72 ScopedVector<sync_driver::DeviceInfo> GetAllDeviceInfo() const override; |
| 72 void AddObserver(Observer* observer) override; | 73 void AddObserver(Observer* observer) override; |
| 73 void RemoveObserver(Observer* observer) override; | 74 void RemoveObserver(Observer* observer) override; |
| 74 int CountActiveDevices() const override; | 75 int CountActiveDevices() const override; |
| 75 | 76 |
| 76 private: | 77 private: |
| 77 friend class DeviceInfoServiceTest; | 78 friend class DeviceInfoServiceTest; |
| 78 | 79 |
| 79 // Formats ClientTag form DeviceInfoSpecifics. | 80 // Formats ClientTag form DeviceInfoSpecifics. |
| 80 static std::string SpecificsToTag( | 81 static std::string SpecificsToTag( |
| 81 const sync_pb::DeviceInfoSpecifics& specifics); | 82 const sync_pb::DeviceInfoSpecifics& specifics); |
| 82 | 83 |
| 83 // Extracts cache_guid from ClientTag. | 84 // Extracts cache_guid from ClientTag. |
| 84 static std::string TagToCacheGuid(const std::string& tag); | 85 static std::string TagToCacheGuid(const std::string& tag); |
| 85 | 86 |
| 86 static scoped_ptr<sync_pb::DeviceInfoSpecifics> CopyToSpecifics( | 87 static std::unique_ptr<sync_pb::DeviceInfoSpecifics> CopyToSpecifics( |
| 87 const sync_driver::DeviceInfo& info); | 88 const sync_driver::DeviceInfo& info); |
| 88 | 89 |
| 89 // Allocate new DeviceInfo from SyncData. | 90 // Allocate new DeviceInfo from SyncData. |
| 90 static scoped_ptr<sync_driver::DeviceInfo> CopyToModel( | 91 static std::unique_ptr<sync_driver::DeviceInfo> CopyToModel( |
| 91 const sync_pb::DeviceInfoSpecifics& specifics); | 92 const sync_pb::DeviceInfoSpecifics& specifics); |
| 92 // Conversion as we prepare to hand data to the processor. | 93 // Conversion as we prepare to hand data to the processor. |
| 93 static scoped_ptr<syncer_v2::EntityData> CopyToEntityData( | 94 static std::unique_ptr<syncer_v2::EntityData> CopyToEntityData( |
| 94 const sync_pb::DeviceInfoSpecifics& specifics); | 95 const sync_pb::DeviceInfoSpecifics& specifics); |
| 95 | 96 |
| 96 // Store SyncData in the cache and durable storage. | 97 // Store SyncData in the cache and durable storage. |
| 97 void StoreSpecifics(scoped_ptr<sync_pb::DeviceInfoSpecifics> specifics, | 98 void StoreSpecifics(std::unique_ptr<sync_pb::DeviceInfoSpecifics> specifics, |
| 98 syncer_v2::ModelTypeStore::WriteBatch* batch); | 99 syncer_v2::ModelTypeStore::WriteBatch* batch); |
| 99 // Delete SyncData from the cache and durable storage, returns true if there | 100 // Delete SyncData from the cache and durable storage, returns true if there |
| 100 // was actually anything at the given tag. | 101 // was actually anything at the given tag. |
| 101 bool DeleteSpecifics(const std::string& tag, | 102 bool DeleteSpecifics(const std::string& tag, |
| 102 syncer_v2::ModelTypeStore::WriteBatch* batch); | 103 syncer_v2::ModelTypeStore::WriteBatch* batch); |
| 103 | 104 |
| 104 // Notify all registered observers. | 105 // Notify all registered observers. |
| 105 void NotifyObservers(); | 106 void NotifyObservers(); |
| 106 | 107 |
| 107 // Used as callback given to LocalDeviceInfoProvider. | 108 // Used as callback given to LocalDeviceInfoProvider. |
| 108 void OnProviderInitialized(); | 109 void OnProviderInitialized(); |
| 109 | 110 |
| 110 // Methods used as callbacks given to DataTypeStore. | 111 // Methods used as callbacks given to DataTypeStore. |
| 111 void OnStoreCreated(syncer_v2::ModelTypeStore::Result result, | 112 void OnStoreCreated(syncer_v2::ModelTypeStore::Result result, |
| 112 scoped_ptr<syncer_v2::ModelTypeStore> store); | 113 std::unique_ptr<syncer_v2::ModelTypeStore> store); |
| 113 void OnReadAllData( | 114 void OnReadAllData( |
| 114 syncer_v2::ModelTypeStore::Result result, | 115 syncer_v2::ModelTypeStore::Result result, |
| 115 scoped_ptr<syncer_v2::ModelTypeStore::RecordList> record_list); | 116 std::unique_ptr<syncer_v2::ModelTypeStore::RecordList> record_list); |
| 116 void OnReadAllMetadata( | 117 void OnReadAllMetadata( |
| 117 syncer_v2::ModelTypeStore::Result result, | 118 syncer_v2::ModelTypeStore::Result result, |
| 118 scoped_ptr<syncer_v2::ModelTypeStore::RecordList> metadata_records, | 119 std::unique_ptr<syncer_v2::ModelTypeStore::RecordList> metadata_records, |
| 119 const std::string& global_metadata); | 120 const std::string& global_metadata); |
| 120 void OnCommit(syncer_v2::ModelTypeStore::Result result); | 121 void OnCommit(syncer_v2::ModelTypeStore::Result result); |
| 121 | 122 |
| 122 // Checks if conditions have been met to perform reconciliation between the | 123 // Checks if conditions have been met to perform reconciliation between the |
| 123 // locally provide device info and the stored device info data. If conditions | 124 // locally provide device info and the stored device info data. If conditions |
| 124 // are met and the sets of data differ, than we condier this a local change | 125 // are met and the sets of data differ, than we condier this a local change |
| 125 // and we send it to the processor. | 126 // and we send it to the processor. |
| 126 void TryReconcileLocalAndStored(); | 127 void TryReconcileLocalAndStored(); |
| 127 | 128 |
| 128 // Writes the given device info to both local storage and to sync. | 129 // Writes the given device info to both local storage and to sync. |
| 129 void PutAndStore(const sync_driver::DeviceInfo& device_info); | 130 void PutAndStore(const sync_driver::DeviceInfo& device_info); |
| 130 | 131 |
| 131 // Persists the changes in the given aggregators and notifies observers if | 132 // Persists the changes in the given aggregators and notifies observers if |
| 132 // indicated to do as such. | 133 // indicated to do as such. |
| 133 void CommitAndNotify( | 134 void CommitAndNotify( |
| 134 scoped_ptr<syncer_v2::ModelTypeStore::WriteBatch> batch, | 135 std::unique_ptr<syncer_v2::ModelTypeStore::WriteBatch> batch, |
| 135 scoped_ptr<syncer_v2::MetadataChangeList> metadata_change_list, | 136 std::unique_ptr<syncer_v2::MetadataChangeList> metadata_change_list, |
| 136 bool should_notify); | 137 bool should_notify); |
| 137 | 138 |
| 138 // |local_device_info_provider_| isn't owned. | 139 // |local_device_info_provider_| isn't owned. |
| 139 const sync_driver::LocalDeviceInfoProvider* const local_device_info_provider_; | 140 const sync_driver::LocalDeviceInfoProvider* const local_device_info_provider_; |
| 140 | 141 |
| 141 // Cache of all syncable and local data, stored by device cache guid. | 142 // Cache of all syncable and local data, stored by device cache guid. |
| 142 using ClientIdToSpecifics = | 143 using ClientIdToSpecifics = |
| 143 std::map<std::string, scoped_ptr<sync_pb::DeviceInfoSpecifics>>; | 144 std::map<std::string, std::unique_ptr<sync_pb::DeviceInfoSpecifics>>; |
| 144 ClientIdToSpecifics all_data_; | 145 ClientIdToSpecifics all_data_; |
| 145 | 146 |
| 146 // Registered observers, not owned. | 147 // Registered observers, not owned. |
| 147 base::ObserverList<Observer, true> observers_; | 148 base::ObserverList<Observer, true> observers_; |
| 148 | 149 |
| 149 // Used to listen for provider initialization. If the provider is already | 150 // Used to listen for provider initialization. If the provider is already |
| 150 // initialized during our constructor then the subscription is never used. | 151 // initialized during our constructor then the subscription is never used. |
| 151 scoped_ptr<sync_driver::LocalDeviceInfoProvider::Subscription> subscription_; | 152 std::unique_ptr<sync_driver::LocalDeviceInfoProvider::Subscription> |
| 153 subscription_; |
| 152 | 154 |
| 153 // In charge of actually persiting changes to disk, or loading previous data. | 155 // In charge of actually persiting changes to disk, or loading previous data. |
| 154 scoped_ptr<syncer_v2::ModelTypeStore> store_; | 156 std::unique_ptr<syncer_v2::ModelTypeStore> store_; |
| 155 | 157 |
| 156 // If |local_device_info_provider_| has initialized. | 158 // If |local_device_info_provider_| has initialized. |
| 157 bool has_provider_initialized_ = false; | 159 bool has_provider_initialized_ = false; |
| 158 // if |change_processor()| has been given metadata. | 160 // if |change_processor()| has been given metadata. |
| 159 bool has_metadata_loaded_ = false; | 161 bool has_metadata_loaded_ = false; |
| 160 | 162 |
| 161 // Should always be last member. | 163 // Should always be last member. |
| 162 base::WeakPtrFactory<DeviceInfoService> weak_factory_; | 164 base::WeakPtrFactory<DeviceInfoService> weak_factory_; |
| 163 | 165 |
| 164 DISALLOW_COPY_AND_ASSIGN(DeviceInfoService); | 166 DISALLOW_COPY_AND_ASSIGN(DeviceInfoService); |
| 165 }; | 167 }; |
| 166 | 168 |
| 167 } // namespace sync_driver_v2 | 169 } // namespace sync_driver_v2 |
| 168 | 170 |
| 169 #endif // COMPONENTS_SYNC_DRIVER_DEVICE_INFO_SERVICE_H_ | 171 #endif // COMPONENTS_SYNC_DRIVER_DEVICE_INFO_SERVICE_H_ |
| OLD | NEW |