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