page_set_smoke_test -> story_set_smoke_test.
[chromium-blink-merge.git] / sync / syncable / mutable_entry.cc
blob8e33a67a98f1345cf4729caf8e2dc4c5b0870488
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
3 // found in the LICENSE file.
5 #include "sync/syncable/mutable_entry.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "sync/internal_api/public/base/unique_position.h"
9 #include "sync/syncable/directory.h"
10 #include "sync/syncable/scoped_kernel_lock.h"
11 #include "sync/syncable/scoped_parent_child_index_updater.h"
12 #include "sync/syncable/syncable-inl.h"
13 #include "sync/syncable/syncable_changes_version.h"
14 #include "sync/syncable/syncable_util.h"
15 #include "sync/syncable/syncable_write_transaction.h"
17 using std::string;
19 namespace syncer {
20 namespace syncable {
22 void MutableEntry::Init(WriteTransaction* trans,
23 ModelType model_type,
24 const Id& parent_id,
25 const string& name) {
26 scoped_ptr<EntryKernel> kernel(new EntryKernel);
27 kernel_ = NULL;
29 kernel->put(ID, trans->directory_->NextId());
30 kernel->put(META_HANDLE, trans->directory_->NextMetahandle());
31 kernel->mark_dirty(&trans->directory_->kernel()->dirty_metahandles);
32 kernel->put(NON_UNIQUE_NAME, name);
33 const base::Time& now = base::Time::Now();
34 kernel->put(CTIME, now);
35 kernel->put(MTIME, now);
36 // We match the database defaults here
37 kernel->put(BASE_VERSION, CHANGES_VERSION);
39 if (!parent_id.IsNull()) {
40 kernel->put(PARENT_ID, parent_id);
43 // Normally the SPECIFICS setting code is wrapped in logic to deal with
44 // unknown fields and encryption. Since all we want to do here is ensure that
45 // GetModelType() returns a correct value from the very beginning, these
46 // few lines are sufficient.
47 sync_pb::EntitySpecifics specifics;
48 AddDefaultFieldValue(model_type, &specifics);
49 kernel->put(SPECIFICS, specifics);
51 // Because this entry is new, it was originally deleted.
52 kernel->put(IS_DEL, true);
53 trans->TrackChangesTo(kernel.get());
54 kernel->put(IS_DEL, false);
56 // Now swap the pointers.
57 kernel_ = kernel.release();
60 MutableEntry::MutableEntry(WriteTransaction* trans,
61 Create,
62 ModelType model_type,
63 const string& name)
64 : ModelNeutralMutableEntry(trans), write_transaction_(trans) {
65 Init(trans, model_type, Id(), name);
66 // We need to have a valid position ready before we can index the item.
67 DCHECK_NE(BOOKMARKS, model_type);
68 DCHECK(!ShouldMaintainPosition());
70 bool result = trans->directory()->InsertEntry(trans, kernel_);
71 DCHECK(result);
74 MutableEntry::MutableEntry(WriteTransaction* trans,
75 Create,
76 ModelType model_type,
77 const Id& parent_id,
78 const string& name)
79 : ModelNeutralMutableEntry(trans), write_transaction_(trans) {
80 Init(trans, model_type, parent_id, name);
81 // We need to have a valid position ready before we can index the item.
82 if (model_type == BOOKMARKS) {
83 // Base the tag off of our cache-guid and local "c-" style ID.
84 std::string unique_tag = syncable::GenerateSyncableBookmarkHash(
85 trans->directory()->cache_guid(), GetId().GetServerId());
86 kernel_->put(UNIQUE_BOOKMARK_TAG, unique_tag);
87 kernel_->put(UNIQUE_POSITION, UniquePosition::InitialPosition(unique_tag));
88 } else {
89 DCHECK(!ShouldMaintainPosition());
92 bool result = trans->directory()->InsertEntry(trans, kernel_);
93 DCHECK(result);
96 MutableEntry::MutableEntry(WriteTransaction* trans, CreateNewUpdateItem,
97 const Id& id)
98 : ModelNeutralMutableEntry(trans, CREATE_NEW_UPDATE_ITEM, id),
99 write_transaction_(trans) {}
101 MutableEntry::MutableEntry(WriteTransaction* trans, GetById, const Id& id)
102 : ModelNeutralMutableEntry(trans, GET_BY_ID, id),
103 write_transaction_(trans) {
106 MutableEntry::MutableEntry(WriteTransaction* trans, GetByHandle,
107 int64 metahandle)
108 : ModelNeutralMutableEntry(trans, GET_BY_HANDLE, metahandle),
109 write_transaction_(trans) {
112 MutableEntry::MutableEntry(WriteTransaction* trans, GetByClientTag,
113 const std::string& tag)
114 : ModelNeutralMutableEntry(trans, GET_BY_CLIENT_TAG, tag),
115 write_transaction_(trans) {
118 MutableEntry::MutableEntry(WriteTransaction* trans, GetTypeRoot, ModelType type)
119 : ModelNeutralMutableEntry(trans, GET_TYPE_ROOT, type),
120 write_transaction_(trans) {
123 void MutableEntry::PutLocalExternalId(int64 value) {
124 DCHECK(kernel_);
125 write_transaction()->TrackChangesTo(kernel_);
126 if (kernel_->ref(LOCAL_EXTERNAL_ID) != value) {
127 ScopedKernelLock lock(dir());
128 kernel_->put(LOCAL_EXTERNAL_ID, value);
129 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
133 void MutableEntry::PutMtime(base::Time value) {
134 DCHECK(kernel_);
135 write_transaction()->TrackChangesTo(kernel_);
136 if (kernel_->ref(MTIME) != value) {
137 kernel_->put(MTIME, value);
138 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
142 void MutableEntry::PutCtime(base::Time value) {
143 DCHECK(kernel_);
144 write_transaction()->TrackChangesTo(kernel_);
145 if (kernel_->ref(CTIME) != value) {
146 kernel_->put(CTIME, value);
147 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
151 void MutableEntry::PutParentId(const Id& value) {
152 DCHECK(kernel_);
153 write_transaction()->TrackChangesTo(kernel_);
154 if (kernel_->ref(PARENT_ID) != value) {
155 PutParentIdPropertyOnly(value);
156 if (!GetIsDel()) {
157 if (!PutPredecessor(Id())) {
158 // TODO(lipalani) : Propagate the error to caller. crbug.com/100444.
159 NOTREACHED();
165 void MutableEntry::PutIsDir(bool value) {
166 DCHECK(kernel_);
167 write_transaction()->TrackChangesTo(kernel_);
168 bool old_value = kernel_->ref(IS_DIR);
169 if (old_value != value) {
170 kernel_->put(IS_DIR, value);
171 kernel_->mark_dirty(GetDirtyIndexHelper());
175 void MutableEntry::PutIsDel(bool value) {
176 DCHECK(kernel_);
177 write_transaction()->TrackChangesTo(kernel_);
178 if (value == kernel_->ref(IS_DEL)) {
179 return;
181 if (value) {
182 // If the server never knew about this item and it's deleted then we don't
183 // need to keep it around. Unsetting IS_UNSYNCED will:
184 // - Ensure that the item is never committed to the server.
185 // - Allow any items with the same UNIQUE_CLIENT_TAG created on other
186 // clients to override this entry.
187 // - Let us delete this entry permanently when we next restart sync - see
188 // DirectoryBackingStore::SafeToPurgeOnLoading.
189 // This will avoid crbug.com/125381.
190 // Note: do not unset IsUnsynced if the syncer has started but not yet
191 // finished committing this entity.
192 if (!GetId().ServerKnows() && !GetSyncing()) {
193 PutIsUnsynced(false);
198 ScopedKernelLock lock(dir());
199 // Some indices don't include deleted items and must be updated
200 // upon a value change.
201 ScopedParentChildIndexUpdater updater(lock, kernel_,
202 &dir()->kernel()->parent_child_index);
204 kernel_->put(IS_DEL, value);
205 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
209 void MutableEntry::PutNonUniqueName(const std::string& value) {
210 DCHECK(kernel_);
211 write_transaction()->TrackChangesTo(kernel_);
213 if (kernel_->ref(NON_UNIQUE_NAME) != value) {
214 kernel_->put(NON_UNIQUE_NAME, value);
215 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
219 void MutableEntry::PutSpecifics(const sync_pb::EntitySpecifics& value) {
220 DCHECK(kernel_);
221 CHECK(!value.password().has_client_only_encrypted_data());
222 write_transaction()->TrackChangesTo(kernel_);
223 // TODO(ncarter): This is unfortunately heavyweight. Can we do
224 // better?
225 const std::string& serialized_value = value.SerializeAsString();
226 if (serialized_value != kernel_->ref(SPECIFICS).SerializeAsString()) {
227 // Check for potential sharing - SPECIFICS is often
228 // copied from SERVER_SPECIFICS.
229 if (serialized_value ==
230 kernel_->ref(SERVER_SPECIFICS).SerializeAsString()) {
231 kernel_->copy(SERVER_SPECIFICS, SPECIFICS);
232 } else {
233 kernel_->put(SPECIFICS, value);
235 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
239 void MutableEntry::PutUniquePosition(const UniquePosition& value) {
240 DCHECK(kernel_);
241 write_transaction()->TrackChangesTo(kernel_);
242 if(!kernel_->ref(UNIQUE_POSITION).Equals(value)) {
243 // We should never overwrite a valid position with an invalid one.
244 DCHECK(value.IsValid());
245 ScopedKernelLock lock(dir());
246 ScopedParentChildIndexUpdater updater(
247 lock, kernel_, &dir()->kernel()->parent_child_index);
248 kernel_->put(UNIQUE_POSITION, value);
249 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
253 bool MutableEntry::PutPredecessor(const Id& predecessor_id) {
254 if (predecessor_id.IsNull()) {
255 dir()->PutPredecessor(kernel_, NULL);
256 } else {
257 MutableEntry predecessor(write_transaction(), GET_BY_ID, predecessor_id);
258 if (!predecessor.good())
259 return false;
260 dir()->PutPredecessor(kernel_, predecessor.kernel_);
262 return true;
265 void MutableEntry::PutAttachmentMetadata(
266 const sync_pb::AttachmentMetadata& value) {
267 DCHECK(kernel_);
268 write_transaction()->TrackChangesTo(kernel_);
269 const std::string& serialized_value = value.SerializeAsString();
270 if (serialized_value !=
271 kernel_->ref(ATTACHMENT_METADATA).SerializeAsString()) {
272 dir()->UpdateAttachmentIndex(GetMetahandle(),
273 kernel_->ref(ATTACHMENT_METADATA), value);
274 // Check for potential sharing - ATTACHMENT_METADATA is often
275 // copied from SERVER_ATTACHMENT_METADATA.
276 if (serialized_value ==
277 kernel_->ref(SERVER_ATTACHMENT_METADATA).SerializeAsString()) {
278 kernel_->copy(SERVER_ATTACHMENT_METADATA, ATTACHMENT_METADATA);
279 } else {
280 kernel_->put(ATTACHMENT_METADATA, value);
282 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
286 void MutableEntry::MarkAttachmentAsOnServer(
287 const sync_pb::AttachmentIdProto& attachment_id) {
288 DCHECK(kernel_);
289 DCHECK(!attachment_id.unique_id().empty());
290 write_transaction()->TrackChangesTo(kernel_);
291 sync_pb::AttachmentMetadata attachment_metadata =
292 kernel_->ref(ATTACHMENT_METADATA);
293 for (int i = 0; i < attachment_metadata.record_size(); ++i) {
294 sync_pb::AttachmentMetadataRecord* record =
295 attachment_metadata.mutable_record(i);
296 if (record->id().unique_id() != attachment_id.unique_id())
297 continue;
298 record->set_is_on_server(true);
300 kernel_->put(ATTACHMENT_METADATA, attachment_metadata);
301 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
302 MarkForSyncing(this);
305 // This function sets only the flags needed to get this entry to sync.
306 bool MarkForSyncing(MutableEntry* e) {
307 DCHECK_NE(static_cast<MutableEntry*>(NULL), e);
308 DCHECK(!e->IsRoot()) << "We shouldn't mark a permanent object for syncing.";
309 if (!(e->PutIsUnsynced(true)))
310 return false;
311 if (e->GetSyncing())
312 e->PutDirtySync(true);
313 return true;
316 } // namespace syncable
317 } // namespace syncer