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