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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_
6 #define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_
12 #include "base/basictypes.h"
13 #include "chrome/browser/sync/profile_sync_service.h"
14 #include "chrome/browser/sync/profile_sync_service_observer.h"
15 #include "chrome/browser/sync/syncable/model_type.h"
19 namespace browser_sync
{
21 struct SyncSessionSnapshot
;
25 // An instance of this class is basically our notion of a "sync client" for
26 // automation purposes. It harnesses the ProfileSyncService member of the
27 // profile passed to it on construction and automates certain things like setup
28 // and authentication. It provides ways to "wait" adequate periods of time for
29 // several clients to get to the same state.
30 class ProfileSyncServiceHarness
: public ProfileSyncServiceObserver
{
32 ProfileSyncServiceHarness(Profile
* profile
,
33 const std::string
& username
,
34 const std::string
& password
);
36 virtual ~ProfileSyncServiceHarness() {}
38 // Creates a ProfileSyncServiceHarness object and attaches it to |profile|, a
39 // profile that is assumed to have been signed into sync in the past. Caller
41 static ProfileSyncServiceHarness
* CreateAndAttach(Profile
* profile
);
43 // Sets the GAIA credentials with which to sign in to sync.
44 void SetCredentials(const std::string
& username
, const std::string
& password
);
46 // Returns true if sync has been enabled on |profile_|.
47 bool IsSyncAlreadySetup();
49 // Creates a ProfileSyncService for the profile passed at construction and
50 // enables sync for all available datatypes. Returns true only after sync has
51 // been fully initialized and authenticated, and we are ready to process
55 // Same as the above method, but enables sync only for the datatypes contained
56 // in |synced_datatypes|.
57 bool SetupSync(const syncable::ModelTypeSet
& synced_datatypes
);
59 // ProfileSyncServiceObserver implementation.
60 virtual void OnStateChanged();
62 // Blocks the caller until the sync backend host associated with this harness
63 // has been initialized. Returns true if the wait was successful.
64 bool AwaitBackendInitialized();
66 // Blocks the caller until the datatype manager is configured and sync has
67 // been initialized (for example, after a browser restart). Returns true if
68 // the wait was successful.
69 bool AwaitSyncRestart();
71 // Blocks the caller until this harness has completed a single sync cycle
72 // since the previous one. Returns true if a sync cycle has completed.
73 bool AwaitSyncCycleCompletion(const std::string
& reason
);
75 // Blocks the caller until this harness has observed that the sync engine
76 // has downloaded all the changes seen by the |partner| harness's client.
77 bool WaitUntilTimestampMatches(
78 ProfileSyncServiceHarness
* partner
, const std::string
& reason
);
80 // Calling this acts as a barrier and blocks the caller until |this| and
81 // |partner| have both completed a sync cycle. When calling this method,
82 // the |partner| should be the passive responder who responds to the actions
83 // of |this|. This method relies upon the synchronization of callbacks
84 // from the message queue. Returns true if two sync cycles have completed.
85 // Note: Use this method when exactly one client makes local change(s), and
86 // exactly one client is waiting to receive those changes.
87 bool AwaitMutualSyncCycleCompletion(ProfileSyncServiceHarness
* partner
);
89 // Blocks the caller until |this| completes its ongoing sync cycle and every
90 // other client in |partners| have achieved identical download progresses.
91 // Note: Use this method when exactly one client makes local change(s),
92 // and more than one client is waiting to receive those changes.
93 bool AwaitGroupSyncCycleCompletion(
94 std::vector
<ProfileSyncServiceHarness
*>& partners
);
96 // Blocks the caller until every client in |clients| completes its ongoing
97 // sync cycle and all the clients' timestamps match. Note: Use this method
98 // when more than one client makes local change(s), and more than one client
99 // is waiting to receive those changes.
100 static bool AwaitQuiescence(
101 std::vector
<ProfileSyncServiceHarness
*>& clients
);
103 // Blocks the caller until |service_| indicates that a passphrase is required.
104 bool AwaitPassphraseRequired();
106 // Blocks the caller until |service_| indicates that the passphrase set by
107 // calling SetPassphrase has been accepted.
108 bool AwaitPassphraseAccepted();
110 // Returns the ProfileSyncService member of the the sync client.
111 ProfileSyncService
* service() { return service_
; }
113 // Returns the status of the ProfileSyncService member of the the sync client.
114 ProfileSyncService::Status
GetStatus();
116 // See ProfileSyncService::ShouldPushChanges().
117 bool ServiceIsPushingChanges() { return service_
->ShouldPushChanges(); }
119 // Enables sync for a particular sync datatype. Returns true on success.
120 bool EnableSyncForDatatype(syncable::ModelType datatype
);
122 // Disables sync for a particular sync datatype. Returns true on success.
123 bool DisableSyncForDatatype(syncable::ModelType datatype
);
125 // Enables sync for all sync datatypes. Returns true on success.
126 bool EnableSyncForAllDatatypes();
128 // Disables sync for all sync datatypes. Returns true on success.
129 bool DisableSyncForAllDatatypes();
131 // Returns a snapshot of the current sync session.
132 const browser_sync::sessions::SyncSessionSnapshot
*
133 GetLastSessionSnapshot() const;
135 // Encrypt the datatype |type|. This method will block while the sync backend
136 // host performs the encryption or a timeout is reached.
138 // returns: True if |type| was encrypted and we are fully synced.
139 // False if we timed out.
140 bool EnableEncryptionForType(syncable::ModelType type
);
142 // Wait until |type| is encrypted or we time out.
144 // returns: True if |type| is currently encrypted and we are fully synced.
145 // False if we timed out.
146 bool WaitForTypeEncryption(syncable::ModelType type
);
148 // Check if |type| is encrypted.
149 bool IsTypeEncrypted(syncable::ModelType type
);
152 friend class StateChangeTimeoutEvent
;
155 // The sync client has just been initialized.
156 INITIAL_WAIT_STATE
= 0,
158 // The sync client awaits the OnBackendInitialized() callback.
159 WAITING_FOR_ON_BACKEND_INITIALIZED
,
161 // The sync client is waiting for the first sync cycle to complete.
162 WAITING_FOR_INITIAL_SYNC
,
164 // The sync client is waiting for an ongoing sync cycle to complete.
165 WAITING_FOR_SYNC_TO_FINISH
,
167 // The sync client anticipates incoming updates leading to a new sync cycle.
170 // The sync client is waiting for a passphrase to be required by the
172 WAITING_FOR_PASSPHRASE_REQUIRED
,
174 // The sync client is waiting for its passphrase to be accepted by the
176 WAITING_FOR_PASSPHRASE_ACCEPTED
,
178 // The sync client anticipates encryption of new datatypes.
179 WAITING_FOR_ENCRYPTION
,
181 // The sync client is waiting for the datatype manager to be configured and
182 // for sync to be fully initialized. Used after a browser restart, where a
183 // full sync cycle is not expected to occur.
184 WAITING_FOR_SYNC_CONFIGURATION
,
186 // The sync client needs a passphrase in order to decrypt data.
187 SET_PASSPHRASE_FAILED
,
189 // The sync client cannot reach the server.
192 // The sync client is fully synced and there are no pending updates.
195 // Syncing is disabled for the client.
201 // Called from the observer when the current wait state has been completed.
202 void SignalStateCompleteWithNextState(WaitState next_state
);
204 // Indicates that the operation being waited on is complete.
205 void SignalStateComplete();
207 // Finite state machine for controlling state. Returns true only if a state
208 // change has taken place.
209 bool RunStateChangeMachine();
211 // Returns true if a status change took place, false on timeout.
212 bool AwaitStatusChangeWithTimeout(int timeout_milliseconds
,
213 const std::string
& reason
);
215 // Returns true if the sync client has no unsynced items.
218 // Returns true if this client has downloaded all the items that the
220 bool MatchesOtherClient(ProfileSyncServiceHarness
* partner
);
222 // Returns a string with relevant info about client's sync state (if
223 // available). Useful for logging.
224 std::string
GetClientInfo();
226 // Gets the current progress indicator of the current sync session
227 // for a particular datatype.
228 std::string
GetUpdatedTimestamp(syncable::ModelType model_type
);
230 // Gets detailed status from |service_| in pretty-printable form.
231 std::string
GetServiceStatus();
233 // When in WAITING_FOR_ENCRYPTION state, we check to see if this type is now
234 // encrypted to determine if we're done.
235 syncable::ModelType waiting_for_encryption_type_
;
237 // The WaitState in which the sync client currently is. Helps determine what
238 // action to take when RunStateChangeMachine() is called.
239 WaitState wait_state_
;
241 // Sync profile associated with this sync client.
244 // ProfileSyncService object associated with |profile_|.
245 ProfileSyncService
* service_
;
247 // The harness of the client whose update progress marker we're expecting
249 ProfileSyncServiceHarness
* timestamp_match_partner_
;
251 // Credentials used for GAIA authentication.
252 std::string username_
;
253 std::string password_
;
256 const std::string profile_debug_name_
;
258 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness
);
261 #endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_