| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_HARNESS_H_ | 5 #ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_ |
| 6 #define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_ | 6 #define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "chrome/browser/sync/profile_sync_service.h" | 13 #include "chrome/browser/sync/profile_sync_service.h" |
| 14 | 14 |
| 15 using browser_sync::sessions::SyncSessionSnapshot; | 15 using browser_sync::sessions::SyncSessionSnapshot; |
| 16 | 16 |
| 17 class Profile; | 17 class Profile; |
| 18 | 18 |
| 19 // An instance of this class is basically our notion of a "sync client" for | 19 // An instance of this class is basically our notion of a "sync client" for |
| 20 // automation purposes. It harnesses the ProfileSyncService member of the | 20 // automation purposes. It harnesses the ProfileSyncService member of the |
| 21 // profile passed to it on construction and automates certain things like setup | 21 // profile passed to it on construction and automates certain things like setup |
| 22 // and authentication. It provides ways to "wait" adequate periods of time for | 22 // and authentication. It provides ways to "wait" adequate periods of time for |
| 23 // several clients to get to the same state. In order to use this class for | 23 // several clients to get to the same state. |
| 24 // automation, derived classes must implement 2 methods: SignalStateComplete() | |
| 25 // and AwaitStatusChange(). | |
| 26 class ProfileSyncServiceHarness : public ProfileSyncServiceObserver { | 24 class ProfileSyncServiceHarness : public ProfileSyncServiceObserver { |
| 27 public: | 25 public: |
| 28 ProfileSyncServiceHarness(Profile* p, | 26 ProfileSyncServiceHarness(Profile* profile, |
| 29 const std::string& username, | 27 const std::string& username, |
| 30 const std::string& password, | 28 const std::string& password, |
| 31 int id); | 29 int id); |
| 32 | 30 |
| 33 virtual ~ProfileSyncServiceHarness() {} | 31 virtual ~ProfileSyncServiceHarness() {} |
| 34 | 32 |
| 33 // Creates a ProfileSyncServiceHarness object and attaches it to |profile|, a. |
| 34 // profile that is assumed to have been signed into sync in the past. Caller |
| 35 // takes ownership. |
| 36 static ProfileSyncServiceHarness* CreateAndAttach(Profile* profile); |
| 37 |
| 38 // Sets the GAIA credentials with which to sign in to sync. |
| 39 void SetCredentials(const std::string& username, const std::string& password); |
| 40 |
| 41 // Returns true if sync has been enabled on |profile_|. |
| 42 bool IsSyncAlreadySetup(); |
| 43 |
| 35 // Creates a ProfileSyncService for the profile passed at construction and | 44 // Creates a ProfileSyncService for the profile passed at construction and |
| 36 // enables sync for all available datatypes. Returns true only after sync has | 45 // enables sync for all available datatypes. Returns true only after sync has |
| 37 // been fully initialized and authenticated, and we are ready to process | 46 // been fully initialized and authenticated, and we are ready to process |
| 38 // changes. | 47 // changes. |
| 39 bool SetupSync(); | 48 bool SetupSync(); |
| 40 | 49 |
| 41 // Same as the above method, but enables sync only for the datatypes contained | 50 // Same as the above method, but enables sync only for the datatypes contained |
| 42 // in |synced_datatypes|. | 51 // in |synced_datatypes|. |
| 43 bool SetupSync(const syncable::ModelTypeSet& synced_datatypes); | 52 bool SetupSync(const syncable::ModelTypeSet& synced_datatypes); |
| 44 | 53 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 // Disables sync for all sync datatypes. | 111 // Disables sync for all sync datatypes. |
| 103 void DisableSyncForAllDatatypes(); | 112 void DisableSyncForAllDatatypes(); |
| 104 | 113 |
| 105 // Returns a snapshot of the current sync session. | 114 // Returns a snapshot of the current sync session. |
| 106 const SyncSessionSnapshot* GetLastSessionSnapshot() const; | 115 const SyncSessionSnapshot* GetLastSessionSnapshot() const; |
| 107 | 116 |
| 108 private: | 117 private: |
| 109 friend class StateChangeTimeoutEvent; | 118 friend class StateChangeTimeoutEvent; |
| 110 | 119 |
| 111 enum WaitState { | 120 enum WaitState { |
| 121 // The sync client has just been initialized. |
| 122 INITIAL_WAIT_STATE = 0, |
| 123 |
| 112 // The sync client awaits the OnBackendInitialized() callback. | 124 // The sync client awaits the OnBackendInitialized() callback. |
| 113 WAITING_FOR_ON_BACKEND_INITIALIZED = 0, | 125 WAITING_FOR_ON_BACKEND_INITIALIZED, |
| 114 | 126 |
| 115 // The sync client is waiting for the first sync cycle to complete. | 127 // The sync client is waiting for the first sync cycle to complete. |
| 116 WAITING_FOR_INITIAL_SYNC, | 128 WAITING_FOR_INITIAL_SYNC, |
| 117 | 129 |
| 118 // The sync client is waiting for an ongoing sync cycle to complete. | 130 // The sync client is waiting for an ongoing sync cycle to complete. |
| 119 WAITING_FOR_SYNC_TO_FINISH, | 131 WAITING_FOR_SYNC_TO_FINISH, |
| 120 | 132 |
| 121 // The sync client anticipates incoming updates leading to a new sync cycle. | 133 // The sync client anticipates incoming updates leading to a new sync cycle. |
| 122 WAITING_FOR_UPDATES, | 134 WAITING_FOR_UPDATES, |
| 123 | 135 |
| 124 // The sync client cannot reach the server. | 136 // The sync client cannot reach the server. |
| 125 SERVER_UNREACHABLE, | 137 SERVER_UNREACHABLE, |
| 126 | 138 |
| 127 // The sync client is fully synced and there are no pending updates. | 139 // The sync client is fully synced and there are no pending updates. |
| 128 FULLY_SYNCED, | 140 FULLY_SYNCED, |
| 129 | 141 |
| 130 // Waiting for a set passphrase to be accepted by the cryptographer. | 142 // Waiting for a set passphrase to be accepted by the cryptographer. |
| 131 WAITING_FOR_PASSPHRASE_ACCEPTED, | 143 WAITING_FOR_PASSPHRASE_ACCEPTED, |
| 132 | 144 |
| 133 // Syncing is disabled for the client. | 145 // Syncing is disabled for the client. |
| 134 SYNC_DISABLED, | 146 SYNC_DISABLED, |
| 135 | 147 |
| 136 NUMBER_OF_STATES, | 148 NUMBER_OF_STATES, |
| 137 }; | 149 }; |
| 138 | 150 |
| 139 // Called from the observer when the current wait state has been completed. | 151 // Called from the observer when the current wait state has been completed. |
| 140 void SignalStateCompleteWithNextState(WaitState next_state); | 152 void SignalStateCompleteWithNextState(WaitState next_state); |
| 141 | 153 |
| 142 // Indicates that the operation being waited on is complete. Derived classes | 154 // Indicates that the operation being waited on is complete. |
| 143 // may implement this either by quitting the UI message loop, or by signaling | 155 void SignalStateComplete(); |
| 144 // a WaitableEvent object. | |
| 145 virtual void SignalStateComplete() = 0; | |
| 146 | 156 |
| 147 // Finite state machine for controlling state. Returns true only if a state | 157 // Finite state machine for controlling state. Returns true only if a state |
| 148 // change has taken place. | 158 // change has taken place. |
| 149 bool RunStateChangeMachine(); | 159 bool RunStateChangeMachine(); |
| 150 | 160 |
| 151 // Returns true if a status change took place, false on timeout. | 161 // Returns true if a status change took place, false on timeout. |
| 152 bool AwaitStatusChangeWithTimeout(int timeout_milliseconds, | 162 bool AwaitStatusChangeWithTimeout(int timeout_milliseconds, |
| 153 const std::string& reason); | 163 const std::string& reason); |
| 154 | 164 |
| 155 // Waits until the sync client's status changes. Derived classes may implement | |
| 156 // this either by running the UI message loop, or by waiting on a | |
| 157 // WaitableEvent object. | |
| 158 virtual void AwaitStatusChange() = 0; | |
| 159 | |
| 160 // Returns true if the sync client has no unsynced items. | 165 // Returns true if the sync client has no unsynced items. |
| 161 bool IsSynced(); | 166 bool IsSynced(); |
| 162 | 167 |
| 163 // Logs message with relevant info about client's sync state (if available). | 168 // Logs message with relevant info about client's sync state (if available). |
| 164 void LogClientInfo(std::string message); | 169 void LogClientInfo(std::string message); |
| 165 | 170 |
| 171 // Updates |last_timestamp_| with the timestamp of the current sync session. |
| 172 // Returns the new value of |last_timestamp_|. |
| 173 int64 GetUpdatedTimestamp(); |
| 174 |
| 166 WaitState wait_state_; | 175 WaitState wait_state_; |
| 167 | 176 |
| 168 Profile* profile_; | 177 Profile* profile_; |
| 169 ProfileSyncService* service_; | 178 ProfileSyncService* service_; |
| 170 | 179 |
| 171 // Updates |last_timestamp_| with the timestamp of the current sync session. | |
| 172 // Returns the new value of |last_timestamp_|. | |
| 173 int64 GetUpdatedTimestamp(); | |
| 174 | |
| 175 // This value tracks the max sync timestamp (e.g. synced-to revision) inside | 180 // This value tracks the max sync timestamp (e.g. synced-to revision) inside |
| 176 // the sync engine. It gets updated when a sync cycle ends and the session | 181 // the sync engine. It gets updated when a sync cycle ends and the session |
| 177 // snapshot implies syncing is "done". | 182 // snapshot implies syncing is "done". |
| 178 int64 last_timestamp_; | 183 int64 last_timestamp_; |
| 179 | 184 |
| 180 // The minimum value of the 'max_local_timestamp' member of a | 185 // The minimum value of the 'max_local_timestamp' member of a |
| 181 // SyncSessionSnapshot we need to wait to observe in OnStateChanged when told | 186 // SyncSessionSnapshot we need to wait to observe in OnStateChanged when told |
| 182 // to WaitUntilTimestampIsAtLeast(...). | 187 // to WaitUntilTimestampIsAtLeast(...). |
| 183 int64 min_timestamp_needed_; | 188 int64 min_timestamp_needed_; |
| 184 | 189 |
| 185 // Credentials used for GAIA authentication. | 190 // Credentials used for GAIA authentication. |
| 186 std::string username_; | 191 std::string username_; |
| 187 std::string password_; | 192 std::string password_; |
| 188 | 193 |
| 189 // Client ID, used for logging purposes. | 194 // Client ID, used for logging purposes. |
| 190 int id_; | 195 int id_; |
| 191 | 196 |
| 192 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness); | 197 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness); |
| 193 }; | 198 }; |
| 194 | 199 |
| 195 #endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_ | 200 #endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_ |
| OLD | NEW |