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 namespace browser_sync { | 20 namespace browser_sync { |
21 | 21 |
22 GenericChangeProcessor::GenericChangeProcessor( | 22 GenericChangeProcessor::GenericChangeProcessor( |
| 23 UnrecoverableErrorHandler* error_handler) |
| 24 : ChangeProcessor(error_handler), |
| 25 local_service_(NULL), |
| 26 share_handle_(NULL) {} |
| 27 |
| 28 GenericChangeProcessor::GenericChangeProcessor( |
| 29 UnrecoverableErrorHandler* error_handler, |
23 SyncableService* local_service, | 30 SyncableService* local_service, |
24 UnrecoverableErrorHandler* error_handler, | |
25 sync_api::UserShare* user_share) | 31 sync_api::UserShare* user_share) |
26 : ChangeProcessor(error_handler), | 32 : ChangeProcessor(error_handler), |
27 local_service_(local_service), | 33 local_service_(local_service), |
28 user_share_(user_share) { | 34 share_handle_(user_share) { |
29 DCHECK(local_service_); | |
30 } | 35 } |
31 | 36 |
32 GenericChangeProcessor::~GenericChangeProcessor() { | 37 GenericChangeProcessor::~GenericChangeProcessor() {} |
33 // Set to null to ensure it's not used after destruction. | |
34 local_service_ = NULL; | |
35 } | |
36 | 38 |
37 void GenericChangeProcessor::ApplyChangesFromSyncModel( | 39 void GenericChangeProcessor::ApplyChangesFromSyncModel( |
38 const sync_api::BaseTransaction* trans, | 40 const sync_api::BaseTransaction* trans, |
39 const sync_api::ImmutableChangeRecordList& changes) { | 41 const sync_api::ImmutableChangeRecordList& changes) { |
| 42 DCHECK(CalledOnValidThread()); |
40 DCHECK(running()); | 43 DCHECK(running()); |
41 DCHECK(syncer_changes_.empty()); | 44 DCHECK(syncer_changes_.empty()); |
42 for (sync_api::ChangeRecordList::const_iterator it = | 45 for (sync_api::ChangeRecordList::const_iterator it = |
43 changes.Get().begin(); it != changes.Get().end(); ++it) { | 46 changes.Get().begin(); it != changes.Get().end(); ++it) { |
44 if (it->action == sync_api::ChangeRecord::ACTION_DELETE) { | 47 if (it->action == sync_api::ChangeRecord::ACTION_DELETE) { |
45 syncer_changes_.push_back( | 48 syncer_changes_.push_back( |
46 SyncChange(SyncChange::ACTION_DELETE, | 49 SyncChange(SyncChange::ACTION_DELETE, |
47 SyncData::CreateRemoteData(it->id, it->specifics))); | 50 SyncData::CreateRemoteData(it->id, it->specifics))); |
48 } else { | 51 } else { |
49 SyncChange::SyncChangeType action = | 52 SyncChange::SyncChangeType action = |
50 (it->action == sync_api::ChangeRecord::ACTION_ADD) ? | 53 (it->action == sync_api::ChangeRecord::ACTION_ADD) ? |
51 SyncChange::ACTION_ADD : SyncChange::ACTION_UPDATE; | 54 SyncChange::ACTION_ADD : SyncChange::ACTION_UPDATE; |
52 // Need to load specifics from node. | 55 // Need to load specifics from node. |
53 sync_api::ReadNode read_node(trans); | 56 sync_api::ReadNode read_node(trans); |
54 if (!read_node.InitByIdLookup(it->id)) { | 57 if (!read_node.InitByIdLookup(it->id)) { |
55 error_handler()->OnUnrecoverableError(FROM_HERE, "Failed to look up " | 58 error_handler()->OnUnrecoverableError(FROM_HERE, "Failed to look up " |
56 " data for received change with id " + it->id); | 59 " data for received change with id " + it->id); |
57 return; | 60 return; |
58 } | 61 } |
59 syncer_changes_.push_back( | 62 syncer_changes_.push_back( |
60 SyncChange(action, | 63 SyncChange(action, |
61 SyncData::CreateRemoteData( | 64 SyncData::CreateRemoteData( |
62 it->id, read_node.GetEntitySpecifics()))); | 65 it->id, read_node.GetEntitySpecifics()))); |
63 } | 66 } |
64 } | 67 } |
65 } | 68 } |
66 | 69 |
67 void GenericChangeProcessor::CommitChangesFromSyncModel() { | 70 void GenericChangeProcessor::CommitChangesFromSyncModel() { |
| 71 DCHECK(CalledOnValidThread()); |
68 if (!running()) | 72 if (!running()) |
69 return; | 73 return; |
70 if (syncer_changes_.empty()) | 74 if (syncer_changes_.empty()) |
71 return; | 75 return; |
72 SyncError error = local_service_->ProcessSyncChanges(FROM_HERE, | 76 SyncError error = local_service_->ProcessSyncChanges(FROM_HERE, |
73 syncer_changes_); | 77 syncer_changes_); |
74 syncer_changes_.clear(); | 78 syncer_changes_.clear(); |
75 if (error.IsSet()) { | 79 if (error.IsSet()) { |
76 error_handler()->OnUnrecoverableError(error.location(), error.message()); | 80 error_handler()->OnUnrecoverableError(error.location(), error.message()); |
77 } | 81 } |
78 } | 82 } |
79 | 83 |
80 SyncError GenericChangeProcessor::GetSyncDataForType( | 84 SyncError GenericChangeProcessor::GetSyncDataForType( |
81 syncable::ModelType type, | 85 syncable::ModelType type, |
82 SyncDataList* current_sync_data) { | 86 SyncDataList* current_sync_data) { |
| 87 DCHECK(CalledOnValidThread()); |
83 std::string type_name = syncable::ModelTypeToString(type); | 88 std::string type_name = syncable::ModelTypeToString(type); |
84 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); | 89 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); |
85 sync_api::ReadNode root(&trans); | 90 sync_api::ReadNode root(&trans); |
86 if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { | 91 if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { |
87 SyncError error(FROM_HERE, | 92 SyncError error(FROM_HERE, |
88 "Server did not create the top-level " + type_name + | 93 "Server did not create the top-level " + type_name + |
89 " node. We might be running against an out-of-date server.", | 94 " node. We might be running against an out-of-date server.", |
90 type); | 95 type); |
91 return error; | 96 return error; |
92 } | 97 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } | 135 } |
131 node->Remove(); | 136 node->Remove(); |
132 return true; | 137 return true; |
133 } | 138 } |
134 | 139 |
135 } // namespace | 140 } // namespace |
136 | 141 |
137 SyncError GenericChangeProcessor::ProcessSyncChanges( | 142 SyncError GenericChangeProcessor::ProcessSyncChanges( |
138 const tracked_objects::Location& from_here, | 143 const tracked_objects::Location& from_here, |
139 const SyncChangeList& list_of_changes) { | 144 const SyncChangeList& list_of_changes) { |
| 145 DCHECK(CalledOnValidThread()); |
140 sync_api::WriteTransaction trans(from_here, share_handle()); | 146 sync_api::WriteTransaction trans(from_here, share_handle()); |
141 | 147 |
142 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); | 148 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); |
143 iter != list_of_changes.end(); | 149 iter != list_of_changes.end(); |
144 ++iter) { | 150 ++iter) { |
145 const SyncChange& change = *iter; | 151 const SyncChange& change = *iter; |
146 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED); | 152 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED); |
147 syncable::ModelType type = change.sync_data().GetDataType(); | 153 syncable::ModelType type = change.sync_data().GetDataType(); |
148 std::string type_str = syncable::ModelTypeToString(type); | 154 std::string type_str = syncable::ModelTypeToString(type); |
149 sync_api::WriteNode sync_node(&trans); | 155 sync_api::WriteNode sync_node(&trans); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 error.message()); | 215 error.message()); |
210 return error; | 216 return error; |
211 } | 217 } |
212 } | 218 } |
213 return SyncError(); | 219 return SyncError(); |
214 } | 220 } |
215 | 221 |
216 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes( | 222 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes( |
217 syncable::ModelType type, | 223 syncable::ModelType type, |
218 bool* has_nodes) { | 224 bool* has_nodes) { |
| 225 DCHECK(CalledOnValidThread()); |
219 DCHECK(has_nodes); | 226 DCHECK(has_nodes); |
220 DCHECK_NE(type, syncable::UNSPECIFIED); | 227 DCHECK_NE(type, syncable::UNSPECIFIED); |
221 std::string type_name = syncable::ModelTypeToString(type); | 228 std::string type_name = syncable::ModelTypeToString(type); |
222 std::string err_str = "Server did not create the top-level " + type_name + | 229 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."; | 230 " node. We might be running against an out-of-date server."; |
224 *has_nodes = false; | 231 *has_nodes = false; |
225 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); | 232 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); |
226 sync_api::ReadNode type_root_node(&trans); | 233 sync_api::ReadNode type_root_node(&trans); |
227 if (!type_root_node.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { | 234 if (!type_root_node.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { |
228 LOG(ERROR) << err_str; | 235 LOG(ERROR) << err_str; |
229 return false; | 236 return false; |
230 } | 237 } |
231 | 238 |
232 // The sync model has user created nodes if the type's root node has any | 239 // The sync model has user created nodes if the type's root node has any |
233 // children. | 240 // children. |
234 *has_nodes = sync_api::kInvalidId != type_root_node.GetFirstChildId(); | 241 *has_nodes = sync_api::kInvalidId != type_root_node.GetFirstChildId(); |
235 return true; | 242 return true; |
236 } | 243 } |
237 | 244 |
238 bool GenericChangeProcessor::CryptoReadyIfNecessary(syncable::ModelType type) { | 245 bool GenericChangeProcessor::CryptoReadyIfNecessary(syncable::ModelType type) { |
| 246 DCHECK(CalledOnValidThread()); |
239 DCHECK_NE(type, syncable::UNSPECIFIED); | 247 DCHECK_NE(type, syncable::UNSPECIFIED); |
240 // We only access the cryptographer while holding a transaction. | 248 // We only access the cryptographer while holding a transaction. |
241 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); | 249 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); |
242 const syncable::ModelTypeSet& encrypted_types = | 250 const syncable::ModelTypeSet& encrypted_types = |
243 GetEncryptedTypes(&trans); | 251 GetEncryptedTypes(&trans); |
244 return encrypted_types.count(type) == 0 || | 252 return encrypted_types.count(type) == 0 || |
245 trans.GetCryptographer()->is_ready(); | 253 trans.GetCryptographer()->is_ready(); |
246 } | 254 } |
247 | 255 |
248 void GenericChangeProcessor::StartImpl(Profile* profile) {} | 256 void GenericChangeProcessor::set_local_service(SyncableService* local_service) { |
| 257 DCHECK(CalledOnValidThread()); |
| 258 local_service_ = local_service; |
| 259 } |
249 | 260 |
250 void GenericChangeProcessor::StopImpl() {} | 261 SyncableService* GenericChangeProcessor::local_service() const { |
| 262 DCHECK(CalledOnValidThread()); |
| 263 return local_service_; |
| 264 } |
251 | 265 |
252 sync_api::UserShare* GenericChangeProcessor::share_handle() { | 266 void GenericChangeProcessor::set_share_handle(sync_api::UserShare* user_share) { |
253 return user_share_; | 267 DCHECK(CalledOnValidThread()); |
| 268 share_handle_ = user_share; |
| 269 } |
| 270 |
| 271 void GenericChangeProcessor::StartImpl(Profile* profile) { |
| 272 DCHECK(CalledOnValidThread()); |
| 273 } |
| 274 |
| 275 void GenericChangeProcessor::StopImpl() { |
| 276 } |
| 277 |
| 278 sync_api::UserShare* GenericChangeProcessor::share_handle() const { |
| 279 DCHECK(CalledOnValidThread()); |
| 280 return share_handle_; |
254 } | 281 } |
255 | 282 |
256 } // namespace browser_sync | 283 } // namespace browser_sync |
OLD | NEW |