#ifndef SYNC_INTERNAL_API_SYNC_MANAGER_H_
#define SYNC_INTERNAL_API_SYNC_MANAGER_H_
#include <string>
#include <vector>
#include "net/base/network_change_notifier.h"
#include "sync/engine/all_status.h"
#include "sync/engine/net/server_connection_manager.h"
#include "sync/engine/sync_engine_event.h"
#include "sync/engine/throttled_data_type_tracker.h"
#include "sync/engine/traffic_recorder.h"
#include "sync/internal_api/change_reorder_buffer.h"
#include "sync/internal_api/debug_info_event_listener.h"
#include "sync/internal_api/js_mutation_event_observer.h"
#include "sync/internal_api/js_sync_encryption_handler_observer.h"
#include "sync/internal_api/js_sync_manager_observer.h"
#include "sync/internal_api/public/sync_manager.h"
#include "sync/internal_api/sync_encryption_handler_impl.h"
#include "sync/js/js_backend.h"
#include "sync/notifier/invalidation_handler.h"
#include "sync/notifier/invalidator_state.h"
#include "sync/syncable/directory_change_delegate.h"
#include "sync/util/cryptographer.h"
#include "sync/util/time.h"
namespace syncer {
class SyncAPIServerConnectionManager;
class WriteNode;
class WriteTransaction;
namespace sessions {
class SyncSessionContext;
}
class SyncManagerImpl : public SyncManager,
public net::NetworkChangeNotifier::IPAddressObserver,
public InvalidationHandler,
public JsBackend,
public SyncEngineEventListener,
public ServerConnectionEventListener,
public syncable::DirectoryChangeDelegate,
public SyncEncryptionHandler::Observer {
public:
explicit SyncManagerImpl(const std::string& name);
virtual ~SyncManagerImpl();
virtual void Init(
const FilePath& database_location,
const WeakHandle<JsEventHandler>& event_handler,
const std::string& sync_server_and_path,
int sync_server_port,
bool use_ssl,
const scoped_refptr<base::TaskRunner>& blocking_task_runner,
scoped_ptr<HttpPostProviderFactory> post_factory,
const std::vector<ModelSafeWorker*>& workers,
ExtensionsActivityMonitor* extensions_activity_monitor,
SyncManager::ChangeDelegate* change_delegate,
const SyncCredentials& credentials,
scoped_ptr<Invalidator> invalidator,
const std::string& restored_key_for_bootstrapping,
const std::string& restored_keystore_key_for_bootstrapping,
scoped_ptr<InternalComponentsFactory> internal_components_factory,
Encryptor* encryptor,
UnrecoverableErrorHandler* unrecoverable_error_handler,
ReportUnrecoverableErrorFunction
report_unrecoverable_error_function) OVERRIDE;
virtual void ThrowUnrecoverableError() OVERRIDE;
virtual ModelTypeSet InitialSyncEndedTypes() OVERRIDE;
virtual ModelTypeSet GetTypesWithEmptyProgressMarkerToken(
ModelTypeSet types) OVERRIDE;
virtual bool PurgePartiallySyncedTypes() OVERRIDE;
virtual void UpdateCredentials(const SyncCredentials& credentials) OVERRIDE;
virtual void UpdateEnabledTypes(ModelTypeSet enabled_types) OVERRIDE;
virtual void RegisterInvalidationHandler(
InvalidationHandler* handler) OVERRIDE;
virtual void UpdateRegisteredInvalidationIds(
InvalidationHandler* handler,
const ObjectIdSet& ids) OVERRIDE;
virtual void UnregisterInvalidationHandler(
InvalidationHandler* handler) OVERRIDE;
virtual void StartSyncingNormally(
const ModelSafeRoutingInfo& routing_info) OVERRIDE;
virtual void ConfigureSyncer(
ConfigureReason reason,
ModelTypeSet types_to_config,
const ModelSafeRoutingInfo& new_routing_info,
const base::Closure& ready_task,
const base::Closure& retry_task) OVERRIDE;
virtual void AddObserver(SyncManager::Observer* observer) OVERRIDE;
virtual void RemoveObserver(SyncManager::Observer* observer) OVERRIDE;
virtual SyncStatus GetDetailedStatus() const OVERRIDE;
virtual void SaveChanges() OVERRIDE;
virtual void StopSyncingForShutdown(const base::Closure& callback) OVERRIDE;
virtual void ShutdownOnSyncThread() OVERRIDE;
virtual UserShare* GetUserShare() OVERRIDE;
virtual bool ReceivedExperiment(Experiments* experiments) OVERRIDE;
virtual bool HasUnsyncedItems() OVERRIDE;
virtual SyncEncryptionHandler* GetEncryptionHandler() OVERRIDE;
virtual void OnPassphraseRequired(
PassphraseRequiredReason reason,
const sync_pb::EncryptedData& pending_keys) OVERRIDE;
virtual void OnPassphraseAccepted() OVERRIDE;
virtual void OnBootstrapTokenUpdated(
const std::string& bootstrap_token,
BootstrapTokenType type) OVERRIDE;
virtual void OnEncryptedTypesChanged(
ModelTypeSet encrypted_types,
bool encrypt_everything) OVERRIDE;
virtual void OnEncryptionComplete() OVERRIDE;
virtual void OnCryptographerStateChanged(
Cryptographer* cryptographer) OVERRIDE;
virtual void OnPassphraseTypeChanged(PassphraseType type) OVERRIDE;
const std::string& username_for_share() const;
static int GetDefaultNudgeDelay();
static int GetPreferencesNudgeDelay();
virtual void OnSyncEngineEvent(const SyncEngineEvent& event) OVERRIDE;
virtual void OnServerConnectionEvent(
const ServerConnectionEvent& event) OVERRIDE;
virtual void SetJsEventHandler(
const WeakHandle<JsEventHandler>& event_handler) OVERRIDE;
virtual void ProcessJsMessage(
const std::string& name, const JsArgList& args,
const WeakHandle<JsReplyHandler>& reply_handler) OVERRIDE;
virtual void HandleTransactionCompleteChangeEvent(
ModelTypeSet models_with_changes) OVERRIDE;
virtual ModelTypeSet HandleTransactionEndingChangeEvent(
const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
syncable::BaseTransaction* trans) OVERRIDE;
virtual void HandleCalculateChangesChangeEventFromSyncApi(
const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
syncable::BaseTransaction* trans) OVERRIDE;
virtual void HandleCalculateChangesChangeEventFromSyncer(
const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
syncable::BaseTransaction* trans) OVERRIDE;
virtual void OnInvalidatorStateChange(InvalidatorState state) OVERRIDE;
virtual void OnIncomingInvalidation(
const ObjectIdStateMap& id_state_map,
IncomingInvalidationSource source) OVERRIDE;
virtual void OnIPAddressChanged() OVERRIDE;
const SyncScheduler* scheduler() const;
private:
friend class SyncManagerTest;
FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, NudgeDelayTest);
FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, OnNotificationStateChange);
FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, OnIncomingNotification);
FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, PurgeDisabledTypes);
struct NotificationInfo {
NotificationInfo();
~NotificationInfo();
int total_count;
std::string payload;
DictionaryValue* ToValue() const;
};
base::TimeDelta GetNudgeDelayTimeDelta(const ModelType& model_type);
typedef std::map<ModelType, NotificationInfo> NotificationInfoMap;
typedef JsArgList (SyncManagerImpl::*UnboundJsMessageHandler)(
const JsArgList&);
typedef base::Callback<JsArgList(const JsArgList&)> JsMessageHandler;
typedef std::map<std::string, JsMessageHandler> JsMessageHandlerMap;
bool VisiblePositionsDiffer(
const syncable::EntryKernelMutation& mutation) const;
bool VisiblePropertiesDiffer(
const syncable::EntryKernelMutation& mutation,
Cryptographer* cryptographer) const;
bool ChangeBuffersAreEmpty();
bool OpenDirectory();
bool PurgeDisabledTypes(ModelTypeSet previously_enabled_types,
ModelTypeSet currently_enabled_types);
void RequestNudgeForDataTypes(
const tracked_objects::Location& nudge_location,
ModelTypeSet type);
void SetExtraChangeRecordData(int64 id,
ModelType type,
ChangeReorderBuffer* buffer,
Cryptographer* cryptographer,
const syncable::EntryKernel& original,
bool existed_before,
bool exists_now);
void UpdateSessionNameCallback(const std::string& chrome_version,
const std::string& session_name);
void UpdateNotificationInfo(
const ModelTypeStateMap& type_state_map);
void OnIPAddressChangedImpl();
void BindJsMessageHandler(
const std::string& name, UnboundJsMessageHandler unbound_message_handler);
static DictionaryValue* NotificationInfoToValue(
const NotificationInfoMap& notification_info);
static std::string NotificationInfoToString(
const NotificationInfoMap& notification_info);
JsArgList GetNotificationState(const JsArgList& args);
JsArgList GetNotificationInfo(const JsArgList& args);
JsArgList GetRootNodeDetails(const JsArgList& args);
JsArgList GetAllNodes(const JsArgList& args);
JsArgList GetNodeSummariesById(const JsArgList& args);
JsArgList GetNodeDetailsById(const JsArgList& args);
JsArgList GetChildNodeIds(const JsArgList& args);
JsArgList GetClientServerTraffic(const JsArgList& args);
syncable::Directory* directory();
FilePath database_path_;
const std::string name_;
base::ThreadChecker thread_checker_;
base::WeakPtrFactory<SyncManagerImpl> weak_ptr_factory_;
WeakHandle<SyncManagerImpl> weak_handle_this_;
scoped_refptr<base::TaskRunner> blocking_task_runner_;
UserShare share_;
WeakHandle<SyncManager::ChangeObserver> change_observer_;
ObserverList<SyncManager::Observer> observers_;
scoped_ptr<SyncAPIServerConnectionManager> connection_manager_;
scoped_ptr<sessions::SyncSessionContext> session_context_;
scoped_ptr<SyncScheduler> scheduler_;
scoped_ptr<Invalidator> invalidator_;
AllStatus allstatus_;
ChangeReorderBuffer change_buffers_[MODEL_TYPE_COUNT];
SyncManager::ChangeDelegate* change_delegate_;
bool initialized_;
bool observing_ip_address_changes_;
InvalidatorState invalidator_state_;
NotificationInfoMap notification_info_map_;
JsMessageHandlerMap js_message_handlers_;
WeakHandle<JsEventHandler> js_event_handler_;
JsSyncManagerObserver js_sync_manager_observer_;
JsMutationEventObserver js_mutation_event_observer_;
JsSyncEncryptionHandlerObserver js_sync_encryption_handler_observer_;
ThrottledDataTypeTracker throttled_data_type_tracker_;
DebugInfoEventListener debug_info_event_listener_;
TrafficRecorder traffic_recorder_;
Encryptor* encryptor_;
UnrecoverableErrorHandler* unrecoverable_error_handler_;
ReportUnrecoverableErrorFunction report_unrecoverable_error_function_;
scoped_ptr<SyncEncryptionHandlerImpl> sync_encryption_handler_;
DISALLOW_COPY_AND_ASSIGN(SyncManagerImpl);
};
}
#endif