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_GENERIC_CHANGE_PROCESSOR_H_ | 5 #ifndef COMPONENTS_SYNC_DRIVER_GENERIC_CHANGE_PROCESSOR_H_ |
6 #define COMPONENTS_SYNC_DRIVER_GENERIC_CHANGE_PROCESSOR_H_ | 6 #define COMPONENTS_SYNC_DRIVER_GENERIC_CHANGE_PROCESSOR_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "base/threading/non_thread_safe.h" | 17 #include "base/threading/non_thread_safe.h" |
18 #include "components/sync/api/attachments/attachment_store.h" | 18 #include "components/sync/api/attachments/attachment_store.h" |
19 #include "components/sync/api/data_type_error_handler.h" | 19 #include "components/sync/api/data_type_error_handler.h" |
20 #include "components/sync/api/sync_change_processor.h" | 20 #include "components/sync/api/sync_change_processor.h" |
21 #include "components/sync/api/sync_merge_result.h" | 21 #include "components/sync/api/sync_merge_result.h" |
22 #include "components/sync/core/attachments/attachment_service.h" | 22 #include "components/sync/core/attachments/attachment_service.h" |
23 #include "components/sync/core/attachments/attachment_service_proxy.h" | 23 #include "components/sync/core/attachments/attachment_service_proxy.h" |
24 #include "components/sync/driver/change_processor.h" | 24 #include "components/sync/driver/change_processor.h" |
25 | 25 |
26 namespace syncer { | 26 namespace syncer { |
| 27 |
| 28 class SyncApiComponentFactory; |
| 29 class SyncClient; |
27 class SyncData; | 30 class SyncData; |
28 class SyncableService; | 31 class SyncableService; |
29 class WriteNode; | 32 class WriteNode; |
30 class WriteTransaction; | 33 class WriteTransaction; |
31 | 34 |
32 typedef std::vector<syncer::SyncData> SyncDataList; | |
33 | |
34 namespace syncable { | 35 namespace syncable { |
35 class Entry; | 36 class Entry; |
36 } // namespace syncable | 37 } // namespace syncable |
37 } // namespace syncer | |
38 | 38 |
39 namespace sync_driver { | 39 typedef std::vector<SyncData> SyncDataList; |
40 | |
41 class SyncApiComponentFactory; | |
42 class SyncClient; | |
43 | 40 |
44 // Datatype agnostic change processor. One instance of GenericChangeProcessor | 41 // Datatype agnostic change processor. One instance of GenericChangeProcessor |
45 // is created for each datatype and lives on the datatype's thread. It then | 42 // is created for each datatype and lives on the datatype's thread. It then |
46 // handles all interaction with the sync api, both translating pushes from the | 43 // handles all interaction with the sync api, both translating pushes from the |
47 // local service into transactions and receiving changes from the sync model, | 44 // local service into transactions and receiving changes from the sync model, |
48 // which then get converted into SyncChange's and sent to the local service. | 45 // which then get converted into SyncChange's and sent to the local service. |
49 // | 46 // |
50 // As a rule, the GenericChangeProcessor is not thread safe, and should only | 47 // As a rule, the GenericChangeProcessor is not thread safe, and should only |
51 // be used on the same thread in which it was created. | 48 // be used on the same thread in which it was created. |
52 class GenericChangeProcessor : public ChangeProcessor, | 49 class GenericChangeProcessor : public ChangeProcessor, |
53 public syncer::SyncChangeProcessor, | 50 public SyncChangeProcessor, |
54 public syncer::AttachmentService::Delegate, | 51 public AttachmentService::Delegate, |
55 public base::NonThreadSafe { | 52 public base::NonThreadSafe { |
56 public: | 53 public: |
57 // Create a change processor for |type| and connect it to the syncer. | 54 // Create a change processor for |type| and connect it to the syncer. |
58 // |attachment_store| can be NULL which means that datatype will not use sync | 55 // |attachment_store| can be NULL which means that datatype will not use sync |
59 // attachments. | 56 // attachments. |
60 GenericChangeProcessor( | 57 GenericChangeProcessor( |
61 syncer::ModelType type, | 58 ModelType type, |
62 std::unique_ptr<syncer::DataTypeErrorHandler> error_handler, | 59 std::unique_ptr<DataTypeErrorHandler> error_handler, |
63 const base::WeakPtr<syncer::SyncableService>& local_service, | 60 const base::WeakPtr<SyncableService>& local_service, |
64 const base::WeakPtr<syncer::SyncMergeResult>& merge_result, | 61 const base::WeakPtr<SyncMergeResult>& merge_result, |
65 syncer::UserShare* user_share, | 62 UserShare* user_share, |
66 SyncClient* sync_client, | 63 SyncClient* sync_client, |
67 std::unique_ptr<syncer::AttachmentStoreForSync> attachment_store); | 64 std::unique_ptr<AttachmentStoreForSync> attachment_store); |
68 ~GenericChangeProcessor() override; | 65 ~GenericChangeProcessor() override; |
69 | 66 |
70 // ChangeProcessor interface. | 67 // ChangeProcessor interface. |
71 // Build and store a list of all changes into |syncer_changes_|. | 68 // Build and store a list of all changes into |syncer_changes_|. |
72 void ApplyChangesFromSyncModel( | 69 void ApplyChangesFromSyncModel( |
73 const syncer::BaseTransaction* trans, | 70 const BaseTransaction* trans, |
74 int64_t version, | 71 int64_t version, |
75 const syncer::ImmutableChangeRecordList& changes) override; | 72 const ImmutableChangeRecordList& changes) override; |
76 // Passes |syncer_changes_|, built in ApplyChangesFromSyncModel, onto | 73 // Passes |syncer_changes_|, built in ApplyChangesFromSyncModel, onto |
77 // |local_service_| by way of its ProcessSyncChanges method. | 74 // |local_service_| by way of its ProcessSyncChanges method. |
78 void CommitChangesFromSyncModel() override; | 75 void CommitChangesFromSyncModel() override; |
79 | 76 |
80 // syncer::SyncChangeProcessor implementation. | 77 // SyncChangeProcessor implementation. |
81 syncer::SyncError ProcessSyncChanges( | 78 SyncError ProcessSyncChanges(const tracked_objects::Location& from_here, |
82 const tracked_objects::Location& from_here, | 79 const SyncChangeList& change_list) override; |
83 const syncer::SyncChangeList& change_list) override; | 80 SyncDataList GetAllSyncData(ModelType type) const override; |
84 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override; | 81 SyncError UpdateDataTypeContext( |
85 syncer::SyncError UpdateDataTypeContext( | 82 ModelType type, |
86 syncer::ModelType type, | 83 SyncChangeProcessor::ContextRefreshStatus refresh_status, |
87 syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status, | |
88 const std::string& context) override; | 84 const std::string& context) override; |
89 void AddLocalChangeObserver(syncer::LocalChangeObserver* observer) override; | 85 void AddLocalChangeObserver(LocalChangeObserver* observer) override; |
90 void RemoveLocalChangeObserver( | 86 void RemoveLocalChangeObserver(LocalChangeObserver* observer) override; |
91 syncer::LocalChangeObserver* observer) override; | |
92 | 87 |
93 // syncer::AttachmentService::Delegate implementation. | 88 // AttachmentService::Delegate implementation. |
94 void OnAttachmentUploaded(const syncer::AttachmentId& attachment_id) override; | 89 void OnAttachmentUploaded(const AttachmentId& attachment_id) override; |
95 | 90 |
96 // Similar to above, but returns a SyncError for use by direct clients | 91 // Similar to above, but returns a SyncError for use by direct clients |
97 // of GenericChangeProcessor that may need more error visibility. | 92 // of GenericChangeProcessor that may need more error visibility. |
98 virtual syncer::SyncError GetAllSyncDataReturnError( | 93 virtual SyncError GetAllSyncDataReturnError(SyncDataList* data) const; |
99 syncer::SyncDataList* data) const; | |
100 | 94 |
101 // If a datatype context associated with this GenericChangeProcessor's type | 95 // If a datatype context associated with this GenericChangeProcessor's type |
102 // exists, fills |context| and returns true. Otheriwse, if there has not been | 96 // exists, fills |context| and returns true. Otheriwse, if there has not been |
103 // a context set, returns false. | 97 // a context set, returns false. |
104 virtual bool GetDataTypeContext(std::string* context) const; | 98 virtual bool GetDataTypeContext(std::string* context) const; |
105 | 99 |
106 // Returns the number of items for this type. | 100 // Returns the number of items for this type. |
107 virtual int GetSyncCount(); | 101 virtual int GetSyncCount(); |
108 | 102 |
109 // Generic versions of AssociatorInterface methods. Called by | 103 // Generic versions of AssociatorInterface methods. Called by |
110 // syncer::SyncableServiceAdapter or the DataTypeController. | 104 // SyncableServiceAdapter or the DataTypeController. |
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 // Gets AttachmentService proxy for datatype. | 108 // Gets AttachmentService proxy for datatype. |
115 std::unique_ptr<syncer::AttachmentService> GetAttachmentService() const; | 109 std::unique_ptr<AttachmentService> GetAttachmentService() const; |
116 | 110 |
117 protected: | 111 protected: |
118 // ChangeProcessor interface. | 112 // ChangeProcessor interface. |
119 void StartImpl() override; // Does nothing. | 113 void StartImpl() override; // Does nothing. |
120 syncer::UserShare* share_handle() const override; | 114 UserShare* share_handle() const override; |
121 | 115 |
122 private: | 116 private: |
123 syncer::SyncError AttemptDelete(const syncer::SyncChange& change, | 117 SyncError AttemptDelete(const SyncChange& change, |
124 syncer::ModelType type, | 118 ModelType type, |
125 const std::string& type_str, | 119 const std::string& type_str, |
126 syncer::WriteNode* node, | 120 WriteNode* node, |
127 syncer::DataTypeErrorHandler* error_handler); | 121 DataTypeErrorHandler* error_handler); |
128 // Logically part of ProcessSyncChanges. | 122 // Logically part of ProcessSyncChanges. |
129 // | 123 // |
130 // |new_attachments| is an output parameter containing newly added attachments | 124 // |new_attachments| is an output parameter containing newly added attachments |
131 // that need to be stored. This method will append to it. | 125 // that need to be stored. This method will append to it. |
132 syncer::SyncError HandleActionAdd(const syncer::SyncChange& change, | 126 SyncError HandleActionAdd(const SyncChange& change, |
133 const std::string& type_str, | 127 const std::string& type_str, |
134 const syncer::WriteTransaction& trans, | 128 const WriteTransaction& trans, |
135 syncer::WriteNode* sync_node, | 129 WriteNode* sync_node, |
136 syncer::AttachmentIdSet* new_attachments); | 130 AttachmentIdSet* new_attachments); |
137 | 131 |
138 // Logically part of ProcessSyncChanges. | 132 // Logically part of ProcessSyncChanges. |
139 // | 133 // |
140 // |new_attachments| is an output parameter containing newly added attachments | 134 // |new_attachments| is an output parameter containing newly added attachments |
141 // that need to be stored. This method will append to it. | 135 // that need to be stored. This method will append to it. |
142 syncer::SyncError HandleActionUpdate( | 136 SyncError HandleActionUpdate(const SyncChange& change, |
143 const syncer::SyncChange& change, | 137 const std::string& type_str, |
144 const std::string& type_str, | 138 const WriteTransaction& trans, |
145 const syncer::WriteTransaction& trans, | 139 WriteNode* sync_node, |
146 syncer::WriteNode* sync_node, | 140 AttachmentIdSet* new_attachments); |
147 syncer::AttachmentIdSet* new_attachments); | |
148 | 141 |
149 // Begin uploading attachments that have not yet been uploaded to the sync | 142 // Begin uploading attachments that have not yet been uploaded to the sync |
150 // server. | 143 // server. |
151 void UploadAllAttachmentsNotOnServer(); | 144 void UploadAllAttachmentsNotOnServer(); |
152 | 145 |
153 // Notify every registered local change observer that |change| is about to be | 146 // Notify every registered local change observer that |change| is about to be |
154 // applied to |current_entry|. | 147 // applied to |current_entry|. |
155 void NotifyLocalChangeObservers(const syncer::syncable::Entry* current_entry, | 148 void NotifyLocalChangeObservers(const syncable::Entry* current_entry, |
156 const syncer::SyncChange& change); | 149 const SyncChange& change); |
157 | 150 |
158 const syncer::ModelType type_; | 151 const ModelType type_; |
159 | 152 |
160 // The SyncableService this change processor will forward changes on to. | 153 // The SyncableService this change processor will forward changes on to. |
161 const base::WeakPtr<syncer::SyncableService> local_service_; | 154 const base::WeakPtr<SyncableService> local_service_; |
162 | 155 |
163 // A SyncMergeResult used to track the changes made during association. The | 156 // A SyncMergeResult used to track the changes made during association. The |
164 // owner will invalidate the weak pointer when association is complete. While | 157 // owner will invalidate the weak pointer when association is complete. While |
165 // the pointer is valid though, we increment it with any changes received | 158 // the pointer is valid though, we increment it with any changes received |
166 // via ProcessSyncChanges. | 159 // via ProcessSyncChanges. |
167 const base::WeakPtr<syncer::SyncMergeResult> merge_result_; | 160 const base::WeakPtr<SyncMergeResult> merge_result_; |
168 | 161 |
169 // The current list of changes received from the syncer. We buffer because | 162 // The current list of changes received from the syncer. We buffer because |
170 // we must ensure no syncapi transaction is held when we pass it on to | 163 // we must ensure no syncapi transaction is held when we pass it on to |
171 // |local_service_|. | 164 // |local_service_|. |
172 // Set in ApplyChangesFromSyncModel, consumed in CommitChangesFromSyncModel. | 165 // Set in ApplyChangesFromSyncModel, consumed in CommitChangesFromSyncModel. |
173 syncer::SyncChangeList syncer_changes_; | 166 SyncChangeList syncer_changes_; |
174 | 167 |
175 // Our handle to the sync model. Unlike normal ChangeProcessors, we need to | 168 // Our handle to the sync model. Unlike normal ChangeProcessors, we need to |
176 // be able to access the sync model before the change processor begins | 169 // be able to access the sync model before the change processor begins |
177 // listening to changes (the local_service_ will be interacting with us | 170 // listening to changes (the local_service_ will be interacting with us |
178 // when it starts up). As such we can't wait until Start(_) has been called, | 171 // when it starts up). As such we can't wait until Start(_) has been called, |
179 // and have to keep a local pointer to the user_share. | 172 // and have to keep a local pointer to the user_share. |
180 syncer::UserShare* const share_handle_; | 173 UserShare* const share_handle_; |
181 | 174 |
182 // AttachmentService for datatype. Can be NULL if datatype doesn't use | 175 // AttachmentService for datatype. Can be NULL if datatype doesn't use |
183 // attachments. | 176 // attachments. |
184 std::unique_ptr<syncer::AttachmentService> attachment_service_; | 177 std::unique_ptr<AttachmentService> attachment_service_; |
185 | 178 |
186 // List of observers that want to be notified of local changes being written. | 179 // List of observers that want to be notified of local changes being written. |
187 base::ObserverList<syncer::LocalChangeObserver> local_change_observers_; | 180 base::ObserverList<LocalChangeObserver> local_change_observers_; |
188 | 181 |
189 // Must be destroyed before attachment_service_ to ensure WeakPtrs are | 182 // Must be destroyed before attachment_service_ to ensure WeakPtrs are |
190 // invalidated before attachment_service_ is destroyed. | 183 // invalidated before attachment_service_ is destroyed. |
191 // Can be NULL if attachment_service_ is NULL; | 184 // Can be NULL if attachment_service_ is NULL; |
192 std::unique_ptr<base::WeakPtrFactory<syncer::AttachmentService>> | 185 std::unique_ptr<base::WeakPtrFactory<AttachmentService>> |
193 attachment_service_weak_ptr_factory_; | 186 attachment_service_weak_ptr_factory_; |
194 syncer::AttachmentServiceProxy attachment_service_proxy_; | 187 AttachmentServiceProxy attachment_service_proxy_; |
195 base::WeakPtrFactory<GenericChangeProcessor> weak_ptr_factory_; | 188 base::WeakPtrFactory<GenericChangeProcessor> weak_ptr_factory_; |
196 | 189 |
197 DISALLOW_COPY_AND_ASSIGN(GenericChangeProcessor); | 190 DISALLOW_COPY_AND_ASSIGN(GenericChangeProcessor); |
198 }; | 191 }; |
199 | 192 |
200 } // namespace sync_driver | 193 } // namespace syncer |
201 | 194 |
202 #endif // COMPONENTS_SYNC_DRIVER_GENERIC_CHANGE_PROCESSOR_H_ | 195 #endif // COMPONENTS_SYNC_DRIVER_GENERIC_CHANGE_PROCESSOR_H_ |
OLD | NEW |