OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/syncable/mutable_entry.h" | 5 #include "sync/syncable/mutable_entry.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "sync/internal_api/public/base/node_ordinal.h" | 8 #include "sync/internal_api/public/base/node_ordinal.h" |
9 #include "sync/syncable/directory.h" | 9 #include "sync/syncable/directory.h" |
10 #include "sync/syncable/scoped_index_updater.h" | 10 #include "sync/syncable/scoped_index_updater.h" |
11 #include "sync/syncable/scoped_kernel_lock.h" | 11 #include "sync/syncable/scoped_kernel_lock.h" |
12 #include "sync/syncable/syncable-inl.h" | 12 #include "sync/syncable/syncable-inl.h" |
13 #include "sync/syncable/syncable_changes_version.h" | 13 #include "sync/syncable/syncable_changes_version.h" |
14 #include "sync/syncable/syncable_util.h" | 14 #include "sync/syncable/syncable_util.h" |
15 #include "sync/syncable/syncable_write_transaction.h" | 15 #include "sync/syncable/syncable_write_transaction.h" |
16 | 16 |
17 using std::string; | 17 using std::string; |
18 | 18 |
19 namespace syncer { | 19 namespace syncer { |
20 namespace syncable { | 20 namespace syncable { |
21 | 21 |
22 MutableEntry::MutableEntry(WriteTransaction* trans, Create, | 22 void MutableEntry::Init(WriteTransaction* trans, |
23 const Id& parent_id, const string& name) | 23 ModelType model_type, |
24 : Entry(trans), | 24 const Id& parent_id, |
25 write_transaction_(trans) { | |
26 Init(trans, parent_id, name); | |
27 } | |
28 | |
29 | |
30 void MutableEntry::Init(WriteTransaction* trans, const Id& parent_id, | |
31 const string& name) { | 25 const string& name) { |
32 scoped_ptr<EntryKernel> kernel(new EntryKernel); | 26 scoped_ptr<EntryKernel> kernel(new EntryKernel); |
33 kernel_ = NULL; | 27 kernel_ = NULL; |
34 | 28 |
35 kernel->put(ID, trans->directory_->NextId()); | 29 kernel->put(ID, trans->directory_->NextId()); |
36 kernel->put(META_HANDLE, trans->directory_->NextMetahandle()); | 30 kernel->put(META_HANDLE, trans->directory_->NextMetahandle()); |
37 kernel->mark_dirty(trans->directory_->kernel_->dirty_metahandles); | 31 kernel->mark_dirty(trans->directory_->kernel_->dirty_metahandles); |
38 kernel->put(PARENT_ID, parent_id); | 32 kernel->put(PARENT_ID, parent_id); |
39 kernel->put(NON_UNIQUE_NAME, name); | 33 kernel->put(NON_UNIQUE_NAME, name); |
40 const base::Time& now = base::Time::Now(); | 34 const base::Time& now = base::Time::Now(); |
41 kernel->put(CTIME, now); | 35 kernel->put(CTIME, now); |
42 kernel->put(MTIME, now); | 36 kernel->put(MTIME, now); |
43 // We match the database defaults here | 37 // We match the database defaults here |
44 kernel->put(BASE_VERSION, CHANGES_VERSION); | 38 kernel->put(BASE_VERSION, CHANGES_VERSION); |
45 kernel->put(SERVER_ORDINAL_IN_PARENT, NodeOrdinal::CreateInitialOrdinal()); | 39 kernel->put(SERVER_ORDINAL_IN_PARENT, NodeOrdinal::CreateInitialOrdinal()); |
46 if (!trans->directory()->InsertEntry(trans, kernel.get())) { | 40 |
47 return; // We failed inserting, nothing more to do. | 41 // Normally the SPECIFICS setting code is wrapped in logic to deal with |
48 } | 42 // unknown fields and encryption. Since all we want to do here is ensure that |
| 43 // GetModelType() returns a correct value from the very beginning, these |
| 44 // few lines are sufficient. |
| 45 sync_pb::EntitySpecifics specifics; |
| 46 AddDefaultFieldValue(model_type, &specifics); |
| 47 kernel->put(SPECIFICS, specifics); |
| 48 |
49 // Because this entry is new, it was originally deleted. | 49 // Because this entry is new, it was originally deleted. |
50 kernel->put(IS_DEL, true); | 50 kernel->put(IS_DEL, true); |
51 trans->SaveOriginal(kernel.get()); | 51 trans->SaveOriginal(kernel.get()); |
52 kernel->put(IS_DEL, false); | 52 kernel->put(IS_DEL, false); |
53 | 53 |
54 // Now swap the pointers. | 54 // Now swap the pointers. |
55 kernel_ = kernel.release(); | 55 kernel_ = kernel.release(); |
56 } | 56 } |
57 | 57 |
| 58 MutableEntry::MutableEntry(WriteTransaction* trans, |
| 59 Create, |
| 60 ModelType model_type, |
| 61 const Id& parent_id, |
| 62 const string& name) |
| 63 : Entry(trans), |
| 64 write_transaction_(trans) { |
| 65 Init(trans, model_type, parent_id, name); |
| 66 bool insert_result = trans->directory()->InsertEntry(trans, kernel_); |
| 67 DCHECK(insert_result); |
| 68 } |
| 69 |
58 MutableEntry::MutableEntry(WriteTransaction* trans, CreateNewUpdateItem, | 70 MutableEntry::MutableEntry(WriteTransaction* trans, CreateNewUpdateItem, |
59 const Id& id) | 71 const Id& id) |
60 : Entry(trans), write_transaction_(trans) { | 72 : Entry(trans), write_transaction_(trans) { |
61 Entry same_id(trans, GET_BY_ID, id); | 73 Entry same_id(trans, GET_BY_ID, id); |
62 kernel_ = NULL; | 74 kernel_ = NULL; |
63 if (same_id.good()) { | 75 if (same_id.good()) { |
64 return; // already have an item with this ID. | 76 return; // already have an item with this ID. |
65 } | 77 } |
66 scoped_ptr<EntryKernel> kernel(new EntryKernel()); | 78 scoped_ptr<EntryKernel> kernel(new EntryKernel()); |
67 | 79 |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 DCHECK_NE(static_cast<MutableEntry*>(NULL), e); | 449 DCHECK_NE(static_cast<MutableEntry*>(NULL), e); |
438 DCHECK(!e->IsRoot()) << "We shouldn't mark a permanent object for syncing."; | 450 DCHECK(!e->IsRoot()) << "We shouldn't mark a permanent object for syncing."; |
439 if (!(e->Put(IS_UNSYNCED, true))) | 451 if (!(e->Put(IS_UNSYNCED, true))) |
440 return false; | 452 return false; |
441 e->Put(SYNCING, false); | 453 e->Put(SYNCING, false); |
442 return true; | 454 return true; |
443 } | 455 } |
444 | 456 |
445 } // namespace syncable | 457 } // namespace syncable |
446 } // namespace syncer | 458 } // namespace syncer |
OLD | NEW |