blob: 14db31e14463401f462c64abe0a2dec4172296eb [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]72a31b42010-02-17 22:26:3317#include "chrome/browser/sync/glue/data_type_controller.h"
[email protected]e3e43d92010-02-26 22:02:3818#include "chrome/browser/sync/glue/data_type_manager.h"
[email protected]132c85652009-08-05 01:18:2719#include "chrome/browser/sync/glue/sync_backend_host.h"
[email protected]93d960ce2010-02-11 04:27:0020#include "chrome/browser/sync/notification_method.h"
[email protected]d80033e2009-10-16 10:32:0421#include "chrome/browser/sync/sync_setup_wizard.h"
[email protected]4aea04a2010-02-10 20:13:4322#include "chrome/browser/sync/syncable/model_type.h"
[email protected]eafb2182010-03-03 00:46:3123#include "chrome/browser/sync/unrecoverable_error_handler.h"
[email protected]c4c672a2010-03-23 21:47:0424#include "chrome/common/notification_observer.h"
25#include "chrome/common/notification_registrar.h"
[email protected]132c85652009-08-05 01:18:2726#include "googleurl/src/gurl.h"
27
[email protected]c4c672a2010-03-23 21:47:0428class NotificationDetails;
29class NotificationSource;
30class NotificationType;
[email protected]942690b132010-05-11 06:42:1431class Profile;
32class ProfileSyncFactory;
[email protected]c4c672a2010-03-23 21:47:0433
[email protected]132c85652009-08-05 01:18:2734// Various UI components such as the New Tab page can be driven by observing
35// the ProfileSyncService through this interface.
36class ProfileSyncServiceObserver {
37 public:
38 // When one of the following events occurs, OnStateChanged() is called.
39 // Observers should query the service to determine what happened.
40 // - We initialized successfully.
41 // - There was an authentication error and the user needs to reauthenticate.
42 // - The sync servers are unavailable at this time.
43 // - Credentials are now in flight for authentication.
44 virtual void OnStateChanged() = 0;
45 protected:
46 virtual ~ProfileSyncServiceObserver() { }
47};
48
49// ProfileSyncService is the layer between browser subsystems like bookmarks,
[email protected]a1c2aef62010-04-02 02:58:0550// and the sync backend. Each subsystem is logically thought of as being
51// a sync datatype.
52//
53// Individual datatypes can, at any point, be in a variety of stages of being
54// "enabled". Here are some specific terms for concepts used in this class:
55//
56// 'Registered' (feature suppression for a datatype)
57//
58// When a datatype is registered, the user has the option of syncing it.
59// The sync opt-in UI will show only registered types; a checkbox should
60// never be shown for an unregistered type, and nor should it ever be
61// synced.
62//
63// A datatype is considered registered once RegisterDataTypeController
64// has been called with that datatype's DataTypeController.
65//
66// 'Preferred' (user preferences and opt-out for a datatype)
67//
68// This means the user's opt-in or opt-out preference on a per-datatype
69// basis. The sync service will try to make active exactly these types.
70// If a user has opted out of syncing a particular datatype, it will
71// be registered, but not preferred.
72//
73// This state is controlled by the ConfigurePreferredDataTypes and
74// GetPreferredDataTypes. They are stored in the preferences system,
75// and persist; though if a datatype is not registered, it cannot
76// be a preferred datatype.
77//
78// 'Active' (run-time initialization of sync system for a datatype)
79//
80// An active datatype is a preferred datatype that is actively being
81// synchronized: the syncer has been instructed to querying the server
82// for this datatype, first-time merges have finished, and there is an
83// actively installed ChangeProcessor that listens for changes to this
84// datatype, propagating such changes into and out of the sync backend
85// as necessary.
86//
87// When a datatype is in the process of becoming active, it may be
88// in some intermediate state. Those finer-grained intermediate states
89// are differentiated by the DataTypeController state.
90//
[email protected]4aea04a2010-02-10 20:13:4391class ProfileSyncService : public browser_sync::SyncFrontend,
[email protected]c4c672a2010-03-23 21:47:0492 public browser_sync::UnrecoverableErrorHandler,
93 public NotificationObserver {
[email protected]132c85652009-08-05 01:18:2794 public:
95 typedef ProfileSyncServiceObserver Observer;
96 typedef browser_sync::SyncBackendHost::Status Status;
97
[email protected]1f97a112009-08-11 02:17:0698 enum SyncEventCodes {
99 MIN_SYNC_EVENT_CODE = 0,
100
101 // Events starting the sync service.
102 START_FROM_NTP = 1, // Sync was started from the ad in NTP
103 START_FROM_WRENCH = 2, // Sync was started from the Wrench menu.
104 START_FROM_OPTIONS = 3, // Sync was started from Wrench->Options.
[email protected]b220d13f2009-10-09 04:12:46105 START_FROM_BOOKMARK_MANAGER = 4, // Sync was started from Bookmark manager.
[email protected]1f97a112009-08-11 02:17:06106
[email protected]a7ab8332010-03-31 20:49:04107 // Events regarding cancellation of the signon process of sync.
[email protected]8ac2dc112009-10-01 23:19:13108 CANCEL_FROM_SIGNON_WITHOUT_AUTH = 10, // Cancelled before submitting
[email protected]1f97a112009-08-11 02:17:06109 // username and password.
110 CANCEL_DURING_SIGNON = 11, // Cancelled after auth.
[email protected]8c94d632010-06-25 22:38:00111 CANCEL_FROM_CHOOSE_DATA_TYPES = 12, // Cancelled before choosing data
112 // types and clicking OK.
[email protected]1f97a112009-08-11 02:17:06113 // Events resulting in the stoppage of sync service.
114 STOP_FROM_OPTIONS = 20, // Sync was stopped from Wrench->Options.
115
116 // Miscellaneous events caused by sync service.
[email protected]1f97a112009-08-11 02:17:06117
118 MAX_SYNC_EVENT_CODE
119 };
120
[email protected]f66bcdf2010-05-17 20:31:14121 // Default sync server URL.
122 static const char* kSyncServerUrl;
123 // Sync server URL for dev channel users
124 static const char* kDevServerUrl;
125
[email protected]e3e43d92010-02-26 22:02:38126 ProfileSyncService(ProfileSyncFactory* factory_,
127 Profile* profile,
128 bool bootstrap_sync_authentication);
[email protected]132c85652009-08-05 01:18:27129 virtual ~ProfileSyncService();
130
131 // Initializes the object. This should be called every time an object of this
132 // class is constructed.
133 void Initialize();
134
[email protected]4aea04a2010-02-10 20:13:43135 // Registers a data type controller with the sync service. This
136 // makes the data type controller available for use, it does not
137 // enable or activate the synchronization of the data type (see
138 // ActivateDataType). Takes ownership of the pointer.
139 void RegisterDataTypeController(
140 browser_sync::DataTypeController* data_type_controller);
141
[email protected]a7ab8332010-03-31 20:49:04142 // Fills state_map with a map of current data types that are possible to
143 // sync, as well as their states.
144 void GetDataTypeControllerStates(
145 browser_sync::DataTypeController::StateMap* state_map) const;
[email protected]4aea04a2010-02-10 20:13:43146
[email protected]132c85652009-08-05 01:18:27147 // Enables/disables sync for user.
148 virtual void EnableForUser();
149 virtual void DisableForUser();
150
151 // Whether sync is enabled by user or not.
[email protected]4772b072010-03-30 17:45:46152 virtual bool HasSyncSetupCompleted() const;
[email protected]48352c12009-08-15 01:19:11153 void SetSyncSetupCompleted();
[email protected]132c85652009-08-05 01:18:27154
[email protected]132c85652009-08-05 01:18:27155 // SyncFrontend implementation.
156 virtual void OnBackendInitialized();
157 virtual void OnSyncCycleCompleted();
158 virtual void OnAuthError();
[email protected]132c85652009-08-05 01:18:27159
160 // Called when a user enters credentials through UI.
161 virtual void OnUserSubmittedAuth(const std::string& username,
[email protected]1fc9b3f2009-11-12 21:22:09162 const std::string& password,
163 const std::string& captcha);
[email protected]132c85652009-08-05 01:18:27164
[email protected]af909492010-06-10 18:45:38165 // Called when a user chooses which data types to sync as part of the sync
166 // setup wizard. |sync_everything| represents whether they chose the
[email protected]8c94d632010-06-25 22:38:00167 // "keep everything synced" option; if true, |chosen_types| will be ignored
168 // and all data types will be synced. |sync_everything| means "sync all
169 // current and future data types."
[email protected]af909492010-06-10 18:45:38170 virtual void OnUserChoseDatatypes(bool sync_everything,
[email protected]8c94d632010-06-25 22:38:00171 const syncable::ModelTypeSet& chosen_types);
[email protected]af909492010-06-10 18:45:38172
[email protected]a7ab8332010-03-31 20:49:04173 // Called when a user cancels any setup dialog (login, etc).
[email protected]132c85652009-08-05 01:18:27174 virtual void OnUserCancelledDialog();
175
176 // Get various information for displaying in the user interface.
177 browser_sync::SyncBackendHost::StatusSummary QuerySyncStatusSummary();
178 browser_sync::SyncBackendHost::Status QueryDetailedSyncStatus();
179
[email protected]712257e2009-11-11 22:57:46180 const GoogleServiceAuthError& GetAuthError() const {
[email protected]132c85652009-08-05 01:18:27181 return last_auth_error_;
182 }
183
184 // Displays a dialog for the user to enter GAIA credentials and attempt
185 // re-authentication, and returns true if it actually opened the dialog.
186 // Returns false if a dialog is already showing, an auth attempt is in
187 // progress, the sync system is already authenticated, or some error
188 // occurred preventing the action. We make it the duty of ProfileSyncService
189 // to open the dialog to easily ensure only one is ever showing.
190 bool SetupInProgress() const {
[email protected]fc186ca2010-05-11 22:27:00191 return !HasSyncSetupCompleted() &&
192 (WizardIsVisible() || bootstrap_sync_authentication_);
[email protected]132c85652009-08-05 01:18:27193 }
[email protected]44b78f90a2009-10-15 17:32:20194 bool WizardIsVisible() const {
195 return wizard_.IsVisible();
196 }
[email protected]132c85652009-08-05 01:18:27197 void ShowLoginDialog();
198
[email protected]8c94d632010-06-25 22:38:00199 void ShowChooseDataTypes();
200
[email protected]132c85652009-08-05 01:18:27201 // Pretty-printed strings for a given StatusSummary.
202 static std::wstring BuildSyncStatusSummaryText(
203 const browser_sync::SyncBackendHost::StatusSummary& summary);
204
205 // Returns true if the SyncBackendHost has told us it's ready to accept
206 // changes.
207 // TODO(timsteele): What happens if the bookmark model is loaded, a change
208 // takes place, and the backend isn't initialized yet?
209 bool sync_initialized() const { return backend_initialized_; }
[email protected]fb42c982009-09-16 22:33:33210 bool unrecoverable_error_detected() const {
211 return unrecoverable_error_detected_;
212 }
[email protected]f6ec8b22010-06-08 23:40:06213 const std::string& unrecoverable_error_message() {
214 return unrecoverable_error_message_;
215 }
216 tracked_objects::Location unrecoverable_error_location() {
217 return unrecoverable_error_location_.get() ?
218 *unrecoverable_error_location_.get() : tracked_objects::Location();
219 }
[email protected]132c85652009-08-05 01:18:27220
221 bool UIShouldDepictAuthInProgress() const {
222 return is_auth_in_progress_;
223 }
224
225 // A timestamp marking the last time the service observed a transition from
226 // the SYNCING state to the READY state. Note that this does not reflect the
227 // last time we polled the server to see if there were any changes; the
228 // timestamp is only snapped when syncing takes place and we download or
229 // upload some bookmark entity.
230 const base::Time& last_synced_time() const { return last_synced_time_; }
231
232 // Returns a user-friendly string form of last synced time (in minutes).
233 std::wstring GetLastSyncedTimeString() const;
234
235 // Returns the authenticated username of the sync user, or empty if none
236 // exists. It will only exist if the authentication service provider (e.g
237 // GAIA) has confirmed the username is authentic.
238 virtual string16 GetAuthenticatedUsername() const;
239
240 const std::string& last_attempted_user_email() const {
241 return last_attempted_user_email_;
242 }
243
244 // The profile we are syncing for.
245 Profile* profile() { return profile_; }
246
247 // Adds/removes an observer. ProfileSyncService does not take ownership of
248 // the observer.
[email protected]4772b072010-03-30 17:45:46249 virtual void AddObserver(Observer* observer);
250 virtual void RemoveObserver(Observer* observer);
[email protected]132c85652009-08-05 01:18:27251
[email protected]1f97a112009-08-11 02:17:06252 // Record stats on various events.
253 static void SyncEvent(SyncEventCodes code);
254
[email protected]eec3bcfa2009-11-12 17:23:43255 // Returns whether sync is enabled. Sync can be enabled/disabled both
256 // at compile time (e.g., on a per-OS basis) or at run time (e.g.,
257 // command-line switches).
258 static bool IsSyncEnabled();
259
[email protected]db1df0f2009-08-18 22:36:05260 // UnrecoverableErrorHandler implementation.
[email protected]f6ec8b22010-06-08 23:40:06261 virtual void OnUnrecoverableError(
262 const tracked_objects::Location& from_here,
263 const std::string& message);
[email protected]db1df0f2009-08-18 22:36:05264
265 browser_sync::SyncBackendHost* backend() { return backend_.get(); }
266
[email protected]4aea04a2010-02-10 20:13:43267 virtual void ActivateDataType(
268 browser_sync::DataTypeController* data_type_controller,
269 browser_sync::ChangeProcessor* change_processor);
270 virtual void DeactivateDataType(
271 browser_sync::DataTypeController* data_type_controller,
272 browser_sync::ChangeProcessor* change_processor);
273
[email protected]c4c672a2010-03-23 21:47:04274 // NotificationObserver implementation.
275 virtual void Observe(NotificationType type,
276 const NotificationSource& source,
277 const NotificationDetails& details);
278
[email protected]a1c2aef62010-04-02 02:58:05279 // Changes which data types we're going to be syncing to |preferred_types|.
280 // If it is running, the DataTypeManager will be instructed to reconfigure
281 // the sync backend so that exactly these datatypes are actively synced. See
282 // class comment for more on what it means for a datatype to be Preferred.
283 virtual void ChangePreferredDataTypes(
284 const syncable::ModelTypeSet& preferred_types);
285
286 // Get the set of currently enabled data types (as chosen or configured by
287 // the user). See class comment for more on what it means for a datatype
288 // to be Preferred.
289 virtual void GetPreferredDataTypes(
290 syncable::ModelTypeSet* preferred_types) const;
291
292 // Gets the set of all data types that could be allowed (the set that
293 // should be advertised to the user). These will typically only change
294 // via a command-line option. See class comment for more on what it means
295 // for a datatype to be Registered.
296 virtual void GetRegisteredDataTypes(
297 syncable::ModelTypeSet* registered_types) const;
[email protected]a7ab8332010-03-31 20:49:04298
[email protected]ff0aa752010-06-10 22:40:52299 // Checks whether the Cryptographer is ready to encrypt and decrypt updates
300 // for sensitive data types.
301 virtual bool IsCryptographerReady() const;
302
[email protected]bb899b3f2010-06-23 21:35:32303 // Sets the Cryptographer's passphrase. This will check asynchronously whether
304 // the passphrase is valid and notify ProfileSyncServiceObservers via the
305 // NotificationService when the outcome is known.
306 virtual void SetPassphrase(const std::string& passphrase);
307
[email protected]132c85652009-08-05 01:18:27308 protected:
[email protected]82d9d2952010-05-15 00:36:16309 // Used by ProfileSyncServiceMock only.
310 //
311 // TODO(akalin): Separate this class out into an abstract
312 // ProfileSyncService interface and a ProfileSyncServiceImpl class
313 // so we don't need this hack anymore.
314 ProfileSyncService();
315
[email protected]7cef1c442010-07-07 17:05:22316 // Helper to install and configure a data type manager.
317 void ConfigureDataTypeManager();
[email protected]132c85652009-08-05 01:18:27318
[email protected]132c85652009-08-05 01:18:27319 // Returns whether processing changes is allowed. Check this before doing
320 // any model-modifying operations.
321 bool ShouldPushChanges();
322
323 // Starts up the backend sync components.
324 void StartUp();
325 // Shuts down the backend sync components.
326 // |sync_disabled| indicates if syncing is being disabled or not.
327 void Shutdown(bool sync_disabled);
328
[email protected]48352c12009-08-15 01:19:11329 // Methods to register and remove preferences.
330 void RegisterPreferences();
331 void ClearPreferences();
332
[email protected]9b3f2ae2010-01-29 01:56:00333 // Tests need to override this. If |delete_sync_data_folder| is true, then
334 // this method will delete all previous "Sync Data" folders. (useful if the
335 // folder is partial/corrupt)
336 virtual void InitializeBackend(bool delete_sync_data_folder);
[email protected]132c85652009-08-05 01:18:27337
[email protected]7cef1c442010-07-07 17:05:22338 const browser_sync::DataTypeController::TypeMap& data_type_controllers() {
339 return data_type_controllers_;
340 }
341
[email protected]132c85652009-08-05 01:18:27342 // We keep track of the last auth error observed so we can cover up the first
343 // "expected" auth failure from observers.
344 // TODO(timsteele): Same as expecting_first_run_auth_needed_event_. Remove
345 // this!
[email protected]712257e2009-11-11 22:57:46346 GoogleServiceAuthError last_auth_error_;
[email protected]132c85652009-08-05 01:18:27347
[email protected]7cef1c442010-07-07 17:05:22348 // Our asynchronous backend to communicate with sync components living on
349 // other threads.
350 scoped_ptr<browser_sync::SyncBackendHost> backend_;
351
[email protected]132c85652009-08-05 01:18:27352 // Cache of the last name the client attempted to authenticate.
353 std::string last_attempted_user_email_;
354
355 private:
[email protected]132c85652009-08-05 01:18:27356 friend class ProfileSyncServiceTest;
[email protected]65b3aa12010-02-22 22:49:00357 friend class ProfileSyncServicePreferenceTest;
[email protected]132c85652009-08-05 01:18:27358 friend class ProfileSyncServiceTestHarness;
[email protected]61b694a2010-06-10 10:40:46359 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceTest, InitialState);
360 FRIEND_TEST_ALL_PREFIXES(ProfileSyncServiceTest,
361 UnrecoverableErrorSuspendsService);
[email protected]132c85652009-08-05 01:18:27362
[email protected]132c85652009-08-05 01:18:27363 // Initializes the various settings from the command line.
364 void InitSettings();
365
[email protected]132c85652009-08-05 01:18:27366 // Sets the last synced time to the current time.
367 void UpdateLastSyncedTime();
368
[email protected]a1c2aef62010-04-02 02:58:05369 static const wchar_t* GetPrefNameForDataType(syncable::ModelType data_type);
370
[email protected]35e4c0d2009-12-01 00:39:53371 // When running inside Chrome OS, extract the LSID cookie from the cookie
372 // store to bootstrap the authentication process.
[email protected]72a31b42010-02-17 22:26:33373 virtual std::string GetLsidForAuthBootstraping();
374
[email protected]cb4c06e2009-08-22 00:24:39375 // Time at which we begin an attempt a GAIA authorization.
376 base::TimeTicks auth_start_time_;
377
378 // Time at which error UI is presented for the new tab page.
[email protected]1f97a112009-08-11 02:17:06379 base::TimeTicks auth_error_time_;
380
[email protected]e3e43d92010-02-26 22:02:38381 // Factory used to create various dependent objects.
382 ProfileSyncFactory* factory_;
383
[email protected]132c85652009-08-05 01:18:27384 // The profile whose data we are synchronizing.
385 Profile* profile_;
386
[email protected]72a31b42010-02-17 22:26:33387 // True if the profile sync service should attempt to use an LSID
388 // cookie for authentication. This is typically set to true in
389 // ChromiumOS since we want to use the system level authentication
390 // for sync.
391 bool bootstrap_sync_authentication_;
392
[email protected]132c85652009-08-05 01:18:27393 // TODO(ncarter): Put this in a profile, once there is UI for it.
394 // This specifies where to find the sync server.
395 GURL sync_service_url_;
396
[email protected]132c85652009-08-05 01:18:27397 // The last time we detected a successful transition from SYNCING state.
398 // Our backend notifies us whenever we should take a new snapshot.
399 base::Time last_synced_time_;
400
[email protected]4aea04a2010-02-10 20:13:43401 // List of available data type controllers.
[email protected]72a31b42010-02-17 22:26:33402 browser_sync::DataTypeController::TypeMap data_type_controllers_;
[email protected]db1df0f2009-08-18 22:36:05403
[email protected]132c85652009-08-05 01:18:27404 // Whether the SyncBackendHost has been initialized.
405 bool backend_initialized_;
406
407 // Set to true when the user first enables sync, and we are waiting for
408 // syncapi to give us the green light on providing credentials for the first
409 // time. It is set back to false as soon as we get this message, and is
410 // false all other times so we don't have to persist this value as it will
411 // get initialized to false.
412 // TODO(timsteele): Remove this by way of starting the wizard when enabling
413 // sync *before* initializing the backend. syncapi will need to change, but
414 // it means we don't have to wait for the first AuthError; if we ever get
415 // one, it is actually an error and this bool isn't needed.
416 bool expecting_first_run_auth_needed_event_;
417
418 // Various pieces of UI query this value to determine if they should show
419 // an "Authenticating.." type of message. We are the only central place
420 // all auth attempts funnel through, so it makes sense to provide this.
421 // As its name suggests, this should NOT be used for anything other than UI.
422 bool is_auth_in_progress_;
423
[email protected]44b78f90a2009-10-15 17:32:20424 SyncSetupWizard wizard_;
425
[email protected]132c85652009-08-05 01:18:27426 // True if an unrecoverable error (e.g. violation of an assumed invariant)
427 // occurred during syncer operation. This value should be checked before
428 // doing any work that might corrupt things further.
429 bool unrecoverable_error_detected_;
430
[email protected]f6ec8b22010-06-08 23:40:06431 // A message sent when an unrecoverable error occurred.
432 std::string unrecoverable_error_message_;
433 scoped_ptr<tracked_objects::Location> unrecoverable_error_location_;
434
[email protected]93d960ce2010-02-11 04:27:00435 // Which peer-to-peer notification method to use.
436 browser_sync::NotificationMethod notification_method_;
437
[email protected]e3e43d92010-02-26 22:02:38438 // Manages the start and stop of the various data types.
439 scoped_ptr<browser_sync::DataTypeManager> data_type_manager_;
440
[email protected]132c85652009-08-05 01:18:27441 ObserverList<Observer> observers_;
442
[email protected]c4c672a2010-03-23 21:47:04443 NotificationRegistrar registrar_;
444
[email protected]2be7bf22010-04-23 21:02:37445 ScopedRunnableMethodFactory<ProfileSyncService>
446 scoped_runnable_method_factory_;
447
[email protected]132c85652009-08-05 01:18:27448 DISALLOW_COPY_AND_ASSIGN(ProfileSyncService);
449};
450
451#endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_