1// Copyright 2014 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_ROLLBACK_MANAGER_BASE_H_
6#define SYNC_INTERNAL_API_SYNC_ROLLBACK_MANAGER_BASE_H_
7
8#include <string>
9#include <vector>
10
11#include "sync/base/sync_export.h"
12#include "sync/internal_api/public/http_post_provider_factory.h"
13#include "sync/internal_api/public/internal_components_factory.h"
14#include "sync/internal_api/public/sync_manager.h"
15#include "sync/internal_api/public/user_share.h"
16#include "sync/syncable/directory_change_delegate.h"
17#include "sync/syncable/transaction_observer.h"
18
19namespace syncer {
20
21class WriteTransaction;
22
23// Base class of sync managers used for backup and rollback. Two major
24// functions are:
25//   * Init(): load backup DB into sync directory.
26//   * ConfigureSyncer(): initialize permanent sync nodes (root, bookmark
27//                        permanent folders) for configured type as needed.
28//
29// Most of other functions are no ops.
30class SYNC_EXPORT_PRIVATE SyncRollbackManagerBase :
31    public SyncManager,
32    public syncable::DirectoryChangeDelegate,
33    public syncable::TransactionObserver {
34 public:
35  SyncRollbackManagerBase();
36  virtual ~SyncRollbackManagerBase();
37
38  // SyncManager implementation.
39  virtual ModelTypeSet InitialSyncEndedTypes() OVERRIDE;
40  virtual ModelTypeSet GetTypesWithEmptyProgressMarkerToken(
41      ModelTypeSet types) OVERRIDE;
42  virtual bool PurgePartiallySyncedTypes() OVERRIDE;
43  virtual void UpdateCredentials(const SyncCredentials& credentials) OVERRIDE;
44  virtual void StartSyncingNormally(const ModelSafeRoutingInfo& routing_info)
45      OVERRIDE;
46  virtual void ConfigureSyncer(
47      ConfigureReason reason,
48      ModelTypeSet to_download,
49      ModelTypeSet to_purge,
50      ModelTypeSet to_journal,
51      ModelTypeSet to_unapply,
52      const ModelSafeRoutingInfo& new_routing_info,
53      const base::Closure& ready_task,
54      const base::Closure& retry_task) OVERRIDE;
55  virtual void SetInvalidatorEnabled(bool invalidator_enabled) OVERRIDE;
56  virtual void OnIncomingInvalidation(
57      syncer::ModelType type,
58      scoped_ptr<InvalidationInterface> invalidation) OVERRIDE;
59  virtual void AddObserver(SyncManager::Observer* observer) OVERRIDE;
60  virtual void RemoveObserver(SyncManager::Observer* observer) OVERRIDE;
61  virtual SyncStatus GetDetailedStatus() const OVERRIDE;
62  virtual void SaveChanges() OVERRIDE;
63  virtual void ShutdownOnSyncThread(ShutdownReason reason) OVERRIDE;
64  virtual UserShare* GetUserShare() OVERRIDE;
65  virtual const std::string cache_guid() OVERRIDE;
66  virtual bool ReceivedExperiment(Experiments* experiments) OVERRIDE;
67  virtual bool HasUnsyncedItems() OVERRIDE;
68  virtual SyncEncryptionHandler* GetEncryptionHandler() OVERRIDE;
69  virtual void RefreshTypes(ModelTypeSet types) OVERRIDE;
70  virtual SyncContextProxy* GetSyncContextProxy() OVERRIDE;
71  virtual ScopedVector<ProtocolEvent> GetBufferedProtocolEvents()
72      OVERRIDE;
73  virtual scoped_ptr<base::ListValue> GetAllNodesForType(
74      syncer::ModelType type) OVERRIDE;
75
76  // DirectoryChangeDelegate implementation.
77  virtual void HandleTransactionCompleteChangeEvent(
78      ModelTypeSet models_with_changes) OVERRIDE;
79  virtual ModelTypeSet HandleTransactionEndingChangeEvent(
80      const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
81      syncable::BaseTransaction* trans) OVERRIDE;
82  virtual void HandleCalculateChangesChangeEventFromSyncApi(
83      const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
84      syncable::BaseTransaction* trans,
85      std::vector<int64>* entries_changed) OVERRIDE;
86  virtual void HandleCalculateChangesChangeEventFromSyncer(
87      const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
88      syncable::BaseTransaction* trans,
89      std::vector<int64>* entries_changed) OVERRIDE;
90
91  // syncable::TransactionObserver implementation.
92  virtual void OnTransactionWrite(
93      const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
94      ModelTypeSet models_with_changes) OVERRIDE;
95
96 protected:
97  ObserverList<SyncManager::Observer>* GetObservers();
98
99  // Initialize sync backup DB.
100  bool InitInternal(
101      const base::FilePath& database_location,
102      InternalComponentsFactory* internal_components_factory,
103      InternalComponentsFactory::StorageOption storage,
104      scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler,
105      ReportUnrecoverableErrorFunction report_unrecoverable_error_function);
106
107  virtual void RegisterDirectoryTypeDebugInfoObserver(
108      syncer::TypeDebugInfoObserver* observer) OVERRIDE;
109  virtual void UnregisterDirectoryTypeDebugInfoObserver(
110      syncer::TypeDebugInfoObserver* observer) OVERRIDE;
111  virtual bool HasDirectoryTypeDebugInfoObserver(
112      syncer::TypeDebugInfoObserver* observer) OVERRIDE;
113  virtual void RequestEmitDebugInfo() OVERRIDE;
114
115  bool initialized() const {
116    return initialized_;
117  }
118
119 private:
120  void NotifyInitializationSuccess();
121  void NotifyInitializationFailure();
122
123  bool InitBackupDB(const base::FilePath& sync_folder,
124                    InternalComponentsFactory* internal_components_factory,
125                    InternalComponentsFactory::StorageOption storage);
126
127  bool InitTypeRootNode(ModelType type);
128  void InitBookmarkFolder(const std::string& folder);
129
130  UserShare share_;
131  ObserverList<SyncManager::Observer> observers_;
132
133  scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler_;
134  ReportUnrecoverableErrorFunction report_unrecoverable_error_function_;
135
136  scoped_ptr<SyncEncryptionHandler> dummy_handler_;
137
138  bool initialized_;
139
140  base::WeakPtrFactory<SyncRollbackManagerBase> weak_ptr_factory_;
141
142  DISALLOW_COPY_AND_ASSIGN(SyncRollbackManagerBase);
143};
144
145}  // namespace syncer
146
147#endif  // SYNC_INTERNAL_API_SYNC_ROLLBACK_MANAGER_BASE_H_
148