Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / sync_driver / data_type_controller.h
1 // Copyright 2014 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 COMPONENTS_SYNC_DRIVER_DATA_TYPE_CONTROLLER_H__
6 #define COMPONENTS_SYNC_DRIVER_DATA_TYPE_CONTROLLER_H__
7
8 #include <map>
9 #include <string>
10
11 #include "base/callback.h"
12 #include "base/location.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/ref_counted_delete_on_message_loop.h"
15 #include "base/sequenced_task_runner_helpers.h"
16 #include "components/sync_driver/data_type_error_handler.h"
17 #include "sync/api/sync_merge_result.h"
18 #include "sync/internal_api/public/base/model_type.h"
19 #include "sync/internal_api/public/engine/model_safe_worker.h"
20 #include "sync/internal_api/public/util/unrecoverable_error_handler.h"
21
22 namespace syncer {
23 class SyncError;
24 }
25
26 namespace browser_sync {
27
28 // Data type controllers need to be refcounted threadsafe, as they may
29 // need to run model associator or change processor on other threads.
30 class DataTypeController
31     : public base::RefCountedDeleteOnMessageLoop<DataTypeController>,
32       public DataTypeErrorHandler {
33  public:
34   enum State {
35     NOT_RUNNING,    // The controller has never been started or has
36                     // previously been stopped.  Must be in this state to start.
37     MODEL_STARTING, // The controller is waiting on dependent services
38                     // that need to be available before model
39                     // association.
40     MODEL_LOADED,   // The model has finished loading and can start
41                     // associating now.
42     ASSOCIATING,    // Model association is in progress.
43     RUNNING,        // The controller is running and the data type is
44                     // in sync with the cloud.
45     STOPPING,       // The controller is in the process of stopping
46                     // and is waiting for dependent services to stop.
47     DISABLED        // The controller was started but encountered an error
48                     // so it is disabled waiting for it to be stopped.
49   };
50
51   enum StartResult {
52     OK,                   // The data type has started normally.
53     OK_FIRST_RUN,         // Same as OK, but sent on first successful
54                           // start for this type for this user as
55                           // determined by cloud state.
56     ASSOCIATION_FAILED,   // An error occurred during model association.
57     ABORTED,              // Start was aborted by calling Stop().
58     UNRECOVERABLE_ERROR,  // An unrecoverable error occured.
59     NEEDS_CRYPTO,         // The data type cannot be started yet because it
60                           // depends on the cryptographer.
61     MAX_START_RESULT
62   };
63
64   typedef base::Callback<void(StartResult,
65                               const syncer::SyncMergeResult&,
66                               const syncer::SyncMergeResult&)> StartCallback;
67
68   typedef base::Callback<void(syncer::ModelType,
69                               syncer::SyncError)> ModelLoadCallback;
70
71   typedef std::map<syncer::ModelType,
72                    scoped_refptr<DataTypeController> > TypeMap;
73   typedef std::map<syncer::ModelType, DataTypeController::State> StateMap;
74
75   // Returns true if the start result should trigger an unrecoverable error.
76   // Public so unit tests can use this function as well.
77   static bool IsUnrecoverableResult(StartResult result);
78
79   // Returns true if the datatype started successfully.
80   static bool IsSuccessfulResult(StartResult result);
81
82   // Begins asynchronous operation of loading the model to get it ready for
83   // model association. Once the models are loaded the callback will be invoked
84   // with the result. If the models are already loaded it is safe to call the
85   // callback right away. Else the callback needs to be stored and called when
86   // the models are ready.
87   virtual void LoadModels(const ModelLoadCallback& model_load_callback) = 0;
88
89   // Will start a potentially asynchronous operation to perform the
90   // model association. Once the model association is done the callback will
91   // be invoked.
92   virtual void StartAssociating(const StartCallback& start_callback) = 0;
93
94   // Synchronously stops the data type. If StartAssociating has already been
95   // called but is not done yet it will be aborted. Similarly if LoadModels
96   // has not completed it will also be aborted.
97   virtual void Stop() = 0;
98
99   // Unique model type for this data type controller.
100   virtual syncer::ModelType type() const = 0;
101
102   // Name of this data type.  For logging purposes only.
103   virtual std::string name() const = 0;
104
105   // The model safe group of this data type.  This should reflect the
106   // thread that should be used to modify the data type's native
107   // model.
108   virtual syncer::ModelSafeGroup model_safe_group() const = 0;
109
110   // Current state of the data type controller.
111   virtual State state() const = 0;
112
113   // Partial implementation of DataTypeErrorHandler.
114   // This is thread safe.
115   virtual syncer::SyncError CreateAndUploadError(
116       const tracked_objects::Location& location,
117       const std::string& message,
118       syncer::ModelType type) OVERRIDE;
119
120  protected:
121   friend class base::RefCountedDeleteOnMessageLoop<DataTypeController>;
122   friend class base::DeleteHelper<DataTypeController>;
123
124   DataTypeController(scoped_refptr<base::MessageLoopProxy> ui_thread,
125                      const base::Closure& error_callback);
126
127   // If the DTC is waiting for models to load, once the models are
128   // loaded the datatype service will call this function on DTC to let
129   // us know that it is safe to start associating.
130   virtual void OnModelLoaded() = 0;
131
132   virtual ~DataTypeController();
133
134   // Handles the reporting of unrecoverable error. It records stuff in
135   // UMA and reports to breakpad.
136   // Virtual for testing purpose.
137   virtual void RecordUnrecoverableError(
138       const tracked_objects::Location& from_here,
139       const std::string& message);
140
141  private:
142   // The callback that will be invoked when an unrecoverable error occurs.
143   base::Closure error_callback_;
144 };
145
146 }  // namespace browser_sync
147
148 #endif  // COMPONENTS_SYNC_DRIVER_DATA_TYPE_CONTROLLER_H__