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