Don't relaunch app for non-auth errors.
[chromium-blink-merge.git] / components / sync_driver / generic_change_processor.h
blob8a94936472ad6603ee6882deb37b94be01fdcb39
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_
8 #include <vector>
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"
22 namespace syncer {
23 class SyncData;
24 class SyncableService;
25 class WriteNode;
26 class WriteTransaction;
28 typedef std::vector<syncer::SyncData> SyncDataList;
29 } // namespace syncer
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 {
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;
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;
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;
80 // syncer::AttachmentService::Delegate implementation.
81 void OnAttachmentUploaded(const syncer::AttachmentId& attachment_id) override;
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;
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;
93 // Returns the number of items for this type.
94 virtual int GetSyncCount();
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();
101 // Gets AttachmentService proxy for datatype.
102 scoped_ptr<syncer::AttachmentService> GetAttachmentService() const;
104 protected:
105 // ChangeProcessor interface.
106 void StartImpl() override; // Does nothing.
107 syncer::UserShare* share_handle() const override;
109 private:
110 // Logically part of ProcessSyncChanges.
112 // |new_attachments| is an output parameter containing newly added attachments
113 // that need to be stored. This method will append to it.
114 syncer::SyncError HandleActionAdd(const syncer::SyncChange& change,
115 const std::string& type_str,
116 const syncer::WriteTransaction& trans,
117 syncer::WriteNode* sync_node,
118 syncer::AttachmentIdSet* new_attachments);
120 // Logically part of ProcessSyncChanges.
122 // |new_attachments| is an output parameter containing newly added attachments
123 // that need to be stored. This method will append to it.
124 syncer::SyncError HandleActionUpdate(
125 const syncer::SyncChange& change,
126 const std::string& type_str,
127 const syncer::WriteTransaction& trans,
128 syncer::WriteNode* sync_node,
129 syncer::AttachmentIdSet* new_attachments);
131 // Begin uploading attachments that have not yet been uploaded to the sync
132 // server.
133 void UploadAllAttachmentsNotOnServer();
135 const syncer::ModelType type_;
137 // The SyncableService this change processor will forward changes on to.
138 const base::WeakPtr<syncer::SyncableService> local_service_;
140 // A SyncMergeResult used to track the changes made during association. The
141 // owner will invalidate the weak pointer when association is complete. While
142 // the pointer is valid though, we increment it with any changes received
143 // via ProcessSyncChanges.
144 const base::WeakPtr<syncer::SyncMergeResult> merge_result_;
146 // The current list of changes received from the syncer. We buffer because
147 // we must ensure no syncapi transaction is held when we pass it on to
148 // |local_service_|.
149 // Set in ApplyChangesFromSyncModel, consumed in CommitChangesFromSyncModel.
150 syncer::SyncChangeList syncer_changes_;
152 // Our handle to the sync model. Unlike normal ChangeProcessors, we need to
153 // be able to access the sync model before the change processor begins
154 // listening to changes (the local_service_ will be interacting with us
155 // when it starts up). As such we can't wait until Start(_) has been called,
156 // and have to keep a local pointer to the user_share.
157 syncer::UserShare* const share_handle_;
159 // AttachmentService for datatype. Can be NULL if datatype doesn't use
160 // attachments.
161 scoped_ptr<syncer::AttachmentService> attachment_service_;
163 // Must be destroyed before attachment_service_ to ensure WeakPtrs are
164 // invalidated before attachment_service_ is destroyed.
165 // Can be NULL if attachment_service_ is NULL;
166 scoped_ptr<base::WeakPtrFactory<syncer::AttachmentService> >
167 attachment_service_weak_ptr_factory_;
168 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_