| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/sync/driver/generic_change_processor.h" | 5 #include "components/sync/driver/generic_change_processor.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "components/sync/core/base_node.h" | 21 #include "components/sync/core/base_node.h" |
| 22 #include "components/sync/core/change_record.h" | 22 #include "components/sync/core/change_record.h" |
| 23 #include "components/sync/core/read_node.h" | 23 #include "components/sync/core/read_node.h" |
| 24 #include "components/sync/core/read_transaction.h" | 24 #include "components/sync/core/read_transaction.h" |
| 25 #include "components/sync/core/write_node.h" | 25 #include "components/sync/core/write_node.h" |
| 26 #include "components/sync/core/write_transaction.h" | 26 #include "components/sync/core/write_transaction.h" |
| 27 #include "components/sync/driver/sync_api_component_factory.h" | 27 #include "components/sync/driver/sync_api_component_factory.h" |
| 28 #include "components/sync/driver/sync_client.h" | 28 #include "components/sync/driver/sync_client.h" |
| 29 #include "components/sync/syncable/entry.h" // TODO(tim): Bug 123674. | 29 #include "components/sync/syncable/entry.h" // TODO(tim): Bug 123674. |
| 30 | 30 |
| 31 namespace syncer { | 31 namespace sync_driver { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 const int kContextSizeLimit = 1024; // Datatype context size limit. | 35 const int kContextSizeLimit = 1024; // Datatype context size limit. |
| 36 | 36 |
| 37 void SetNodeSpecifics(const sync_pb::EntitySpecifics& entity_specifics, | 37 void SetNodeSpecifics(const sync_pb::EntitySpecifics& entity_specifics, |
| 38 WriteNode* write_node) { | 38 syncer::WriteNode* write_node) { |
| 39 if (GetModelTypeFromSpecifics(entity_specifics) == PASSWORDS) { | 39 if (syncer::GetModelTypeFromSpecifics(entity_specifics) == |
| 40 syncer::PASSWORDS) { |
| 40 write_node->SetPasswordSpecifics( | 41 write_node->SetPasswordSpecifics( |
| 41 entity_specifics.password().client_only_encrypted_data()); | 42 entity_specifics.password().client_only_encrypted_data()); |
| 42 } else { | 43 } else { |
| 43 write_node->SetEntitySpecifics(entity_specifics); | 44 write_node->SetEntitySpecifics(entity_specifics); |
| 44 } | 45 } |
| 45 } | 46 } |
| 46 | 47 |
| 47 // Helper function to convert AttachmentId to AttachmentMetadataRecord. | 48 // Helper function to convert AttachmentId to AttachmentMetadataRecord. |
| 48 sync_pb::AttachmentMetadataRecord AttachmentIdToRecord( | 49 sync_pb::AttachmentMetadataRecord AttachmentIdToRecord( |
| 49 const AttachmentId& attachment_id) { | 50 const syncer::AttachmentId& attachment_id) { |
| 50 sync_pb::AttachmentMetadataRecord record; | 51 sync_pb::AttachmentMetadataRecord record; |
| 51 *record.mutable_id() = attachment_id.GetProto(); | 52 *record.mutable_id() = attachment_id.GetProto(); |
| 52 return record; | 53 return record; |
| 53 } | 54 } |
| 54 | 55 |
| 55 // Replace |write_nodes|'s attachment ids with |attachment_ids|. | 56 // Replace |write_nodes|'s attachment ids with |attachment_ids|. |
| 56 void SetAttachmentMetadata(const AttachmentIdList& attachment_ids, | 57 void SetAttachmentMetadata(const syncer::AttachmentIdList& attachment_ids, |
| 57 WriteNode* write_node) { | 58 syncer::WriteNode* write_node) { |
| 58 DCHECK(write_node); | 59 DCHECK(write_node); |
| 59 sync_pb::AttachmentMetadata attachment_metadata; | 60 sync_pb::AttachmentMetadata attachment_metadata; |
| 60 std::transform( | 61 std::transform( |
| 61 attachment_ids.begin(), attachment_ids.end(), | 62 attachment_ids.begin(), attachment_ids.end(), |
| 62 RepeatedFieldBackInserter(attachment_metadata.mutable_record()), | 63 RepeatedFieldBackInserter(attachment_metadata.mutable_record()), |
| 63 AttachmentIdToRecord); | 64 AttachmentIdToRecord); |
| 64 write_node->SetAttachmentMetadata(attachment_metadata); | 65 write_node->SetAttachmentMetadata(attachment_metadata); |
| 65 } | 66 } |
| 66 | 67 |
| 67 SyncData BuildRemoteSyncData( | 68 syncer::SyncData BuildRemoteSyncData( |
| 68 int64_t sync_id, | 69 int64_t sync_id, |
| 69 const ReadNode& read_node, | 70 const syncer::ReadNode& read_node, |
| 70 const AttachmentServiceProxy& attachment_service_proxy) { | 71 const syncer::AttachmentServiceProxy& attachment_service_proxy) { |
| 71 const AttachmentIdList& attachment_ids = read_node.GetAttachmentIds(); | 72 const syncer::AttachmentIdList& attachment_ids = read_node.GetAttachmentIds(); |
| 72 switch (read_node.GetModelType()) { | 73 switch (read_node.GetModelType()) { |
| 73 case PASSWORDS: { | 74 case syncer::PASSWORDS: { |
| 74 // Passwords must be accessed differently, to account for their | 75 // Passwords must be accessed differently, to account for their |
| 75 // encryption, and stored into a temporary EntitySpecifics. | 76 // encryption, and stored into a temporary EntitySpecifics. |
| 76 sync_pb::EntitySpecifics password_holder; | 77 sync_pb::EntitySpecifics password_holder; |
| 77 password_holder.mutable_password() | 78 password_holder.mutable_password() |
| 78 ->mutable_client_only_encrypted_data() | 79 ->mutable_client_only_encrypted_data() |
| 79 ->CopyFrom(read_node.GetPasswordSpecifics()); | 80 ->CopyFrom(read_node.GetPasswordSpecifics()); |
| 80 return SyncData::CreateRemoteData( | 81 return syncer::SyncData::CreateRemoteData( |
| 81 sync_id, password_holder, read_node.GetModificationTime(), | 82 sync_id, password_holder, read_node.GetModificationTime(), |
| 82 attachment_ids, attachment_service_proxy); | 83 attachment_ids, attachment_service_proxy); |
| 83 } | 84 } |
| 84 case SESSIONS: | 85 case syncer::SESSIONS: |
| 85 // Include tag hashes for sessions data type to allow discarding during | 86 // Include tag hashes for sessions data type to allow discarding during |
| 86 // merge if re-hashing by the service gives a different value. This is to | 87 // merge if re-hashing by the service gives a different value. This is to |
| 87 // allow removal of incorrectly hashed values, see crbug.com/604657. This | 88 // allow removal of incorrectly hashed values, see crbug.com/604657. This |
| 88 // cannot be done in the processor because only the service knows how to | 89 // cannot be done in the processor because only the service knows how to |
| 89 // generate a tag from the specifics. We don't set this value for other | 90 // generate a tag from the specifics. We don't set this value for other |
| 90 // data types because they shouldn't need it and it costs memory to hold | 91 // data types because they shouldn't need it and it costs memory to hold |
| 91 // another copy of this string around. | 92 // another copy of this string around. |
| 92 return SyncData::CreateRemoteData( | 93 return syncer::SyncData::CreateRemoteData( |
| 93 sync_id, read_node.GetEntitySpecifics(), | 94 sync_id, read_node.GetEntitySpecifics(), |
| 94 read_node.GetModificationTime(), attachment_ids, | 95 read_node.GetModificationTime(), attachment_ids, |
| 95 attachment_service_proxy, read_node.GetEntry()->GetUniqueClientTag()); | 96 attachment_service_proxy, read_node.GetEntry()->GetUniqueClientTag()); |
| 96 default: | 97 default: |
| 97 // Use the specifics directly, encryption has already been handled. | 98 // Use the specifics directly, encryption has already been handled. |
| 98 return SyncData::CreateRemoteData(sync_id, read_node.GetEntitySpecifics(), | 99 return syncer::SyncData::CreateRemoteData( |
| 99 read_node.GetModificationTime(), | 100 sync_id, read_node.GetEntitySpecifics(), |
| 100 attachment_ids, | 101 read_node.GetModificationTime(), attachment_ids, |
| 101 attachment_service_proxy); | 102 attachment_service_proxy); |
| 102 } | 103 } |
| 103 } | 104 } |
| 104 | 105 |
| 105 } // namespace | 106 } // namespace |
| 106 | 107 |
| 107 GenericChangeProcessor::GenericChangeProcessor( | 108 GenericChangeProcessor::GenericChangeProcessor( |
| 108 ModelType type, | 109 syncer::ModelType type, |
| 109 std::unique_ptr<DataTypeErrorHandler> error_handler, | 110 std::unique_ptr<syncer::DataTypeErrorHandler> error_handler, |
| 110 const base::WeakPtr<SyncableService>& local_service, | 111 const base::WeakPtr<syncer::SyncableService>& local_service, |
| 111 const base::WeakPtr<SyncMergeResult>& merge_result, | 112 const base::WeakPtr<syncer::SyncMergeResult>& merge_result, |
| 112 UserShare* user_share, | 113 syncer::UserShare* user_share, |
| 113 SyncClient* sync_client, | 114 SyncClient* sync_client, |
| 114 std::unique_ptr<AttachmentStoreForSync> attachment_store) | 115 std::unique_ptr<syncer::AttachmentStoreForSync> attachment_store) |
| 115 : ChangeProcessor(std::move(error_handler)), | 116 : ChangeProcessor(std::move(error_handler)), |
| 116 type_(type), | 117 type_(type), |
| 117 local_service_(local_service), | 118 local_service_(local_service), |
| 118 merge_result_(merge_result), | 119 merge_result_(merge_result), |
| 119 share_handle_(user_share), | 120 share_handle_(user_share), |
| 120 weak_ptr_factory_(this) { | 121 weak_ptr_factory_(this) { |
| 121 DCHECK(CalledOnValidThread()); | 122 DCHECK(CalledOnValidThread()); |
| 122 DCHECK_NE(type_, UNSPECIFIED); | 123 DCHECK_NE(type_, syncer::UNSPECIFIED); |
| 123 if (attachment_store) { | 124 if (attachment_store) { |
| 124 std::string store_birthday; | 125 std::string store_birthday; |
| 125 { | 126 { |
| 126 ReadTransaction trans(FROM_HERE, share_handle()); | 127 syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| 127 store_birthday = trans.GetStoreBirthday(); | 128 store_birthday = trans.GetStoreBirthday(); |
| 128 } | 129 } |
| 129 attachment_service_ = | 130 attachment_service_ = |
| 130 sync_client->GetSyncApiComponentFactory()->CreateAttachmentService( | 131 sync_client->GetSyncApiComponentFactory()->CreateAttachmentService( |
| 131 std::move(attachment_store), *user_share, store_birthday, type, | 132 std::move(attachment_store), *user_share, store_birthday, type, |
| 132 this); | 133 this); |
| 133 attachment_service_weak_ptr_factory_.reset( | 134 attachment_service_weak_ptr_factory_.reset( |
| 134 new base::WeakPtrFactory<AttachmentService>(attachment_service_.get())); | 135 new base::WeakPtrFactory<syncer::AttachmentService>( |
| 135 attachment_service_proxy_ = AttachmentServiceProxy( | 136 attachment_service_.get())); |
| 137 attachment_service_proxy_ = syncer::AttachmentServiceProxy( |
| 136 base::ThreadTaskRunnerHandle::Get(), | 138 base::ThreadTaskRunnerHandle::Get(), |
| 137 attachment_service_weak_ptr_factory_->GetWeakPtr()); | 139 attachment_service_weak_ptr_factory_->GetWeakPtr()); |
| 138 UploadAllAttachmentsNotOnServer(); | 140 UploadAllAttachmentsNotOnServer(); |
| 139 } else { | 141 } else { |
| 140 attachment_service_proxy_ = | 142 attachment_service_proxy_ = syncer::AttachmentServiceProxy( |
| 141 AttachmentServiceProxy(base::ThreadTaskRunnerHandle::Get(), | 143 base::ThreadTaskRunnerHandle::Get(), |
| 142 base::WeakPtr<AttachmentService>()); | 144 base::WeakPtr<syncer::AttachmentService>()); |
| 143 } | 145 } |
| 144 } | 146 } |
| 145 | 147 |
| 146 GenericChangeProcessor::~GenericChangeProcessor() { | 148 GenericChangeProcessor::~GenericChangeProcessor() { |
| 147 DCHECK(CalledOnValidThread()); | 149 DCHECK(CalledOnValidThread()); |
| 148 } | 150 } |
| 149 | 151 |
| 150 void GenericChangeProcessor::ApplyChangesFromSyncModel( | 152 void GenericChangeProcessor::ApplyChangesFromSyncModel( |
| 151 const BaseTransaction* trans, | 153 const syncer::BaseTransaction* trans, |
| 152 int64_t model_version, | 154 int64_t model_version, |
| 153 const ImmutableChangeRecordList& changes) { | 155 const syncer::ImmutableChangeRecordList& changes) { |
| 154 DCHECK(CalledOnValidThread()); | 156 DCHECK(CalledOnValidThread()); |
| 155 DCHECK(syncer_changes_.empty()); | 157 DCHECK(syncer_changes_.empty()); |
| 156 for (ChangeRecordList::const_iterator it = changes.Get().begin(); | 158 for (syncer::ChangeRecordList::const_iterator it = changes.Get().begin(); |
| 157 it != changes.Get().end(); ++it) { | 159 it != changes.Get().end(); ++it) { |
| 158 if (it->action == ChangeRecord::ACTION_DELETE) { | 160 if (it->action == syncer::ChangeRecord::ACTION_DELETE) { |
| 159 std::unique_ptr<sync_pb::EntitySpecifics> specifics; | 161 std::unique_ptr<sync_pb::EntitySpecifics> specifics; |
| 160 if (it->specifics.has_password()) { | 162 if (it->specifics.has_password()) { |
| 161 DCHECK(it->extra.get()); | 163 DCHECK(it->extra.get()); |
| 162 specifics.reset(new sync_pb::EntitySpecifics(it->specifics)); | 164 specifics.reset(new sync_pb::EntitySpecifics(it->specifics)); |
| 163 specifics->mutable_password() | 165 specifics->mutable_password() |
| 164 ->mutable_client_only_encrypted_data() | 166 ->mutable_client_only_encrypted_data() |
| 165 ->CopyFrom(it->extra->unencrypted()); | 167 ->CopyFrom(it->extra->unencrypted()); |
| 166 } | 168 } |
| 167 const AttachmentIdList empty_list_of_attachment_ids; | 169 const syncer::AttachmentIdList empty_list_of_attachment_ids; |
| 168 syncer_changes_.push_back(SyncChange( | 170 syncer_changes_.push_back(syncer::SyncChange( |
| 169 FROM_HERE, SyncChange::ACTION_DELETE, | 171 FROM_HERE, syncer::SyncChange::ACTION_DELETE, |
| 170 SyncData::CreateRemoteData( | 172 syncer::SyncData::CreateRemoteData( |
| 171 it->id, specifics ? *specifics : it->specifics, base::Time(), | 173 it->id, specifics ? *specifics : it->specifics, base::Time(), |
| 172 empty_list_of_attachment_ids, attachment_service_proxy_))); | 174 empty_list_of_attachment_ids, attachment_service_proxy_))); |
| 173 } else { | 175 } else { |
| 174 SyncChange::SyncChangeType action = | 176 syncer::SyncChange::SyncChangeType action = |
| 175 (it->action == ChangeRecord::ACTION_ADD) ? SyncChange::ACTION_ADD | 177 (it->action == syncer::ChangeRecord::ACTION_ADD) |
| 176 : SyncChange::ACTION_UPDATE; | 178 ? syncer::SyncChange::ACTION_ADD |
| 179 : syncer::SyncChange::ACTION_UPDATE; |
| 177 // Need to load specifics from node. | 180 // Need to load specifics from node. |
| 178 ReadNode read_node(trans); | 181 syncer::ReadNode read_node(trans); |
| 179 if (read_node.InitByIdLookup(it->id) != BaseNode::INIT_OK) { | 182 if (read_node.InitByIdLookup(it->id) != syncer::BaseNode::INIT_OK) { |
| 180 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, | 183 syncer::SyncError error( |
| 181 "Failed to look up data for received change with id " + | 184 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 182 base::Int64ToString(it->id), | 185 "Failed to look up data for received change with id " + |
| 183 GetModelTypeFromSpecifics(it->specifics)); | 186 base::Int64ToString(it->id), |
| 187 syncer::GetModelTypeFromSpecifics(it->specifics)); |
| 184 error_handler()->OnUnrecoverableError(error); | 188 error_handler()->OnUnrecoverableError(error); |
| 185 return; | 189 return; |
| 186 } | 190 } |
| 187 syncer_changes_.push_back(SyncChange( | 191 syncer_changes_.push_back(syncer::SyncChange( |
| 188 FROM_HERE, action, | 192 FROM_HERE, action, |
| 189 BuildRemoteSyncData(it->id, read_node, attachment_service_proxy_))); | 193 BuildRemoteSyncData(it->id, read_node, attachment_service_proxy_))); |
| 190 } | 194 } |
| 191 } | 195 } |
| 192 } | 196 } |
| 193 | 197 |
| 194 void GenericChangeProcessor::CommitChangesFromSyncModel() { | 198 void GenericChangeProcessor::CommitChangesFromSyncModel() { |
| 195 DCHECK(CalledOnValidThread()); | 199 DCHECK(CalledOnValidThread()); |
| 196 if (syncer_changes_.empty()) | 200 if (syncer_changes_.empty()) |
| 197 return; | 201 return; |
| 198 if (!local_service_.get()) { | 202 if (!local_service_.get()) { |
| 199 ModelType type = syncer_changes_[0].sync_data().GetDataType(); | 203 syncer::ModelType type = syncer_changes_[0].sync_data().GetDataType(); |
| 200 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, | 204 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 201 "Local service destroyed.", type); | 205 "Local service destroyed.", type); |
| 202 error_handler()->OnUnrecoverableError(error); | 206 error_handler()->OnUnrecoverableError(error); |
| 203 return; | 207 return; |
| 204 } | 208 } |
| 205 SyncError error = | 209 syncer::SyncError error = |
| 206 local_service_->ProcessSyncChanges(FROM_HERE, syncer_changes_); | 210 local_service_->ProcessSyncChanges(FROM_HERE, syncer_changes_); |
| 207 syncer_changes_.clear(); | 211 syncer_changes_.clear(); |
| 208 if (error.IsSet()) | 212 if (error.IsSet()) |
| 209 error_handler()->OnUnrecoverableError(error); | 213 error_handler()->OnUnrecoverableError(error); |
| 210 } | 214 } |
| 211 | 215 |
| 212 SyncDataList GenericChangeProcessor::GetAllSyncData(ModelType type) const { | 216 syncer::SyncDataList GenericChangeProcessor::GetAllSyncData( |
| 217 syncer::ModelType type) const { |
| 213 DCHECK_EQ(type_, type); | 218 DCHECK_EQ(type_, type); |
| 214 // This is slow / memory intensive. Should be used sparingly by datatypes. | 219 // This is slow / memory intensive. Should be used sparingly by datatypes. |
| 215 SyncDataList data; | 220 syncer::SyncDataList data; |
| 216 GetAllSyncDataReturnError(&data); | 221 GetAllSyncDataReturnError(&data); |
| 217 return data; | 222 return data; |
| 218 } | 223 } |
| 219 | 224 |
| 220 SyncError GenericChangeProcessor::UpdateDataTypeContext( | 225 syncer::SyncError GenericChangeProcessor::UpdateDataTypeContext( |
| 221 ModelType type, | 226 syncer::ModelType type, |
| 222 SyncChangeProcessor::ContextRefreshStatus refresh_status, | 227 syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status, |
| 223 const std::string& context) { | 228 const std::string& context) { |
| 224 DCHECK(ProtocolTypes().Has(type)); | 229 DCHECK(syncer::ProtocolTypes().Has(type)); |
| 225 DCHECK_EQ(type_, type); | 230 DCHECK_EQ(type_, type); |
| 226 | 231 |
| 227 if (context.size() > static_cast<size_t>(kContextSizeLimit)) { | 232 if (context.size() > static_cast<size_t>(kContextSizeLimit)) { |
| 228 return SyncError(FROM_HERE, SyncError::DATATYPE_ERROR, | 233 return syncer::SyncError(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 229 "Context size limit exceeded.", type); | 234 "Context size limit exceeded.", type); |
| 230 } | 235 } |
| 231 | 236 |
| 232 WriteTransaction trans(FROM_HERE, share_handle()); | 237 syncer::WriteTransaction trans(FROM_HERE, share_handle()); |
| 233 trans.SetDataTypeContext(type, refresh_status, context); | 238 trans.SetDataTypeContext(type, refresh_status, context); |
| 234 | 239 |
| 235 // TODO(zea): plumb a pointer to the PSS or SyncManagerImpl here so we can | 240 // TODO(zea): plumb a pointer to the PSS or SyncManagerImpl here so we can |
| 236 // trigger a datatype nudge if |refresh_status == REFRESH_NEEDED|. | 241 // trigger a datatype nudge if |refresh_status == REFRESH_NEEDED|. |
| 237 | 242 |
| 238 return SyncError(); | 243 return syncer::SyncError(); |
| 239 } | 244 } |
| 240 | 245 |
| 241 void GenericChangeProcessor::AddLocalChangeObserver( | 246 void GenericChangeProcessor::AddLocalChangeObserver( |
| 242 LocalChangeObserver* observer) { | 247 syncer::LocalChangeObserver* observer) { |
| 243 local_change_observers_.AddObserver(observer); | 248 local_change_observers_.AddObserver(observer); |
| 244 } | 249 } |
| 245 | 250 |
| 246 void GenericChangeProcessor::RemoveLocalChangeObserver( | 251 void GenericChangeProcessor::RemoveLocalChangeObserver( |
| 247 LocalChangeObserver* observer) { | 252 syncer::LocalChangeObserver* observer) { |
| 248 local_change_observers_.RemoveObserver(observer); | 253 local_change_observers_.RemoveObserver(observer); |
| 249 } | 254 } |
| 250 | 255 |
| 251 void GenericChangeProcessor::OnAttachmentUploaded( | 256 void GenericChangeProcessor::OnAttachmentUploaded( |
| 252 const AttachmentId& attachment_id) { | 257 const syncer::AttachmentId& attachment_id) { |
| 253 WriteTransaction trans(FROM_HERE, share_handle()); | 258 syncer::WriteTransaction trans(FROM_HERE, share_handle()); |
| 254 trans.UpdateEntriesMarkAttachmentAsOnServer(attachment_id); | 259 trans.UpdateEntriesMarkAttachmentAsOnServer(attachment_id); |
| 255 } | 260 } |
| 256 | 261 |
| 257 SyncError GenericChangeProcessor::GetAllSyncDataReturnError( | 262 syncer::SyncError GenericChangeProcessor::GetAllSyncDataReturnError( |
| 258 SyncDataList* current_sync_data) const { | 263 syncer::SyncDataList* current_sync_data) const { |
| 259 DCHECK(CalledOnValidThread()); | 264 DCHECK(CalledOnValidThread()); |
| 260 std::string type_name = ModelTypeToString(type_); | 265 std::string type_name = syncer::ModelTypeToString(type_); |
| 261 ReadTransaction trans(FROM_HERE, share_handle()); | 266 syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| 262 ReadNode root(&trans); | 267 syncer::ReadNode root(&trans); |
| 263 if (root.InitTypeRoot(type_) != BaseNode::INIT_OK) { | 268 if (root.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) { |
| 264 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, | 269 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 265 "Server did not create the top-level " + type_name + | 270 "Server did not create the top-level " + type_name + |
| 266 " node. We might be running against an out-of-" | 271 " node. We might be running against an out-of-" |
| 267 "date server.", | 272 "date server.", |
| 268 type_); | 273 type_); |
| 269 return error; | 274 return error; |
| 270 } | 275 } |
| 271 | 276 |
| 272 // TODO(akalin): We'll have to do a tree traversal for bookmarks. | 277 // TODO(akalin): We'll have to do a tree traversal for bookmarks. |
| 273 DCHECK_NE(type_, BOOKMARKS); | 278 DCHECK_NE(type_, syncer::BOOKMARKS); |
| 274 | 279 |
| 275 std::vector<int64_t> child_ids; | 280 std::vector<int64_t> child_ids; |
| 276 root.GetChildIds(&child_ids); | 281 root.GetChildIds(&child_ids); |
| 277 | 282 |
| 278 for (std::vector<int64_t>::iterator it = child_ids.begin(); | 283 for (std::vector<int64_t>::iterator it = child_ids.begin(); |
| 279 it != child_ids.end(); ++it) { | 284 it != child_ids.end(); ++it) { |
| 280 ReadNode sync_child_node(&trans); | 285 syncer::ReadNode sync_child_node(&trans); |
| 281 if (sync_child_node.InitByIdLookup(*it) != BaseNode::INIT_OK) { | 286 if (sync_child_node.InitByIdLookup(*it) != syncer::BaseNode::INIT_OK) { |
| 282 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, | 287 syncer::SyncError error( |
| 283 "Failed to fetch child node for type " + type_name + ".", | 288 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 284 type_); | 289 "Failed to fetch child node for type " + type_name + ".", type_); |
| 285 return error; | 290 return error; |
| 286 } | 291 } |
| 287 current_sync_data->push_back(BuildRemoteSyncData( | 292 current_sync_data->push_back(BuildRemoteSyncData( |
| 288 sync_child_node.GetId(), sync_child_node, attachment_service_proxy_)); | 293 sync_child_node.GetId(), sync_child_node, attachment_service_proxy_)); |
| 289 } | 294 } |
| 290 return SyncError(); | 295 return syncer::SyncError(); |
| 291 } | 296 } |
| 292 | 297 |
| 293 bool GenericChangeProcessor::GetDataTypeContext(std::string* context) const { | 298 bool GenericChangeProcessor::GetDataTypeContext(std::string* context) const { |
| 294 ReadTransaction trans(FROM_HERE, share_handle()); | 299 syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| 295 sync_pb::DataTypeContext context_proto; | 300 sync_pb::DataTypeContext context_proto; |
| 296 trans.GetDataTypeContext(type_, &context_proto); | 301 trans.GetDataTypeContext(type_, &context_proto); |
| 297 if (!context_proto.has_context()) | 302 if (!context_proto.has_context()) |
| 298 return false; | 303 return false; |
| 299 | 304 |
| 300 DCHECK_EQ(type_, | 305 DCHECK_EQ(type_, syncer::GetModelTypeFromSpecificsFieldNumber( |
| 301 GetModelTypeFromSpecificsFieldNumber(context_proto.data_type_id())); | 306 context_proto.data_type_id())); |
| 302 *context = context_proto.context(); | 307 *context = context_proto.context(); |
| 303 return true; | 308 return true; |
| 304 } | 309 } |
| 305 | 310 |
| 306 int GenericChangeProcessor::GetSyncCount() { | 311 int GenericChangeProcessor::GetSyncCount() { |
| 307 ReadTransaction trans(FROM_HERE, share_handle()); | 312 syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| 308 ReadNode root(&trans); | 313 syncer::ReadNode root(&trans); |
| 309 if (root.InitTypeRoot(type_) != BaseNode::INIT_OK) | 314 if (root.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) |
| 310 return 0; | 315 return 0; |
| 311 | 316 |
| 312 // Subtract one to account for type's root node. | 317 // Subtract one to account for type's root node. |
| 313 return root.GetTotalNodeCount() - 1; | 318 return root.GetTotalNodeCount() - 1; |
| 314 } | 319 } |
| 315 | 320 |
| 316 namespace { | 321 namespace { |
| 317 | 322 |
| 318 // WARNING: this code is sensitive to compiler optimizations. Be careful | 323 // WARNING: this code is sensitive to compiler optimizations. Be careful |
| 319 // modifying any code around an OnUnrecoverableError call, else the compiler | 324 // modifying any code around an OnUnrecoverableError call, else the compiler |
| 320 // attempts to merge it with other calls, losing useful information in | 325 // attempts to merge it with other calls, losing useful information in |
| 321 // breakpad uploads. | 326 // breakpad uploads. |
| 322 SyncError LogLookupFailure(BaseNode::InitByLookupResult lookup_result, | 327 syncer::SyncError LogLookupFailure( |
| 323 const tracked_objects::Location& from_here, | 328 syncer::BaseNode::InitByLookupResult lookup_result, |
| 324 const std::string& error_prefix, | 329 const tracked_objects::Location& from_here, |
| 325 ModelType type, | 330 const std::string& error_prefix, |
| 326 DataTypeErrorHandler* error_handler) { | 331 syncer::ModelType type, |
| 332 syncer::DataTypeErrorHandler* error_handler) { |
| 327 switch (lookup_result) { | 333 switch (lookup_result) { |
| 328 case BaseNode::INIT_FAILED_ENTRY_NOT_GOOD: { | 334 case syncer::BaseNode::INIT_FAILED_ENTRY_NOT_GOOD: { |
| 329 SyncError error; | 335 syncer::SyncError error; |
| 330 error.Reset( | 336 error.Reset( |
| 331 from_here, | 337 from_here, |
| 332 error_prefix + "could not find entry matching the lookup criteria.", | 338 error_prefix + "could not find entry matching the lookup criteria.", |
| 333 type); | 339 type); |
| 334 error_handler->OnUnrecoverableError(error); | 340 error_handler->OnUnrecoverableError(error); |
| 335 LOG(ERROR) << "Delete: Bad entry."; | 341 LOG(ERROR) << "Delete: Bad entry."; |
| 336 return error; | 342 return error; |
| 337 } | 343 } |
| 338 case BaseNode::INIT_FAILED_ENTRY_IS_DEL: { | 344 case syncer::BaseNode::INIT_FAILED_ENTRY_IS_DEL: { |
| 339 SyncError error; | 345 syncer::SyncError error; |
| 340 error.Reset(from_here, error_prefix + "entry is already deleted.", type); | 346 error.Reset(from_here, error_prefix + "entry is already deleted.", type); |
| 341 error_handler->OnUnrecoverableError(error); | 347 error_handler->OnUnrecoverableError(error); |
| 342 LOG(ERROR) << "Delete: Deleted entry."; | 348 LOG(ERROR) << "Delete: Deleted entry."; |
| 343 return error; | 349 return error; |
| 344 } | 350 } |
| 345 case BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY: { | 351 case syncer::BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY: { |
| 346 SyncError error; | 352 syncer::SyncError error; |
| 347 error.Reset(from_here, error_prefix + "unable to decrypt", type); | 353 error.Reset(from_here, error_prefix + "unable to decrypt", type); |
| 348 error_handler->OnUnrecoverableError(error); | 354 error_handler->OnUnrecoverableError(error); |
| 349 LOG(ERROR) << "Delete: Undecryptable entry."; | 355 LOG(ERROR) << "Delete: Undecryptable entry."; |
| 350 return error; | 356 return error; |
| 351 } | 357 } |
| 352 case BaseNode::INIT_FAILED_PRECONDITION: { | 358 case syncer::BaseNode::INIT_FAILED_PRECONDITION: { |
| 353 SyncError error; | 359 syncer::SyncError error; |
| 354 error.Reset(from_here, | 360 error.Reset(from_here, |
| 355 error_prefix + "a precondition was not met for calling init.", | 361 error_prefix + "a precondition was not met for calling init.", |
| 356 type); | 362 type); |
| 357 error_handler->OnUnrecoverableError(error); | 363 error_handler->OnUnrecoverableError(error); |
| 358 LOG(ERROR) << "Delete: Failed precondition."; | 364 LOG(ERROR) << "Delete: Failed precondition."; |
| 359 return error; | 365 return error; |
| 360 } | 366 } |
| 361 default: { | 367 default: { |
| 362 SyncError error; | 368 syncer::SyncError error; |
| 363 // Should have listed all the possible error cases above. | 369 // Should have listed all the possible error cases above. |
| 364 error.Reset(from_here, error_prefix + "unknown error", type); | 370 error.Reset(from_here, error_prefix + "unknown error", type); |
| 365 error_handler->OnUnrecoverableError(error); | 371 error_handler->OnUnrecoverableError(error); |
| 366 LOG(ERROR) << "Delete: Unknown error."; | 372 LOG(ERROR) << "Delete: Unknown error."; |
| 367 return error; | 373 return error; |
| 368 } | 374 } |
| 369 } | 375 } |
| 370 } | 376 } |
| 371 | 377 |
| 372 } // namespace | 378 } // namespace |
| 373 | 379 |
| 374 SyncError GenericChangeProcessor::AttemptDelete( | 380 syncer::SyncError GenericChangeProcessor::AttemptDelete( |
| 375 const SyncChange& change, | 381 const syncer::SyncChange& change, |
| 376 ModelType type, | 382 syncer::ModelType type, |
| 377 const std::string& type_str, | 383 const std::string& type_str, |
| 378 WriteNode* node, | 384 syncer::WriteNode* node, |
| 379 DataTypeErrorHandler* error_handler) { | 385 syncer::DataTypeErrorHandler* error_handler) { |
| 380 DCHECK_EQ(change.change_type(), SyncChange::ACTION_DELETE); | 386 DCHECK_EQ(change.change_type(), syncer::SyncChange::ACTION_DELETE); |
| 381 if (change.sync_data().IsLocal()) { | 387 if (change.sync_data().IsLocal()) { |
| 382 const std::string& tag = SyncDataLocal(change.sync_data()).GetTag(); | 388 const std::string& tag = syncer::SyncDataLocal(change.sync_data()).GetTag(); |
| 383 if (tag.empty()) { | 389 if (tag.empty()) { |
| 384 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, | 390 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 385 "Failed to delete " + type_str + | 391 "Failed to delete " + type_str + |
| 386 " node. Local data, empty tag. " + | 392 " node. Local data, empty tag. " + |
| 387 change.location().ToString(), | 393 change.location().ToString(), |
| 388 type); | 394 type); |
| 389 error_handler->OnUnrecoverableError(error); | 395 error_handler->OnUnrecoverableError(error); |
| 390 NOTREACHED(); | 396 NOTREACHED(); |
| 391 return error; | 397 return error; |
| 392 } | 398 } |
| 393 | 399 |
| 394 BaseNode::InitByLookupResult result = | 400 syncer::BaseNode::InitByLookupResult result = |
| 395 node->InitByClientTagLookup(change.sync_data().GetDataType(), tag); | 401 node->InitByClientTagLookup(change.sync_data().GetDataType(), tag); |
| 396 if (result != BaseNode::INIT_OK) { | 402 if (result != syncer::BaseNode::INIT_OK) { |
| 397 return LogLookupFailure(result, FROM_HERE, | 403 return LogLookupFailure(result, FROM_HERE, |
| 398 "Failed to delete " + type_str + | 404 "Failed to delete " + type_str + |
| 399 " node. Local data. " + | 405 " node. Local data. " + |
| 400 change.location().ToString(), | 406 change.location().ToString(), |
| 401 type, error_handler); | 407 type, error_handler); |
| 402 } | 408 } |
| 403 } else { | 409 } else { |
| 404 BaseNode::InitByLookupResult result = | 410 syncer::BaseNode::InitByLookupResult result = node->InitByIdLookup( |
| 405 node->InitByIdLookup(SyncDataRemote(change.sync_data()).GetId()); | 411 syncer::SyncDataRemote(change.sync_data()).GetId()); |
| 406 if (result != BaseNode::INIT_OK) { | 412 if (result != syncer::BaseNode::INIT_OK) { |
| 407 return LogLookupFailure(result, FROM_HERE, | 413 return LogLookupFailure(result, FROM_HERE, |
| 408 "Failed to delete " + type_str + | 414 "Failed to delete " + type_str + |
| 409 " node. Non-local data. " + | 415 " node. Non-local data. " + |
| 410 change.location().ToString(), | 416 change.location().ToString(), |
| 411 type, error_handler); | 417 type, error_handler); |
| 412 } | 418 } |
| 413 } | 419 } |
| 414 NotifyLocalChangeObservers(node->GetEntry(), change); | 420 NotifyLocalChangeObservers(node->GetEntry(), change); |
| 415 if (IsActOnceDataType(type)) | 421 if (IsActOnceDataType(type)) |
| 416 node->Drop(); | 422 node->Drop(); |
| 417 else | 423 else |
| 418 node->Tombstone(); | 424 node->Tombstone(); |
| 419 return SyncError(); | 425 return syncer::SyncError(); |
| 420 } | 426 } |
| 421 | 427 |
| 422 SyncError GenericChangeProcessor::ProcessSyncChanges( | 428 syncer::SyncError GenericChangeProcessor::ProcessSyncChanges( |
| 423 const tracked_objects::Location& from_here, | 429 const tracked_objects::Location& from_here, |
| 424 const SyncChangeList& list_of_changes) { | 430 const syncer::SyncChangeList& list_of_changes) { |
| 425 DCHECK(CalledOnValidThread()); | 431 DCHECK(CalledOnValidThread()); |
| 426 | 432 |
| 427 if (list_of_changes.empty()) { | 433 if (list_of_changes.empty()) { |
| 428 // No work. Exit without entering WriteTransaction. | 434 // No work. Exit without entering WriteTransaction. |
| 429 return SyncError(); | 435 return syncer::SyncError(); |
| 430 } | 436 } |
| 431 | 437 |
| 432 // Keep track of brand new attachments so we can persist them on this device | 438 // Keep track of brand new attachments so we can persist them on this device |
| 433 // and upload them to the server. | 439 // and upload them to the server. |
| 434 AttachmentIdSet new_attachments; | 440 syncer::AttachmentIdSet new_attachments; |
| 435 | 441 |
| 436 WriteTransaction trans(from_here, share_handle()); | 442 syncer::WriteTransaction trans(from_here, share_handle()); |
| 437 | 443 |
| 438 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); | 444 for (syncer::SyncChangeList::const_iterator iter = list_of_changes.begin(); |
| 439 iter != list_of_changes.end(); ++iter) { | 445 iter != list_of_changes.end(); ++iter) { |
| 440 const SyncChange& change = *iter; | 446 const syncer::SyncChange& change = *iter; |
| 441 DCHECK_EQ(change.sync_data().GetDataType(), type_); | 447 DCHECK_EQ(change.sync_data().GetDataType(), type_); |
| 442 std::string type_str = ModelTypeToString(type_); | 448 std::string type_str = syncer::ModelTypeToString(type_); |
| 443 WriteNode sync_node(&trans); | 449 syncer::WriteNode sync_node(&trans); |
| 444 if (change.change_type() == SyncChange::ACTION_DELETE) { | 450 if (change.change_type() == syncer::SyncChange::ACTION_DELETE) { |
| 445 SyncError error = | 451 syncer::SyncError error = |
| 446 AttemptDelete(change, type_, type_str, &sync_node, error_handler()); | 452 AttemptDelete(change, type_, type_str, &sync_node, error_handler()); |
| 447 if (error.IsSet()) { | 453 if (error.IsSet()) { |
| 448 NOTREACHED(); | 454 NOTREACHED(); |
| 449 return error; | 455 return error; |
| 450 } | 456 } |
| 451 if (merge_result_.get()) { | 457 if (merge_result_.get()) { |
| 452 merge_result_->set_num_items_deleted( | 458 merge_result_->set_num_items_deleted( |
| 453 merge_result_->num_items_deleted() + 1); | 459 merge_result_->num_items_deleted() + 1); |
| 454 } | 460 } |
| 455 } else if (change.change_type() == SyncChange::ACTION_ADD) { | 461 } else if (change.change_type() == syncer::SyncChange::ACTION_ADD) { |
| 456 SyncError error = HandleActionAdd(change, type_str, trans, &sync_node, | 462 syncer::SyncError error = HandleActionAdd(change, type_str, trans, |
| 457 &new_attachments); | 463 &sync_node, &new_attachments); |
| 458 if (error.IsSet()) { | 464 if (error.IsSet()) { |
| 459 return error; | 465 return error; |
| 460 } | 466 } |
| 461 } else if (change.change_type() == SyncChange::ACTION_UPDATE) { | 467 } else if (change.change_type() == syncer::SyncChange::ACTION_UPDATE) { |
| 462 SyncError error = HandleActionUpdate(change, type_str, trans, &sync_node, | 468 syncer::SyncError error = HandleActionUpdate( |
| 463 &new_attachments); | 469 change, type_str, trans, &sync_node, &new_attachments); |
| 464 if (error.IsSet()) { | 470 if (error.IsSet()) { |
| 465 return error; | 471 return error; |
| 466 } | 472 } |
| 467 } else { | 473 } else { |
| 468 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, | 474 syncer::SyncError error( |
| 469 "Received unset SyncChange in the change processor, " + | 475 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 470 change.location().ToString(), | 476 "Received unset SyncChange in the change processor, " + |
| 471 type_); | 477 change.location().ToString(), |
| 478 type_); |
| 472 error_handler()->OnUnrecoverableError(error); | 479 error_handler()->OnUnrecoverableError(error); |
| 473 NOTREACHED(); | 480 NOTREACHED(); |
| 474 LOG(ERROR) << "Unset sync change."; | 481 LOG(ERROR) << "Unset sync change."; |
| 475 return error; | 482 return error; |
| 476 } | 483 } |
| 477 } | 484 } |
| 478 | 485 |
| 479 if (!new_attachments.empty()) { | 486 if (!new_attachments.empty()) { |
| 480 // If datatype uses attachments it should have supplied attachment store | 487 // If datatype uses attachments it should have supplied attachment store |
| 481 // which would initialize attachment_service_. Fail if it isn't so. | 488 // which would initialize attachment_service_. Fail if it isn't so. |
| 482 if (!attachment_service_.get()) { | 489 if (!attachment_service_.get()) { |
| 483 SyncError error( | 490 syncer::SyncError error( |
| 484 FROM_HERE, SyncError::DATATYPE_ERROR, | 491 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 485 "Datatype performs attachment operation without initializing " | 492 "Datatype performs attachment operation without initializing " |
| 486 "attachment store", | 493 "attachment store", |
| 487 type_); | 494 type_); |
| 488 error_handler()->OnUnrecoverableError(error); | 495 error_handler()->OnUnrecoverableError(error); |
| 489 NOTREACHED(); | 496 NOTREACHED(); |
| 490 return error; | 497 return error; |
| 491 } | 498 } |
| 492 AttachmentIdList ids_to_upload; | 499 syncer::AttachmentIdList ids_to_upload; |
| 493 ids_to_upload.reserve(new_attachments.size()); | 500 ids_to_upload.reserve(new_attachments.size()); |
| 494 std::copy(new_attachments.begin(), new_attachments.end(), | 501 std::copy(new_attachments.begin(), new_attachments.end(), |
| 495 std::back_inserter(ids_to_upload)); | 502 std::back_inserter(ids_to_upload)); |
| 496 attachment_service_->UploadAttachments(ids_to_upload); | 503 attachment_service_->UploadAttachments(ids_to_upload); |
| 497 } | 504 } |
| 498 | 505 |
| 499 return SyncError(); | 506 return syncer::SyncError(); |
| 500 } | 507 } |
| 501 | 508 |
| 502 // WARNING: this code is sensitive to compiler optimizations. Be careful | 509 // WARNING: this code is sensitive to compiler optimizations. Be careful |
| 503 // modifying any code around an OnUnrecoverableError call, else the compiler | 510 // modifying any code around an OnUnrecoverableError call, else the compiler |
| 504 // attempts to merge it with other calls, losing useful information in | 511 // attempts to merge it with other calls, losing useful information in |
| 505 // breakpad uploads. | 512 // breakpad uploads. |
| 506 SyncError GenericChangeProcessor::HandleActionAdd( | 513 syncer::SyncError GenericChangeProcessor::HandleActionAdd( |
| 507 const SyncChange& change, | 514 const syncer::SyncChange& change, |
| 508 const std::string& type_str, | 515 const std::string& type_str, |
| 509 const WriteTransaction& trans, | 516 const syncer::WriteTransaction& trans, |
| 510 WriteNode* sync_node, | 517 syncer::WriteNode* sync_node, |
| 511 AttachmentIdSet* new_attachments) { | 518 syncer::AttachmentIdSet* new_attachments) { |
| 512 // TODO(sync): Handle other types of creation (custom parents, folders, | 519 // TODO(sync): Handle other types of creation (custom parents, folders, |
| 513 // etc.). | 520 // etc.). |
| 514 const SyncDataLocal sync_data_local(change.sync_data()); | 521 const syncer::SyncDataLocal sync_data_local(change.sync_data()); |
| 515 WriteNode::InitUniqueByCreationResult result = | 522 syncer::WriteNode::InitUniqueByCreationResult result = |
| 516 sync_node->InitUniqueByCreation(sync_data_local.GetDataType(), | 523 sync_node->InitUniqueByCreation(sync_data_local.GetDataType(), |
| 517 sync_data_local.GetTag()); | 524 sync_data_local.GetTag()); |
| 518 if (result != WriteNode::INIT_SUCCESS) { | 525 if (result != syncer::WriteNode::INIT_SUCCESS) { |
| 519 std::string error_prefix = "Failed to create " + type_str + " node: " + | 526 std::string error_prefix = "Failed to create " + type_str + " node: " + |
| 520 change.location().ToString() + ", "; | 527 change.location().ToString() + ", "; |
| 521 switch (result) { | 528 switch (result) { |
| 522 case WriteNode::INIT_FAILED_EMPTY_TAG: { | 529 case syncer::WriteNode::INIT_FAILED_EMPTY_TAG: { |
| 523 SyncError error; | 530 syncer::SyncError error; |
| 524 error.Reset(FROM_HERE, error_prefix + "empty tag", type_); | 531 error.Reset(FROM_HERE, error_prefix + "empty tag", type_); |
| 525 error_handler()->OnUnrecoverableError(error); | 532 error_handler()->OnUnrecoverableError(error); |
| 526 LOG(ERROR) << "Create: Empty tag."; | 533 LOG(ERROR) << "Create: Empty tag."; |
| 527 return error; | 534 return error; |
| 528 } | 535 } |
| 529 case WriteNode::INIT_FAILED_COULD_NOT_CREATE_ENTRY: { | 536 case syncer::WriteNode::INIT_FAILED_COULD_NOT_CREATE_ENTRY: { |
| 530 SyncError error; | 537 syncer::SyncError error; |
| 531 error.Reset(FROM_HERE, error_prefix + "failed to create entry", type_); | 538 error.Reset(FROM_HERE, error_prefix + "failed to create entry", type_); |
| 532 error_handler()->OnUnrecoverableError(error); | 539 error_handler()->OnUnrecoverableError(error); |
| 533 LOG(ERROR) << "Create: Could not create entry."; | 540 LOG(ERROR) << "Create: Could not create entry."; |
| 534 return error; | 541 return error; |
| 535 } | 542 } |
| 536 case WriteNode::INIT_FAILED_SET_PREDECESSOR: { | 543 case syncer::WriteNode::INIT_FAILED_SET_PREDECESSOR: { |
| 537 SyncError error; | 544 syncer::SyncError error; |
| 538 error.Reset(FROM_HERE, error_prefix + "failed to set predecessor", | 545 error.Reset(FROM_HERE, error_prefix + "failed to set predecessor", |
| 539 type_); | 546 type_); |
| 540 error_handler()->OnUnrecoverableError(error); | 547 error_handler()->OnUnrecoverableError(error); |
| 541 LOG(ERROR) << "Create: Bad predecessor."; | 548 LOG(ERROR) << "Create: Bad predecessor."; |
| 542 return error; | 549 return error; |
| 543 } | 550 } |
| 544 case WriteNode::INIT_FAILED_DECRYPT_EXISTING_ENTRY: { | 551 case syncer::WriteNode::INIT_FAILED_DECRYPT_EXISTING_ENTRY: { |
| 545 SyncError error; | 552 syncer::SyncError error; |
| 546 error.Reset(FROM_HERE, error_prefix + "failed to decrypt", type_); | 553 error.Reset(FROM_HERE, error_prefix + "failed to decrypt", type_); |
| 547 error_handler()->OnUnrecoverableError(error); | 554 error_handler()->OnUnrecoverableError(error); |
| 548 LOG(ERROR) << "Create: Failed to decrypt."; | 555 LOG(ERROR) << "Create: Failed to decrypt."; |
| 549 return error; | 556 return error; |
| 550 } | 557 } |
| 551 default: { | 558 default: { |
| 552 SyncError error; | 559 syncer::SyncError error; |
| 553 error.Reset(FROM_HERE, error_prefix + "unknown error", type_); | 560 error.Reset(FROM_HERE, error_prefix + "unknown error", type_); |
| 554 error_handler()->OnUnrecoverableError(error); | 561 error_handler()->OnUnrecoverableError(error); |
| 555 LOG(ERROR) << "Create: Unknown error."; | 562 LOG(ERROR) << "Create: Unknown error."; |
| 556 return error; | 563 return error; |
| 557 } | 564 } |
| 558 } | 565 } |
| 559 } | 566 } |
| 560 NotifyLocalChangeObservers(sync_node->GetEntry(), change); | 567 NotifyLocalChangeObservers(sync_node->GetEntry(), change); |
| 561 | 568 |
| 562 sync_node->SetTitle(change.sync_data().GetTitle()); | 569 sync_node->SetTitle(change.sync_data().GetTitle()); |
| 563 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); | 570 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); |
| 564 | 571 |
| 565 AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); | 572 syncer::AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); |
| 566 SetAttachmentMetadata(attachment_ids, sync_node); | 573 SetAttachmentMetadata(attachment_ids, sync_node); |
| 567 | 574 |
| 568 // Return any newly added attachments. | 575 // Return any newly added attachments. |
| 569 new_attachments->insert(attachment_ids.begin(), attachment_ids.end()); | 576 new_attachments->insert(attachment_ids.begin(), attachment_ids.end()); |
| 570 if (merge_result_.get()) { | 577 if (merge_result_.get()) { |
| 571 merge_result_->set_num_items_added(merge_result_->num_items_added() + 1); | 578 merge_result_->set_num_items_added(merge_result_->num_items_added() + 1); |
| 572 } | 579 } |
| 573 return SyncError(); | 580 return syncer::SyncError(); |
| 574 } | 581 } |
| 575 // WARNING: this code is sensitive to compiler optimizations. Be careful | 582 // WARNING: this code is sensitive to compiler optimizations. Be careful |
| 576 // modifying any code around an OnUnrecoverableError call, else the compiler | 583 // modifying any code around an OnUnrecoverableError call, else the compiler |
| 577 // attempts to merge it with other calls, losing useful information in | 584 // attempts to merge it with other calls, losing useful information in |
| 578 // breakpad uploads. | 585 // breakpad uploads. |
| 579 SyncError GenericChangeProcessor::HandleActionUpdate( | 586 syncer::SyncError GenericChangeProcessor::HandleActionUpdate( |
| 580 const SyncChange& change, | 587 const syncer::SyncChange& change, |
| 581 const std::string& type_str, | 588 const std::string& type_str, |
| 582 const WriteTransaction& trans, | 589 const syncer::WriteTransaction& trans, |
| 583 WriteNode* sync_node, | 590 syncer::WriteNode* sync_node, |
| 584 AttachmentIdSet* new_attachments) { | 591 syncer::AttachmentIdSet* new_attachments) { |
| 585 const SyncDataLocal sync_data_local(change.sync_data()); | 592 const syncer::SyncDataLocal sync_data_local(change.sync_data()); |
| 586 BaseNode::InitByLookupResult result = sync_node->InitByClientTagLookup( | 593 syncer::BaseNode::InitByLookupResult result = |
| 587 sync_data_local.GetDataType(), sync_data_local.GetTag()); | 594 sync_node->InitByClientTagLookup(sync_data_local.GetDataType(), |
| 588 if (result != BaseNode::INIT_OK) { | 595 sync_data_local.GetTag()); |
| 596 if (result != syncer::BaseNode::INIT_OK) { |
| 589 std::string error_prefix = "Failed to load " + type_str + " node. " + | 597 std::string error_prefix = "Failed to load " + type_str + " node. " + |
| 590 change.location().ToString() + ", "; | 598 change.location().ToString() + ", "; |
| 591 if (result == BaseNode::INIT_FAILED_PRECONDITION) { | 599 if (result == syncer::BaseNode::INIT_FAILED_PRECONDITION) { |
| 592 SyncError error; | 600 syncer::SyncError error; |
| 593 error.Reset(FROM_HERE, error_prefix + "empty tag", type_); | 601 error.Reset(FROM_HERE, error_prefix + "empty tag", type_); |
| 594 error_handler()->OnUnrecoverableError(error); | 602 error_handler()->OnUnrecoverableError(error); |
| 595 LOG(ERROR) << "Update: Empty tag."; | 603 LOG(ERROR) << "Update: Empty tag."; |
| 596 return error; | 604 return error; |
| 597 } else if (result == BaseNode::INIT_FAILED_ENTRY_NOT_GOOD) { | 605 } else if (result == syncer::BaseNode::INIT_FAILED_ENTRY_NOT_GOOD) { |
| 598 SyncError error; | 606 syncer::SyncError error; |
| 599 error.Reset(FROM_HERE, error_prefix + "bad entry", type_); | 607 error.Reset(FROM_HERE, error_prefix + "bad entry", type_); |
| 600 error_handler()->OnUnrecoverableError(error); | 608 error_handler()->OnUnrecoverableError(error); |
| 601 LOG(ERROR) << "Update: bad entry."; | 609 LOG(ERROR) << "Update: bad entry."; |
| 602 return error; | 610 return error; |
| 603 } else if (result == BaseNode::INIT_FAILED_ENTRY_IS_DEL) { | 611 } else if (result == syncer::BaseNode::INIT_FAILED_ENTRY_IS_DEL) { |
| 604 SyncError error; | 612 syncer::SyncError error; |
| 605 error.Reset(FROM_HERE, error_prefix + "deleted entry", type_); | 613 error.Reset(FROM_HERE, error_prefix + "deleted entry", type_); |
| 606 error_handler()->OnUnrecoverableError(error); | 614 error_handler()->OnUnrecoverableError(error); |
| 607 LOG(ERROR) << "Update: deleted entry."; | 615 LOG(ERROR) << "Update: deleted entry."; |
| 608 return error; | 616 return error; |
| 609 } else if (result == BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY) { | 617 } else if (result == syncer::BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY) { |
| 610 SyncError error; | 618 syncer::SyncError error; |
| 611 error.Reset(FROM_HERE, error_prefix + "failed to decrypt", type_); | 619 error.Reset(FROM_HERE, error_prefix + "failed to decrypt", type_); |
| 612 error_handler()->OnUnrecoverableError(error); | 620 error_handler()->OnUnrecoverableError(error); |
| 613 LOG(ERROR) << "Update: Failed to decrypt."; | 621 LOG(ERROR) << "Update: Failed to decrypt."; |
| 614 return error; | 622 return error; |
| 615 } else { | 623 } else { |
| 616 NOTREACHED(); | 624 NOTREACHED(); |
| 617 SyncError error; | 625 syncer::SyncError error; |
| 618 error.Reset(FROM_HERE, error_prefix + "unknown error", type_); | 626 error.Reset(FROM_HERE, error_prefix + "unknown error", type_); |
| 619 error_handler()->OnUnrecoverableError(error); | 627 error_handler()->OnUnrecoverableError(error); |
| 620 LOG(ERROR) << "Update: Unknown error."; | 628 LOG(ERROR) << "Update: Unknown error."; |
| 621 return error; | 629 return error; |
| 622 } | 630 } |
| 623 } | 631 } |
| 624 | 632 |
| 625 NotifyLocalChangeObservers(sync_node->GetEntry(), change); | 633 NotifyLocalChangeObservers(sync_node->GetEntry(), change); |
| 626 | 634 |
| 627 sync_node->SetTitle(change.sync_data().GetTitle()); | 635 sync_node->SetTitle(change.sync_data().GetTitle()); |
| 628 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); | 636 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); |
| 629 AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); | 637 syncer::AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); |
| 630 SetAttachmentMetadata(attachment_ids, sync_node); | 638 SetAttachmentMetadata(attachment_ids, sync_node); |
| 631 | 639 |
| 632 // Return any newly added attachments. | 640 // Return any newly added attachments. |
| 633 new_attachments->insert(attachment_ids.begin(), attachment_ids.end()); | 641 new_attachments->insert(attachment_ids.begin(), attachment_ids.end()); |
| 634 | 642 |
| 635 if (merge_result_.get()) { | 643 if (merge_result_.get()) { |
| 636 merge_result_->set_num_items_modified(merge_result_->num_items_modified() + | 644 merge_result_->set_num_items_modified(merge_result_->num_items_modified() + |
| 637 1); | 645 1); |
| 638 } | 646 } |
| 639 // TODO(sync): Support updating other parts of the sync node (title, | 647 // TODO(sync): Support updating other parts of the sync node (title, |
| 640 // successor, parent, etc.). | 648 // successor, parent, etc.). |
| 641 return SyncError(); | 649 return syncer::SyncError(); |
| 642 } | 650 } |
| 643 | 651 |
| 644 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes(bool* has_nodes) { | 652 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes(bool* has_nodes) { |
| 645 DCHECK(CalledOnValidThread()); | 653 DCHECK(CalledOnValidThread()); |
| 646 DCHECK(has_nodes); | 654 DCHECK(has_nodes); |
| 647 std::string type_name = ModelTypeToString(type_); | 655 std::string type_name = syncer::ModelTypeToString(type_); |
| 648 std::string err_str = | 656 std::string err_str = |
| 649 "Server did not create the top-level " + type_name + | 657 "Server did not create the top-level " + type_name + |
| 650 " node. We might be running against an out-of-date server."; | 658 " node. We might be running against an out-of-date server."; |
| 651 *has_nodes = false; | 659 *has_nodes = false; |
| 652 ReadTransaction trans(FROM_HERE, share_handle()); | 660 syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| 653 ReadNode type_root_node(&trans); | 661 syncer::ReadNode type_root_node(&trans); |
| 654 if (type_root_node.InitTypeRoot(type_) != BaseNode::INIT_OK) { | 662 if (type_root_node.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) { |
| 655 LOG(ERROR) << err_str; | 663 LOG(ERROR) << err_str; |
| 656 return false; | 664 return false; |
| 657 } | 665 } |
| 658 | 666 |
| 659 // The sync model has user created nodes if the type's root node has any | 667 // The sync model has user created nodes if the type's root node has any |
| 660 // children. | 668 // children. |
| 661 *has_nodes = type_root_node.HasChildren(); | 669 *has_nodes = type_root_node.HasChildren(); |
| 662 return true; | 670 return true; |
| 663 } | 671 } |
| 664 | 672 |
| 665 bool GenericChangeProcessor::CryptoReadyIfNecessary() { | 673 bool GenericChangeProcessor::CryptoReadyIfNecessary() { |
| 666 DCHECK(CalledOnValidThread()); | 674 DCHECK(CalledOnValidThread()); |
| 667 // We only access the cryptographer while holding a transaction. | 675 // We only access the cryptographer while holding a transaction. |
| 668 ReadTransaction trans(FROM_HERE, share_handle()); | 676 syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| 669 const ModelTypeSet encrypted_types = trans.GetEncryptedTypes(); | 677 const syncer::ModelTypeSet encrypted_types = trans.GetEncryptedTypes(); |
| 670 return !encrypted_types.Has(type_) || trans.GetCryptographer()->is_ready(); | 678 return !encrypted_types.Has(type_) || trans.GetCryptographer()->is_ready(); |
| 671 } | 679 } |
| 672 | 680 |
| 673 void GenericChangeProcessor::StartImpl() {} | 681 void GenericChangeProcessor::StartImpl() {} |
| 674 | 682 |
| 675 UserShare* GenericChangeProcessor::share_handle() const { | 683 syncer::UserShare* GenericChangeProcessor::share_handle() const { |
| 676 DCHECK(CalledOnValidThread()); | 684 DCHECK(CalledOnValidThread()); |
| 677 return share_handle_; | 685 return share_handle_; |
| 678 } | 686 } |
| 679 | 687 |
| 680 void GenericChangeProcessor::UploadAllAttachmentsNotOnServer() { | 688 void GenericChangeProcessor::UploadAllAttachmentsNotOnServer() { |
| 681 DCHECK(CalledOnValidThread()); | 689 DCHECK(CalledOnValidThread()); |
| 682 DCHECK(attachment_service_.get()); | 690 DCHECK(attachment_service_.get()); |
| 683 AttachmentIdList ids; | 691 syncer::AttachmentIdList ids; |
| 684 { | 692 { |
| 685 ReadTransaction trans(FROM_HERE, share_handle()); | 693 syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| 686 trans.GetAttachmentIdsToUpload(type_, &ids); | 694 trans.GetAttachmentIdsToUpload(type_, &ids); |
| 687 } | 695 } |
| 688 if (!ids.empty()) { | 696 if (!ids.empty()) { |
| 689 attachment_service_->UploadAttachments(ids); | 697 attachment_service_->UploadAttachments(ids); |
| 690 } | 698 } |
| 691 } | 699 } |
| 692 | 700 |
| 693 void GenericChangeProcessor::NotifyLocalChangeObservers( | 701 void GenericChangeProcessor::NotifyLocalChangeObservers( |
| 694 const syncable::Entry* current_entry, | 702 const syncer::syncable::Entry* current_entry, |
| 695 const SyncChange& change) { | 703 const syncer::SyncChange& change) { |
| 696 FOR_EACH_OBSERVER(LocalChangeObserver, local_change_observers_, | 704 FOR_EACH_OBSERVER(syncer::LocalChangeObserver, local_change_observers_, |
| 697 OnLocalChange(current_entry, change)); | 705 OnLocalChange(current_entry, change)); |
| 698 } | 706 } |
| 699 | 707 |
| 700 std::unique_ptr<AttachmentService> | 708 std::unique_ptr<syncer::AttachmentService> |
| 701 GenericChangeProcessor::GetAttachmentService() const { | 709 GenericChangeProcessor::GetAttachmentService() const { |
| 702 return std::unique_ptr<AttachmentService>( | 710 return std::unique_ptr<syncer::AttachmentService>( |
| 703 new AttachmentServiceProxy(attachment_service_proxy_)); | 711 new syncer::AttachmentServiceProxy(attachment_service_proxy_)); |
| 704 } | 712 } |
| 705 | 713 |
| 706 } // namespace syncer | 714 } // namespace sync_driver |
| OLD | NEW |