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.
4
5#ifndef CHROME_BROWSER_SYNC_GLUE_AUTOFILL_DATA_TYPE_CONTROLLER_H__
6#define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_DATA_TYPE_CONTROLLER_H__
7#pragma once
8
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/synchronization/waitable_event.h"
14#include "chrome/browser/autofill/personal_data_manager.h"
15#include "chrome/browser/sync/profile_sync_factory.h"
16#include "chrome/browser/sync/profile_sync_service.h"
17#include "chrome/browser/sync/glue/data_type_controller.h"
18
19class Profile;
20class ProfileSyncFactory;
21class ProfileSyncService;
22
23namespace browser_sync {
24
25class AssociatorInterface;
26class ChangeProcessor;
27
28// A class that manages the startup and shutdown of autofill sync.
29class AutofillDataTypeController : public DataTypeController,
30                                   public NotificationObserver,
31                                   public PersonalDataManager::Observer {
32 public:
33  AutofillDataTypeController(
34      ProfileSyncFactory* profile_sync_factory,
35      Profile* profile,
36      ProfileSyncService* sync_service);
37  virtual ~AutofillDataTypeController();
38
39  // DataTypeController implementation
40  virtual void Start(StartCallback* start_callback);
41
42  virtual void Stop();
43
44  virtual bool enabled();
45
46  virtual syncable::ModelType type() const;
47
48  virtual browser_sync::ModelSafeGroup model_safe_group() const;
49
50  virtual std::string name() const;
51
52  virtual State state() const;
53
54  // UnrecoverableHandler implementation
55  virtual void OnUnrecoverableError(const tracked_objects::Location& from_here,
56                                    const std::string& message);
57
58  // NotificationObserver implementation.
59  virtual void Observe(NotificationType type,
60                       const NotificationSource& source,
61                       const NotificationDetails& details);
62
63  // PersonalDataManager::Observer implementation:
64  virtual void OnPersonalDataLoaded();
65
66 protected:
67  virtual ProfileSyncFactory::SyncComponents CreateSyncComponents(
68      ProfileSyncService* profile_sync_service,
69      WebDatabase* web_database,
70      PersonalDataManager* personal_data,
71      browser_sync::UnrecoverableErrorHandler* error_handler);
72  ProfileSyncFactory* profile_sync_factory_;
73
74 private:
75  void StartImpl();
76  void StartDone(StartResult result, State state);
77  void StartDoneImpl(StartResult result, State state,
78      const tracked_objects::Location& location);
79  void StopImpl();
80  void StartFailed(StartResult result);
81  void OnUnrecoverableErrorImpl(const tracked_objects::Location& from_here,
82                                const std::string& message);
83
84  // Second-half of "Start" implementation, called once personal data has
85  // loaded.
86  void ContinueStartAfterPersonalDataLoaded();
87
88  void set_state(State state) {
89    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
90    state_ = state;
91  }
92
93  Profile* profile_;
94  ProfileSyncService* sync_service_;
95  State state_;
96
97  PersonalDataManager* personal_data_;
98  scoped_refptr<WebDataService> web_data_service_;
99  scoped_ptr<AssociatorInterface> model_associator_;
100  scoped_ptr<ChangeProcessor> change_processor_;
101  scoped_ptr<StartCallback> start_callback_;
102
103  NotificationRegistrar notification_registrar_;
104
105  base::Lock abort_association_lock_;
106  bool abort_association_;
107  base::WaitableEvent abort_association_complete_;
108
109  // Barrier to ensure that the datatype has been stopped on the DB thread
110  // from the UI thread.
111  base::WaitableEvent datatype_stopped_;
112
113  DISALLOW_COPY_AND_ASSIGN(AutofillDataTypeController);
114};
115
116}  // namespace browser_sync
117
118#endif  // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_DATA_TYPE_CONTROLLER_H__
119