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_DATA_TYPE_CONTROLLER_H__
6#define CHROME_BROWSER_SYNC_GLUE_DATA_TYPE_CONTROLLER_H__
7#pragma once
8
9#include <string>
10#include <map>
11
12#include "base/callback.h"
13#include "base/tracked.h"
14#include "chrome/browser/sync/engine/model_safe_worker.h"
15#include "chrome/browser/sync/syncable/model_type.h"
16#include "chrome/browser/sync/unrecoverable_error_handler.h"
17#include "content/browser/browser_thread.h"
18
19namespace browser_sync {
20
21// Data type controllers need to be refcounted threadsafe, as they may
22// need to run model associator or change processor on other threads.
23class DataTypeController
24    : public base::RefCountedThreadSafe<DataTypeController,
25                                        BrowserThread::DeleteOnUIThread>,
26      public UnrecoverableErrorHandler {
27 public:
28  enum State {
29    NOT_RUNNING,    // The controller has never been started or has
30                    // previously been stopped.  Must be in this state to start.
31    MODEL_STARTING, // The controller is waiting on dependent services
32                    // that need to be available before model
33                    // association.
34    ASSOCIATING,    // Model association is in progress.
35    RUNNING,        // The controller is running and the data type is
36                    // in sync with the cloud.
37    STOPPING        // The controller is in the process of stopping
38                    // and is waiting for dependent services to stop.
39  };
40
41  enum StartResult {
42    OK,                   // The data type has started normally.
43    OK_FIRST_RUN,         // Same as OK, but sent on first successful
44                          // start for this type for this user as
45                          // determined by cloud state.
46    BUSY,                 // Start() was called while already in progress.
47    NOT_ENABLED,          // This data type is not enabled for the current user.
48    ASSOCIATION_FAILED,   // An error occurred during model association.
49    ABORTED,              // Start was aborted by calling Stop().
50    UNRECOVERABLE_ERROR,  // An unrecoverable error occured.
51    NEEDS_CRYPTO,         // The data type cannot be started yet because it
52                          // depends on the cryptographer.
53    MAX_START_RESULT
54  };
55
56  typedef Callback2<StartResult,
57      const tracked_objects::Location&>::Type StartCallback;
58
59  typedef std::map<syncable::ModelType,
60                   scoped_refptr<DataTypeController> > TypeMap;
61  typedef std::map<syncable::ModelType, DataTypeController::State> StateMap;
62
63  // Begins asynchronous start up of this data type.  Start up will
64  // wait for all other dependent services to be available, then
65  // proceed with model association and then change processor
66  // activation.  Upon completion, the start_callback will be invoked
67  // on the UI thread.  See the StartResult enum above for details on the
68  // possible start results.
69  virtual void Start(StartCallback* start_callback) = 0;
70
71  // Synchronously stops the data type.  If called after Start() is
72  // called but before the start callback is called, the start is
73  // aborted and the start callback is invoked with the ABORTED start
74  // result.
75  virtual void Stop() = 0;
76
77  // Unique model type for this data type controller.
78  virtual syncable::ModelType type() const = 0;
79
80  // Name of this data type.  For logging purposes only.
81  virtual std::string name() const = 0;
82
83  // The model safe group of this data type.  This should reflect the
84  // thread that should be used to modify the data type's native
85  // model.
86  virtual browser_sync::ModelSafeGroup model_safe_group() const = 0;
87
88  // Current state of the data type controller.
89  virtual State state() const = 0;
90
91 protected:
92  friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
93  friend class DeleteTask<DataTypeController>;
94  friend class ShutdownTask;
95
96  virtual ~DataTypeController() {}
97};
98
99}  // namespace browser_sync
100
101#endif  // CHROME_BROWSER_SYNC_GLUE_DATA_TYPE_CONTROLLER_H__
102