d72947472e9fbd2e8923d3b963e10606ea5b9cc1
[platform/framework/web/crosswalk.git] / src / components / sync_driver / generic_change_processor.h
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 <vector>
9
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"
21
22 namespace syncer {
23 class SyncData;
24 class SyncableService;
25 class WriteNode;
26 class WriteTransaction;
27
28 typedef std::vector<syncer::SyncData> SyncDataList;
29 }  // namespace syncer
30
31 namespace sync_driver {
32 class SyncApiComponentFactory;
33
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.
39 //
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 {
46  public:
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
49   // attachments.
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   ~GenericChangeProcessor() override;
59
60   // ChangeProcessor interface.
61   // Build and store a list of all changes into |syncer_changes_|.
62   void ApplyChangesFromSyncModel(
63       const syncer::BaseTransaction* trans,
64       int64 version,
65       const syncer::ImmutableChangeRecordList& changes) override;
66   // Passes |syncer_changes_|, built in ApplyChangesFromSyncModel, onto
67   // |local_service_| by way of its ProcessSyncChanges method.
68   void CommitChangesFromSyncModel() override;
69
70   // syncer::SyncChangeProcessor implementation.
71   syncer::SyncError ProcessSyncChanges(
72       const tracked_objects::Location& from_here,
73       const syncer::SyncChangeList& change_list) override;
74   syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
75   syncer::SyncError UpdateDataTypeContext(
76       syncer::ModelType type,
77       syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status,
78       const std::string& context) override;
79
80   // syncer::AttachmentService::Delegate implementation.
81   void OnAttachmentUploaded(const syncer::AttachmentId& attachment_id) override;
82
83   // Similar to above, but returns a SyncError for use by direct clients
84   // of GenericChangeProcessor that may need more error visibility.
85   virtual syncer::SyncError GetAllSyncDataReturnError(
86       syncer::SyncDataList* data) const;
87
88   // If a datatype context associated with this GenericChangeProcessor's type
89   // exists, fills |context| and returns true. Otheriwse, if there has not been
90   // a context set, returns false.
91   virtual bool GetDataTypeContext(std::string* context) const;
92
93   // Returns the number of items for this type.
94   virtual int GetSyncCount();
95
96   // Generic versions of AssociatorInterface methods. Called by
97   // syncer::SyncableServiceAdapter or the DataTypeController.
98   virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes);
99   virtual bool CryptoReadyIfNecessary();
100
101  protected:
102   // ChangeProcessor interface.
103   void StartImpl() override;  // Does nothing.
104   syncer::UserShare* share_handle() const override;
105
106  private:
107   // Logically part of ProcessSyncChanges.
108   //
109   // |new_attachments| is an output parameter containing newly added attachments
110   // that need to be stored.  This method will append to it.
111   syncer::SyncError HandleActionAdd(const syncer::SyncChange& change,
112                                     const std::string& type_str,
113                                     const syncer::WriteTransaction& trans,
114                                     syncer::WriteNode* sync_node,
115                                     syncer::AttachmentIdSet* new_attachments);
116
117   // Logically part of ProcessSyncChanges.
118   //
119   // |new_attachments| is an output parameter containing newly added attachments
120   // that need to be stored.  This method will append to it.
121   syncer::SyncError HandleActionUpdate(
122       const syncer::SyncChange& change,
123       const std::string& type_str,
124       const syncer::WriteTransaction& trans,
125       syncer::WriteNode* sync_node,
126       syncer::AttachmentIdSet* new_attachments);
127
128   // Begin uploading attachments that have not yet been uploaded to the sync
129   // server.
130   void UploadAllAttachmentsNotOnServer();
131
132   const syncer::ModelType type_;
133
134   // The SyncableService this change processor will forward changes on to.
135   const base::WeakPtr<syncer::SyncableService> local_service_;
136
137   // A SyncMergeResult used to track the changes made during association. The
138   // owner will invalidate the weak pointer when association is complete. While
139   // the pointer is valid though, we increment it with any changes received
140   // via ProcessSyncChanges.
141   const base::WeakPtr<syncer::SyncMergeResult> merge_result_;
142
143   // The current list of changes received from the syncer. We buffer because
144   // we must ensure no syncapi transaction is held when we pass it on to
145   // |local_service_|.
146   // Set in ApplyChangesFromSyncModel, consumed in CommitChangesFromSyncModel.
147   syncer::SyncChangeList syncer_changes_;
148
149   // Our handle to the sync model. Unlike normal ChangeProcessors, we need to
150   // be able to access the sync model before the change processor begins
151   // listening to changes (the local_service_ will be interacting with us
152   // when it starts up). As such we can't wait until Start(_) has been called,
153   // and have to keep a local pointer to the user_share.
154   syncer::UserShare* const share_handle_;
155
156   // AttachmentService for datatype. Can be NULL if datatype doesn't use
157   // attachments.
158   scoped_ptr<syncer::AttachmentService> attachment_service_;
159
160   // Must be destroyed before attachment_service_ to ensure WeakPtrs are
161   // invalidated before attachment_service_ is destroyed.
162   // Can be NULL if attachment_service_ is NULL;
163   scoped_ptr<base::WeakPtrFactory<syncer::AttachmentService> >
164       attachment_service_weak_ptr_factory_;
165   scoped_ptr<syncer::AttachmentServiceProxy> attachment_service_proxy_;
166
167   base::WeakPtrFactory<GenericChangeProcessor> weak_ptr_factory_;
168
169   DISALLOW_COPY_AND_ASSIGN(GenericChangeProcessor);
170 };
171
172 }  // namespace sync_driver
173
174 #endif  // COMPONENTS_SYNC_DRIVER_GENERIC_CHANGE_PROCESSOR_H_