blob: e88d852449104edc250a2f57ad701e65c022232f [file] [log] [blame]
[email protected]66761b952010-06-25 21:30:381// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]132c85652009-08-05 01:18:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]132c85652009-08-05 01:18:275#ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_
6#define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_
7
8#include <string>
9#include <map>
[email protected]132c85652009-08-05 01:18:2710
11#include "base/basictypes.h"
[email protected]61b694a2010-06-10 10:40:4612#include "base/gtest_prod_util.h"
[email protected]132c85652009-08-05 01:18:2713#include "base/observer_list.h"
14#include "base/scoped_ptr.h"
[email protected]4aea04a2010-02-10 20:13:4315#include "base/time.h"
[email protected]712257e2009-11-11 22:57:4616#include "chrome/browser/google_service_auth_error.h"
[email protected]d3b98c82010-07-14 07:45:5917#include "chrome/browser/pref_member.h"
[email protected]72a31b42010-02-17 22:26:3318#include "chrome/browser/sync/glue/data_type_controller.h"
[email protected]e3e43d92010-02-26 22:02:3819#include "chrome/browser/sync/glue/data_type_manager.h"
[email protected]132c85652009-08-05 01:18:2720#include "chrome/browser/sync/glue/sync_backend_host.h"
[email protected]93d960ce2010-02-11 04:27:0021#include "chrome/browser/sync/notification_method.h"
[email protected]d3b98c82010-07-14 07:45:5922#include "chrome/browser/sync/profile_sync_service_observer.h"
[email protected]d80033e2009-10-16 10:32:0423#include "chrome/browser/sync/sync_setup_wizard.h"
[email protected]4aea04a2010-02-10 20:13:4324#include "chrome/browser/sync/syncable/model_type.h"
[email protected]eafb2182010-03-03 00:46:3125#include "chrome/browser/sync/unrecoverable_error_handler.h"
[email protected]c4c672a2010-03-23 21:47:0426#include "chrome/common/notification_observer.h"
27#include "chrome/common/notification_registrar.h"
[email protected]132c85652009-08-05 01:18:2728#include "googleurl/src/gurl.h"
29
[email protected]c4c672a2010-03-23 21:47:0430class NotificationDetails;
31class NotificationSource;
32class NotificationType;
[email protected]942690b132010-05-11 06:42:1433class Profile;
34class ProfileSyncFactory;
[email protected]c4c672a2010-03-23 21:47:0435
[email protected]132c85652009-08-05 01:18:2736// ProfileSyncService is the layer between browser subsystems like bookmarks,
[email protected]a1c2aef62010-04-02 02:58:0537// and the sync backend. Each subsystem is logically thought of as being
38// a sync datatype.
39//
40// Individual datatypes can, at any point, be in a variety of stages of being
41// "enabled". Here are some specific terms for concepts used in this class:
42//
43// 'Registered' (feature suppression for a datatype)
44//
45// When a datatype is registered, the user has the option of syncing it.
46// The sync opt-in UI will show only registered types; a checkbox should
47// never be shown for an unregistered type, and nor should it ever be
48// synced.
49//
50// A datatype is considered registered once RegisterDataTypeController
51// has been called with that datatype's DataTypeController.
52//
53// 'Preferred' (user preferences and opt-out for a datatype)
54//
55// This means the user's opt-in or opt-out preference on a per-datatype
56// basis. The sync service will try to make active exactly these types.
57// If a user has opted out of syncing a particular datatype, it will
58// be registered, but not preferred.
59//
60// This state is controlled by the ConfigurePreferredDataTypes and
61// GetPreferredDataTypes. They are stored in the preferences system,
62// and persist; though if a datatype is not registered, it cannot
63// be a preferred datatype.
64//
65// 'Active' (run-time initialization of sync system for a datatype)
66//
67// An active datatype is a preferred datatype that is actively being
68// synchronized: the syncer has been instructed to querying the server
69// for this datatype, first-time merges have finished, and there is an
70// actively installed ChangeProcessor that listens for changes to this
71// datatype, propagating such changes into and out of the sync backend
72// as necessary.
73//
74// When a datatype is in the process of becoming active, it may be
75// in some intermediate state. Those finer-grained intermediate states
76// are differentiated by the DataTypeController state.
77//
[email protected]4aea04a2010-02-10 20:13:4378class ProfileSyncService : public browser_sync::SyncFrontend,
[email protected]c4c672a2010-03-23 21:47:0479 public browser_sync::UnrecoverableErrorHandler,
80 public NotificationObserver {
[email protected]132c85652009-08-05 01:18:2781 public:
82 typedef ProfileSyncServiceObserver Observer;
83 typedef browser_sync::SyncBackendHost::Status Status;
84
[email protected]1f97a112009-08-11 02:17:0685 enum SyncEventCodes {
86 MIN_SYNC_EVENT_CODE = 0,
87
88 // Events starting the sync service.
89 START_FROM_NTP = 1, // Sync was started from the ad in NTP
90 START_FROM_WRENCH = 2, // Sync was started from the Wrench menu.
91 START_FROM_OPTIONS = 3, // Sync was started from Wrench->Options.
[email protected]b220d13f2009-10-09 04:12:4692 START_FROM_BOOKMARK_MANAGER = 4, // Sync was started from Bookmark manager.
[email protected]1f97a112009-08-11 02:17:0693
[email protected]a7ab8332010-03-31 20:49:0494 // Events regarding cancellation of the signon process of sync.
[email protected]8ac2dc112009-10-01 23:19:1395 CANCEL_FROM_SIGNON_WITHOUT_AUTH = 10, // Cancelled before submitting
[email protected]1f97a112009-08-11 02:17:0696 // username and password.
97 CANCEL_DURING_SIGNON = 11, // Cancelled after auth.
[email protected]8c94d632010-06-25 22:38:0098 CANCEL_FROM_CHOOSE_DATA_TYPES = 12, // Cancelled before choosing data
99 // types and clicking OK.
[email protected]1f97a112009-08-11 02:17:06100 // Events resulting in the stoppage of sync service.
101 STOP_FROM_OPTIONS = 20, // Sync was stopped from Wrench->Options.
102
103 // Miscellaneous events caused by sync service.
[email protected]1f97a112009-08-11 02:17:06104
105 MAX_SYNC_EVENT_CODE
106 };
107
[email protected]f66bcdf2010-05-17 20:31:14108 // Default sync server URL.
109 static const char* kSyncServerUrl;
110 // Sync server URL for dev channel users
111 static const char* kDevServerUrl;
112
[email protected]e3e43d92010-02-26 22:02:38113 ProfileSyncService(ProfileSyncFactory* factory_,
114 Profile* profile,
115 bool bootstrap_sync_authentication);
[email protected]132c85652009-08-05 01:18:27116 virtual ~ProfileSyncService();
117
118 // Initializes the object. This should be called every time an object of this
119 // class is constructed.
120 void Initialize();
121
[email protected]4aea04a2010-02-10 20:13:43122 // Registers a data type controller with the sync service. This
123 // makes the data type controller available for use, it does not
124 // enable or activate the synchronization of the data type (see
125 // ActivateDataType). Takes ownership of the pointer.
126 void RegisterDataTypeController(
127 browser_sync::DataTypeController* data_type_controller);
128
[email protected]a7ab8332010-03-31 20:49:04129 // Fills state_map with a map of current data types that are possible to
130 // sync, as well as their states.
131 void GetDataTypeControllerStates(
132 browser_sync::DataTypeController::StateMap* state_map) const;
[email protected]4aea04a2010-02-10 20:13:43133
[email protected]132c85652009-08-05 01:18:27134 // Enables/disables sync for user.
135 virtual void EnableForUser();
136 virtual void DisableForUser();
137
138 // Whether sync is enabled by user or not.
[email protected]4772b072010-03-30 17:45:46139 virtual bool HasSyncSetupCompleted() const;
[email protected]48352c12009-08-15 01:19:11140 void SetSyncSetupCompleted();
[email protected]132c85652009-08-05 01:18:27141
[email protected]132c85652009-08-05 01:18:27142 // SyncFrontend implementation.
143 virtual void OnBackendInitialized();
144 virtual void OnSyncCycleCompleted();
145 virtual void OnAuthError();
[email protected]7f8abea2010-07-14 01:31:36146 virtual void OnStopSyncingPermanently();
[email protected]132c85652009-08-05 01:18:27147
148 // Called when a user enters credentials through UI.
149 virtual void OnUserSubmittedAuth(const std::string& username,
[email protected]1fc9b3f2009-11-12 21:22:09150 const std::string& password,
151 const std::string& captcha);
[email protected]132c85652009-08-05 01:18:27152
[email protected]af909492010-06-10 18:45:38153 // Called when a user chooses which data types to sync as part of the sync
154 // setup wizard. |sync_everything| represents whether they chose the
[email protected]8c94d632010-06-25 22:38:00155 // "keep everything synced" option; if true, |chosen_types| will be ignored
156 // and all data types will be synced. |sync_everything| means "sync all
157 // current and future data types."
[email protected]af909492010-06-10 18:45:38158 virtual void OnUserChoseDatatypes(bool sync_everything,
[email protected]8c94d632010-06-25 22:38:00159 const syncable::ModelTypeSet& chosen_types);
[email protected]af909492010-06-10 18:45:38160
[email protected]a7ab8332010-03-31 20:49:04161 // Called when a user cancels any setup dialog (login, etc).
[email protected]132c85652009-08-05 01:18:27162 virtual void OnUserCancelledDialog();
163
164 // Get various information for displaying in the user interface.
165 browser_sync::SyncBackendHost::StatusSummary QuerySyncStatusSummary();
166 browser_sync::SyncBackendHost::Status QueryDetailedSyncStatus();
167
[email protected]712257e2009-11-11 22:57:46168 const GoogleServiceAuthError& GetAuthError() const {
[email protected]132c85652009-08-05 01:18:27169 return last_auth_error_;
170 }
171
172 // Displays a dialog for the user to enter GAIA credentials and attempt
173 // re-authentication, and returns true if it actually opened the dialog.
174 // Returns false if a dialog is already showing, an auth attempt is in
175 // progress, the sync system is already authenticated, or some error
176 // occurred preventing the action. We make it the duty of ProfileSyncService
177 // to open the dialog to easily ensure only one is ever showing.
178 bool SetupInProgress() const {
[email protected]fc186ca2010-05-11 22:27:00179 return !HasSyncSetupCompleted() &&
180 (WizardIsVisible() || bootstrap_sync_authentication_);
[email protected]132c85652009-08-05 01:18:27181 }
[email protected]44b78f90a2009-10-15 17:32:20182 bool WizardIsVisible() const {
183 return wizard_.IsVisible();
184 }
[email protected]132c85652009-08-05 01:18:27185 void ShowLoginDialog();
186
[email protected]8c94d632010-06-25 22:38:00187 void ShowChooseDataTypes();
188
[email protected]132c85652009-08-05 01:18:27189 // Pretty-printed strings for a given StatusSummary.
190 static std::wstring BuildSyncStatusSummaryText(
191 const browser_sync::SyncBackendHost::StatusSummary& summary);
192
193 // Returns true if the SyncBackendHost has told us it's ready to accept
194 // changes.
195 // TODO(timsteele): What happens if the bookmark model is loaded, a change
196 // takes place, and the backend isn't initialized yet?
197 bool sync_initialized() const { return backend_initialized_; }
[email protected]fb42c982009-09-16 22:33:33198 bool unrecoverable_error_detected() const {
199 return unrecoverable_error_detected_;
200 }
[email protected]f6ec8b22010-06-08 23:40:06201 const std::string& unrecoverable_error_message() {
202 return unrecoverable_error_message_;
203 }
204 tracked_objects::Location unrecoverable_error_location() {
205 return unrecoverable_error_location_.get() ?
206 *unrecoverable_error_location_.get() : tracked_objects::Location();
207 }
[email protected]132c85652009-08-05 01:18:27208
209 bool UIShouldDepictAuthInProgress() const {
210 return is_auth_in_progress_;
211 }
212
213 // A timestamp marking the last time the service observed a transition from
214 // the SYNCING state to the READY state. Note that this does not reflect the
215 // last time we polled the server to see if there were any changes; the
216 // timestamp is only snapped when syncing takes place and we download or
217 // upload some bookmark entity.
218 const base::Time& last_synced_time() const { return last_synced_time_; }
219
220 // Returns a user-friendly string form of last synced time (in minutes).
221 std::wstring GetLastSyncedTimeString() const;
222
223 // Returns the authenticated username of the sync user, or empty if none
224 // exists. It will only exist if the authentication service provider (e.g
225 // GAIA) has confirmed the username is authentic.
226 virtual string16 GetAuthenticatedUsername() const;
227
228 const std::string& last_attempted_user_email() const {
229 return last_attempted_user_email_;
230 }
231
232 // The profile we are syncing for.
233 Profile* profile() { return profile_; }
234
235 // Adds/removes an observer. ProfileSyncService does not take ownership of
236 // the observer.
[email protected]4772b072010-03-30 17:45:46237 virtual void AddObserver(Observer* observer);
238 virtual void RemoveObserver(Observer* observer);
[email protected]132c85652009-08-05 01:18:27239
[email protected]1f97a112009-08-11 02:17:06240 // Record stats on various events.
241 static void SyncEvent(SyncEventCodes code);
242
[email protected]eec3bcfa2009-11-12 17:23:43243 // Returns whether sync is enabled. Sync can be enabled/disabled both
244 // at compile time (e.g., on a per-OS basis) or at run time (e.g.,
245 // command-line switches).
246 static bool IsSyncEnabled();
247
[email protected]d3b98c82010-07-14 07:45:59248 // Retuns whether sync is managed, i.e. controlled by configuration
249 // management. If so, the user is not allowed to configure sync.
250 bool IsManaged();
251
[email protected]db1df0f2009-08-18 22:36:05252 // UnrecoverableErrorHandler implementation.
[email protected]f6ec8b22010-06-08 23:40:06253 virtual void OnUnrecoverableError(
254 const tracked_objects::Location& from_here,
255 const std::string& message);
[email protected]db1df0f2009-08-18 22:36:05256
257 browser_sync::SyncBackendHost* backend() { return backend_.get(); }
258
[email protected]4aea04a2010-02-10 20:13:43259 virtual void ActivateDataType(
260 browser_sync::DataTypeController* data_type_controller,
261 browser_sync::ChangeProcessor* change_processor);
262 virtual void DeactivateDataType(
263 browser_sync::DataTypeController* data_type_controller,
264 browser_sync::ChangeProcessor* change_processor);
265
[email protected]c4c672a2010-03-23 21:47:04266 // NotificationObserver implementation.
267 virtual void Observe(NotificationType type,
268 const NotificationSource& source,
269 const NotificationDetails& details);
270
[email protected]a1c2aef62010-04-02 02:58:05271 // Changes which data types we're going to be syncing to |preferred_types|.
272 // If it is running, the DataTypeManager will be instructed to reconfigure
273 // the sync backend so that exactly these datatypes are actively synced. See
274 // class comment for more on what it means for a datatype to be Preferred.
275 virtual void ChangePreferredDataTypes(
276 const syncable::ModelTypeSet& preferred_types);
277
278 // Get the set of currently enabled data types (as chosen or configured by
279 // the user). See class comment for more on what it means for a datatype
280 // to be Preferred.
281 virtual void GetPreferredDataTypes(
282 syncable::ModelTypeSet* preferred_types) const;
283
284 // Gets the set of all data types that could be allowed (the set that
285 // should be advertised to the user). These will typically only change
286 // via a command-line option. See class comment for more on what it means
287 // for a datatype to be Registered.
288 virtual void GetRegisteredDataTypes(
289 syncable::ModelTypeSet* registered_types) const;
[email protected]a7ab8332010-03-31 20:49:04290
[email protected]ff0aa752010-06-10 22:40:52291 // Checks whether the Cryptographer is ready to encrypt and decrypt updates
292 // for sensitive data types.
293 virtual bool IsCryptographerReady() const;
294
[email protected]bb899b3f2010-06-23 21:35:32295 // Sets the Cryptographer's passphrase. This will check asynchronously whether
296 // the passphrase is valid and notify ProfileSyncServiceObservers via the
297 // NotificationService when the outcome is known.
298 virtual void SetPassphrase(const std::string& passphrase);
299
[email protected]132c85652009-08-05 01:18:27300 protected:
[email protected]82d9d2952010-05-15 00:36:16301 // Used by ProfileSyncServiceMock only.
302 //
303 // TODO(akalin): Separate this class out into an abstract
304 // ProfileSyncService interface and a ProfileSyncServiceImpl class
305 // so we don't need this hack anymore.
306 ProfileSyncService();
307
[email protected]7cef1c442010-07-07 17:05:22308 // Helper to install and configure a data type manager.
309 void ConfigureDataTypeManager();
[email protected]132c85652009-08-05 01:18:27310
[email protected]132c85652009-08-05 01:18:27311 // Returns whether processing changes is allowed. Check this before doing
312 // any model-modifying operations.
313 bool ShouldPushChanges();
314
315 // Starts up the backend sync components.
316 void StartUp();
317 // Shuts down the backend sync components.
318 // |sync_disabled| indicates if syncing is being disabled or not.
319 void Shutdown(bool sync_disabled);
320
[email protected]48352c12009-08-15 01:19:11321 // Methods to register and remove preferences.
322 void RegisterPreferences();
323 void ClearPreferences();
324
[email protected]9b3f2ae2010-01-29 01:56:00325 // Tests need to override this. If |delete_sync_data_folder| is true, then
326 // this method will delete all previous "Sync Data" folders. (useful if the
327 // folder is partial/corrupt)
328 virtual void InitializeBackend(bool delete_sync_data_folder);
[email protected]132c85652009-08-05 01:18:27329
[email protected]7cef1c442010-07-07 17:05:22330 const browser_sync::DataTypeController::TypeMap& data_type_controllers() {
331 return data_type_controllers_;
332 }
333
[email protected]132c85652009-08-05 01:18:27334 // We keep track of the last auth error observed so we can cover up the first
335 // "expected" auth failure from observers.
336 // TODO(timsteele): Same as expecting_first_run_auth_needed_event_. Remove
337 // this!
[email protected]712257e2009-11-11 22:57:46338 GoogleServiceAuthError last_auth_error_;
[email protected]132c85652009-08-05 01:18:27339
[email protected]7cef1c442010-07-07 17:05:22340 // Our asynchronous backend to communicate with sync components living on
341 // other threads.
342 scoped_ptr<browser_sync::SyncBackendHost> backend_;
343
[email protected]132c85652009-08-05 01:18:27344 // Cache of the last name the client attempted to authenticate.
345 std::string last_attempted_user_email_;
346
347 private:
[email protected]132c85652009-08-05 01:18:27348 friend class ProfileSyncServiceTest;
[email protected]65b3aa12010-02-22 22:49:00349 friend class ProfileSyncServicePreferenceTest;
[email protected]132c85652009-08-05 01:18:27350 friend class ProfileSyncServiceTestHarness;
[email protected]61b694a2010-06-10 10:40:46351 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceTest, InitialState);
352 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceTest,
353 UnrecoverableErrorSuspendsService);
[email protected]132c85652009-08-05 01:18:27354
[email protected]132c85652009-08-05 01:18:27355 // Initializes the various settings from the command line.
356 void InitSettings();
357
[email protected]132c85652009-08-05 01:18:27358 // Sets the last synced time to the current time.
359 void UpdateLastSyncedTime();
360
[email protected]a1c2aef62010-04-02 02:58:05361 static const wchar_t* GetPrefNameForDataType(syncable::ModelType data_type);
362
[email protected]cb4c06e2009-08-22 00:24:39363 // Time at which we begin an attempt a GAIA authorization.
364 base::TimeTicks auth_start_time_;
365
366 // Time at which error UI is presented for the new tab page.
[email protected]1f97a112009-08-11 02:17:06367 base::TimeTicks auth_error_time_;
368
[email protected]e3e43d92010-02-26 22:02:38369 // Factory used to create various dependent objects.
370 ProfileSyncFactory* factory_;
371
[email protected]132c85652009-08-05 01:18:27372 // The profile whose data we are synchronizing.
373 Profile* profile_;
374
[email protected]72a31b42010-02-17 22:26:33375 // True if the profile sync service should attempt to use an LSID
376 // cookie for authentication. This is typically set to true in
377 // ChromiumOS since we want to use the system level authentication
378 // for sync.
379 bool bootstrap_sync_authentication_;
380
[email protected]132c85652009-08-05 01:18:27381 // TODO(ncarter): Put this in a profile, once there is UI for it.
382 // This specifies where to find the sync server.
383 GURL sync_service_url_;
384
[email protected]132c85652009-08-05 01:18:27385 // The last time we detected a successful transition from SYNCING state.
386 // Our backend notifies us whenever we should take a new snapshot.
387 base::Time last_synced_time_;
388
[email protected]4aea04a2010-02-10 20:13:43389 // List of available data type controllers.
[email protected]72a31b42010-02-17 22:26:33390 browser_sync::DataTypeController::TypeMap data_type_controllers_;
[email protected]db1df0f2009-08-18 22:36:05391
[email protected]132c85652009-08-05 01:18:27392 // Whether the SyncBackendHost has been initialized.
393 bool backend_initialized_;
394
395 // Set to true when the user first enables sync, and we are waiting for
396 // syncapi to give us the green light on providing credentials for the first
397 // time. It is set back to false as soon as we get this message, and is
398 // false all other times so we don't have to persist this value as it will
399 // get initialized to false.
400 // TODO(timsteele): Remove this by way of starting the wizard when enabling
401 // sync *before* initializing the backend. syncapi will need to change, but
402 // it means we don't have to wait for the first AuthError; if we ever get
403 // one, it is actually an error and this bool isn't needed.
404 bool expecting_first_run_auth_needed_event_;
405
406 // Various pieces of UI query this value to determine if they should show
407 // an "Authenticating.." type of message. We are the only central place
408 // all auth attempts funnel through, so it makes sense to provide this.
409 // As its name suggests, this should NOT be used for anything other than UI.
410 bool is_auth_in_progress_;
411
[email protected]44b78f90a2009-10-15 17:32:20412 SyncSetupWizard wizard_;
413
[email protected]132c85652009-08-05 01:18:27414 // True if an unrecoverable error (e.g. violation of an assumed invariant)
415 // occurred during syncer operation. This value should be checked before
416 // doing any work that might corrupt things further.
417 bool unrecoverable_error_detected_;
418
[email protected]f6ec8b22010-06-08 23:40:06419 // A message sent when an unrecoverable error occurred.
420 std::string unrecoverable_error_message_;
421 scoped_ptr<tracked_objects::Location> unrecoverable_error_location_;
422
[email protected]93d960ce2010-02-11 04:27:00423 // Which peer-to-peer notification method to use.
424 browser_sync::NotificationMethod notification_method_;
425
[email protected]e3e43d92010-02-26 22:02:38426 // Manages the start and stop of the various data types.
427 scoped_ptr<browser_sync::DataTypeManager> data_type_manager_;
428
[email protected]132c85652009-08-05 01:18:27429 ObserverList<Observer> observers_;
430
[email protected]c4c672a2010-03-23 21:47:04431 NotificationRegistrar registrar_;
432
[email protected]2be7bf22010-04-23 21:02:37433 ScopedRunnableMethodFactory<ProfileSyncService>
434 scoped_runnable_method_factory_;
435
[email protected]d3b98c82010-07-14 07:45:59436 // The preference that controls whether sync is under control by configuration
437 // management.
438 BooleanPrefMember pref_sync_managed_;
439
[email protected]132c85652009-08-05 01:18:27440 DISALLOW_COPY_AND_ASSIGN(ProfileSyncService);
441};
442
443#endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_