Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: sync/internal_api/write_node.cc

Issue 11817010: sync: Initialize entries with a valid model type (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update comment Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "sync/internal_api/public/write_node.h" 5 #include "sync/internal_api/public/write_node.h"
6 6
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "sync/internal_api/public/base_transaction.h" 9 #include "sync/internal_api/public/base_transaction.h"
10 #include "sync/internal_api/public/write_transaction.h" 10 #include "sync/internal_api/public/write_transaction.h"
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 SetEntitySpecifics(entity_specifics); 198 SetEntitySpecifics(entity_specifics);
199 } 199 }
200 200
201 void WriteNode::SetEntitySpecifics( 201 void WriteNode::SetEntitySpecifics(
202 const sync_pb::EntitySpecifics& new_value) { 202 const sync_pb::EntitySpecifics& new_value) {
203 ModelType new_specifics_type = 203 ModelType new_specifics_type =
204 GetModelTypeFromSpecifics(new_value); 204 GetModelTypeFromSpecifics(new_value);
205 DCHECK_NE(new_specifics_type, UNSPECIFIED); 205 DCHECK_NE(new_specifics_type, UNSPECIFIED);
206 DVLOG(1) << "Writing entity specifics of type " 206 DVLOG(1) << "Writing entity specifics of type "
207 << ModelTypeToString(new_specifics_type); 207 << ModelTypeToString(new_specifics_type);
208 // GetModelType() can be unspecified if this is the first time this 208 DCHECK_EQ(new_specifics_type, GetModelType());
209 // node is being initialized (see PutModelType()). Otherwise, it
210 // should match |new_specifics_type|.
211 if (GetModelType() != UNSPECIFIED) {
212 DCHECK_EQ(new_specifics_type, GetModelType());
213 }
214 209
215 // Preserve unknown fields. 210 // Preserve unknown fields.
216 const sync_pb::EntitySpecifics& old_specifics = entry_->Get(SPECIFICS); 211 const sync_pb::EntitySpecifics& old_specifics = entry_->Get(SPECIFICS);
217 sync_pb::EntitySpecifics new_specifics; 212 sync_pb::EntitySpecifics new_specifics;
218 new_specifics.CopyFrom(new_value); 213 new_specifics.CopyFrom(new_value);
219 new_specifics.mutable_unknown_fields()->MergeFrom( 214 new_specifics.mutable_unknown_fields()->MergeFrom(
220 old_specifics.unknown_fields()); 215 old_specifics.unknown_fields());
221 216
222 // Will update the entry if encryption was necessary. 217 // Will update the entry if encryption was necessary.
223 if (!UpdateEntryWithEncryption(GetTransaction()->GetWrappedTrans(), 218 if (!UpdateEntryWithEncryption(GetTransaction()->GetWrappedTrans(),
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 syncable::GET_BY_SERVER_TAG, tag); 308 syncable::GET_BY_SERVER_TAG, tag);
314 if (!entry_->good()) 309 if (!entry_->good())
315 return INIT_FAILED_ENTRY_NOT_GOOD; 310 return INIT_FAILED_ENTRY_NOT_GOOD;
316 if (entry_->Get(syncable::IS_DEL)) 311 if (entry_->Get(syncable::IS_DEL))
317 return INIT_FAILED_ENTRY_IS_DEL; 312 return INIT_FAILED_ENTRY_IS_DEL;
318 ModelType model_type = GetModelType(); 313 ModelType model_type = GetModelType();
319 DCHECK_EQ(model_type, NIGORI); 314 DCHECK_EQ(model_type, NIGORI);
320 return INIT_OK; 315 return INIT_OK;
321 } 316 }
322 317
323 void WriteNode::PutModelType(ModelType model_type) {
324 // Set an empty specifics of the appropriate datatype. The presence
325 // of the specific field will identify the model type.
326 DCHECK(GetModelType() == model_type ||
327 GetModelType() == UNSPECIFIED); // Immutable once set.
328
329 sync_pb::EntitySpecifics specifics;
330 AddDefaultFieldValue(model_type, &specifics);
331 SetEntitySpecifics(specifics);
332 }
333
334 // Create a new node with default properties, and bind this WriteNode to it. 318 // Create a new node with default properties, and bind this WriteNode to it.
335 // Return true on success. 319 // Return true on success.
336 bool WriteNode::InitByCreation(ModelType model_type, 320 bool WriteNode::InitBookmarkByCreation(const BaseNode& parent,
337 const BaseNode& parent, 321 const BaseNode* predecessor) {
338 const BaseNode* predecessor) {
339 DCHECK(!entry_) << "Init called twice"; 322 DCHECK(!entry_) << "Init called twice";
340 // |predecessor| must be a child of |parent| or NULL. 323 // |predecessor| must be a child of |parent| or NULL.
341 if (predecessor && predecessor->GetParentId() != parent.GetId()) { 324 if (predecessor && predecessor->GetParentId() != parent.GetId()) {
342 DCHECK(false); 325 DCHECK(false);
343 return false; 326 return false;
344 } 327 }
345 328
346 syncable::Id parent_id = parent.GetEntry()->Get(syncable::ID); 329 syncable::Id parent_id = parent.GetEntry()->Get(syncable::ID);
347 330
348 // Start out with a dummy name. We expect 331 // Start out with a dummy name. We expect
349 // the caller to set a meaningful name after creation. 332 // the caller to set a meaningful name after creation.
350 string dummy(kDefaultNameForNewNodes); 333 string dummy(kDefaultNameForNewNodes);
351 334
352 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(), 335 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
353 syncable::CREATE, parent_id, dummy); 336 syncable::CREATE, BOOKMARKS,
337 parent_id, dummy);
354 338
355 if (!entry_->good()) 339 if (!entry_->good())
356 return false; 340 return false;
357 341
358 // Entries are untitled folders by default. 342 // Entries are untitled folders by default.
359 entry_->Put(syncable::IS_DIR, true); 343 entry_->Put(syncable::IS_DIR, true);
360 344
361 PutModelType(model_type);
362
363 // Now set the predecessor, which sets IS_UNSYNCED as necessary. 345 // Now set the predecessor, which sets IS_UNSYNCED as necessary.
364 return PutPredecessor(predecessor); 346 return PutPredecessor(predecessor);
365 } 347 }
366 348
367 // Create a new node with default properties and a client defined unique tag, 349 // Create a new node with default properties and a client defined unique tag,
368 // and bind this WriteNode to it. 350 // and bind this WriteNode to it.
369 // Return true on success. If the tag exists in the database, then 351 // Return true on success. If the tag exists in the database, then
370 // we will attempt to undelete the node. 352 // we will attempt to undelete the node.
371 // TODO(chron): Code datatype into hash tag. 353 // TODO(chron): Code datatype into hash tag.
372 // TODO(chron): Is model type ever lost? 354 // TODO(chron): Is model type ever lost?
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 // tags and updates. 402 // tags and updates.
421 403
422 existing_entry->Put(syncable::NON_UNIQUE_NAME, dummy); 404 existing_entry->Put(syncable::NON_UNIQUE_NAME, dummy);
423 existing_entry->Put(syncable::PARENT_ID, parent_id); 405 existing_entry->Put(syncable::PARENT_ID, parent_id);
424 entry_ = existing_entry.release(); 406 entry_ = existing_entry.release();
425 } else { 407 } else {
426 return INIT_FAILED_ENTRY_ALREADY_EXISTS; 408 return INIT_FAILED_ENTRY_ALREADY_EXISTS;
427 } 409 }
428 } else { 410 } else {
429 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(), 411 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
430 syncable::CREATE, parent_id, dummy); 412 syncable::CREATE,
413 model_type, parent_id, dummy);
431 if (!entry_->good()) 414 if (!entry_->good())
432 return INIT_FAILED_COULD_NOT_CREATE_ENTRY; 415 return INIT_FAILED_COULD_NOT_CREATE_ENTRY;
433 416
434 // Only set IS_DIR for new entries. Don't bitflip undeleted ones. 417 // Only set IS_DIR for new entries. Don't bitflip undeleted ones.
435 entry_->Put(syncable::UNIQUE_CLIENT_TAG, hash); 418 entry_->Put(syncable::UNIQUE_CLIENT_TAG, hash);
436 } 419 }
437 420
438 // We don't support directory and tag combinations. 421 // We don't support directory and tag combinations.
439 entry_->Put(syncable::IS_DIR, false); 422 entry_->Put(syncable::IS_DIR, false);
440 423
441 // Will clear specifics data.
442 PutModelType(model_type);
443
444 // Now set the predecessor, which sets IS_UNSYNCED as necessary. 424 // Now set the predecessor, which sets IS_UNSYNCED as necessary.
445 bool success = PutPredecessor(NULL); 425 bool success = PutPredecessor(NULL);
446 if (!success) 426 if (!success)
447 return INIT_FAILED_SET_PREDECESSOR; 427 return INIT_FAILED_SET_PREDECESSOR;
448 428
449 return INIT_SUCCESS; 429 return INIT_SUCCESS;
450 } 430 }
451 431
452 bool WriteNode::SetPosition(const BaseNode& new_parent, 432 bool WriteNode::SetPosition(const BaseNode& new_parent,
453 const BaseNode* predecessor) { 433 const BaseNode* predecessor) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 MarkForSyncing(); 486 MarkForSyncing();
507 487
508 return true; 488 return true;
509 } 489 }
510 490
511 void WriteNode::MarkForSyncing() { 491 void WriteNode::MarkForSyncing() {
512 syncable::MarkForSyncing(entry_); 492 syncable::MarkForSyncing(entry_);
513 } 493 }
514 494
515 } // namespace syncer 495 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698