profile_sync_service_harness.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_
6#define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/time.h"
13#include "chrome/browser/sync/profile_sync_service.h"
14
15using browser_sync::sessions::SyncSessionSnapshot;
16
17class Profile;
18
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
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
23// several clients to get to the same state.
24class ProfileSyncServiceHarness : public ProfileSyncServiceObserver {
25 public:
26  ProfileSyncServiceHarness(Profile* profile,
27                            const std::string& username,
28                            const std::string& password,
29                            int id);
30
31  virtual ~ProfileSyncServiceHarness() {}
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
44  // Creates a ProfileSyncService for the profile passed at construction and
45  // enables sync for all available datatypes. Returns true only after sync has
46  // been fully initialized and authenticated, and we are ready to process
47  // changes.
48  bool SetupSync();
49
50  // Same as the above method, but enables sync only for the datatypes contained
51  // in |synced_datatypes|.
52  bool SetupSync(const syncable::ModelTypeSet& synced_datatypes);
53
54  // ProfileSyncServiceObserver implementation.
55  virtual void OnStateChanged();
56
57  // Blocks the caller until this harness has completed a single sync cycle
58  // since the previous one.  Returns true if a sync cycle has completed.
59  bool AwaitSyncCycleCompletion(const std::string& reason);
60
61  // Blocks the caller until this harness has observed that the sync engine
62  // has "synced" up to at least the specified local timestamp.
63  bool WaitUntilTimestampIsAtLeast(int64 timestamp, const std::string& reason);
64
65  // Calling this acts as a barrier and blocks the caller until |this| and
66  // |partner| have both completed a sync cycle.  When calling this method,
67  // the |partner| should be the passive responder who responds to the actions
68  // of |this|.  This method relies upon the synchronization of callbacks
69  // from the message queue. Returns true if two sync cycles have completed.
70  // Note: Use this method when exactly one client makes local change(s), and
71  // exactly one client is waiting to receive those changes.
72  bool AwaitMutualSyncCycleCompletion(ProfileSyncServiceHarness* partner);
73
74  // Blocks the caller until |this| completes its ongoing sync cycle and every
75  // other client in |partners| has a timestamp that is greater than or equal to
76  // the timestamp of |this|. Note: Use this method when exactly one client
77  // makes local change(s), and more than one client is waiting to receive those
78  // changes.
79  bool AwaitGroupSyncCycleCompletion(
80      std::vector<ProfileSyncServiceHarness*>& partners);
81
82  // Blocks the caller until every client in |clients| completes its ongoing
83  // sync cycle and all the clients' timestamps match.  Note: Use this method
84  // when more than one client makes local change(s), and more than one client
85  // is waiting to receive those changes.
86  static bool AwaitQuiescence(
87      std::vector<ProfileSyncServiceHarness*>& clients);
88
89  // If a SetPassphrase call has been issued with a valid passphrase, this
90  // will wait until the Cryptographer broadcasts SYNC_PASSPHRASE_ACCEPTED.
91  bool AwaitPassphraseAccepted();
92
93  // Returns the ProfileSyncService member of the the sync client.
94  ProfileSyncService* service() { return service_; }
95
96  // Returns the status of the ProfileSyncService member of the the sync client.
97  ProfileSyncService::Status GetStatus();
98
99  // See ProfileSyncService::ShouldPushChanges().
100  bool ServiceIsPushingChanges() { return service_->ShouldPushChanges(); }
101
102  // Enables sync for a particular sync datatype.
103  void EnableSyncForDatatype(syncable::ModelType datatype);
104
105  // Disables sync for a particular sync datatype.
106  void DisableSyncForDatatype(syncable::ModelType datatype);
107
108  // Enables sync for all sync datatypes.
109  void EnableSyncForAllDatatypes();
110
111  // Disables sync for all sync datatypes.
112  void DisableSyncForAllDatatypes();
113
114  // Returns a snapshot of the current sync session.
115  const SyncSessionSnapshot* GetLastSessionSnapshot() const;
116
117 private:
118  friend class StateChangeTimeoutEvent;
119
120  enum WaitState {
121    // The sync client has just been initialized.
122    INITIAL_WAIT_STATE = 0,
123
124    // The sync client awaits the OnBackendInitialized() callback.
125    WAITING_FOR_ON_BACKEND_INITIALIZED,
126
127    // Waiting for a passphrase to be required.
128    WAITING_FOR_PASSPHRASE_REQUIRED,
129
130    // Waiting for a set passphrase to be accepted by the cryptographer.
131    WAITING_FOR_PASSPHRASE_ACCEPTED,
132
133    // The sync client is waiting for the first sync cycle to complete.
134    WAITING_FOR_INITIAL_SYNC,
135
136    // The sync client is waiting for an ongoing sync cycle to complete.
137    WAITING_FOR_SYNC_TO_FINISH,
138
139    // The sync client anticipates incoming updates leading to a new sync cycle.
140    WAITING_FOR_UPDATES,
141
142    // The sync client cannot reach the server.
143    SERVER_UNREACHABLE,
144
145    // The sync client is fully synced and there are no pending updates.
146    FULLY_SYNCED,
147
148    // Syncing is disabled for the client.
149    SYNC_DISABLED,
150
151    NUMBER_OF_STATES,
152  };
153
154  // Called from the observer when the current wait state has been completed.
155  void SignalStateCompleteWithNextState(WaitState next_state);
156
157  // Indicates that the operation being waited on is complete.
158  void SignalStateComplete();
159
160  // Finite state machine for controlling state.  Returns true only if a state
161  // change has taken place.
162  bool RunStateChangeMachine();
163
164  // Returns true if a status change took place, false on timeout.
165  bool AwaitStatusChangeWithTimeout(int timeout_milliseconds,
166                                    const std::string& reason);
167
168  // Returns true if the sync client has no unsynced items.
169  bool IsSynced();
170
171  // Logs message with relevant info about client's sync state (if available).
172  void LogClientInfo(std::string message);
173
174  // Updates |last_timestamp_| with the timestamp of the current sync session.
175  // Returns the new value of |last_timestamp_|.
176  int64 GetUpdatedTimestamp();
177
178  WaitState wait_state_;
179
180  Profile* profile_;
181  ProfileSyncService* service_;
182
183  // This value tracks the max sync timestamp (e.g. synced-to revision) inside
184  // the sync engine.  It gets updated when a sync cycle ends and the session
185  // snapshot implies syncing is "done".
186  int64 last_timestamp_;
187
188  // The minimum value of the 'max_local_timestamp' member of a
189  // SyncSessionSnapshot we need to wait to observe in OnStateChanged when told
190  // to WaitUntilTimestampIsAtLeast(...).
191  int64 min_timestamp_needed_;
192
193  // Credentials used for GAIA authentication.
194  std::string username_;
195  std::string password_;
196
197  // Client ID, used for logging purposes.
198  int id_;
199
200  DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness);
201};
202
203#endif  // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_HARNESS_H_
204