| OLD | NEW |
| (Empty) |
| 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. | |
| 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/sync_backend_host.h" | |
| 17 #include "components/invalidation/public/invalidation_handler.h" | |
| 18 #include "components/sync_driver/backend_data_type_configurer.h" | |
| 19 #include "sync/internal_api/public/base/model_type.h" | |
| 20 #include "sync/internal_api/public/configure_reason.h" | |
| 21 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" | |
| 22 #include "sync/internal_api/public/sessions/type_debug_info_observer.h" | |
| 23 #include "sync/internal_api/public/sync_manager.h" | |
| 24 #include "sync/internal_api/public/util/weak_handle.h" | |
| 25 #include "sync/protocol/encryption.pb.h" | |
| 26 #include "sync/protocol/sync_protocol_error.h" | |
| 27 #include "sync/util/extensions_activity.h" | |
| 28 | |
| 29 class GURL; | |
| 30 | |
| 31 namespace base { | |
| 32 class MessageLoop; | |
| 33 } | |
| 34 | |
| 35 namespace invalidation { | |
| 36 class InvalidationService; | |
| 37 } | |
| 38 | |
| 39 namespace syncer { | |
| 40 class SyncManagerFactory; | |
| 41 class UnrecoverableErrorHandler; | |
| 42 } | |
| 43 | |
| 44 namespace sync_driver { | |
| 45 class SyncClient; | |
| 46 class SyncPrefs; | |
| 47 } | |
| 48 | |
| 49 namespace browser_sync { | |
| 50 | |
| 51 class ChangeProcessor; | |
| 52 class SyncBackendHostCore; | |
| 53 class SyncBackendRegistrar; | |
| 54 struct DoInitializeOptions; | |
| 55 | |
| 56 // The only real implementation of the SyncBackendHost. See that interface's | |
| 57 // definition for documentation of public methods. | |
| 58 class SyncBackendHostImpl | |
| 59 : public SyncBackendHost, | |
| 60 public syncer::InvalidationHandler { | |
| 61 public: | |
| 62 typedef syncer::SyncStatus Status; | |
| 63 | |
| 64 // Create a SyncBackendHost with a reference to the |frontend| that | |
| 65 // it serves and communicates to via the SyncFrontend interface (on | |
| 66 // the same thread it used to call the constructor). Must outlive | |
| 67 // |sync_prefs|. | |
| 68 SyncBackendHostImpl( | |
| 69 const std::string& name, | |
| 70 sync_driver::SyncClient* sync_client, | |
| 71 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, | |
| 72 invalidation::InvalidationService* invalidator, | |
| 73 const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs, | |
| 74 const base::FilePath& sync_folder); | |
| 75 ~SyncBackendHostImpl() override; | |
| 76 | |
| 77 // SyncBackendHost implementation. | |
| 78 void Initialize( | |
| 79 sync_driver::SyncFrontend* frontend, | |
| 80 scoped_ptr<base::Thread> sync_thread, | |
| 81 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread, | |
| 82 const scoped_refptr<base::SingleThreadTaskRunner>& file_thread, | |
| 83 const syncer::WeakHandle<syncer::JsEventHandler>& event_handler, | |
| 84 const GURL& service_url, | |
| 85 const std::string& sync_user_agent, | |
| 86 const syncer::SyncCredentials& credentials, | |
| 87 bool delete_sync_data_folder, | |
| 88 scoped_ptr<syncer::SyncManagerFactory> sync_manager_factory, | |
| 89 const syncer::WeakHandle<syncer::UnrecoverableErrorHandler>& | |
| 90 unrecoverable_error_handler, | |
| 91 const base::Closure& report_unrecoverable_error_function, | |
| 92 const HttpPostProviderFactoryGetter& http_post_provider_factory_getter, | |
| 93 scoped_ptr<syncer::SyncEncryptionHandler::NigoriState> saved_nigori_state) | |
| 94 override; | |
| 95 void TriggerRefresh(const syncer::ModelTypeSet& types) override; | |
| 96 void UpdateCredentials(const syncer::SyncCredentials& credentials) override; | |
| 97 void StartSyncingWithServer() override; | |
| 98 void SetEncryptionPassphrase(const std::string& passphrase, | |
| 99 bool is_explicit) override; | |
| 100 bool SetDecryptionPassphrase(const std::string& passphrase) override | |
| 101 WARN_UNUSED_RESULT; | |
| 102 void StopSyncingForShutdown() override; | |
| 103 scoped_ptr<base::Thread> Shutdown(syncer::ShutdownReason reason) override; | |
| 104 void UnregisterInvalidationIds() override; | |
| 105 syncer::ModelTypeSet ConfigureDataTypes( | |
| 106 syncer::ConfigureReason reason, | |
| 107 const DataTypeConfigStateMap& config_state_map, | |
| 108 const base::Callback<void(syncer::ModelTypeSet, syncer::ModelTypeSet)>& | |
| 109 ready_task, | |
| 110 const base::Callback<void()>& retry_callback) override; | |
| 111 void ActivateDirectoryDataType( | |
| 112 syncer::ModelType type, | |
| 113 syncer::ModelSafeGroup group, | |
| 114 sync_driver::ChangeProcessor* change_processor) override; | |
| 115 void DeactivateDirectoryDataType(syncer::ModelType type) override; | |
| 116 void ActivateNonBlockingDataType( | |
| 117 syncer::ModelType type, | |
| 118 scoped_ptr<syncer_v2::ActivationContext>) override; | |
| 119 void DeactivateNonBlockingDataType(syncer::ModelType type) override; | |
| 120 void EnableEncryptEverything() override; | |
| 121 syncer::UserShare* GetUserShare() const override; | |
| 122 Status GetDetailedStatus() override; | |
| 123 syncer::sessions::SyncSessionSnapshot GetLastSessionSnapshot() const override; | |
| 124 bool HasUnsyncedItems() const override; | |
| 125 bool IsNigoriEnabled() const override; | |
| 126 syncer::PassphraseType GetPassphraseType() const override; | |
| 127 base::Time GetExplicitPassphraseTime() const override; | |
| 128 bool IsCryptographerReady( | |
| 129 const syncer::BaseTransaction* trans) const override; | |
| 130 void GetModelSafeRoutingInfo( | |
| 131 syncer::ModelSafeRoutingInfo* out) const override; | |
| 132 void FlushDirectory() const override; | |
| 133 void RequestBufferedProtocolEventsAndEnableForwarding() override; | |
| 134 void DisableProtocolEventForwarding() override; | |
| 135 void EnableDirectoryTypeDebugInfoForwarding() override; | |
| 136 void DisableDirectoryTypeDebugInfoForwarding() override; | |
| 137 void GetAllNodesForTypes( | |
| 138 syncer::ModelTypeSet types, | |
| 139 base::Callback<void(const std::vector<syncer::ModelType>&, | |
| 140 ScopedVector<base::ListValue>)> type) override; | |
| 141 base::MessageLoop* GetSyncLoopForTesting() override; | |
| 142 void RefreshTypesForTest(syncer::ModelTypeSet types) override; | |
| 143 void ClearServerData( | |
| 144 const syncer::SyncManager::ClearServerDataCallback& callback) override; | |
| 145 | |
| 146 // InvalidationHandler implementation. | |
| 147 void OnInvalidatorStateChange(syncer::InvalidatorState state) override; | |
| 148 void OnIncomingInvalidation( | |
| 149 const syncer::ObjectIdInvalidationMap& invalidation_map) override; | |
| 150 std::string GetOwnerName() const override; | |
| 151 | |
| 152 protected: | |
| 153 // The types and functions below are protected so that test | |
| 154 // subclasses can use them. | |
| 155 | |
| 156 // Allows tests to perform alternate core initialization work. | |
| 157 virtual void InitCore(scoped_ptr<DoInitializeOptions> options); | |
| 158 | |
| 159 // Request the syncer to reconfigure with the specfied params. | |
| 160 // Virtual for testing. | |
| 161 virtual void RequestConfigureSyncer( | |
| 162 syncer::ConfigureReason reason, | |
| 163 syncer::ModelTypeSet to_download, | |
| 164 syncer::ModelTypeSet to_purge, | |
| 165 syncer::ModelTypeSet to_journal, | |
| 166 syncer::ModelTypeSet to_unapply, | |
| 167 syncer::ModelTypeSet to_ignore, | |
| 168 const syncer::ModelSafeRoutingInfo& routing_info, | |
| 169 const base::Callback<void(syncer::ModelTypeSet, | |
| 170 syncer::ModelTypeSet)>& ready_task, | |
| 171 const base::Closure& retry_callback); | |
| 172 | |
| 173 // Called when the syncer has finished performing a configuration. | |
| 174 void FinishConfigureDataTypesOnFrontendLoop( | |
| 175 const syncer::ModelTypeSet enabled_types, | |
| 176 const syncer::ModelTypeSet succeeded_configuration_types, | |
| 177 const syncer::ModelTypeSet failed_configuration_types, | |
| 178 const base::Callback<void(syncer::ModelTypeSet, | |
| 179 syncer::ModelTypeSet)>& ready_task); | |
| 180 | |
| 181 // Reports backend initialization success. Includes some objects from sync | |
| 182 // manager initialization to be passed back to the UI thread. | |
| 183 // | |
| 184 // |sync_context_proxy| points to an object owned by the SyncManager. | |
| 185 // Ownership is not transferred, but we can obtain our own copy of the object | |
| 186 // using its Clone() method. | |
| 187 virtual void HandleInitializationSuccessOnFrontendLoop( | |
| 188 const syncer::WeakHandle<syncer::JsBackend> js_backend, | |
| 189 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener> | |
| 190 debug_info_listener, | |
| 191 syncer_v2::SyncContextProxy* sync_context_proxy, | |
| 192 const std::string& cache_guid); | |
| 193 | |
| 194 // Forwards a ProtocolEvent to the frontend. Will not be called unless a | |
| 195 // call to SetForwardProtocolEvents() explicitly requested that we start | |
| 196 // forwarding these events. | |
| 197 void HandleProtocolEventOnFrontendLoop(syncer::ProtocolEvent* event); | |
| 198 | |
| 199 // Forwards a directory commit counter update to the frontend loop. Will not | |
| 200 // be called unless a call to EnableDirectoryTypeDebugInfoForwarding() | |
| 201 // explicitly requested that we start forwarding these events. | |
| 202 void HandleDirectoryCommitCountersUpdatedOnFrontendLoop( | |
| 203 syncer::ModelType type, | |
| 204 const syncer::CommitCounters& counters); | |
| 205 | |
| 206 // Forwards a directory update counter update to the frontend loop. Will not | |
| 207 // be called unless a call to EnableDirectoryTypeDebugInfoForwarding() | |
| 208 // explicitly requested that we start forwarding these events. | |
| 209 void HandleDirectoryUpdateCountersUpdatedOnFrontendLoop( | |
| 210 syncer::ModelType type, | |
| 211 const syncer::UpdateCounters& counters); | |
| 212 | |
| 213 // Forwards a directory status counter update to the frontend loop. Will not | |
| 214 // be called unless a call to EnableDirectoryTypeDebugInfoForwarding() | |
| 215 // explicitly requested that we start forwarding these events. | |
| 216 void HandleDirectoryStatusCountersUpdatedOnFrontendLoop( | |
| 217 syncer::ModelType type, | |
| 218 const syncer::StatusCounters& counters); | |
| 219 | |
| 220 // Overwrites the kSyncInvalidationVersions preference with the most recent | |
| 221 // set of invalidation versions for each type. | |
| 222 void UpdateInvalidationVersions( | |
| 223 const std::map<syncer::ModelType, int64>& invalidation_versions); | |
| 224 | |
| 225 sync_driver::SyncFrontend* frontend() { | |
| 226 return frontend_; | |
| 227 } | |
| 228 | |
| 229 private: | |
| 230 friend class SyncBackendHostCore; | |
| 231 | |
| 232 // Checks if we have received a notice to turn on experimental datatypes | |
| 233 // (via the nigori node) and informs the frontend if that is the case. | |
| 234 // Note: it is illegal to call this before the backend is initialized. | |
| 235 void AddExperimentalTypes(); | |
| 236 | |
| 237 // Handles backend initialization failure. | |
| 238 void HandleInitializationFailureOnFrontendLoop(); | |
| 239 | |
| 240 // Called from Core::OnSyncCycleCompleted to handle updating frontend | |
| 241 // thread components. | |
| 242 void HandleSyncCycleCompletedOnFrontendLoop( | |
| 243 const syncer::sessions::SyncSessionSnapshot& snapshot); | |
| 244 | |
| 245 // Called when the syncer failed to perform a configuration and will | |
| 246 // eventually retry. FinishingConfigurationOnFrontendLoop(..) will be called | |
| 247 // on successful completion. | |
| 248 void RetryConfigurationOnFrontendLoop(const base::Closure& retry_callback); | |
| 249 | |
| 250 // Helpers to persist a token that can be used to bootstrap sync encryption | |
| 251 // across browser restart to avoid requiring the user to re-enter their | |
| 252 // passphrase. |token| must be valid UTF-8 as we use the PrefService for | |
| 253 // storage. | |
| 254 void PersistEncryptionBootstrapToken( | |
| 255 const std::string& token, | |
| 256 syncer::BootstrapTokenType token_type); | |
| 257 | |
| 258 // For convenience, checks if initialization state is INITIALIZED. | |
| 259 bool initialized() const { return initialized_; } | |
| 260 | |
| 261 // Let the front end handle the actionable error event. | |
| 262 void HandleActionableErrorEventOnFrontendLoop( | |
| 263 const syncer::SyncProtocolError& sync_error); | |
| 264 | |
| 265 // Handle a migration request. | |
| 266 void HandleMigrationRequestedOnFrontendLoop(const syncer::ModelTypeSet types); | |
| 267 | |
| 268 // Checks if |passphrase| can be used to decrypt the cryptographer's pending | |
| 269 // keys that were cached during NotifyPassphraseRequired. Returns true if | |
| 270 // decryption was successful. Returns false otherwise. Must be called with a | |
| 271 // non-empty pending keys cache. | |
| 272 bool CheckPassphraseAgainstCachedPendingKeys( | |
| 273 const std::string& passphrase) const; | |
| 274 | |
| 275 // Invoked when a passphrase is required to decrypt a set of Nigori keys, | |
| 276 // or for encrypting. |reason| denotes why the passphrase was required. | |
| 277 // |pending_keys| is a copy of the cryptographer's pending keys, that are | |
| 278 // cached by the frontend. If there are no pending keys, or if the passphrase | |
| 279 // required reason is REASON_ENCRYPTION, an empty EncryptedData object is | |
| 280 // passed. | |
| 281 void NotifyPassphraseRequired(syncer::PassphraseRequiredReason reason, | |
| 282 sync_pb::EncryptedData pending_keys); | |
| 283 | |
| 284 // Invoked when the passphrase provided by the user has been accepted. | |
| 285 void NotifyPassphraseAccepted(); | |
| 286 | |
| 287 // Invoked when the set of encrypted types or the encrypt | |
| 288 // everything flag changes. | |
| 289 void NotifyEncryptedTypesChanged( | |
| 290 syncer::ModelTypeSet encrypted_types, | |
| 291 bool encrypt_everything); | |
| 292 | |
| 293 // Invoked when sync finishes encrypting new datatypes. | |
| 294 void NotifyEncryptionComplete(); | |
| 295 | |
| 296 // Invoked when the passphrase state has changed. Caches the passphrase state | |
| 297 // for later use on the UI thread. | |
| 298 // If |type| is FROZEN_IMPLICIT_PASSPHRASE or CUSTOM_PASSPHRASE, | |
| 299 // |explicit_passphrase_time| is the time at which that passphrase was set | |
| 300 // (if available). | |
| 301 void HandlePassphraseTypeChangedOnFrontendLoop( | |
| 302 syncer::PassphraseType type, | |
| 303 base::Time explicit_passphrase_time); | |
| 304 | |
| 305 void HandleLocalSetPassphraseEncryptionOnFrontendLoop( | |
| 306 const syncer::SyncEncryptionHandler::NigoriState& nigori_state); | |
| 307 | |
| 308 // Dispatched to from OnConnectionStatusChange to handle updating | |
| 309 // frontend UI components. | |
| 310 void HandleConnectionStatusChangeOnFrontendLoop( | |
| 311 syncer::ConnectionStatus status); | |
| 312 | |
| 313 void ClearServerDataDoneOnFrontendLoop( | |
| 314 const syncer::SyncManager::ClearServerDataCallback& frontend_callback); | |
| 315 | |
| 316 // A reference to the MessageLoop used to construct |this|, so we know how | |
| 317 // to safely talk back to the SyncFrontend. | |
| 318 base::MessageLoop* const frontend_loop_; | |
| 319 | |
| 320 sync_driver::SyncClient* const sync_client_; | |
| 321 | |
| 322 // The UI thread's task runner. | |
| 323 const scoped_refptr<base::SingleThreadTaskRunner> ui_thread_; | |
| 324 | |
| 325 // Name used for debugging (set from profile_->GetDebugName()). | |
| 326 const std::string name_; | |
| 327 | |
| 328 // Our core, which communicates directly to the syncapi. Use refptr instead | |
| 329 // of WeakHandle because |core_| is created on UI loop but released on | |
| 330 // sync loop. | |
| 331 scoped_refptr<SyncBackendHostCore> core_; | |
| 332 | |
| 333 // A handle referencing the main interface for non-blocking sync types. | |
| 334 scoped_ptr<syncer_v2::SyncContextProxy> sync_context_proxy_; | |
| 335 | |
| 336 bool initialized_; | |
| 337 | |
| 338 const base::WeakPtr<sync_driver::SyncPrefs> sync_prefs_; | |
| 339 | |
| 340 scoped_ptr<SyncBackendRegistrar> registrar_; | |
| 341 | |
| 342 // The frontend which we serve (and are owned by). | |
| 343 sync_driver::SyncFrontend* frontend_; | |
| 344 | |
| 345 // We cache the cryptographer's pending keys whenever NotifyPassphraseRequired | |
| 346 // is called. This way, before the UI calls SetDecryptionPassphrase on the | |
| 347 // syncer, it can avoid the overhead of an asynchronous decryption call and | |
| 348 // give the user immediate feedback about the passphrase entered by first | |
| 349 // trying to decrypt the cached pending keys on the UI thread. Note that | |
| 350 // SetDecryptionPassphrase can still fail after the cached pending keys are | |
| 351 // successfully decrypted if the pending keys have changed since the time they | |
| 352 // were cached. | |
| 353 sync_pb::EncryptedData cached_pending_keys_; | |
| 354 | |
| 355 // The state of the passphrase required to decrypt the bag of encryption keys | |
| 356 // in the nigori node. Updated whenever a new nigori node arrives or the user | |
| 357 // manually changes their passphrase state. Cached so we can synchronously | |
| 358 // check it from the UI thread. | |
| 359 syncer::PassphraseType cached_passphrase_type_; | |
| 360 | |
| 361 // If an explicit passphrase is in use, the time at which the passphrase was | |
| 362 // first set (if available). | |
| 363 base::Time cached_explicit_passphrase_time_; | |
| 364 | |
| 365 // UI-thread cache of the last SyncSessionSnapshot received from syncapi. | |
| 366 syncer::sessions::SyncSessionSnapshot last_snapshot_; | |
| 367 | |
| 368 invalidation::InvalidationService* invalidator_; | |
| 369 bool invalidation_handler_registered_; | |
| 370 | |
| 371 base::WeakPtrFactory<SyncBackendHostImpl> weak_ptr_factory_; | |
| 372 | |
| 373 DISALLOW_COPY_AND_ASSIGN(SyncBackendHostImpl); | |
| 374 }; | |
| 375 | |
| 376 } // namespace browser_sync | |
| 377 | |
| 378 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_IMPL_H_ | |
| OLD | NEW |