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.
5 #ifndef COMPONENTS_SYNC_DRIVER_DATA_TYPE_CONTROLLER_H__
6 #define COMPONENTS_SYNC_DRIVER_DATA_TYPE_CONTROLLER_H__
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"
26 namespace browser_sync {
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 {
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
40 MODEL_LOADED, // The model has finished loading and can start
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.
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.
64 typedef base::Callback<void(StartResult,
65 const syncer::SyncMergeResult&,
66 const syncer::SyncMergeResult&)> StartCallback;
68 typedef base::Callback<void(syncer::ModelType,
69 syncer::SyncError)> ModelLoadCallback;
71 typedef std::map<syncer::ModelType,
72 scoped_refptr<DataTypeController> > TypeMap;
73 typedef std::map<syncer::ModelType, DataTypeController::State> StateMap;
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);
79 // Returns true if the datatype started successfully.
80 static bool IsSuccessfulResult(StartResult result);
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;
89 // Will start a potentially asynchronous operation to perform the
90 // model association. Once the model association is done the callback will
92 virtual void StartAssociating(const StartCallback& start_callback) = 0;
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;
99 // Unique model type for this data type controller.
100 virtual syncer::ModelType type() const = 0;
102 // Name of this data type. For logging purposes only.
103 virtual std::string name() const = 0;
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
108 virtual syncer::ModelSafeGroup model_safe_group() const = 0;
110 // Current state of the data type controller.
111 virtual State state() const = 0;
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;
121 friend class base::RefCountedDeleteOnMessageLoop<DataTypeController>;
122 friend class base::DeleteHelper<DataTypeController>;
124 DataTypeController(scoped_refptr<base::MessageLoopProxy> ui_thread,
125 const base::Closure& error_callback);
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;
132 virtual ~DataTypeController();
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);
142 // The callback that will be invoked when an unrecoverable error occurs.
143 base::Closure error_callback_;
146 } // namespace browser_sync
148 #endif // COMPONENTS_SYNC_DRIVER_DATA_TYPE_CONTROLLER_H__