OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_SYNC_DRIVER_SHARED_CHANGE_PROCESSOR_H_ | 5 #ifndef COMPONENTS_SYNC_DRIVER_SHARED_CHANGE_PROCESSOR_H_ |
6 #define COMPONENTS_SYNC_DRIVER_SHARED_CHANGE_PROCESSOR_H_ | 6 #define COMPONENTS_SYNC_DRIVER_SHARED_CHANGE_PROCESSOR_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "components/sync/api/data_type_error_handler.h" | 17 #include "components/sync/api/data_type_error_handler.h" |
18 #include "components/sync/api/sync_change_processor.h" | 18 #include "components/sync/api/sync_change_processor.h" |
19 #include "components/sync/api/sync_data.h" | 19 #include "components/sync/api/sync_data.h" |
20 #include "components/sync/api/sync_error.h" | 20 #include "components/sync/api/sync_error.h" |
21 #include "components/sync/api/sync_error_factory.h" | 21 #include "components/sync/api/sync_error_factory.h" |
22 #include "components/sync/api/sync_merge_result.h" | 22 #include "components/sync/api/sync_merge_result.h" |
23 #include "components/sync/driver/data_type_controller.h" | 23 #include "components/sync/driver/data_type_controller.h" |
24 #include "components/sync/engine/model_safe_worker.h" | 24 #include "components/sync/engine/model_safe_worker.h" |
25 | 25 |
26 namespace syncer { | 26 namespace syncer { |
| 27 class SyncableService; |
| 28 struct UserShare; |
| 29 } // namespace syncer |
| 30 |
| 31 namespace sync_driver { |
27 | 32 |
28 class ChangeProcessor; | 33 class ChangeProcessor; |
29 class GenericChangeProcessor; | 34 class GenericChangeProcessor; |
30 class GenericChangeProcessorFactory; | 35 class GenericChangeProcessorFactory; |
31 class SyncClient; | 36 class SyncClient; |
32 class SyncableService; | |
33 struct UserShare; | |
34 | 37 |
35 // A ref-counted wrapper around a GenericChangeProcessor for use with datatypes | 38 // A ref-counted wrapper around a GenericChangeProcessor for use with datatypes |
36 // that don't live on the UI thread. | 39 // that don't live on the UI thread. |
37 // | 40 // |
38 // We need to make it refcounted as the ownership transfer from the | 41 // We need to make it refcounted as the ownership transfer from the |
39 // DataTypeController is dependent on threading, and hence racy. The | 42 // DataTypeController is dependent on threading, and hence racy. The |
40 // SharedChangeProcessor should be created on the UI thread, but should only be | 43 // SharedChangeProcessor should be created on the UI thread, but should only be |
41 // connected and used on the same thread as the datatype it interacts with. | 44 // connected and used on the same thread as the datatype it interacts with. |
42 // | 45 // |
43 // The only thread-safe method is Disconnect, which will disconnect from the | 46 // The only thread-safe method is Disconnect, which will disconnect from the |
44 // generic change processor, letting us shut down the syncer/datatype without | 47 // generic change processor, letting us shut down the syncer/datatype without |
45 // waiting for non-UI threads. | 48 // waiting for non-UI threads. |
46 // | 49 // |
47 // Note: since we control the work being done while holding the lock, we ensure | 50 // Note: since we control the work being done while holding the lock, we ensure |
48 // no I/O or other intensive work is done while blocking the UI thread (all | 51 // no I/O or other intensive work is done while blocking the UI thread (all |
49 // the work is in-memory sync interactions). | 52 // the work is in-memory sync interactions). |
50 // | 53 // |
51 // We use virtual methods so that we can use mock's in testing. | 54 // We use virtual methods so that we can use mock's in testing. |
52 class SharedChangeProcessor | 55 class SharedChangeProcessor |
53 : public base::RefCountedThreadSafe<SharedChangeProcessor> { | 56 : public base::RefCountedThreadSafe<SharedChangeProcessor> { |
54 public: | 57 public: |
55 typedef base::Callback<void(DataTypeController::ConfigureResult start_result, | 58 typedef base::Callback<void( |
56 const SyncMergeResult& local_merge_result, | 59 DataTypeController::ConfigureResult start_result, |
57 const SyncMergeResult& syncer_merge_result)> | 60 const syncer::SyncMergeResult& local_merge_result, |
| 61 const syncer::SyncMergeResult& syncer_merge_result)> |
58 StartDoneCallback; | 62 StartDoneCallback; |
59 | 63 |
60 // Create an uninitialized SharedChangeProcessor. | 64 // Create an uninitialized SharedChangeProcessor. |
61 explicit SharedChangeProcessor(ModelType type); | 65 explicit SharedChangeProcessor(syncer::ModelType type); |
62 | 66 |
63 void StartAssociation(StartDoneCallback start_done, | 67 void StartAssociation( |
64 SyncClient* const sync_client, | 68 StartDoneCallback start_done, |
65 UserShare* user_share, | 69 SyncClient* const sync_client, |
66 std::unique_ptr<DataTypeErrorHandler> error_handler); | 70 syncer::UserShare* user_share, |
| 71 std::unique_ptr<syncer::DataTypeErrorHandler> error_handler); |
67 | 72 |
68 // Connect to the Syncer and prepare to handle changes for |type|. Will | 73 // Connect to the Syncer and prepare to handle changes for |type|. Will |
69 // create and store a new GenericChangeProcessor and return a weak pointer to | 74 // create and store a new GenericChangeProcessor and return a weak pointer to |
70 // the SyncableService associated with |type|. | 75 // the syncer::SyncableService associated with |type|. |
71 // Note: If this SharedChangeProcessor has been disconnected, or the | 76 // Note: If this SharedChangeProcessor has been disconnected, or the |
72 // SyncableService was not alive, will return a null weak pointer. | 77 // syncer::SyncableService was not alive, will return a null weak pointer. |
73 virtual base::WeakPtr<SyncableService> Connect( | 78 virtual base::WeakPtr<syncer::SyncableService> Connect( |
74 SyncClient* sync_client, | 79 SyncClient* sync_client, |
75 GenericChangeProcessorFactory* processor_factory, | 80 GenericChangeProcessorFactory* processor_factory, |
76 UserShare* user_share, | 81 syncer::UserShare* user_share, |
77 std::unique_ptr<DataTypeErrorHandler> error_handler, | 82 std::unique_ptr<syncer::DataTypeErrorHandler> error_handler, |
78 const base::WeakPtr<SyncMergeResult>& merge_result); | 83 const base::WeakPtr<syncer::SyncMergeResult>& merge_result); |
79 | 84 |
80 // Disconnects from the generic change processor. May be called from any | 85 // Disconnects from the generic change processor. May be called from any |
81 // thread. After this, all attempts to interact with the change processor by | 86 // thread. After this, all attempts to interact with the change processor by |
82 // |local_service_| are dropped and return errors. The syncer will be safe to | 87 // |local_service_| are dropped and return errors. The syncer will be safe to |
83 // shut down from the point of view of this datatype. | 88 // shut down from the point of view of this datatype. |
84 // Note: Once disconnected, you cannot reconnect without creating a new | 89 // Note: Once disconnected, you cannot reconnect without creating a new |
85 // SharedChangeProcessor. | 90 // SharedChangeProcessor. |
86 // Returns: true if we were previously succesfully connected, false if we were | 91 // Returns: true if we were previously succesfully connected, false if we were |
87 // already disconnected. | 92 // already disconnected. |
88 virtual bool Disconnect(); | 93 virtual bool Disconnect(); |
89 | 94 |
90 // GenericChangeProcessor stubs (with disconnect support). | 95 // GenericChangeProcessor stubs (with disconnect support). |
91 // Should only be called on the same thread the datatype resides. | 96 // Should only be called on the same thread the datatype resides. |
92 virtual int GetSyncCount(); | 97 virtual int GetSyncCount(); |
93 virtual SyncError ProcessSyncChanges( | 98 virtual syncer::SyncError ProcessSyncChanges( |
94 const tracked_objects::Location& from_here, | 99 const tracked_objects::Location& from_here, |
95 const SyncChangeList& change_list); | 100 const syncer::SyncChangeList& change_list); |
96 virtual SyncDataList GetAllSyncData(ModelType type) const; | 101 virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const; |
97 virtual SyncError GetAllSyncDataReturnError(ModelType type, | 102 virtual syncer::SyncError GetAllSyncDataReturnError( |
98 SyncDataList* data) const; | 103 syncer::ModelType type, |
99 virtual SyncError UpdateDataTypeContext( | 104 syncer::SyncDataList* data) const; |
100 ModelType type, | 105 virtual syncer::SyncError UpdateDataTypeContext( |
101 SyncChangeProcessor::ContextRefreshStatus refresh_status, | 106 syncer::ModelType type, |
| 107 syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status, |
102 const std::string& context); | 108 const std::string& context); |
103 virtual void AddLocalChangeObserver(LocalChangeObserver* observer); | 109 virtual void AddLocalChangeObserver(syncer::LocalChangeObserver* observer); |
104 virtual void RemoveLocalChangeObserver(LocalChangeObserver* observer); | 110 virtual void RemoveLocalChangeObserver(syncer::LocalChangeObserver* observer); |
105 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); | 111 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); |
106 virtual bool CryptoReadyIfNecessary(); | 112 virtual bool CryptoReadyIfNecessary(); |
107 | 113 |
108 // If a datatype context associated with the current type exists, fills | 114 // If a datatype context associated with the current type exists, fills |
109 // |context| and returns true. Otheriwse, if there has not been a context | 115 // |context| and returns true. Otheriwse, if there has not been a context |
110 // set, returns false. | 116 // set, returns false. |
111 virtual bool GetDataTypeContext(std::string* context) const; | 117 virtual bool GetDataTypeContext(std::string* context) const; |
112 | 118 |
113 virtual SyncError CreateAndUploadError( | 119 virtual syncer::SyncError CreateAndUploadError( |
114 const tracked_objects::Location& location, | 120 const tracked_objects::Location& location, |
115 const std::string& message); | 121 const std::string& message); |
116 | 122 |
117 // Calls local_service_->StopSyncing() and releases our reference to it. | 123 // Calls local_service_->StopSyncing() and releases our reference to it. |
118 void StopLocalService(); | 124 void StopLocalService(); |
119 | 125 |
120 ChangeProcessor* generic_change_processor(); | 126 ChangeProcessor* generic_change_processor(); |
121 | 127 |
122 protected: | 128 protected: |
123 friend class base::RefCountedThreadSafe<SharedChangeProcessor>; | 129 friend class base::RefCountedThreadSafe<SharedChangeProcessor>; |
124 virtual ~SharedChangeProcessor(); | 130 virtual ~SharedChangeProcessor(); |
125 | 131 |
126 private: | 132 private: |
127 // Record association time. | 133 // Record association time. |
128 virtual void RecordAssociationTime(base::TimeDelta time); | 134 virtual void RecordAssociationTime(base::TimeDelta time); |
129 | 135 |
130 // Monitor lock for this object. All methods that interact with the change | 136 // Monitor lock for this object. All methods that interact with the change |
131 // processor must aquire this lock and check whether we're disconnected or | 137 // processor must aquire this lock and check whether we're disconnected or |
132 // not. Once disconnected, all attempted changes to or loads from the change | 138 // not. Once disconnected, all attempted changes to or loads from the change |
133 // processor return errors. This enables us to shut down the syncer without | 139 // processor return errors. This enables us to shut down the syncer without |
134 // having to wait for possibly non-UI thread datatypes to complete work. | 140 // having to wait for possibly non-UI thread datatypes to complete work. |
135 mutable base::Lock monitor_lock_; | 141 mutable base::Lock monitor_lock_; |
136 bool disconnected_; | 142 bool disconnected_; |
137 | 143 |
138 // The sync datatype we process changes for. | 144 // The sync datatype we process changes for. |
139 const ModelType type_; | 145 const syncer::ModelType type_; |
140 | 146 |
141 // The frontend / UI MessageLoop this object is constructed on. May also be | 147 // The frontend / UI MessageLoop this object is constructed on. May also be |
142 // destructed and/or disconnected on this loop, see ~SharedChangeProcessor. | 148 // destructed and/or disconnected on this loop, see ~SharedChangeProcessor. |
143 const scoped_refptr<const base::SingleThreadTaskRunner> frontend_task_runner_; | 149 const scoped_refptr<const base::SingleThreadTaskRunner> frontend_task_runner_; |
144 | 150 |
145 // The loop that all methods except the constructor, destructor, and | 151 // The loop that all methods except the constructor, destructor, and |
146 // Disconnect() should be called on. Set in Connect(). | 152 // Disconnect() should be called on. Set in Connect(). |
147 scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner_; | 153 scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner_; |
148 | 154 |
149 // Used only on |backend_loop_|. | 155 // Used only on |backend_loop_|. |
150 GenericChangeProcessor* generic_change_processor_; | 156 GenericChangeProcessor* generic_change_processor_; |
151 | 157 |
152 std::unique_ptr<DataTypeErrorHandler> error_handler_; | 158 std::unique_ptr<syncer::DataTypeErrorHandler> error_handler_; |
153 | 159 |
154 // The local service for this type. Only set if the DTC for the type uses | 160 // The local service for this type. Only set if the DTC for the type uses |
155 // SharedChangeProcessor::StartAssociation(). | 161 // SharedChangeProcessor::StartAssociation(). |
156 base::WeakPtr<SyncableService> local_service_; | 162 base::WeakPtr<syncer::SyncableService> local_service_; |
157 | 163 |
158 DISALLOW_COPY_AND_ASSIGN(SharedChangeProcessor); | 164 DISALLOW_COPY_AND_ASSIGN(SharedChangeProcessor); |
159 }; | 165 }; |
160 | 166 |
161 } // namespace syncer | 167 } // namespace sync_driver |
162 | 168 |
163 #endif // COMPONENTS_SYNC_DRIVER_SHARED_CHANGE_PROCESSOR_H_ | 169 #endif // COMPONENTS_SYNC_DRIVER_SHARED_CHANGE_PROCESSOR_H_ |
OLD | NEW |