OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_PROFILE_SYNC_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_ |
6 #define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_ | 6 #define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
18 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
20 #include "base/time.h" | 20 #include "base/time.h" |
21 #include "base/timer.h" | 21 #include "base/timer.h" |
| 22 #include "chrome/browser/invalidation/invalidation_frontend.h" |
| 23 #include "chrome/browser/invalidation/invalidator_storage.h" |
22 #include "chrome/browser/signin/oauth2_token_service.h" | 24 #include "chrome/browser/signin/oauth2_token_service.h" |
23 #include "chrome/browser/signin/signin_global_error.h" | 25 #include "chrome/browser/signin/signin_global_error.h" |
24 #include "chrome/browser/sync/backend_unrecoverable_error_handler.h" | 26 #include "chrome/browser/sync/backend_unrecoverable_error_handler.h" |
25 #include "chrome/browser/sync/glue/data_type_controller.h" | 27 #include "chrome/browser/sync/glue/data_type_controller.h" |
26 #include "chrome/browser/sync/glue/data_type_encryption_handler.h" | 28 #include "chrome/browser/sync/glue/data_type_encryption_handler.h" |
27 #include "chrome/browser/sync/glue/data_type_manager.h" | 29 #include "chrome/browser/sync/glue/data_type_manager.h" |
28 #include "chrome/browser/sync/glue/data_type_manager_observer.h" | 30 #include "chrome/browser/sync/glue/data_type_manager_observer.h" |
29 #include "chrome/browser/sync/glue/failed_data_types_handler.h" | 31 #include "chrome/browser/sync/glue/failed_data_types_handler.h" |
30 #include "chrome/browser/sync/glue/sync_backend_host.h" | 32 #include "chrome/browser/sync/glue/sync_backend_host.h" |
31 #include "chrome/browser/sync/profile_sync_service_base.h" | 33 #include "chrome/browser/sync/profile_sync_service_base.h" |
(...skipping 24 matching lines...) Expand all Loading... |
56 class DeviceInfo; | 58 class DeviceInfo; |
57 class DataTypeManager; | 59 class DataTypeManager; |
58 class JsController; | 60 class JsController; |
59 class SessionModelAssociator; | 61 class SessionModelAssociator; |
60 | 62 |
61 namespace sessions { class SyncSessionSnapshot; } | 63 namespace sessions { class SyncSessionSnapshot; } |
62 } | 64 } |
63 | 65 |
64 namespace syncer { | 66 namespace syncer { |
65 class BaseTransaction; | 67 class BaseTransaction; |
| 68 class InvalidatorRegistrar; |
66 struct SyncCredentials; | 69 struct SyncCredentials; |
67 struct UserShare; | 70 struct UserShare; |
68 } | 71 } |
69 | 72 |
70 namespace sync_pb { | 73 namespace sync_pb { |
71 class EncryptedData; | 74 class EncryptedData; |
72 } // namespace sync_pb | 75 } // namespace sync_pb |
73 | 76 |
74 // ProfileSyncService is the layer between browser subsystems like bookmarks, | 77 // ProfileSyncService is the layer between browser subsystems like bookmarks, |
75 // and the sync backend. Each subsystem is logically thought of as being | 78 // and the sync backend. Each subsystem is logically thought of as being |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 // data from the sync server. | 158 // data from the sync server. |
156 // | 159 // |
157 class ProfileSyncService : public ProfileSyncServiceBase, | 160 class ProfileSyncService : public ProfileSyncServiceBase, |
158 public browser_sync::SyncFrontend, | 161 public browser_sync::SyncFrontend, |
159 public browser_sync::SyncPrefObserver, | 162 public browser_sync::SyncPrefObserver, |
160 public browser_sync::DataTypeManagerObserver, | 163 public browser_sync::DataTypeManagerObserver, |
161 public SigninGlobalError::AuthStatusProvider, | 164 public SigninGlobalError::AuthStatusProvider, |
162 public syncer::UnrecoverableErrorHandler, | 165 public syncer::UnrecoverableErrorHandler, |
163 public content::NotificationObserver, | 166 public content::NotificationObserver, |
164 public BrowserContextKeyedService, | 167 public BrowserContextKeyedService, |
| 168 public invalidation::InvalidationFrontend, |
165 public browser_sync::DataTypeEncryptionHandler, | 169 public browser_sync::DataTypeEncryptionHandler, |
166 public OAuth2TokenService::Consumer { | 170 public OAuth2TokenService::Consumer { |
167 public: | 171 public: |
168 typedef browser_sync::SyncBackendHost::Status Status; | 172 typedef browser_sync::SyncBackendHost::Status Status; |
169 | 173 |
170 enum SyncEventCodes { | 174 enum SyncEventCodes { |
171 MIN_SYNC_EVENT_CODE = 0, | 175 MIN_SYNC_EVENT_CODE = 0, |
172 | 176 |
173 // Events starting the sync service. | 177 // Events starting the sync service. |
174 START_FROM_NTP = 1, // Sync was started from the ad in NTP | 178 START_FROM_NTP = 1, // Sync was started from the ad in NTP |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 const std::string& client_id) const; | 281 const std::string& client_id) const; |
278 | 282 |
279 // Fills state_map with a map of current data types that are possible to | 283 // Fills state_map with a map of current data types that are possible to |
280 // sync, as well as their states. | 284 // sync, as well as their states. |
281 void GetDataTypeControllerStates( | 285 void GetDataTypeControllerStates( |
282 browser_sync::DataTypeController::StateMap* state_map) const; | 286 browser_sync::DataTypeController::StateMap* state_map) const; |
283 | 287 |
284 // Disables sync for user. Use ShowLoginDialog to enable. | 288 // Disables sync for user. Use ShowLoginDialog to enable. |
285 virtual void DisableForUser(); | 289 virtual void DisableForUser(); |
286 | 290 |
| 291 // syncer::InvalidationHandler implementation (via SyncFrontend). |
| 292 virtual void OnInvalidatorStateChange( |
| 293 syncer::InvalidatorState state) OVERRIDE; |
| 294 virtual void OnIncomingInvalidation( |
| 295 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; |
| 296 |
287 // SyncFrontend implementation. | 297 // SyncFrontend implementation. |
288 virtual void OnBackendInitialized( | 298 virtual void OnBackendInitialized( |
289 const syncer::WeakHandle<syncer::JsBackend>& js_backend, | 299 const syncer::WeakHandle<syncer::JsBackend>& js_backend, |
290 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>& | 300 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>& |
291 debug_info_listener, | 301 debug_info_listener, |
292 bool success) OVERRIDE; | 302 bool success) OVERRIDE; |
293 virtual void OnSyncCycleCompleted() OVERRIDE; | 303 virtual void OnSyncCycleCompleted() OVERRIDE; |
294 virtual void OnSyncConfigureRetry() OVERRIDE; | 304 virtual void OnSyncConfigureRetry() OVERRIDE; |
295 virtual void OnConnectionStatusChange( | 305 virtual void OnConnectionStatusChange( |
296 syncer::ConnectionStatus status) OVERRIDE; | 306 syncer::ConnectionStatus status) OVERRIDE; |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 // If true, the ProfileSyncService has detected that a new GAIA signin has | 593 // If true, the ProfileSyncService has detected that a new GAIA signin has |
584 // succeeded, and is waiting for initialization to complete. This is used by | 594 // succeeded, and is waiting for initialization to complete. This is used by |
585 // the UI to differentiate between a new auth error (encountered as part of | 595 // the UI to differentiate between a new auth error (encountered as part of |
586 // the initialization process) and a pre-existing auth error that just hasn't | 596 // the initialization process) and a pre-existing auth error that just hasn't |
587 // been cleared yet. Virtual for testing purposes. | 597 // been cleared yet. Virtual for testing purposes. |
588 virtual bool waiting_for_auth() const; | 598 virtual bool waiting_for_auth() const; |
589 | 599 |
590 // The set of currently enabled sync experiments. | 600 // The set of currently enabled sync experiments. |
591 const syncer::Experiments& current_experiments() const; | 601 const syncer::Experiments& current_experiments() const; |
592 | 602 |
| 603 // InvalidationFrontend implementation. It is an error to have |
| 604 // registered handlers when Shutdown() is called. |
| 605 virtual void RegisterInvalidationHandler( |
| 606 syncer::InvalidationHandler* handler) OVERRIDE; |
| 607 virtual void UpdateRegisteredInvalidationIds( |
| 608 syncer::InvalidationHandler* handler, |
| 609 const syncer::ObjectIdSet& ids) OVERRIDE; |
| 610 virtual void UnregisterInvalidationHandler( |
| 611 syncer::InvalidationHandler* handler) OVERRIDE; |
| 612 virtual void AcknowledgeInvalidation( |
| 613 const invalidation::ObjectId& id, |
| 614 const syncer::AckHandle& ack_handle) OVERRIDE; |
| 615 |
| 616 virtual syncer::InvalidatorState GetInvalidatorState() const OVERRIDE; |
| 617 |
593 // OAuth2TokenService::Consumer implementation | 618 // OAuth2TokenService::Consumer implementation |
594 virtual void OnGetTokenSuccess( | 619 virtual void OnGetTokenSuccess( |
595 const OAuth2TokenService::Request* request, | 620 const OAuth2TokenService::Request* request, |
596 const std::string& access_token, | 621 const std::string& access_token, |
597 const base::Time& expiration_time) OVERRIDE; | 622 const base::Time& expiration_time) OVERRIDE; |
598 virtual void OnGetTokenFailure( | 623 virtual void OnGetTokenFailure( |
599 const OAuth2TokenService::Request* request, | 624 const OAuth2TokenService::Request* request, |
600 const GoogleServiceAuthError& error) OVERRIDE; | 625 const GoogleServiceAuthError& error) OVERRIDE; |
601 | 626 |
602 // BrowserContextKeyedService implementation. This must be called exactly | 627 // BrowserContextKeyedService implementation. This must be called exactly |
603 // once (before this object is destroyed). | 628 // once (before this object is destroyed). |
604 virtual void Shutdown() OVERRIDE; | 629 virtual void Shutdown() OVERRIDE; |
605 | 630 |
| 631 // Simulate an incoming notification for the given id and payload. |
| 632 void EmitInvalidationForTest( |
| 633 const invalidation::ObjectId& id, |
| 634 const std::string& payload); |
| 635 |
606 // Called when a datatype (SyncableService) has a need for sync to start | 636 // Called when a datatype (SyncableService) has a need for sync to start |
607 // ASAP, presumably because a local change event has occurred but we're | 637 // ASAP, presumably because a local change event has occurred but we're |
608 // still in deferred start mode, meaning the SyncableService hasn't been | 638 // still in deferred start mode, meaning the SyncableService hasn't been |
609 // told to MergeDataAndStartSyncing yet. | 639 // told to MergeDataAndStartSyncing yet. |
610 void OnDataTypeRequestsSyncStartup(syncer::ModelType type); | 640 void OnDataTypeRequestsSyncStartup(syncer::ModelType type); |
611 | 641 |
612 protected: | 642 protected: |
613 // Used by test classes that derive from ProfileSyncService. | 643 // Used by test classes that derive from ProfileSyncService. |
614 virtual browser_sync::SyncBackendHost* GetBackendForTest(); | 644 virtual browser_sync::SyncBackendHost* GetBackendForTest(); |
615 | 645 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 private: | 696 private: |
667 enum UnrecoverableErrorReason { | 697 enum UnrecoverableErrorReason { |
668 ERROR_REASON_UNSET, | 698 ERROR_REASON_UNSET, |
669 ERROR_REASON_SYNCER, | 699 ERROR_REASON_SYNCER, |
670 ERROR_REASON_BACKEND_INIT_FAILURE, | 700 ERROR_REASON_BACKEND_INIT_FAILURE, |
671 ERROR_REASON_CONFIGURATION_RETRY, | 701 ERROR_REASON_CONFIGURATION_RETRY, |
672 ERROR_REASON_CONFIGURATION_FAILURE, | 702 ERROR_REASON_CONFIGURATION_FAILURE, |
673 ERROR_REASON_ACTIONABLE_ERROR, | 703 ERROR_REASON_ACTIONABLE_ERROR, |
674 ERROR_REASON_LIMIT | 704 ERROR_REASON_LIMIT |
675 }; | 705 }; |
| 706 typedef std::vector<std::pair<invalidation::ObjectId, |
| 707 syncer::AckHandle> > AckHandleReplayQueue; |
676 friend class ProfileSyncServicePasswordTest; | 708 friend class ProfileSyncServicePasswordTest; |
677 friend class SyncTest; | 709 friend class SyncTest; |
678 friend class TestProfileSyncService; | 710 friend class TestProfileSyncService; |
679 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceTest, InitialState); | 711 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceTest, InitialState); |
680 | 712 |
681 // Detects and attempts to recover from a previous improper datatype | 713 // Detects and attempts to recover from a previous improper datatype |
682 // configuration where Keep Everything Synced and the preferred types were | 714 // configuration where Keep Everything Synced and the preferred types were |
683 // not correctly set. | 715 // not correctly set. |
684 void TrySyncDatatypePrefRecovery(); | 716 void TrySyncDatatypePrefRecovery(); |
685 | 717 |
(...skipping 26 matching lines...) Expand all Loading... |
712 // previous "Sync Data" folders. (useful if the folder is partial/corrupt). | 744 // previous "Sync Data" folders. (useful if the folder is partial/corrupt). |
713 void InitializeBackend(bool delete_sync_data_folder); | 745 void InitializeBackend(bool delete_sync_data_folder); |
714 | 746 |
715 // Initializes the various settings from the command line. | 747 // Initializes the various settings from the command line. |
716 void InitSettings(); | 748 void InitSettings(); |
717 | 749 |
718 // Sets the last synced time to the current time. | 750 // Sets the last synced time to the current time. |
719 void UpdateLastSyncedTime(); | 751 void UpdateLastSyncedTime(); |
720 | 752 |
721 void NotifyObservers(); | 753 void NotifyObservers(); |
722 void NotifySyncCycleCompleted(); | |
723 | 754 |
724 void ClearStaleErrors(); | 755 void ClearStaleErrors(); |
725 | 756 |
726 void ClearUnrecoverableError(); | 757 void ClearUnrecoverableError(); |
727 | 758 |
728 enum StartUpDeferredOption { | 759 enum StartUpDeferredOption { |
729 STARTUP_BACKEND_DEFERRED, | 760 STARTUP_BACKEND_DEFERRED, |
730 STARTUP_IMMEDIATE | 761 STARTUP_IMMEDIATE |
731 }; | 762 }; |
732 void StartUp(StartUpDeferredOption deferred_option); | 763 void StartUp(StartUpDeferredOption deferred_option); |
(...skipping 29 matching lines...) Expand all Loading... |
762 void RefreshSpareBootstrapToken(const std::string& passphrase); | 793 void RefreshSpareBootstrapToken(const std::string& passphrase); |
763 #endif | 794 #endif |
764 | 795 |
765 // Internal unrecoverable error handler. Used to track error reason via | 796 // Internal unrecoverable error handler. Used to track error reason via |
766 // Sync.UnrecoverableErrors histogram. | 797 // Sync.UnrecoverableErrors histogram. |
767 void OnInternalUnrecoverableError(const tracked_objects::Location& from_here, | 798 void OnInternalUnrecoverableError(const tracked_objects::Location& from_here, |
768 const std::string& message, | 799 const std::string& message, |
769 bool delete_sync_database, | 800 bool delete_sync_database, |
770 UnrecoverableErrorReason reason); | 801 UnrecoverableErrorReason reason); |
771 | 802 |
| 803 // Must be called every time |backend_initialized_| or |
| 804 // |invalidator_state_| is changed (but only if |
| 805 // |invalidator_registrar_| is not NULL). |
| 806 void UpdateInvalidatorRegistrarState(); |
| 807 |
772 // Returns the username (in form of an email address) that should be used in | 808 // Returns the username (in form of an email address) that should be used in |
773 // the credentials. | 809 // the credentials. |
774 std::string GetEffectiveUsername(); | 810 std::string GetEffectiveUsername(); |
775 | 811 |
776 // Factory used to create various dependent objects. | 812 // Factory used to create various dependent objects. |
777 scoped_ptr<ProfileSyncComponentsFactory> factory_; | 813 scoped_ptr<ProfileSyncComponentsFactory> factory_; |
778 | 814 |
779 // The profile whose data we are synchronizing. | 815 // The profile whose data we are synchronizing. |
780 Profile* profile_; | 816 Profile* profile_; |
781 | 817 |
782 // The class that handles getting, setting, and persisting sync | 818 // The class that handles getting, setting, and persisting sync |
783 // preferences. | 819 // preferences. |
784 browser_sync::SyncPrefs sync_prefs_; | 820 browser_sync::SyncPrefs sync_prefs_; |
785 | 821 |
| 822 // TODO(tim): Move this to InvalidationService, once it exists. Bug 124137. |
| 823 invalidation::InvalidatorStorage invalidator_storage_; |
| 824 |
786 // TODO(ncarter): Put this in a profile, once there is UI for it. | 825 // TODO(ncarter): Put this in a profile, once there is UI for it. |
787 // This specifies where to find the sync server. | 826 // This specifies where to find the sync server. |
788 GURL sync_service_url_; | 827 GURL sync_service_url_; |
789 | 828 |
790 // The last time we detected a successful transition from SYNCING state. | 829 // The last time we detected a successful transition from SYNCING state. |
791 // Our backend notifies us whenever we should take a new snapshot. | 830 // Our backend notifies us whenever we should take a new snapshot. |
792 base::Time last_synced_time_; | 831 base::Time last_synced_time_; |
793 | 832 |
794 // The time that StartUp() is called. This member is zero if StartUp() has | 833 // The time that StartUp() is called. This member is zero if StartUp() has |
795 // never been called, and is reset to zero once OnBackendInitialized() is | 834 // never been called, and is reset to zero once OnBackendInitialized() is |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 // If |true|, there is setup UI visible so we should not start downloading | 935 // If |true|, there is setup UI visible so we should not start downloading |
897 // data types. | 936 // data types. |
898 bool setup_in_progress_; | 937 bool setup_in_progress_; |
899 | 938 |
900 // The set of currently enabled sync experiments. | 939 // The set of currently enabled sync experiments. |
901 syncer::Experiments current_experiments_; | 940 syncer::Experiments current_experiments_; |
902 | 941 |
903 // Factory the backend will use to build the SyncManager. | 942 // Factory the backend will use to build the SyncManager. |
904 syncer::SyncManagerFactory sync_manager_factory_; | 943 syncer::SyncManagerFactory sync_manager_factory_; |
905 | 944 |
| 945 // Holds the current invalidator state as updated by |
| 946 // OnInvalidatorStateChange(). Note that this is different from the |
| 947 // state known by |invalidator_registrar_| (See |
| 948 // UpdateInvalidatorState()). |
| 949 syncer::InvalidatorState invalidator_state_; |
| 950 |
| 951 // Dispatches invalidations to handlers. Set in Initialize() and |
| 952 // unset in Shutdown(). |
| 953 scoped_ptr<syncer::InvalidatorRegistrar> invalidator_registrar_; |
| 954 // Queues any acknowledgements received while the backend is uninitialized. |
| 955 AckHandleReplayQueue ack_replay_queue_; |
| 956 |
906 // Sync's internal debug info listener. Used to record datatype configuration | 957 // Sync's internal debug info listener. Used to record datatype configuration |
907 // and association information. | 958 // and association information. |
908 syncer::WeakHandle<syncer::DataTypeDebugInfoListener> debug_info_listener_; | 959 syncer::WeakHandle<syncer::DataTypeDebugInfoListener> debug_info_listener_; |
909 | 960 |
910 // Specifies whenever to use oauth2 access token or ClientLogin token in | 961 // Specifies whenever to use oauth2 access token or ClientLogin token in |
911 // communications with sync and xmpp servers. | 962 // communications with sync and xmpp servers. |
912 // TODO(pavely): Remove once android is converted to oauth2 tokens. | 963 // TODO(pavely): Remove once android is converted to oauth2 tokens. |
913 bool use_oauth2_token_; | 964 bool use_oauth2_token_; |
914 | 965 |
915 // ProfileSyncService needs to remember access token in order to invalidate it | 966 // ProfileSyncService needs to remember access token in order to invalidate it |
(...skipping 10 matching lines...) Expand all Loading... |
926 net::BackoffEntry request_access_token_backoff_; | 977 net::BackoffEntry request_access_token_backoff_; |
927 | 978 |
928 DISALLOW_COPY_AND_ASSIGN(ProfileSyncService); | 979 DISALLOW_COPY_AND_ASSIGN(ProfileSyncService); |
929 }; | 980 }; |
930 | 981 |
931 bool ShouldShowActionOnUI( | 982 bool ShouldShowActionOnUI( |
932 const syncer::SyncProtocolError& error); | 983 const syncer::SyncProtocolError& error); |
933 | 984 |
934 | 985 |
935 #endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_ | 986 #endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_ |
OLD | NEW |