| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 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_CORE_IMPL_SYNC_MANAGER_IMPL_H_ | |
| 6 #define COMPONENTS_SYNC_CORE_IMPL_SYNC_MANAGER_IMPL_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/gtest_prod_util.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "components/sync/base/cryptographer.h" | |
| 18 #include "components/sync/base/time.h" | |
| 19 #include "components/sync/core/sync_manager.h" | |
| 20 #include "components/sync/core_impl/debug_info_event_listener.h" | |
| 21 #include "components/sync/core_impl/js_mutation_event_observer.h" | |
| 22 #include "components/sync/core_impl/js_sync_encryption_handler_observer.h" | |
| 23 #include "components/sync/core_impl/js_sync_manager_observer.h" | |
| 24 #include "components/sync/core_impl/protocol_event_buffer.h" | |
| 25 #include "components/sync/core_impl/sync_encryption_handler_impl.h" | |
| 26 #include "components/sync/engine_impl/all_status.h" | |
| 27 #include "components/sync/engine_impl/net/server_connection_manager.h" | |
| 28 #include "components/sync/engine_impl/nudge_handler.h" | |
| 29 #include "components/sync/engine_impl/sync_engine_event_listener.h" | |
| 30 #include "components/sync/js/js_backend.h" | |
| 31 #include "components/sync/syncable/change_reorder_buffer.h" | |
| 32 #include "components/sync/syncable/directory_change_delegate.h" | |
| 33 #include "components/sync/syncable/user_share.h" | |
| 34 #include "net/base/network_change_notifier.h" | |
| 35 | |
| 36 class GURL; | |
| 37 | |
| 38 namespace syncer { | |
| 39 | |
| 40 class ModelTypeRegistry; | |
| 41 class SyncServerConnectionManager; | |
| 42 class SyncCycleContext; | |
| 43 class TypeDebugInfoObserver; | |
| 44 class WriteNode; | |
| 45 class WriteTransaction; | |
| 46 | |
| 47 // SyncManager encapsulates syncable::Directory and serves as the parent of all | |
| 48 // other objects in the sync API. If multiple threads interact with the same | |
| 49 // local sync repository (i.e. the same sqlite database), they should share a | |
| 50 // single SyncManager instance. The caller should typically create one | |
| 51 // SyncManager for the lifetime of a user session. | |
| 52 // | |
| 53 // Unless stated otherwise, all methods of SyncManager should be called on the | |
| 54 // same thread. | |
| 55 class SyncManagerImpl | |
| 56 : public SyncManager, | |
| 57 public net::NetworkChangeNotifier::IPAddressObserver, | |
| 58 public net::NetworkChangeNotifier::ConnectionTypeObserver, | |
| 59 public JsBackend, | |
| 60 public SyncEngineEventListener, | |
| 61 public ServerConnectionEventListener, | |
| 62 public syncable::DirectoryChangeDelegate, | |
| 63 public SyncEncryptionHandler::Observer, | |
| 64 public NudgeHandler { | |
| 65 public: | |
| 66 // Create an uninitialized SyncManager. Callers must Init() before using. | |
| 67 explicit SyncManagerImpl(const std::string& name); | |
| 68 ~SyncManagerImpl() override; | |
| 69 | |
| 70 // SyncManager implementation. | |
| 71 void Init(InitArgs* args) override; | |
| 72 ModelTypeSet InitialSyncEndedTypes() override; | |
| 73 ModelTypeSet GetTypesWithEmptyProgressMarkerToken( | |
| 74 ModelTypeSet types) override; | |
| 75 bool PurgePartiallySyncedTypes() override; | |
| 76 void UpdateCredentials(const SyncCredentials& credentials) override; | |
| 77 void StartSyncingNormally(const ModelSafeRoutingInfo& routing_info, | |
| 78 base::Time last_poll_time) override; | |
| 79 void ConfigureSyncer(ConfigureReason reason, | |
| 80 ModelTypeSet to_download, | |
| 81 ModelTypeSet to_purge, | |
| 82 ModelTypeSet to_journal, | |
| 83 ModelTypeSet to_unapply, | |
| 84 const ModelSafeRoutingInfo& new_routing_info, | |
| 85 const base::Closure& ready_task, | |
| 86 const base::Closure& retry_task) override; | |
| 87 void SetInvalidatorEnabled(bool invalidator_enabled) override; | |
| 88 void OnIncomingInvalidation( | |
| 89 ModelType type, | |
| 90 std::unique_ptr<InvalidationInterface> invalidation) override; | |
| 91 void AddObserver(SyncManager::Observer* observer) override; | |
| 92 void RemoveObserver(SyncManager::Observer* observer) override; | |
| 93 SyncStatus GetDetailedStatus() const override; | |
| 94 void SaveChanges() override; | |
| 95 void ShutdownOnSyncThread(ShutdownReason reason) override; | |
| 96 UserShare* GetUserShare() override; | |
| 97 std::unique_ptr<ModelTypeConnector> GetModelTypeConnectorProxy() override; | |
| 98 const std::string cache_guid() override; | |
| 99 bool ReceivedExperiment(Experiments* experiments) override; | |
| 100 bool HasUnsyncedItems() override; | |
| 101 SyncEncryptionHandler* GetEncryptionHandler() override; | |
| 102 std::vector<std::unique_ptr<ProtocolEvent>> GetBufferedProtocolEvents() | |
| 103 override; | |
| 104 void RegisterDirectoryTypeDebugInfoObserver( | |
| 105 TypeDebugInfoObserver* observer) override; | |
| 106 void UnregisterDirectoryTypeDebugInfoObserver( | |
| 107 TypeDebugInfoObserver* observer) override; | |
| 108 bool HasDirectoryTypeDebugInfoObserver( | |
| 109 TypeDebugInfoObserver* observer) override; | |
| 110 void RequestEmitDebugInfo() override; | |
| 111 void ClearServerData(const ClearServerDataCallback& callback) override; | |
| 112 void OnCookieJarChanged(bool account_mismatch, bool empty_jar) override; | |
| 113 | |
| 114 // SyncEncryptionHandler::Observer implementation. | |
| 115 void OnPassphraseRequired( | |
| 116 PassphraseRequiredReason reason, | |
| 117 const sync_pb::EncryptedData& pending_keys) override; | |
| 118 void OnPassphraseAccepted() override; | |
| 119 void OnBootstrapTokenUpdated(const std::string& bootstrap_token, | |
| 120 BootstrapTokenType type) override; | |
| 121 void OnEncryptedTypesChanged(ModelTypeSet encrypted_types, | |
| 122 bool encrypt_everything) override; | |
| 123 void OnEncryptionComplete() override; | |
| 124 void OnCryptographerStateChanged(Cryptographer* cryptographer) override; | |
| 125 void OnPassphraseTypeChanged(PassphraseType type, | |
| 126 base::Time explicit_passphrase_time) override; | |
| 127 void OnLocalSetPassphraseEncryption( | |
| 128 const SyncEncryptionHandler::NigoriState& nigori_state) override; | |
| 129 | |
| 130 // SyncEngineEventListener implementation. | |
| 131 void OnSyncCycleEvent(const SyncCycleEvent& event) override; | |
| 132 void OnActionableError(const SyncProtocolError& error) override; | |
| 133 void OnRetryTimeChanged(base::Time retry_time) override; | |
| 134 void OnThrottledTypesChanged(ModelTypeSet throttled_types) override; | |
| 135 void OnMigrationRequested(ModelTypeSet types) override; | |
| 136 void OnProtocolEvent(const ProtocolEvent& event) override; | |
| 137 | |
| 138 // ServerConnectionEventListener implementation. | |
| 139 void OnServerConnectionEvent(const ServerConnectionEvent& event) override; | |
| 140 | |
| 141 // JsBackend implementation. | |
| 142 void SetJsEventHandler( | |
| 143 const WeakHandle<JsEventHandler>& event_handler) override; | |
| 144 | |
| 145 // DirectoryChangeDelegate implementation. | |
| 146 // This listener is called upon completion of a syncable transaction, and | |
| 147 // builds the list of sync-engine initiated changes that will be forwarded to | |
| 148 // the SyncManager's Observers. | |
| 149 void HandleTransactionCompleteChangeEvent( | |
| 150 ModelTypeSet models_with_changes) override; | |
| 151 ModelTypeSet HandleTransactionEndingChangeEvent( | |
| 152 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, | |
| 153 syncable::BaseTransaction* trans) override; | |
| 154 void HandleCalculateChangesChangeEventFromSyncApi( | |
| 155 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, | |
| 156 syncable::BaseTransaction* trans, | |
| 157 std::vector<int64_t>* entries_changed) override; | |
| 158 void HandleCalculateChangesChangeEventFromSyncer( | |
| 159 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, | |
| 160 syncable::BaseTransaction* trans, | |
| 161 std::vector<int64_t>* entries_changed) override; | |
| 162 | |
| 163 // Handle explicit requests to fetch updates for the given types. | |
| 164 void RefreshTypes(ModelTypeSet types) override; | |
| 165 | |
| 166 // These OnYYYChanged() methods are only called by our NetworkChangeNotifier. | |
| 167 // Called when IP address of primary interface changes. | |
| 168 void OnIPAddressChanged() override; | |
| 169 // Called when the connection type of the system has changed. | |
| 170 void OnConnectionTypeChanged( | |
| 171 net::NetworkChangeNotifier::ConnectionType) override; | |
| 172 | |
| 173 // NudgeHandler implementation. | |
| 174 void NudgeForInitialDownload(ModelType type) override; | |
| 175 void NudgeForCommit(ModelType type) override; | |
| 176 void NudgeForRefresh(ModelType type) override; | |
| 177 | |
| 178 const SyncScheduler* scheduler() const; | |
| 179 | |
| 180 bool GetHasInvalidAuthTokenForTest() const; | |
| 181 | |
| 182 protected: | |
| 183 // Helper functions. Virtual for testing. | |
| 184 virtual void NotifyInitializationSuccess(); | |
| 185 virtual void NotifyInitializationFailure(); | |
| 186 | |
| 187 private: | |
| 188 friend class SyncManagerTest; | |
| 189 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, NudgeDelayTest); | |
| 190 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, PurgeDisabledTypes); | |
| 191 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, PurgeUnappliedTypes); | |
| 192 | |
| 193 struct NotificationInfo { | |
| 194 NotificationInfo(); | |
| 195 ~NotificationInfo(); | |
| 196 | |
| 197 int total_count; | |
| 198 std::string payload; | |
| 199 | |
| 200 // Returned pointer owned by the caller. | |
| 201 base::DictionaryValue* ToValue() const; | |
| 202 }; | |
| 203 | |
| 204 base::TimeDelta GetNudgeDelayTimeDelta(const ModelType& model_type); | |
| 205 | |
| 206 typedef std::map<ModelType, NotificationInfo> NotificationInfoMap; | |
| 207 | |
| 208 // Determine if the parents or predecessors differ between the old and new | |
| 209 // versions of an entry. Note that a node's index may change without its | |
| 210 // UNIQUE_POSITION changing if its sibling nodes were changed. To handle such | |
| 211 // cases, we rely on the caller to treat a position update on any sibling as | |
| 212 // updating the positions of all siblings. | |
| 213 bool VisiblePositionsDiffer( | |
| 214 const syncable::EntryKernelMutation& mutation) const; | |
| 215 | |
| 216 // Determine if any of the fields made visible to clients of the Sync API | |
| 217 // differ between the versions of an entry stored in |a| and |b|. A return | |
| 218 // value of false means that it should be OK to ignore this change. | |
| 219 bool VisiblePropertiesDiffer(const syncable::EntryKernelMutation& mutation, | |
| 220 Cryptographer* cryptographer) const; | |
| 221 | |
| 222 // Open the directory named with |username|. | |
| 223 bool OpenDirectory(const std::string& username); | |
| 224 | |
| 225 // Purge those disabled types as specified by |to_purge|. |to_journal| and | |
| 226 // |to_unapply| specify subsets that require special handling. |to_journal| | |
| 227 // types are saved into the delete journal, while |to_unapply| have only | |
| 228 // their local data deleted, while their server data is preserved. | |
| 229 bool PurgeDisabledTypes(ModelTypeSet to_purge, | |
| 230 ModelTypeSet to_journal, | |
| 231 ModelTypeSet to_unapply); | |
| 232 | |
| 233 void RequestNudgeForDataTypes(const tracked_objects::Location& nudge_location, | |
| 234 ModelTypeSet type); | |
| 235 | |
| 236 // If this is a deletion for a password, sets the legacy | |
| 237 // ExtraPasswordChangeRecordData field of |buffer|. Otherwise sets | |
| 238 // |buffer|'s specifics field to contain the unencrypted data. | |
| 239 void SetExtraChangeRecordData(int64_t id, | |
| 240 ModelType type, | |
| 241 ChangeReorderBuffer* buffer, | |
| 242 Cryptographer* cryptographer, | |
| 243 const syncable::EntryKernel& original, | |
| 244 bool existed_before, | |
| 245 bool exists_now); | |
| 246 | |
| 247 // Checks for server reachabilty and requests a nudge. | |
| 248 void OnNetworkConnectivityChangedImpl(); | |
| 249 | |
| 250 syncable::Directory* directory(); | |
| 251 | |
| 252 base::FilePath database_path_; | |
| 253 | |
| 254 const std::string name_; | |
| 255 | |
| 256 base::ThreadChecker thread_checker_; | |
| 257 | |
| 258 // Thread-safe handle used by | |
| 259 // HandleCalculateChangesChangeEventFromSyncApi(), which can be | |
| 260 // called from any thread. Valid only between between calls to | |
| 261 // Init() and Shutdown(). | |
| 262 // | |
| 263 // TODO(akalin): Ideally, we wouldn't need to store this; instead, | |
| 264 // we'd have another worker class which implements | |
| 265 // HandleCalculateChangesChangeEventFromSyncApi() and we'd pass it a | |
| 266 // WeakHandle when we construct it. | |
| 267 WeakHandle<SyncManagerImpl> weak_handle_this_; | |
| 268 | |
| 269 // We give a handle to share_ to clients of the API for use when constructing | |
| 270 // any transaction type. | |
| 271 UserShare share_; | |
| 272 | |
| 273 // This can be called from any thread, but only between calls to | |
| 274 // OpenDirectory() and ShutdownOnSyncThread(). | |
| 275 WeakHandle<SyncManager::ChangeObserver> change_observer_; | |
| 276 | |
| 277 base::ObserverList<SyncManager::Observer> observers_; | |
| 278 | |
| 279 // The ServerConnectionManager used to abstract communication between the | |
| 280 // client (the Syncer) and the sync server. | |
| 281 std::unique_ptr<SyncServerConnectionManager> connection_manager_; | |
| 282 | |
| 283 // Maintains state that affects the way we interact with different sync types. | |
| 284 // This state changes when entering or exiting a configuration cycle. | |
| 285 std::unique_ptr<ModelTypeRegistry> model_type_registry_; | |
| 286 | |
| 287 // A container of various bits of information used by the SyncScheduler to | |
| 288 // create SyncCycles. Must outlive the SyncScheduler. | |
| 289 std::unique_ptr<SyncCycleContext> cycle_context_; | |
| 290 | |
| 291 // The scheduler that runs the Syncer. Needs to be explicitly | |
| 292 // Start()ed. | |
| 293 std::unique_ptr<SyncScheduler> scheduler_; | |
| 294 | |
| 295 // A multi-purpose status watch object that aggregates stats from various | |
| 296 // sync components. | |
| 297 AllStatus allstatus_; | |
| 298 | |
| 299 // Each element of this map is a store of change records produced by | |
| 300 // HandleChangeEventFromSyncer during the CALCULATE_CHANGES step. The changes | |
| 301 // are grouped by model type, and are stored here in tree order to be | |
| 302 // forwarded to the observer slightly later, at the TRANSACTION_ENDING step | |
| 303 // by HandleTransactionEndingChangeEvent. The list is cleared after observer | |
| 304 // finishes processing. | |
| 305 typedef std::map<int, ImmutableChangeRecordList> ChangeRecordMap; | |
| 306 ChangeRecordMap change_records_; | |
| 307 | |
| 308 SyncManager::ChangeDelegate* change_delegate_; | |
| 309 | |
| 310 // Set to true once Init has been called. | |
| 311 bool initialized_; | |
| 312 | |
| 313 bool observing_network_connectivity_changes_; | |
| 314 | |
| 315 // Map used to store the notification info to be displayed in | |
| 316 // about:sync page. | |
| 317 NotificationInfoMap notification_info_map_; | |
| 318 | |
| 319 // These are for interacting with chrome://sync-internals. | |
| 320 JsSyncManagerObserver js_sync_manager_observer_; | |
| 321 JsMutationEventObserver js_mutation_event_observer_; | |
| 322 JsSyncEncryptionHandlerObserver js_sync_encryption_handler_observer_; | |
| 323 | |
| 324 // This is for keeping track of client events to send to the server. | |
| 325 DebugInfoEventListener debug_info_event_listener_; | |
| 326 | |
| 327 ProtocolEventBuffer protocol_event_buffer_; | |
| 328 | |
| 329 base::Closure report_unrecoverable_error_function_; | |
| 330 | |
| 331 // Sync's encryption handler. It tracks the set of encrypted types, manages | |
| 332 // changing passphrases, and in general handles sync-specific interactions | |
| 333 // with the cryptographer. | |
| 334 std::unique_ptr<SyncEncryptionHandlerImpl> sync_encryption_handler_; | |
| 335 | |
| 336 base::WeakPtrFactory<SyncManagerImpl> weak_ptr_factory_; | |
| 337 | |
| 338 DISALLOW_COPY_AND_ASSIGN(SyncManagerImpl); | |
| 339 }; | |
| 340 | |
| 341 } // namespace syncer | |
| 342 | |
| 343 #endif // COMPONENTS_SYNC_CORE_IMPL_SYNC_MANAGER_IMPL_H_ | |
| OLD | NEW |