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