Make |track_| in MediaStreamTrack const. and a couple of other cosmetic changes.
[chromium-blink-merge.git] / components / sync_driver / shared_change_processor.cc
blobce494303414c8cede1730a9eb0b7b7b7f8f32c8c
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 #include "components/sync_driver/shared_change_processor.h"
7 #include "base/message_loop/message_loop_proxy.h"
8 #include "components/sync_driver/generic_change_processor.h"
9 #include "components/sync_driver/generic_change_processor_factory.h"
10 #include "components/sync_driver/sync_api_component_factory.h"
11 #include "sync/api/sync_change.h"
13 using base::AutoLock;
15 namespace sync_driver {
17 SharedChangeProcessor::SharedChangeProcessor()
18 : disconnected_(false),
19 type_(syncer::UNSPECIFIED),
20 frontend_loop_(base::MessageLoopProxy::current()),
21 generic_change_processor_(NULL),
22 error_handler_(NULL) {
25 SharedChangeProcessor::~SharedChangeProcessor() {
26 // We can either be deleted when the DTC is destroyed (on UI
27 // thread), or when the syncer::SyncableService stops syncing (datatype
28 // thread). |generic_change_processor_|, if non-NULL, must be
29 // deleted on |backend_loop_|.
30 if (backend_loop_.get()) {
31 if (backend_loop_->BelongsToCurrentThread()) {
32 delete generic_change_processor_;
33 } else {
34 DCHECK(frontend_loop_->BelongsToCurrentThread());
35 if (!backend_loop_->DeleteSoon(FROM_HERE, generic_change_processor_)) {
36 NOTREACHED();
39 } else {
40 DCHECK(!generic_change_processor_);
44 base::WeakPtr<syncer::SyncableService> SharedChangeProcessor::Connect(
45 SyncApiComponentFactory* sync_factory,
46 GenericChangeProcessorFactory* processor_factory,
47 syncer::UserShare* user_share,
48 DataTypeErrorHandler* error_handler,
49 syncer::ModelType type,
50 const base::WeakPtr<syncer::SyncMergeResult>& merge_result) {
51 DCHECK(sync_factory);
52 DCHECK(error_handler);
53 DCHECK_NE(type, syncer::UNSPECIFIED);
54 backend_loop_ = base::MessageLoopProxy::current();
55 AutoLock lock(monitor_lock_);
56 if (disconnected_)
57 return base::WeakPtr<syncer::SyncableService>();
58 type_ = type;
59 error_handler_ = error_handler;
60 base::WeakPtr<syncer::SyncableService> local_service =
61 sync_factory->GetSyncableServiceForType(type);
62 if (!local_service.get()) {
63 LOG(WARNING) << "SyncableService destroyed before DTC was stopped.";
64 disconnected_ = true;
65 return base::WeakPtr<syncer::SyncableService>();
68 generic_change_processor_ =
69 processor_factory->CreateGenericChangeProcessor(type,
70 user_share,
71 error_handler,
72 local_service,
73 merge_result,
74 sync_factory).release();
75 return local_service;
78 bool SharedChangeProcessor::Disconnect() {
79 // May be called from any thread.
80 DVLOG(1) << "Disconnecting change processor.";
81 AutoLock lock(monitor_lock_);
82 bool was_connected = !disconnected_;
83 disconnected_ = true;
84 error_handler_ = NULL;
85 return was_connected;
88 ChangeProcessor* SharedChangeProcessor::generic_change_processor() {
89 return generic_change_processor_;
92 int SharedChangeProcessor::GetSyncCount() {
93 DCHECK(backend_loop_.get());
94 DCHECK(backend_loop_->BelongsToCurrentThread());
95 AutoLock lock(monitor_lock_);
96 if (disconnected_) {
97 LOG(ERROR) << "Change processor disconnected.";
98 return 0;
100 return generic_change_processor_->GetSyncCount();
103 syncer::SyncError SharedChangeProcessor::ProcessSyncChanges(
104 const tracked_objects::Location& from_here,
105 const syncer::SyncChangeList& list_of_changes) {
106 DCHECK(backend_loop_.get());
107 DCHECK(backend_loop_->BelongsToCurrentThread());
108 AutoLock lock(monitor_lock_);
109 if (disconnected_) {
110 // The DTC that disconnects us must ensure it posts a StopSyncing task.
111 // If we reach this, it means it just hasn't executed yet.
112 syncer::SyncError error(FROM_HERE,
113 syncer::SyncError::DATATYPE_ERROR,
114 "Change processor disconnected.",
115 type_);
116 return error;
118 return generic_change_processor_->ProcessSyncChanges(
119 from_here, list_of_changes);
122 syncer::SyncDataList SharedChangeProcessor::GetAllSyncData(
123 syncer::ModelType type) const {
124 syncer::SyncDataList data;
125 GetAllSyncDataReturnError(type, &data); // Handles the disconnect case.
126 return data;
129 syncer::SyncError SharedChangeProcessor::GetAllSyncDataReturnError(
130 syncer::ModelType type,
131 syncer::SyncDataList* data) const {
132 DCHECK(backend_loop_.get());
133 DCHECK(backend_loop_->BelongsToCurrentThread());
134 AutoLock lock(monitor_lock_);
135 if (disconnected_) {
136 syncer::SyncError error(FROM_HERE,
137 syncer::SyncError::DATATYPE_ERROR,
138 "Change processor disconnected.",
139 type_);
140 return error;
142 return generic_change_processor_->GetAllSyncDataReturnError(data);
145 syncer::SyncError SharedChangeProcessor::UpdateDataTypeContext(
146 syncer::ModelType type,
147 syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status,
148 const std::string& context) {
149 DCHECK(backend_loop_.get());
150 DCHECK(backend_loop_->BelongsToCurrentThread());
151 AutoLock lock(monitor_lock_);
152 if (disconnected_) {
153 syncer::SyncError error(FROM_HERE,
154 syncer::SyncError::DATATYPE_ERROR,
155 "Change processor disconnected.",
156 type_);
157 return error;
159 return generic_change_processor_->UpdateDataTypeContext(
160 type, refresh_status, context);
163 bool SharedChangeProcessor::SyncModelHasUserCreatedNodes(bool* has_nodes) {
164 DCHECK(backend_loop_.get());
165 DCHECK(backend_loop_->BelongsToCurrentThread());
166 AutoLock lock(monitor_lock_);
167 if (disconnected_) {
168 LOG(ERROR) << "Change processor disconnected.";
169 return false;
171 return generic_change_processor_->SyncModelHasUserCreatedNodes(has_nodes);
174 bool SharedChangeProcessor::CryptoReadyIfNecessary() {
175 DCHECK(backend_loop_.get());
176 DCHECK(backend_loop_->BelongsToCurrentThread());
177 AutoLock lock(monitor_lock_);
178 if (disconnected_) {
179 LOG(ERROR) << "Change processor disconnected.";
180 return true; // Otherwise we get into infinite spin waiting.
182 return generic_change_processor_->CryptoReadyIfNecessary();
185 bool SharedChangeProcessor::GetDataTypeContext(std::string* context) const {
186 DCHECK(backend_loop_.get());
187 DCHECK(backend_loop_->BelongsToCurrentThread());
188 AutoLock lock(monitor_lock_);
189 if (disconnected_) {
190 LOG(ERROR) << "Change processor disconnected.";
191 return false;
193 return generic_change_processor_->GetDataTypeContext(context);
196 syncer::SyncError SharedChangeProcessor::CreateAndUploadError(
197 const tracked_objects::Location& location,
198 const std::string& message) {
199 AutoLock lock(monitor_lock_);
200 if (!disconnected_) {
201 return error_handler_->CreateAndUploadError(location, message, type_);
202 } else {
203 return syncer::SyncError(location,
204 syncer::SyncError::DATATYPE_ERROR,
205 message,
206 type_);
210 } // namespace sync_driver