| 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_CORE_H_ | |
| 6 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_CORE_H_ | |
| 7 | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 | |
| 10 #include "base/timer/timer.h" | |
| 11 #include "chrome/browser/sync/glue/sync_backend_host_impl.h" | |
| 12 #include "components/invalidation/public/invalidation.h" | |
| 13 #include "components/sync_driver/system_encryptor.h" | |
| 14 #include "sync/internal_api/public/base/cancelation_signal.h" | |
| 15 #include "sync/internal_api/public/sessions/type_debug_info_observer.h" | |
| 16 #include "sync/internal_api/public/shutdown_reason.h" | |
| 17 #include "sync/internal_api/public/sync_encryption_handler.h" | |
| 18 #include "url/gurl.h" | |
| 19 | |
| 20 namespace browser_sync { | |
| 21 | |
| 22 class SyncBackendHostImpl; | |
| 23 | |
| 24 // Utility struct for holding initialization options. | |
| 25 struct DoInitializeOptions { | |
| 26 DoInitializeOptions( | |
| 27 base::MessageLoop* sync_loop, | |
| 28 SyncBackendRegistrar* registrar, | |
| 29 const syncer::ModelSafeRoutingInfo& routing_info, | |
| 30 const std::vector<scoped_refptr<syncer::ModelSafeWorker>>& workers, | |
| 31 const scoped_refptr<syncer::ExtensionsActivity>& extensions_activity, | |
| 32 const syncer::WeakHandle<syncer::JsEventHandler>& event_handler, | |
| 33 const GURL& service_url, | |
| 34 const std::string& sync_user_agent, | |
| 35 scoped_ptr<syncer::HttpPostProviderFactory> http_bridge_factory, | |
| 36 const syncer::SyncCredentials& credentials, | |
| 37 const std::string& invalidator_client_id, | |
| 38 scoped_ptr<syncer::SyncManagerFactory> sync_manager_factory, | |
| 39 bool delete_sync_data_folder, | |
| 40 const std::string& restored_key_for_bootstrapping, | |
| 41 const std::string& restored_keystore_key_for_bootstrapping, | |
| 42 scoped_ptr<syncer::InternalComponentsFactory> internal_components_factory, | |
| 43 const syncer::WeakHandle<syncer::UnrecoverableErrorHandler>& | |
| 44 unrecoverable_error_handler, | |
| 45 const base::Closure& report_unrecoverable_error_function, | |
| 46 scoped_ptr<syncer::SyncEncryptionHandler::NigoriState> saved_nigori_state, | |
| 47 syncer::PassphraseTransitionClearDataOption clear_data_option, | |
| 48 const std::map<syncer::ModelType, int64>& invalidation_versions); | |
| 49 ~DoInitializeOptions(); | |
| 50 | |
| 51 base::MessageLoop* sync_loop; | |
| 52 SyncBackendRegistrar* registrar; | |
| 53 syncer::ModelSafeRoutingInfo routing_info; | |
| 54 std::vector<scoped_refptr<syncer::ModelSafeWorker> > workers; | |
| 55 scoped_refptr<syncer::ExtensionsActivity> extensions_activity; | |
| 56 syncer::WeakHandle<syncer::JsEventHandler> event_handler; | |
| 57 GURL service_url; | |
| 58 std::string sync_user_agent; | |
| 59 // Overridden by tests. | |
| 60 scoped_ptr<syncer::HttpPostProviderFactory> http_bridge_factory; | |
| 61 syncer::SyncCredentials credentials; | |
| 62 const std::string invalidator_client_id; | |
| 63 scoped_ptr<syncer::SyncManagerFactory> sync_manager_factory; | |
| 64 std::string lsid; | |
| 65 bool delete_sync_data_folder; | |
| 66 std::string restored_key_for_bootstrapping; | |
| 67 std::string restored_keystore_key_for_bootstrapping; | |
| 68 scoped_ptr<syncer::InternalComponentsFactory> internal_components_factory; | |
| 69 const syncer::WeakHandle<syncer::UnrecoverableErrorHandler> | |
| 70 unrecoverable_error_handler; | |
| 71 base::Closure report_unrecoverable_error_function; | |
| 72 scoped_ptr<syncer::SyncEncryptionHandler::NigoriState> saved_nigori_state; | |
| 73 const syncer::PassphraseTransitionClearDataOption clear_data_option; | |
| 74 const std::map<syncer::ModelType, int64> invalidation_versions; | |
| 75 }; | |
| 76 | |
| 77 // Helper struct to handle currying params to | |
| 78 // SyncBackendHost::Core::DoConfigureSyncer. | |
| 79 struct DoConfigureSyncerTypes { | |
| 80 DoConfigureSyncerTypes(); | |
| 81 ~DoConfigureSyncerTypes(); | |
| 82 syncer::ModelTypeSet to_download; | |
| 83 syncer::ModelTypeSet to_purge; | |
| 84 syncer::ModelTypeSet to_journal; | |
| 85 syncer::ModelTypeSet to_unapply; | |
| 86 }; | |
| 87 | |
| 88 class SyncBackendHostCore | |
| 89 : public base::RefCountedThreadSafe<SyncBackendHostCore>, | |
| 90 public syncer::SyncEncryptionHandler::Observer, | |
| 91 public syncer::SyncManager::Observer, | |
| 92 public syncer::TypeDebugInfoObserver { | |
| 93 public: | |
| 94 SyncBackendHostCore(const std::string& name, | |
| 95 const base::FilePath& sync_data_folder_path, | |
| 96 bool has_sync_setup_completed, | |
| 97 const base::WeakPtr<SyncBackendHostImpl>& backend); | |
| 98 | |
| 99 // SyncManager::Observer implementation. The Core just acts like an air | |
| 100 // traffic controller here, forwarding incoming messages to appropriate | |
| 101 // landing threads. | |
| 102 void OnSyncCycleCompleted( | |
| 103 const syncer::sessions::SyncSessionSnapshot& snapshot) override; | |
| 104 void OnInitializationComplete( | |
| 105 const syncer::WeakHandle<syncer::JsBackend>& js_backend, | |
| 106 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>& | |
| 107 debug_info_listener, | |
| 108 bool success, | |
| 109 syncer::ModelTypeSet restored_types) override; | |
| 110 void OnConnectionStatusChange(syncer::ConnectionStatus status) override; | |
| 111 void OnActionableError(const syncer::SyncProtocolError& sync_error) override; | |
| 112 void OnMigrationRequested(syncer::ModelTypeSet types) override; | |
| 113 void OnProtocolEvent(const syncer::ProtocolEvent& event) override; | |
| 114 | |
| 115 // SyncEncryptionHandler::Observer implementation. | |
| 116 void OnPassphraseRequired( | |
| 117 syncer::PassphraseRequiredReason reason, | |
| 118 const sync_pb::EncryptedData& pending_keys) override; | |
| 119 void OnPassphraseAccepted() override; | |
| 120 void OnBootstrapTokenUpdated(const std::string& bootstrap_token, | |
| 121 syncer::BootstrapTokenType type) override; | |
| 122 void OnEncryptedTypesChanged(syncer::ModelTypeSet encrypted_types, | |
| 123 bool encrypt_everything) override; | |
| 124 void OnEncryptionComplete() override; | |
| 125 void OnCryptographerStateChanged( | |
| 126 syncer::Cryptographer* cryptographer) override; | |
| 127 void OnPassphraseTypeChanged(syncer::PassphraseType type, | |
| 128 base::Time passphrase_time) override; | |
| 129 void OnLocalSetPassphraseEncryption( | |
| 130 const syncer::SyncEncryptionHandler::NigoriState& nigori_state) override; | |
| 131 | |
| 132 // TypeDebugInfoObserver implementation | |
| 133 void OnCommitCountersUpdated(syncer::ModelType type, | |
| 134 const syncer::CommitCounters& counters) override; | |
| 135 void OnUpdateCountersUpdated(syncer::ModelType type, | |
| 136 const syncer::UpdateCounters& counters) override; | |
| 137 void OnStatusCountersUpdated(syncer::ModelType type, | |
| 138 const syncer::StatusCounters& counters) override; | |
| 139 | |
| 140 // Forwards an invalidation state change to the sync manager. | |
| 141 void DoOnInvalidatorStateChange(syncer::InvalidatorState state); | |
| 142 | |
| 143 // Forwards an invalidation to the sync manager. | |
| 144 void DoOnIncomingInvalidation( | |
| 145 const syncer::ObjectIdInvalidationMap& invalidation_map); | |
| 146 | |
| 147 // Note: | |
| 148 // | |
| 149 // The Do* methods are the various entry points from our | |
| 150 // SyncBackendHost. They are all called on the sync thread to | |
| 151 // actually perform synchronous (and potentially blocking) syncapi | |
| 152 // operations. | |
| 153 // | |
| 154 // Called to perform initialization of the syncapi on behalf of | |
| 155 // SyncBackendHost::Initialize. | |
| 156 void DoInitialize(scoped_ptr<DoInitializeOptions> options); | |
| 157 | |
| 158 // Called to perform credential update on behalf of | |
| 159 // SyncBackendHost::UpdateCredentials. | |
| 160 void DoUpdateCredentials(const syncer::SyncCredentials& credentials); | |
| 161 | |
| 162 // Called to tell the syncapi to start syncing (generally after | |
| 163 // initialization and authentication). | |
| 164 void DoStartSyncing(const syncer::ModelSafeRoutingInfo& routing_info, | |
| 165 base::Time last_poll_time); | |
| 166 | |
| 167 // Called to set the passphrase for encryption. | |
| 168 void DoSetEncryptionPassphrase(const std::string& passphrase, | |
| 169 bool is_explicit); | |
| 170 | |
| 171 // Called to decrypt the pending keys. | |
| 172 void DoSetDecryptionPassphrase(const std::string& passphrase); | |
| 173 | |
| 174 // Called to turn on encryption of all sync data as well as | |
| 175 // reencrypt everything. | |
| 176 void DoEnableEncryptEverything(); | |
| 177 | |
| 178 // Ask the syncer to check for updates for the specified types. | |
| 179 void DoRefreshTypes(syncer::ModelTypeSet types); | |
| 180 | |
| 181 // Invoked if we failed to download the necessary control types at startup. | |
| 182 // Invokes SyncBackendHost::HandleControlTypesDownloadRetry. | |
| 183 void OnControlTypesDownloadRetry(); | |
| 184 | |
| 185 // Called to perform tasks which require the control data to be downloaded. | |
| 186 // This includes refreshing encryption, etc. | |
| 187 void DoInitialProcessControlTypes(); | |
| 188 | |
| 189 // The shutdown order is a bit complicated: | |
| 190 // 1) Call ShutdownOnUIThread() from |frontend_loop_| to request sync manager | |
| 191 // to stop as soon as possible. | |
| 192 // 2) Post DoShutdown() to sync loop to clean up backend state, save | |
| 193 // directory and destroy sync manager. | |
| 194 void ShutdownOnUIThread(); | |
| 195 void DoShutdown(syncer::ShutdownReason reason); | |
| 196 void DoDestroySyncManager(syncer::ShutdownReason reason); | |
| 197 | |
| 198 // Configuration methods that must execute on sync loop. | |
| 199 void DoConfigureSyncer( | |
| 200 syncer::ConfigureReason reason, | |
| 201 const DoConfigureSyncerTypes& config_types, | |
| 202 const syncer::ModelSafeRoutingInfo routing_info, | |
| 203 const base::Callback<void(syncer::ModelTypeSet, | |
| 204 syncer::ModelTypeSet)>& ready_task, | |
| 205 const base::Closure& retry_callback); | |
| 206 void DoFinishConfigureDataTypes( | |
| 207 syncer::ModelTypeSet types_to_config, | |
| 208 const base::Callback<void(syncer::ModelTypeSet, | |
| 209 syncer::ModelTypeSet)>& ready_task); | |
| 210 void DoRetryConfiguration( | |
| 211 const base::Closure& retry_callback); | |
| 212 | |
| 213 // Set the base request context to use when making HTTP calls. | |
| 214 // This method will add a reference to the context to persist it | |
| 215 // on the IO thread. Must be removed from IO thread. | |
| 216 | |
| 217 syncer::SyncManager* sync_manager() { return sync_manager_.get(); } | |
| 218 | |
| 219 void SendBufferedProtocolEventsAndEnableForwarding(); | |
| 220 void DisableProtocolEventForwarding(); | |
| 221 | |
| 222 // Enables the forwarding of directory type debug counters to the | |
| 223 // SyncBackendHost. Also requests that updates to all counters be | |
| 224 // emitted right away to initialize any new listeners' states. | |
| 225 void EnableDirectoryTypeDebugInfoForwarding(); | |
| 226 | |
| 227 // Disables forwarding of directory type debug counters. | |
| 228 void DisableDirectoryTypeDebugInfoForwarding(); | |
| 229 | |
| 230 // Delete the sync data folder to cleanup backend data. Happens the first | |
| 231 // time sync is enabled for a user (to prevent accidentally reusing old | |
| 232 // sync databases), as well as shutdown when you're no longer syncing. | |
| 233 void DeleteSyncDataFolder(); | |
| 234 | |
| 235 // We expose this member because it's required in the construction of the | |
| 236 // HttpBridgeFactory. | |
| 237 syncer::CancelationSignal* GetRequestContextCancelationSignal() { | |
| 238 return &release_request_context_signal_; | |
| 239 } | |
| 240 | |
| 241 void GetAllNodesForTypes( | |
| 242 syncer::ModelTypeSet types, | |
| 243 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
| 244 base::Callback<void(const std::vector<syncer::ModelType>& type, | |
| 245 ScopedVector<base::ListValue>) > callback); | |
| 246 | |
| 247 // Tell the sync manager to persist its state by writing to disk. | |
| 248 // Called on the sync thread, both by a timer and, on Android, when the | |
| 249 // application is backgrounded. | |
| 250 void SaveChanges(); | |
| 251 | |
| 252 void DoClearServerData( | |
| 253 const syncer::SyncManager::ClearServerDataCallback& frontend_callback); | |
| 254 | |
| 255 private: | |
| 256 friend class base::RefCountedThreadSafe<SyncBackendHostCore>; | |
| 257 friend class SyncBackendHostForProfileSyncTest; | |
| 258 | |
| 259 ~SyncBackendHostCore() override; | |
| 260 | |
| 261 // Invoked when initialization of syncapi is complete and we can start | |
| 262 // our timer. | |
| 263 // This must be called from the thread on which SaveChanges is intended to | |
| 264 // be run on; the host's |registrar_->sync_thread()|. | |
| 265 void StartSavingChanges(); | |
| 266 | |
| 267 void ClearServerDataDone(const base::Closure& frontend_callback); | |
| 268 | |
| 269 // Name used for debugging. | |
| 270 const std::string name_; | |
| 271 | |
| 272 // Path of the folder that stores the sync data files. | |
| 273 const base::FilePath sync_data_folder_path_; | |
| 274 | |
| 275 // Our parent SyncBackendHost. | |
| 276 syncer::WeakHandle<SyncBackendHostImpl> host_; | |
| 277 | |
| 278 // The loop where all the sync backend operations happen. | |
| 279 // Non-NULL only between calls to DoInitialize() and ~Core(). | |
| 280 base::MessageLoop* sync_loop_; | |
| 281 | |
| 282 // Our parent's registrar (not owned). Non-NULL only between | |
| 283 // calls to DoInitialize() and DoShutdown(). | |
| 284 SyncBackendRegistrar* registrar_; | |
| 285 | |
| 286 // The timer used to periodically call SaveChanges. | |
| 287 scoped_ptr<base::RepeatingTimer> save_changes_timer_; | |
| 288 | |
| 289 // Our encryptor, which uses Chrome's encryption functions. | |
| 290 sync_driver::SystemEncryptor encryptor_; | |
| 291 | |
| 292 // The top-level syncapi entry point. Lives on the sync thread. | |
| 293 scoped_ptr<syncer::SyncManager> sync_manager_; | |
| 294 | |
| 295 // Temporary holder of sync manager's initialization results. Set by | |
| 296 // OnInitializeComplete, and consumed when we pass it via OnBackendInitialized | |
| 297 // in the final state of HandleInitializationSuccessOnFrontendLoop. | |
| 298 syncer::WeakHandle<syncer::JsBackend> js_backend_; | |
| 299 syncer::WeakHandle<syncer::DataTypeDebugInfoListener> debug_info_listener_; | |
| 300 | |
| 301 // These signals allow us to send requests to shut down the HttpBridgeFactory | |
| 302 // and ServerConnectionManager without having to wait for those classes to | |
| 303 // finish initializing first. | |
| 304 // | |
| 305 // See comments in SyncBackendHostCore::ShutdownOnUIThread() for more details. | |
| 306 syncer::CancelationSignal release_request_context_signal_; | |
| 307 syncer::CancelationSignal stop_syncing_signal_; | |
| 308 | |
| 309 // Matches the value of SyncPref's HasSyncSetupCompleted() flag at init time. | |
| 310 // Should not be used for anything except for UMAs and logging. | |
| 311 const bool has_sync_setup_completed_; | |
| 312 | |
| 313 // Set when we've been asked to forward sync protocol events to the frontend. | |
| 314 bool forward_protocol_events_; | |
| 315 | |
| 316 // Set when the forwarding of per-type debug counters is enabled. | |
| 317 bool forward_type_info_; | |
| 318 | |
| 319 // A map of data type -> invalidation version to track the most recently | |
| 320 // received invalidation version for each type. | |
| 321 // This allows dropping any invalidations with versions older than those | |
| 322 // most recently received for that data type. | |
| 323 std::map<syncer::ModelType, int64> last_invalidation_versions_; | |
| 324 | |
| 325 base::WeakPtrFactory<SyncBackendHostCore> weak_ptr_factory_; | |
| 326 | |
| 327 DISALLOW_COPY_AND_ASSIGN(SyncBackendHostCore); | |
| 328 }; | |
| 329 | |
| 330 } // namespace browser_sync | |
| 331 | |
| 332 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_CORE_H_ | |
| OLD | NEW |