Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: chrome/browser/sync/internal_api/sync_manager.h

Issue 7926001: [Sync] Move change-related methods out of SyncManager::Observer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix bug in async encryption, sync to head Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_SYNC_INTERNAL_API_SYNC_MANAGER_H_ 5 #ifndef CHROME_BROWSER_SYNC_INTERNAL_API_SYNC_MANAGER_H_
6 #define CHROME_BROWSER_SYNC_INTERNAL_API_SYNC_MANAGER_H_ 6 #define CHROME_BROWSER_SYNC_INTERNAL_API_SYNC_MANAGER_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/callback_old.h" 12 #include "base/callback_old.h"
13 #include "base/threading/thread_checker.h"
13 #include "chrome/browser/sync/internal_api/change_record.h" 14 #include "chrome/browser/sync/internal_api/change_record.h"
14 #include "chrome/browser/sync/internal_api/configure_reason.h" 15 #include "chrome/browser/sync/internal_api/configure_reason.h"
15 #include "chrome/browser/sync/protocol/sync_protocol_error.h" 16 #include "chrome/browser/sync/protocol/sync_protocol_error.h"
16 #include "chrome/browser/sync/syncable/model_type.h" 17 #include "chrome/browser/sync/syncable/model_type.h"
17 #include "chrome/browser/sync/util/weak_handle.h" 18 #include "chrome/browser/sync/util/weak_handle.h"
18 #include "chrome/common/net/gaia/google_service_auth_error.h" 19 #include "chrome/common/net/gaia/google_service_auth_error.h"
19 20
20 class FilePath; 21 class FilePath;
21 22
22 namespace base { 23 namespace base {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 // decryption with the cached passphrase 59 // decryption with the cached passphrase
59 // was unsuccessful. 60 // was unsuccessful.
60 }; 61 };
61 62
62 // Contains everything needed to talk to and identify a user account. 63 // Contains everything needed to talk to and identify a user account.
63 struct SyncCredentials { 64 struct SyncCredentials {
64 std::string email; 65 std::string email;
65 std::string sync_token; 66 std::string sync_token;
66 }; 67 };
67 68
68 // SyncManager encapsulates syncable::DirectoryManager and serves as the parent 69 // SyncManager encapsulates syncable::DirectoryManager and serves as
69 // of all other objects in the sync API. SyncManager is thread-safe. If 70 // the parent of all other objects in the sync API. If multiple
70 // multiple threads interact with the same local sync repository (i.e. the 71 // threads interact with the same local sync repository (i.e. the same
71 // same sqlite database), they should share a single SyncManager instance. The 72 // sqlite database), they should share a single SyncManager instance.
72 // caller should typically create one SyncManager for the lifetime of a user 73 // The caller should typically create one SyncManager for the lifetime
73 // session. 74 // of a user session.
75 //
76 // Unless stated otherwise, all methods of SyncManager should be
77 // called on the same thread.
74 class SyncManager { 78 class SyncManager {
75 public: 79 public:
76 // SyncInternal contains the implementation of SyncManager, while abstracting 80 // SyncInternal contains the implementation of SyncManager, while abstracting
77 // internal types from clients of the interface. 81 // internal types from clients of the interface.
78 class SyncInternal; 82 class SyncInternal;
79 83
80 // Status encapsulates detailed state about the internals of the SyncManager. 84 // Status encapsulates detailed state about the internals of the SyncManager.
81 struct Status { 85 struct Status {
82 // Summary is a distilled set of important information that the end-user may 86 // Summary is a distilled set of important information that the end-user may
83 // wish to be informed about (through UI, for example). Note that if a 87 // wish to be informed about (through UI, for example). Note that if a
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 // Count of useless and useful syncs we perform. 160 // Count of useless and useful syncs we perform.
157 int useless_sync_cycles; 161 int useless_sync_cycles;
158 int useful_sync_cycles; 162 int useful_sync_cycles;
159 163
160 // Encryption related. 164 // Encryption related.
161 syncable::ModelTypeSet encrypted_types; 165 syncable::ModelTypeSet encrypted_types;
162 bool cryptographer_ready; 166 bool cryptographer_ready;
163 bool crypto_has_pending_keys; 167 bool crypto_has_pending_keys;
164 }; 168 };
165 169
166 // An interface the embedding application implements to receive notifications 170 // An interface the embedding application implements to be notified
167 // from the SyncManager. Register an observer via SyncManager::AddObserver. 171 // on change events. Note that these methods may be called on *any*
168 // This observer is an event driven model as the events may be raised from 172 // thread.
169 // different internal threads, and simply providing an "OnStatusChanged" type 173 class ChangeDelegate {
170 // notification complicates things such as trying to determine "what changed",
171 // if different members of the Status object are modified from different
172 // threads. This way, the event is explicit, and it is safe for the Observer
173 // to dispatch to a native thread or synchronize accordingly.
174 class Observer {
175 public: 174 public:
176 // Notify the observer that changes have been applied to the sync model. 175 // Notify the delegate that changes have been applied to the sync model.
177 // 176 //
178 // This will be invoked on the same thread as on which ApplyChanges was 177 // This will be invoked on the same thread as on which ApplyChanges was
179 // called. |changes| is an array of size |change_count|, and contains the 178 // called. |changes| is an array of size |change_count|, and contains the
180 // ID of each individual item that was changed. |changes| exists only for 179 // ID of each individual item that was changed. |changes| exists only for
181 // the duration of the call. If items of multiple data types change at 180 // the duration of the call. If items of multiple data types change at
182 // the same time, this method is invoked once per data type and |changes| 181 // the same time, this method is invoked once per data type and |changes|
183 // is restricted to items of the ModelType indicated by |model_type|. 182 // is restricted to items of the ModelType indicated by |model_type|.
184 // Because the observer is passed a |trans|, the observer can assume a 183 // Because the observer is passed a |trans|, the observer can assume a
185 // read lock on the sync model that will be released after the function 184 // read lock on the sync model that will be released after the function
186 // returns. 185 // returns.
(...skipping 22 matching lines...) Expand all
209 // 208 //
210 // The purpose of this function is to support processors that require 209 // The purpose of this function is to support processors that require
211 // split-transactions changes. For example, if a model processor wants to 210 // split-transactions changes. For example, if a model processor wants to
212 // perform blocking I/O due to a change, it should calculate the changes 211 // perform blocking I/O due to a change, it should calculate the changes
213 // while holding the transaction lock (from within OnChangesApplied), buffer 212 // while holding the transaction lock (from within OnChangesApplied), buffer
214 // those changes, let the transaction fall out of scope, and then commit 213 // those changes, let the transaction fall out of scope, and then commit
215 // those changes from within OnChangesComplete (postponing the blocking 214 // those changes from within OnChangesComplete (postponing the blocking
216 // I/O to when it no longer holds any lock). 215 // I/O to when it no longer holds any lock).
217 virtual void OnChangesComplete(syncable::ModelType model_type) = 0; 216 virtual void OnChangesComplete(syncable::ModelType model_type) = 0;
218 217
218 protected:
219 virtual ~ChangeDelegate();
220 };
221
222 // Like ChangeDelegate, except called only on the sync thread and
223 // not while a transaction is held. For objects that want to know
224 // when changes happen, but don't need to process them.
225 class ChangeObserver {
226 public:
227 // Ids referred to in |changes| may or may not be in |mutations|.
228 // If they're not, that means that the node didn't actually
229 // change, but we marked them as changed for some other reason
230 // (e.g., siblings of re-ordered nodes).
231 //
232 // TODO(sync, long-term): Ideally, ChangeDelegate would also be
233 // passed |mutations| instead of a transaction that would have to
234 // be used to look up the changed nodes. That is,
235 // ChangeDelegate::OnChangesApplied() would still be called under
236 // the transaction, but all the needed data will be passed down.
237 //
238 // Even more ideally, we would have sync semantics such that we'd
239 // be able to apply changes without being under a transaction.
240 // But that's a ways off...
241 virtual void OnChangesApplied(
242 syncable::ModelType model_type,
243 const syncable::ImmutableEntryKernelMutationMap& mutations,
tim (not reviewing) 2011/09/20 16:04:59 We shouldn't add new deps on syncable:: stuff here
akalin 2011/09/20 21:14:42 Introduced the notion of a write transaction ID, a
244 const ImmutableChangeRecordList& changes) = 0;
245
246 virtual void OnChangesComplete(syncable::ModelType model_type) = 0;
247
248 protected:
249 virtual ~ChangeObserver();
250 };
251
252 // An interface the embedding application implements to receive
253 // notifications from the SyncManager. Register an observer via
254 // SyncManager::AddObserver. All methods are called only on the
255 // sync thread.
256 class Observer {
257 public:
219 // A round-trip sync-cycle took place and the syncer has resolved any 258 // A round-trip sync-cycle took place and the syncer has resolved any
220 // conflicts that may have arisen. 259 // conflicts that may have arisen.
221 virtual void OnSyncCycleCompleted( 260 virtual void OnSyncCycleCompleted(
222 const browser_sync::sessions::SyncSessionSnapshot* snapshot) = 0; 261 const browser_sync::sessions::SyncSessionSnapshot* snapshot) = 0;
223 262
224 // Called when user interaction may be required due to an auth problem. 263 // Called when user interaction may be required due to an auth problem.
225 virtual void OnAuthError(const GoogleServiceAuthError& auth_error) = 0; 264 virtual void OnAuthError(const GoogleServiceAuthError& auth_error) = 0;
226 265
227 // Called when a new auth token is provided by the sync server. 266 // Called when a new auth token is provided by the sync server.
228 virtual void OnUpdatedToken(const std::string& token) = 0; 267 virtual void OnUpdatedToken(const std::string& token) = 0;
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 // After a request to clear server data, these callbacks are invoked to 372 // After a request to clear server data, these callbacks are invoked to
334 // indicate success or failure. 373 // indicate success or failure.
335 virtual void OnClearServerDataSucceeded() = 0; 374 virtual void OnClearServerDataSucceeded() = 0;
336 virtual void OnClearServerDataFailed() = 0; 375 virtual void OnClearServerDataFailed() = 0;
337 376
338 // Called after we finish encrypting all appropriate datatypes. 377 // Called after we finish encrypting all appropriate datatypes.
339 virtual void OnEncryptionComplete( 378 virtual void OnEncryptionComplete(
340 const syncable::ModelTypeSet& encrypted_types) = 0; 379 const syncable::ModelTypeSet& encrypted_types) = 0;
341 380
342 virtual void OnActionableError( 381 virtual void OnActionableError(
343 const browser_sync::SyncProtocolError& sync_protocol_error) 382 const browser_sync::SyncProtocolError& sync_protocol_error) = 0;
344 = 0;
345 383
346 protected: 384 protected:
347 virtual ~Observer(); 385 virtual ~Observer();
348 }; 386 };
349 387
350 typedef Callback0::Type ModeChangeCallback; 388 typedef Callback0::Type ModeChangeCallback;
351 389
352 // Create an uninitialized SyncManager. Callers must Init() before using. 390 // Create an uninitialized SyncManager. Callers must Init() before using.
353 explicit SyncManager(const std::string& name); 391 explicit SyncManager(const std::string& name);
354 virtual ~SyncManager(); 392 virtual ~SyncManager();
(...skipping 14 matching lines...) Expand all
369 // HTTP header. Used internally when collecting stats to classify clients. 407 // HTTP header. Used internally when collecting stats to classify clients.
370 // |sync_notifier| is owned and used to listen for notifications. 408 // |sync_notifier| is owned and used to listen for notifications.
371 bool Init(const FilePath& database_location, 409 bool Init(const FilePath& database_location,
372 const browser_sync::WeakHandle<browser_sync::JsEventHandler>& 410 const browser_sync::WeakHandle<browser_sync::JsEventHandler>&
373 event_handler, 411 event_handler,
374 const std::string& sync_server_and_path, 412 const std::string& sync_server_and_path,
375 int sync_server_port, 413 int sync_server_port,
376 bool use_ssl, 414 bool use_ssl,
377 HttpPostProviderFactory* post_factory, 415 HttpPostProviderFactory* post_factory,
378 browser_sync::ModelSafeWorkerRegistrar* registrar, 416 browser_sync::ModelSafeWorkerRegistrar* registrar,
417 ChangeDelegate* change_delegate,
379 const std::string& user_agent, 418 const std::string& user_agent,
380 const SyncCredentials& credentials, 419 const SyncCredentials& credentials,
381 sync_notifier::SyncNotifier* sync_notifier, 420 sync_notifier::SyncNotifier* sync_notifier,
382 const std::string& restored_key_for_bootstrapping, 421 const std::string& restored_key_for_bootstrapping,
383 bool setup_for_test_mode); 422 bool setup_for_test_mode);
384 423
385 // Returns the username last used for a successful authentication. 424 // Returns the username last used for a successful authentication.
386 // Returns empty if there is no such username. 425 // Returns empty if there is no such username. May be called on any
426 // thread.
387 const std::string& GetAuthenticatedUsername(); 427 const std::string& GetAuthenticatedUsername();
388 428
389 // Check if the database has been populated with a full "initial" download of 429 // Check if the database has been populated with a full "initial" download of
390 // sync items for each data type currently present in the routing info. 430 // sync items for each data type currently present in the routing info.
391 // Prerequisite for calling this is that OnInitializationComplete has been 431 // Prerequisite for calling this is that OnInitializationComplete has been
392 // called. 432 // called. May be called from any thread.
393 bool InitialSyncEndedForAllEnabledTypes(); 433 bool InitialSyncEndedForAllEnabledTypes();
394 434
395 // Update tokens that we're using in Sync. Email must stay the same. 435 // Update tokens that we're using in Sync. Email must stay the same.
396 void UpdateCredentials(const SyncCredentials& credentials); 436 void UpdateCredentials(const SyncCredentials& credentials);
397 437
398 // Called when the user disables or enables a sync type. 438 // Called when the user disables or enables a sync type.
399 void UpdateEnabledTypes(); 439 void UpdateEnabledTypes();
400 440
401 // Conditionally sets the flag in the Nigori node which instructs other 441 // Conditionally sets the flag in the Nigori node which instructs other
402 // clients to start syncing tabs. 442 // clients to start syncing tabs.
(...skipping 24 matching lines...) Expand all
427 // Switches the mode of operation to CONFIGURATION_MODE and 467 // Switches the mode of operation to CONFIGURATION_MODE and
428 // schedules a config task to fetch updates for |types|. 468 // schedules a config task to fetch updates for |types|.
429 void RequestConfig(const syncable::ModelTypeBitSet& types, 469 void RequestConfig(const syncable::ModelTypeBitSet& types,
430 sync_api::ConfigureReason reason); 470 sync_api::ConfigureReason reason);
431 471
432 void RequestCleanupDisabledTypes(); 472 void RequestCleanupDisabledTypes();
433 473
434 // Request a clearing of all data on the server 474 // Request a clearing of all data on the server
435 void RequestClearServerData(); 475 void RequestClearServerData();
436 476
477 // Add/remove change observers.
478 void AddChangeObserver(ChangeObserver* observer);
479 void RemoveChangeObserver(ChangeObserver* observer);
480
437 // Adds a listener to be notified of sync events. 481 // Adds a listener to be notified of sync events.
438 // NOTE: It is OK (in fact, it's probably a good idea) to call this before 482 // NOTE: It is OK (in fact, it's probably a good idea) to call this before
439 // having received OnInitializationCompleted. 483 // having received OnInitializationCompleted.
440 void AddObserver(Observer* observer); 484 void AddObserver(Observer* observer);
441 485
442 // Remove the given observer. Make sure to call this if the 486 // Remove the given observer. Make sure to call this if the
443 // Observer is being destroyed so the SyncManager doesn't 487 // Observer is being destroyed so the SyncManager doesn't
444 // potentially dereference garbage. 488 // potentially dereference garbage.
445 void RemoveObserver(Observer* observer); 489 void RemoveObserver(Observer* observer);
446 490
447 // Status-related getters. Typically GetStatusSummary will suffice, but 491 // Status-related getters. Typically GetStatusSummary will suffice, but
448 // GetDetailedSyncStatus can be useful for gathering debug-level details of 492 // GetDetailedSyncStatus can be useful for gathering debug-level details of
449 // the internals of the sync engine. 493 // the internals of the sync engine. May be called on any thread.
450 Status::Summary GetStatusSummary() const; 494 Status::Summary GetStatusSummary() const;
451 Status GetDetailedStatus() const; 495 Status GetDetailedStatus() const;
452 496
453 // Whether or not the Nigori node is encrypted using an explicit passphrase. 497 // Whether or not the Nigori node is encrypted using an explicit passphrase.
498 // May be called on any thread.
454 bool IsUsingExplicitPassphrase(); 499 bool IsUsingExplicitPassphrase();
455 500
456 // Get the internal implementation for use by BaseTransaction, etc.
457 SyncInternal* GetImpl() const;
458
459 // Call periodically from a database-safe thread to persist recent changes 501 // Call periodically from a database-safe thread to persist recent changes
460 // to the syncapi model. 502 // to the syncapi model.
461 void SaveChanges(); 503 void SaveChanges();
462 504
505 // Requests the syncer to stop as soon as possible. May be called
506 // from any thread.
463 void RequestEarlyExit(); 507 void RequestEarlyExit();
464 508
465 // Issue a final SaveChanges, close sqlite handles, and stop running threads. 509 // Issue a final SaveChanges, close sqlite handles, and stop running threads.
466 // Must be called from the same thread that called Init().
467 void Shutdown(); 510 void Shutdown();
468 511
512 // May be called from any thread.
469 UserShare* GetUserShare() const; 513 UserShare* GetUserShare() const;
470 514
471 // Inform the cryptographer of the most recent passphrase and set of encrypted 515 // Inform the cryptographer of the most recent passphrase and set of encrypted
472 // types (from nigori node), then ensure all data that needs encryption is 516 // types (from nigori node), then ensure all data that needs encryption is
473 // encrypted with the appropriate passphrase. 517 // encrypted with the appropriate passphrase.
474 // Note: opens a transaction and can trigger ON_PASSPHRASE_REQUIRED, so must 518 // Note: opens a transaction and can trigger ON_PASSPHRASE_REQUIRED, so must
475 // only be called after syncapi has been initialized. 519 // only be called after syncapi has been initialized.
476 void RefreshEncryption(); 520 void RefreshEncryption();
477 521
478 // Enable encryption of all sync data. Once enabled, it can never be disabled 522 // Enable encryption of all sync data. Once enabled, it can never be disabled
479 // without clearing the server data. 523 // without clearing the server data.
480 void EnableEncryptEverything(); 524 void EnableEncryptEverything();
481 525
482 // Returns true if we are currently encrypting all sync data. 526 // Returns true if we are currently encrypting all sync data. May
527 // be called on any thread.
483 bool EncryptEverythingEnabled() const; 528 bool EncryptEverythingEnabled() const;
484 529
485 // Gets the set of encrypted types from the cryptographer 530 // Gets the set of encrypted types from the cryptographer
486 // Note: opens a transaction. 531 // Note: opens a transaction. May be called from any thread.
487 syncable::ModelTypeSet GetEncryptedDataTypes() const; 532 syncable::ModelTypeSet GetEncryptedDataTypes() const;
488 533
489 // Reads the nigori node to determine if any experimental types should be 534 // Reads the nigori node to determine if any experimental types should be
490 // enabled. 535 // enabled.
491 // Note: opens a transaction. 536 // Note: opens a transaction. May be called on any thread.
492 bool ReceivedExperimentalTypes(syncable::ModelTypeSet* to_add) const; 537 bool ReceivedExperimentalTypes(syncable::ModelTypeSet* to_add) const;
493 538
494 // Uses a read-only transaction to determine if the directory being synced has 539 // Uses a read-only transaction to determine if the directory being synced has
495 // any remaining unsynced items. 540 // any remaining unsynced items. May be called on any thread.
496 bool HasUnsyncedItems() const; 541 bool HasUnsyncedItems() const;
497 542
498 // Logs the list of unsynced meta handles.
499 void LogUnsyncedItems(int level) const;
500
501 // Functions used for testing. 543 // Functions used for testing.
502 544
503 void TriggerOnNotificationStateChangeForTest( 545 void TriggerOnNotificationStateChangeForTest(
504 bool notifications_enabled); 546 bool notifications_enabled);
505 547
506 void TriggerOnIncomingNotificationForTest( 548 void TriggerOnIncomingNotificationForTest(
507 const syncable::ModelTypeBitSet& model_types); 549 const syncable::ModelTypeBitSet& model_types);
508 550
509 private: 551 private:
552 base::ThreadChecker thread_checker_;
553
510 // An opaque pointer to the nested private class. 554 // An opaque pointer to the nested private class.
511 SyncInternal* data_; 555 SyncInternal* data_;
512 556
513 DISALLOW_COPY_AND_ASSIGN(SyncManager); 557 DISALLOW_COPY_AND_ASSIGN(SyncManager);
514 }; 558 };
515 559
516 bool InitialSyncEndedForTypes(syncable::ModelTypeSet types, UserShare* share); 560 bool InitialSyncEndedForTypes(syncable::ModelTypeSet types, UserShare* share);
517 561
518 // Returns the string representation of a PassphraseRequiredReason value. 562 // Returns the string representation of a PassphraseRequiredReason value.
519 std::string PassphraseRequiredReasonToString(PassphraseRequiredReason reason); 563 std::string PassphraseRequiredReasonToString(PassphraseRequiredReason reason);
520 564
521 } // namespace sync_api 565 } // namespace sync_api
522 566
523 #endif // CHROME_BROWSER_SYNC_INTERNAL_API_SYNC_MANAGER_H_ 567 #endif // CHROME_BROWSER_SYNC_INTERNAL_API_SYNC_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698