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