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 void MutableEntry::Init(WriteTransaction* trans, | 22 MutableEntry::MutableEntry(WriteTransaction* trans, Create, |
23 ModelType model_type, | 23 const Id& parent_id, const string& name) |
24 const Id& parent_id, | 24 : Entry(trans), |
| 25 write_transaction_(trans) { |
| 26 Init(trans, parent_id, name); |
| 27 } |
| 28 |
| 29 |
| 30 void MutableEntry::Init(WriteTransaction* trans, const Id& parent_id, |
25 const string& name) { | 31 const string& name) { |
26 scoped_ptr<EntryKernel> kernel(new EntryKernel); | 32 scoped_ptr<EntryKernel> kernel(new EntryKernel); |
27 kernel_ = NULL; | 33 kernel_ = NULL; |
28 | 34 |
29 kernel->put(ID, trans->directory_->NextId()); | 35 kernel->put(ID, trans->directory_->NextId()); |
30 kernel->put(META_HANDLE, trans->directory_->NextMetahandle()); | 36 kernel->put(META_HANDLE, trans->directory_->NextMetahandle()); |
31 kernel->mark_dirty(trans->directory_->kernel_->dirty_metahandles); | 37 kernel->mark_dirty(trans->directory_->kernel_->dirty_metahandles); |
32 kernel->put(PARENT_ID, parent_id); | 38 kernel->put(PARENT_ID, parent_id); |
33 kernel->put(NON_UNIQUE_NAME, name); | 39 kernel->put(NON_UNIQUE_NAME, name); |
34 const base::Time& now = base::Time::Now(); | 40 const base::Time& now = base::Time::Now(); |
35 kernel->put(CTIME, now); | 41 kernel->put(CTIME, now); |
36 kernel->put(MTIME, now); | 42 kernel->put(MTIME, now); |
37 // We match the database defaults here | 43 // We match the database defaults here |
38 kernel->put(BASE_VERSION, CHANGES_VERSION); | 44 kernel->put(BASE_VERSION, CHANGES_VERSION); |
39 kernel->put(SERVER_ORDINAL_IN_PARENT, NodeOrdinal::CreateInitialOrdinal()); | 45 kernel->put(SERVER_ORDINAL_IN_PARENT, NodeOrdinal::CreateInitialOrdinal()); |
40 | 46 if (!trans->directory()->InsertEntry(trans, kernel.get())) { |
41 // Normally the SPECIFICS setting code is wrapped in logic to deal with | 47 return; // We failed inserting, nothing more to do. |
42 // unknown fields and encryption. Since all we want to do here is ensure that | 48 } |
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 DCHECK(trans->directory()->InsertEntry(trans, kernel_)); | |
67 } | |
68 | |
69 MutableEntry::MutableEntry(WriteTransaction* trans, CreateNewUpdateItem, | 58 MutableEntry::MutableEntry(WriteTransaction* trans, CreateNewUpdateItem, |
70 const Id& id) | 59 const Id& id) |
71 : Entry(trans), write_transaction_(trans) { | 60 : Entry(trans), write_transaction_(trans) { |
72 Entry same_id(trans, GET_BY_ID, id); | 61 Entry same_id(trans, GET_BY_ID, id); |
73 kernel_ = NULL; | 62 kernel_ = NULL; |
74 if (same_id.good()) { | 63 if (same_id.good()) { |
75 return; // already have an item with this ID. | 64 return; // already have an item with this ID. |
76 } | 65 } |
77 scoped_ptr<EntryKernel> kernel(new EntryKernel()); | 66 scoped_ptr<EntryKernel> kernel(new EntryKernel()); |
78 | 67 |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 DCHECK_NE(static_cast<MutableEntry*>(NULL), e); | 437 DCHECK_NE(static_cast<MutableEntry*>(NULL), e); |
449 DCHECK(!e->IsRoot()) << "We shouldn't mark a permanent object for syncing."; | 438 DCHECK(!e->IsRoot()) << "We shouldn't mark a permanent object for syncing."; |
450 if (!(e->Put(IS_UNSYNCED, true))) | 439 if (!(e->Put(IS_UNSYNCED, true))) |
451 return false; | 440 return false; |
452 e->Put(SYNCING, false); | 441 e->Put(SYNCING, false); |
453 return true; | 442 return true; |
454 } | 443 } |
455 | 444 |
456 } // namespace syncable | 445 } // namespace syncable |
457 } // namespace syncer | 446 } // namespace syncer |
OLD | NEW |