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