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_service.h"
17 #include "sync/api/attachments/attachment_service_proxy.h"
18 #include "sync/api/sync_change_processor.h"
19 #include "sync/api/sync_merge_result.h"
23 class SyncableService
;
25 class WriteTransaction
;
27 typedef std::vector
<syncer::SyncData
> SyncDataList
;
30 namespace browser_sync
{
32 // Datatype agnostic change processor. One instance of GenericChangeProcessor
33 // is created for each datatype and lives on the datatype's thread. It then
34 // handles all interaction with the sync api, both translating pushes from the
35 // local service into transactions and receiving changes from the sync model,
36 // which then get converted into SyncChange's and sent to the local service.
38 // As a rule, the GenericChangeProcessor is not thread safe, and should only
39 // be used on the same thread in which it was created.
40 class GenericChangeProcessor
: public ChangeProcessor
,
41 public syncer::SyncChangeProcessor
,
42 public base::NonThreadSafe
{
44 // Create a change processor and connect it to the syncer.
45 GenericChangeProcessor(
46 DataTypeErrorHandler
* error_handler
,
47 const base::WeakPtr
<syncer::SyncableService
>& local_service
,
48 const base::WeakPtr
<syncer::SyncMergeResult
>& merge_result
,
49 syncer::UserShare
* user_share
,
50 scoped_ptr
<syncer::AttachmentService
> attachment_service
);
51 virtual ~GenericChangeProcessor();
53 // ChangeProcessor interface.
54 // Build and store a list of all changes into |syncer_changes_|.
55 virtual void ApplyChangesFromSyncModel(
56 const syncer::BaseTransaction
* trans
,
58 const syncer::ImmutableChangeRecordList
& changes
) OVERRIDE
;
59 // Passes |syncer_changes_|, built in ApplyChangesFromSyncModel, onto
60 // |local_service_| by way of its ProcessSyncChanges method.
61 virtual void CommitChangesFromSyncModel() OVERRIDE
;
63 // syncer::SyncChangeProcessor implementation.
64 virtual syncer::SyncError
ProcessSyncChanges(
65 const tracked_objects::Location
& from_here
,
66 const syncer::SyncChangeList
& change_list
) OVERRIDE
;
67 virtual syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
)
69 virtual syncer::SyncError
UpdateDataTypeContext(
70 syncer::ModelType type
,
71 syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status
,
72 const std::string
& context
) OVERRIDE
;
74 // Similar to above, but returns a SyncError for use by direct clients
75 // of GenericChangeProcessor that may need more error visibility.
76 virtual syncer::SyncError
GetAllSyncDataReturnError(
77 syncer::ModelType type
,
78 syncer::SyncDataList
* data
) const;
80 // If a datatype context associated with |type| exists, fills |context| and
81 // returns true. Otheriwse, if there has not been a context set, returns
83 virtual bool GetDataTypeContext(syncer::ModelType type
,
84 std::string
* context
) const;
86 // Returns the number of items for this type.
87 virtual int GetSyncCountForType(syncer::ModelType type
);
89 // Generic versions of AssociatorInterface methods. Called by
90 // syncer::SyncableServiceAdapter or the DataTypeController.
91 virtual bool SyncModelHasUserCreatedNodes(syncer::ModelType type
,
93 virtual bool CryptoReadyIfNecessary(syncer::ModelType type
);
96 // ChangeProcessor interface.
97 virtual void StartImpl() OVERRIDE
; // Does nothing.
98 virtual syncer::UserShare
* share_handle() const OVERRIDE
;
101 // Logically part of ProcessSyncChanges.
103 // |new_attachments| is an output parameter containing newly added attachments
104 // that need to be stored. This method will append to it.
105 syncer::SyncError
HandleActionAdd(const syncer::SyncChange
& change
,
106 const std::string
& type_str
,
107 const syncer::ModelType
& type
,
108 const syncer::WriteTransaction
& trans
,
109 syncer::WriteNode
* sync_node
,
110 syncer::AttachmentList
* new_attachments
);
112 // Logically part of ProcessSyncChanges.
114 // |new_attachments| is an output parameter containing newly added attachments
115 // that need to be stored. This method will append to it.
116 syncer::SyncError
HandleActionUpdate(const syncer::SyncChange
& change
,
117 const std::string
& type_str
,
118 const syncer::ModelType
& type
,
119 const syncer::WriteTransaction
& trans
,
120 syncer::WriteNode
* sync_node
,
121 syncer::AttachmentList
* new_attachments
);
123 // The SyncableService this change processor will forward changes on to.
124 const base::WeakPtr
<syncer::SyncableService
> local_service_
;
126 // A SyncMergeResult used to track the changes made during association. The
127 // owner will invalidate the weak pointer when association is complete. While
128 // the pointer is valid though, we increment it with any changes received
129 // via ProcessSyncChanges.
130 const base::WeakPtr
<syncer::SyncMergeResult
> merge_result_
;
132 // The current list of changes received from the syncer. We buffer because
133 // we must ensure no syncapi transaction is held when we pass it on to
135 // Set in ApplyChangesFromSyncModel, consumed in CommitChangesFromSyncModel.
136 syncer::SyncChangeList syncer_changes_
;
138 // Our handle to the sync model. Unlike normal ChangeProcessors, we need to
139 // be able to access the sync model before the change processor begins
140 // listening to changes (the local_service_ will be interacting with us
141 // when it starts up). As such we can't wait until Start(_) has been called,
142 // and have to keep a local pointer to the user_share.
143 syncer::UserShare
* const share_handle_
;
145 scoped_ptr
<syncer::AttachmentService
> attachment_service_
;
146 // Must be destroyed before attachment_service_ to ensure WeakPtrs are
147 // invalidated before attachment_service_ is destroyed.
148 base::WeakPtrFactory
<syncer::AttachmentService
>
149 attachment_service_weak_ptr_factory_
;
150 syncer::AttachmentServiceProxy attachment_service_proxy_
;
152 DISALLOW_COPY_AND_ASSIGN(GenericChangeProcessor
);
155 } // namespace browser_sync
157 #endif // COMPONENTS_SYNC_DRIVER_GENERIC_CHANGE_PROCESSOR_H_