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