OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/glue/generic_change_processor.h" | 5 #include "chrome/browser/sync/glue/generic_change_processor.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/sync/api/syncable_service.h" | 9 #include "chrome/browser/sync/api/syncable_service.h" |
10 #include "chrome/browser/sync/api/sync_change.h" | 10 #include "chrome/browser/sync/api/sync_change.h" |
11 #include "chrome/browser/sync/api/sync_error.h" | 11 #include "chrome/browser/sync/api/sync_error.h" |
12 #include "chrome/browser/sync/internal_api/base_node.h" | 12 #include "chrome/browser/sync/internal_api/base_node.h" |
13 #include "chrome/browser/sync/internal_api/change_record.h" | 13 #include "chrome/browser/sync/internal_api/change_record.h" |
14 #include "chrome/browser/sync/internal_api/read_node.h" | 14 #include "chrome/browser/sync/internal_api/read_node.h" |
15 #include "chrome/browser/sync/internal_api/read_transaction.h" | 15 #include "chrome/browser/sync/internal_api/read_transaction.h" |
16 #include "chrome/browser/sync/internal_api/write_node.h" | 16 #include "chrome/browser/sync/internal_api/write_node.h" |
17 #include "chrome/browser/sync/internal_api/write_transaction.h" | 17 #include "chrome/browser/sync/internal_api/write_transaction.h" |
18 #include "chrome/browser/sync/unrecoverable_error_handler.h" | 18 #include "chrome/browser/sync/unrecoverable_error_handler.h" |
19 | 19 |
| 20 using base::AutoLock; |
| 21 |
20 namespace browser_sync { | 22 namespace browser_sync { |
21 | 23 |
22 GenericChangeProcessor::GenericChangeProcessor( | 24 GenericChangeProcessor::GenericChangeProcessor( |
| 25 UnrecoverableErrorHandler* error_handler) |
| 26 : ChangeProcessor(error_handler), |
| 27 local_service_(NULL), |
| 28 user_share_(NULL), |
| 29 disconnected_(false) {} |
| 30 |
| 31 GenericChangeProcessor::GenericChangeProcessor( |
| 32 UnrecoverableErrorHandler* error_handler, |
23 SyncableService* local_service, | 33 SyncableService* local_service, |
24 UnrecoverableErrorHandler* error_handler, | |
25 sync_api::UserShare* user_share) | 34 sync_api::UserShare* user_share) |
26 : ChangeProcessor(error_handler), | 35 : ChangeProcessor(error_handler), |
27 local_service_(local_service), | 36 local_service_(NULL), |
28 user_share_(user_share) { | 37 user_share_(NULL), |
29 DCHECK(local_service_); | 38 disconnected_(false) { |
| 39 Connect(local_service, user_share); |
30 } | 40 } |
31 | 41 |
32 GenericChangeProcessor::~GenericChangeProcessor() { | 42 GenericChangeProcessor::~GenericChangeProcessor() {} |
33 // Set to null to ensure it's not used after destruction. | 43 |
34 local_service_ = NULL; | 44 // Connect to the syncer and SyncableService specified. |
| 45 void GenericChangeProcessor::Connect(SyncableService* local_service, |
| 46 sync_api::UserShare* user_share) { |
| 47 AutoLock lock(monitor_lock_); |
| 48 if (disconnected_) |
| 49 return; |
| 50 DCHECK(local_service); |
| 51 local_service_ = local_service; |
| 52 user_share_ = user_share; |
| 53 } |
| 54 |
| 55 void GenericChangeProcessor::Disconnect() { |
| 56 VLOG(1) << "Disconnecting change processor."; |
| 57 AutoLock lock(monitor_lock_); |
| 58 disconnected_ = true; |
35 } | 59 } |
36 | 60 |
37 void GenericChangeProcessor::ApplyChangesFromSyncModel( | 61 void GenericChangeProcessor::ApplyChangesFromSyncModel( |
38 const sync_api::BaseTransaction* trans, | 62 const sync_api::BaseTransaction* trans, |
39 const sync_api::ImmutableChangeRecordList& changes) { | 63 const sync_api::ImmutableChangeRecordList& changes) { |
40 DCHECK(running()); | 64 DCHECK(running()); |
41 DCHECK(syncer_changes_.empty()); | 65 DCHECK(syncer_changes_.empty()); |
42 for (sync_api::ChangeRecordList::const_iterator it = | 66 for (sync_api::ChangeRecordList::const_iterator it = |
43 changes.Get().begin(); it != changes.Get().end(); ++it) { | 67 changes.Get().begin(); it != changes.Get().end(); ++it) { |
44 if (it->action == sync_api::ChangeRecord::ACTION_DELETE) { | 68 if (it->action == sync_api::ChangeRecord::ACTION_DELETE) { |
(...skipping 18 matching lines...) Expand all Loading... |
63 } | 87 } |
64 } | 88 } |
65 } | 89 } |
66 | 90 |
67 void GenericChangeProcessor::CommitChangesFromSyncModel() { | 91 void GenericChangeProcessor::CommitChangesFromSyncModel() { |
68 if (!running()) | 92 if (!running()) |
69 return; | 93 return; |
70 if (syncer_changes_.empty()) | 94 if (syncer_changes_.empty()) |
71 return; | 95 return; |
72 SyncError error = local_service_->ProcessSyncChanges(FROM_HERE, | 96 SyncError error = local_service_->ProcessSyncChanges(FROM_HERE, |
73 syncer_changes_); | 97 syncer_changes_); |
74 syncer_changes_.clear(); | 98 syncer_changes_.clear(); |
75 if (error.IsSet()) { | 99 if (error.IsSet()) { |
76 error_handler()->OnUnrecoverableError(error.location(), error.message()); | 100 error_handler()->OnUnrecoverableError(error.location(), error.message()); |
77 } | 101 } |
78 } | 102 } |
79 | 103 |
80 SyncError GenericChangeProcessor::GetSyncDataForType( | 104 SyncError GenericChangeProcessor::GetSyncDataForType( |
81 syncable::ModelType type, | 105 syncable::ModelType type, |
82 SyncDataList* current_sync_data) { | 106 SyncDataList* current_sync_data) { |
| 107 AutoLock lock(monitor_lock_); |
| 108 if (disconnected_) { |
| 109 SyncError error(FROM_HERE, "Change processor disconnected.", type); |
| 110 return error; |
| 111 } |
83 std::string type_name = syncable::ModelTypeToString(type); | 112 std::string type_name = syncable::ModelTypeToString(type); |
84 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); | 113 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); |
85 sync_api::ReadNode root(&trans); | 114 sync_api::ReadNode root(&trans); |
86 if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { | 115 if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { |
87 SyncError error(FROM_HERE, | 116 SyncError error(FROM_HERE, |
88 "Server did not create the top-level " + type_name + | 117 "Server did not create the top-level " + type_name + |
89 " node. We might be running against an out-of-date server.", | 118 " node. We might be running against an out-of-date server.", |
90 type); | 119 type); |
91 return error; | 120 return error; |
92 } | 121 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } | 159 } |
131 node->Remove(); | 160 node->Remove(); |
132 return true; | 161 return true; |
133 } | 162 } |
134 | 163 |
135 } // namespace | 164 } // namespace |
136 | 165 |
137 SyncError GenericChangeProcessor::ProcessSyncChanges( | 166 SyncError GenericChangeProcessor::ProcessSyncChanges( |
138 const tracked_objects::Location& from_here, | 167 const tracked_objects::Location& from_here, |
139 const SyncChangeList& list_of_changes) { | 168 const SyncChangeList& list_of_changes) { |
| 169 AutoLock lock(monitor_lock_); |
| 170 if (disconnected_) { |
| 171 syncable::ModelType type; |
| 172 if (list_of_changes.size() > 0) { |
| 173 type = list_of_changes[0].sync_data().GetDataType(); |
| 174 local_service_->StopSyncing(type); |
| 175 } |
| 176 SyncError error(FROM_HERE, "Change processor disconnected.", |
| 177 syncable::UNSPECIFIED); |
| 178 return error; |
| 179 } |
140 sync_api::WriteTransaction trans(from_here, share_handle()); | 180 sync_api::WriteTransaction trans(from_here, share_handle()); |
141 | 181 |
142 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); | 182 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); |
143 iter != list_of_changes.end(); | 183 iter != list_of_changes.end(); |
144 ++iter) { | 184 ++iter) { |
145 const SyncChange& change = *iter; | 185 const SyncChange& change = *iter; |
146 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED); | 186 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED); |
147 syncable::ModelType type = change.sync_data().GetDataType(); | 187 syncable::ModelType type = change.sync_data().GetDataType(); |
148 std::string type_str = syncable::ModelTypeToString(type); | 188 std::string type_str = syncable::ModelTypeToString(type); |
149 sync_api::WriteNode sync_node(&trans); | 189 sync_api::WriteNode sync_node(&trans); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 error.message()); | 249 error.message()); |
210 return error; | 250 return error; |
211 } | 251 } |
212 } | 252 } |
213 return SyncError(); | 253 return SyncError(); |
214 } | 254 } |
215 | 255 |
216 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes( | 256 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes( |
217 syncable::ModelType type, | 257 syncable::ModelType type, |
218 bool* has_nodes) { | 258 bool* has_nodes) { |
| 259 AutoLock lock(monitor_lock_); |
| 260 if (disconnected_) { |
| 261 LOG(ERROR) << "Change processor disconnected."; |
| 262 return false; |
| 263 } |
219 DCHECK(has_nodes); | 264 DCHECK(has_nodes); |
220 DCHECK_NE(type, syncable::UNSPECIFIED); | 265 DCHECK_NE(type, syncable::UNSPECIFIED); |
221 std::string type_name = syncable::ModelTypeToString(type); | 266 std::string type_name = syncable::ModelTypeToString(type); |
222 std::string err_str = "Server did not create the top-level " + type_name + | 267 std::string err_str = "Server did not create the top-level " + type_name + |
223 " node. We might be running against an out-of-date server."; | 268 " node. We might be running against an out-of-date server."; |
224 *has_nodes = false; | 269 *has_nodes = false; |
225 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); | 270 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); |
226 sync_api::ReadNode type_root_node(&trans); | 271 sync_api::ReadNode type_root_node(&trans); |
227 if (!type_root_node.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { | 272 if (!type_root_node.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { |
228 LOG(ERROR) << err_str; | 273 LOG(ERROR) << err_str; |
229 return false; | 274 return false; |
230 } | 275 } |
231 | 276 |
232 // The sync model has user created nodes if the type's root node has any | 277 // The sync model has user created nodes if the type's root node has any |
233 // children. | 278 // children. |
234 *has_nodes = sync_api::kInvalidId != type_root_node.GetFirstChildId(); | 279 *has_nodes = sync_api::kInvalidId != type_root_node.GetFirstChildId(); |
235 return true; | 280 return true; |
236 } | 281 } |
237 | 282 |
238 bool GenericChangeProcessor::CryptoReadyIfNecessary(syncable::ModelType type) { | 283 bool GenericChangeProcessor::CryptoReadyIfNecessary(syncable::ModelType type) { |
| 284 AutoLock lock(monitor_lock_); |
| 285 if (disconnected_) { |
| 286 LOG(ERROR) << "Change processor disconnected."; |
| 287 return true; // Otherwise we get into infinite spin waiting. |
| 288 } |
239 DCHECK_NE(type, syncable::UNSPECIFIED); | 289 DCHECK_NE(type, syncable::UNSPECIFIED); |
240 // We only access the cryptographer while holding a transaction. | 290 // We only access the cryptographer while holding a transaction. |
241 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); | 291 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); |
242 const syncable::ModelTypeSet& encrypted_types = | 292 const syncable::ModelTypeSet& encrypted_types = |
243 GetEncryptedTypes(&trans); | 293 GetEncryptedTypes(&trans); |
244 return encrypted_types.count(type) == 0 || | 294 return encrypted_types.count(type) == 0 || |
245 trans.GetCryptographer()->is_ready(); | 295 trans.GetCryptographer()->is_ready(); |
246 } | 296 } |
247 | 297 |
248 void GenericChangeProcessor::StartImpl(Profile* profile) {} | 298 void GenericChangeProcessor::StartImpl(Profile* profile) {} |
249 | 299 |
250 void GenericChangeProcessor::StopImpl() {} | 300 void GenericChangeProcessor::StopImpl() { |
| 301 Disconnect(); |
| 302 } |
251 | 303 |
252 sync_api::UserShare* GenericChangeProcessor::share_handle() { | 304 sync_api::UserShare* GenericChangeProcessor::share_handle() { |
253 return user_share_; | 305 return user_share_; |
254 } | 306 } |
255 | 307 |
256 } // namespace browser_sync | 308 } // namespace browser_sync |
OLD | NEW |