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