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