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