1 // Copyright 2013 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 CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_IMPL_H_
6 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_IMPL_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/thread.h"
16 #include "chrome/browser/sync/glue/extensions_activity_monitor.h"
17 #include "chrome/browser/sync/glue/sync_backend_host.h"
18 #include "components/sync_driver/backend_data_type_configurer.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "sync/internal_api/public/base/model_type.h"
22 #include "sync/internal_api/public/configure_reason.h"
23 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
24 #include "sync/internal_api/public/sync_manager.h"
25 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h"
26 #include "sync/internal_api/public/util/unrecoverable_error_handler.h"
27 #include "sync/internal_api/public/util/weak_handle.h"
28 #include "sync/notifier/invalidation_handler.h"
29 #include "sync/protocol/encryption.pb.h"
30 #include "sync/protocol/sync_protocol_error.h"
31 #include "sync/util/extensions_activity.h"
40 namespace invalidation {
41 class InvalidationService;
45 class NetworkResources;
46 class SyncManagerFactory;
49 namespace browser_sync {
51 class ChangeProcessor;
52 class SyncBackendHostCore;
53 class SyncBackendRegistrar;
55 class SyncedDeviceTracker;
56 struct DoInitializeOptions;
58 // The only real implementation of the SyncBackendHost. See that interface's
59 // definition for documentation of public methods.
60 class SyncBackendHostImpl
61 : public SyncBackendHost,
62 public content::NotificationObserver,
63 public syncer::InvalidationHandler {
65 typedef syncer::SyncStatus Status;
67 // Create a SyncBackendHost with a reference to the |frontend| that
68 // it serves and communicates to via the SyncFrontend interface (on
69 // the same thread it used to call the constructor). Must outlive
72 const std::string& name,
74 const base::WeakPtr<SyncPrefs>& sync_prefs);
75 virtual ~SyncBackendHostImpl();
77 // SyncBackendHost implementation.
78 virtual void Initialize(
79 SyncFrontend* frontend,
80 scoped_ptr<base::Thread> sync_thread,
81 const syncer::WeakHandle<syncer::JsEventHandler>& event_handler,
82 const GURL& service_url,
83 const syncer::SyncCredentials& credentials,
84 bool delete_sync_data_folder,
85 scoped_ptr<syncer::SyncManagerFactory> sync_manager_factory,
86 scoped_ptr<syncer::UnrecoverableErrorHandler> unrecoverable_error_handler,
87 syncer::ReportUnrecoverableErrorFunction
88 report_unrecoverable_error_function,
89 syncer::NetworkResources* network_resources) OVERRIDE;
90 virtual void UpdateCredentials(
91 const syncer::SyncCredentials& credentials) OVERRIDE;
92 virtual void StartSyncingWithServer() OVERRIDE;
93 virtual void SetEncryptionPassphrase(
94 const std::string& passphrase,
95 bool is_explicit) OVERRIDE;
96 virtual bool SetDecryptionPassphrase(const std::string& passphrase)
97 OVERRIDE WARN_UNUSED_RESULT;
98 virtual void StopSyncingForShutdown() OVERRIDE;
99 virtual scoped_ptr<base::Thread> Shutdown(ShutdownOption option) OVERRIDE;
100 virtual void UnregisterInvalidationIds() OVERRIDE;
101 virtual void ConfigureDataTypes(
102 syncer::ConfigureReason reason,
103 const DataTypeConfigStateMap& config_state_map,
104 const base::Callback<void(syncer::ModelTypeSet,
105 syncer::ModelTypeSet)>& ready_task,
106 const base::Callback<void()>& retry_callback) OVERRIDE;
107 virtual void EnableEncryptEverything() OVERRIDE;
108 virtual void ActivateDataType(
109 syncer::ModelType type, syncer::ModelSafeGroup group,
110 ChangeProcessor* change_processor) OVERRIDE;
111 virtual void DeactivateDataType(syncer::ModelType type) OVERRIDE;
112 virtual syncer::UserShare* GetUserShare() const OVERRIDE;
113 virtual Status GetDetailedStatus() OVERRIDE;
114 virtual syncer::sessions::SyncSessionSnapshot
115 GetLastSessionSnapshot() const OVERRIDE;
116 virtual bool HasUnsyncedItems() const OVERRIDE;
117 virtual bool IsNigoriEnabled() const OVERRIDE;
118 virtual syncer::PassphraseType GetPassphraseType() const OVERRIDE;
119 virtual base::Time GetExplicitPassphraseTime() const OVERRIDE;
120 virtual bool IsCryptographerReady(
121 const syncer::BaseTransaction* trans) const OVERRIDE;
122 virtual void GetModelSafeRoutingInfo(
123 syncer::ModelSafeRoutingInfo* out) const OVERRIDE;
124 virtual SyncedDeviceTracker* GetSyncedDeviceTracker() const OVERRIDE;
125 virtual base::MessageLoop* GetSyncLoopForTesting() OVERRIDE;
128 // The types and functions below are protected so that test
129 // subclasses can use them.
131 // Allows tests to perform alternate core initialization work.
132 virtual void InitCore(scoped_ptr<DoInitializeOptions> options);
134 // Request the syncer to reconfigure with the specfied params.
135 // Virtual for testing.
136 virtual void RequestConfigureSyncer(
137 syncer::ConfigureReason reason,
138 syncer::ModelTypeSet to_download,
139 syncer::ModelTypeSet to_purge,
140 syncer::ModelTypeSet to_journal,
141 syncer::ModelTypeSet to_unapply,
142 syncer::ModelTypeSet to_ignore,
143 const syncer::ModelSafeRoutingInfo& routing_info,
144 const base::Callback<void(syncer::ModelTypeSet,
145 syncer::ModelTypeSet)>& ready_task,
146 const base::Closure& retry_callback);
148 // Called when the syncer has finished performing a configuration.
149 void FinishConfigureDataTypesOnFrontendLoop(
150 const syncer::ModelTypeSet enabled_types,
151 const syncer::ModelTypeSet succeeded_configuration_types,
152 const syncer::ModelTypeSet failed_configuration_types,
153 const base::Callback<void(syncer::ModelTypeSet,
154 syncer::ModelTypeSet)>& ready_task);
156 // Reports backend initialization success. Includes some objects from sync
157 // manager initialization to be passed back to the UI thread.
158 virtual void HandleInitializationSuccessOnFrontendLoop(
159 const syncer::WeakHandle<syncer::JsBackend> js_backend,
160 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>
161 debug_info_listener);
163 // Downloading of control types failed and will be retried. Invokes the
164 // frontend's sync configure retry method.
165 void HandleControlTypesDownloadRetry();
167 SyncFrontend* frontend() { return frontend_; }
170 friend class SyncBackendHostCore;
172 // Checks if we have received a notice to turn on experimental datatypes
173 // (via the nigori node) and informs the frontend if that is the case.
174 // Note: it is illegal to call this before the backend is initialized.
175 void AddExperimentalTypes();
177 // Handles backend initialization failure.
178 void HandleInitializationFailureOnFrontendLoop();
180 // Called from Core::OnSyncCycleCompleted to handle updating frontend
181 // thread components.
182 void HandleSyncCycleCompletedOnFrontendLoop(
183 const syncer::sessions::SyncSessionSnapshot& snapshot);
185 // Called when the syncer failed to perform a configuration and will
186 // eventually retry. FinishingConfigurationOnFrontendLoop(..) will be called
187 // on successful completion.
188 void RetryConfigurationOnFrontendLoop(const base::Closure& retry_callback);
190 // Helpers to persist a token that can be used to bootstrap sync encryption
191 // across browser restart to avoid requiring the user to re-enter their
192 // passphrase. |token| must be valid UTF-8 as we use the PrefService for
194 void PersistEncryptionBootstrapToken(
195 const std::string& token,
196 syncer::BootstrapTokenType token_type);
198 // For convenience, checks if initialization state is INITIALIZED.
199 bool initialized() const { return initialized_; }
201 // Let the front end handle the actionable error event.
202 void HandleActionableErrorEventOnFrontendLoop(
203 const syncer::SyncProtocolError& sync_error);
205 // Handle a migration request.
206 void HandleMigrationRequestedOnFrontendLoop(const syncer::ModelTypeSet types);
208 // Checks if |passphrase| can be used to decrypt the cryptographer's pending
209 // keys that were cached during NotifyPassphraseRequired. Returns true if
210 // decryption was successful. Returns false otherwise. Must be called with a
211 // non-empty pending keys cache.
212 bool CheckPassphraseAgainstCachedPendingKeys(
213 const std::string& passphrase) const;
215 // Invoked when a passphrase is required to decrypt a set of Nigori keys,
216 // or for encrypting. |reason| denotes why the passphrase was required.
217 // |pending_keys| is a copy of the cryptographer's pending keys, that are
218 // cached by the frontend. If there are no pending keys, or if the passphrase
219 // required reason is REASON_ENCRYPTION, an empty EncryptedData object is
221 void NotifyPassphraseRequired(syncer::PassphraseRequiredReason reason,
222 sync_pb::EncryptedData pending_keys);
224 // Invoked when the passphrase provided by the user has been accepted.
225 void NotifyPassphraseAccepted();
227 // Invoked when the set of encrypted types or the encrypt
228 // everything flag changes.
229 void NotifyEncryptedTypesChanged(
230 syncer::ModelTypeSet encrypted_types,
231 bool encrypt_everything);
233 // Invoked when sync finishes encrypting new datatypes.
234 void NotifyEncryptionComplete();
236 // Invoked when the passphrase state has changed. Caches the passphrase state
237 // for later use on the UI thread.
238 // If |type| is FROZEN_IMPLICIT_PASSPHRASE or CUSTOM_PASSPHRASE,
239 // |explicit_passphrase_time| is the time at which that passphrase was set
241 void HandlePassphraseTypeChangedOnFrontendLoop(
242 syncer::PassphraseType type,
243 base::Time explicit_passphrase_time);
245 void HandleStopSyncingPermanentlyOnFrontendLoop();
247 // Dispatched to from OnConnectionStatusChange to handle updating
248 // frontend UI components.
249 void HandleConnectionStatusChangeOnFrontendLoop(
250 syncer::ConnectionStatus status);
252 // NotificationObserver implementation.
253 virtual void Observe(
255 const content::NotificationSource& source,
256 const content::NotificationDetails& details) OVERRIDE;
258 // InvalidationHandler implementation.
259 virtual void OnInvalidatorStateChange(
260 syncer::InvalidatorState state) OVERRIDE;
261 virtual void OnIncomingInvalidation(
262 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
264 content::NotificationRegistrar notification_registrar_;
266 // A reference to the MessageLoop used to construct |this|, so we know how
267 // to safely talk back to the SyncFrontend.
268 base::MessageLoop* const frontend_loop_;
270 Profile* const profile_;
272 // Name used for debugging (set from profile_->GetDebugName()).
273 const std::string name_;
275 // Our core, which communicates directly to the syncapi. Use refptr instead
276 // of WeakHandle because |core_| is created on UI loop but released on
278 scoped_refptr<SyncBackendHostCore> core_;
282 const base::WeakPtr<SyncPrefs> sync_prefs_;
284 ExtensionsActivityMonitor extensions_activity_monitor_;
286 scoped_ptr<SyncBackendRegistrar> registrar_;
288 // The frontend which we serve (and are owned by).
289 SyncFrontend* frontend_;
291 // We cache the cryptographer's pending keys whenever NotifyPassphraseRequired
292 // is called. This way, before the UI calls SetDecryptionPassphrase on the
293 // syncer, it can avoid the overhead of an asynchronous decryption call and
294 // give the user immediate feedback about the passphrase entered by first
295 // trying to decrypt the cached pending keys on the UI thread. Note that
296 // SetDecryptionPassphrase can still fail after the cached pending keys are
297 // successfully decrypted if the pending keys have changed since the time they
299 sync_pb::EncryptedData cached_pending_keys_;
301 // The state of the passphrase required to decrypt the bag of encryption keys
302 // in the nigori node. Updated whenever a new nigori node arrives or the user
303 // manually changes their passphrase state. Cached so we can synchronously
304 // check it from the UI thread.
305 syncer::PassphraseType cached_passphrase_type_;
307 // If an explicit passphrase is in use, the time at which the passphrase was
308 // first set (if available).
309 base::Time cached_explicit_passphrase_time_;
311 // UI-thread cache of the last SyncSessionSnapshot received from syncapi.
312 syncer::sessions::SyncSessionSnapshot last_snapshot_;
314 invalidation::InvalidationService* invalidator_;
315 bool invalidation_handler_registered_;
317 base::WeakPtrFactory<SyncBackendHostImpl> weak_ptr_factory_;
319 DISALLOW_COPY_AND_ASSIGN(SyncBackendHostImpl);
322 } // namespace browser_sync
324 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_IMPL_H_