Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / sync / syncable / model_neutral_mutable_entry.cc
blob02cdcbbcde27757afd1b6a406fc5154f6b7174f6
1 // Copyright 2013 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/model_neutral_mutable_entry.h"
7 #include <string>
9 #include "sync/internal_api/public/base/unique_position.h"
10 #include "sync/syncable/directory.h"
11 #include "sync/syncable/scoped_kernel_lock.h"
12 #include "sync/syncable/syncable_changes_version.h"
13 #include "sync/syncable/syncable_util.h"
14 #include "sync/syncable/syncable_write_transaction.h"
16 using std::string;
18 namespace syncer {
20 namespace syncable {
22 ModelNeutralMutableEntry::ModelNeutralMutableEntry(BaseWriteTransaction* trans,
23 CreateNewUpdateItem,
24 const Id& id)
25 : Entry(trans), base_write_transaction_(trans) {
26 Entry same_id(trans, GET_BY_ID, id);
27 kernel_ = NULL;
28 if (same_id.good()) {
29 return; // already have an item with this ID.
31 scoped_ptr<EntryKernel> kernel(new EntryKernel());
33 kernel->put(ID, id);
34 kernel->put(META_HANDLE, trans->directory()->NextMetahandle());
35 kernel->mark_dirty(&trans->directory()->kernel()->dirty_metahandles);
36 kernel->put(IS_DEL, true);
37 // We match the database defaults here
38 kernel->put(BASE_VERSION, CHANGES_VERSION);
39 if (!trans->directory()->InsertEntry(trans, kernel.get())) {
40 return; // Failed inserting.
42 trans->TrackChangesTo(kernel.get());
44 kernel_ = kernel.release();
47 ModelNeutralMutableEntry::ModelNeutralMutableEntry(BaseWriteTransaction* trans,
48 CreateNewTypeRoot,
49 ModelType type)
50 : Entry(trans), base_write_transaction_(trans) {
51 DCHECK(IsTypeWithClientGeneratedRoot(type));
52 Entry same_type_root(trans, GET_TYPE_ROOT, type);
53 kernel_ = NULL;
54 if (same_type_root.good()) {
55 return; // already have a type root for the given type
58 scoped_ptr<EntryKernel> kernel(new EntryKernel());
60 sync_pb::EntitySpecifics specifics;
61 AddDefaultFieldValue(type, &specifics);
62 kernel->put(SPECIFICS, specifics);
64 kernel->put(ID,
65 syncable::Id::CreateFromClientString(ModelTypeToString(type)));
66 kernel->put(META_HANDLE, trans->directory()->NextMetahandle());
67 kernel->put(PARENT_ID, syncable::Id::GetRoot());
68 kernel->put(BASE_VERSION, CHANGES_VERSION);
69 kernel->put(NON_UNIQUE_NAME, ModelTypeToString(type));
70 kernel->put(IS_DIR, true);
72 kernel->mark_dirty(&trans->directory()->kernel()->dirty_metahandles);
74 if (!trans->directory()->InsertEntry(trans, kernel.get())) {
75 return; // Failed inserting.
78 trans->TrackChangesTo(kernel.get());
80 kernel_ = kernel.release();
83 ModelNeutralMutableEntry::ModelNeutralMutableEntry(
84 BaseWriteTransaction* trans, GetById, const Id& id)
85 : Entry(trans, GET_BY_ID, id), base_write_transaction_(trans) {
88 ModelNeutralMutableEntry::ModelNeutralMutableEntry(
89 BaseWriteTransaction* trans, GetByHandle, int64 metahandle)
90 : Entry(trans, GET_BY_HANDLE, metahandle), base_write_transaction_(trans) {
93 ModelNeutralMutableEntry::ModelNeutralMutableEntry(
94 BaseWriteTransaction* trans, GetByClientTag, const std::string& tag)
95 : Entry(trans, GET_BY_CLIENT_TAG, tag), base_write_transaction_(trans) {
98 ModelNeutralMutableEntry::ModelNeutralMutableEntry(
99 BaseWriteTransaction* trans, GetTypeRoot, ModelType type)
100 : Entry(trans, GET_TYPE_ROOT, type), base_write_transaction_(trans) {
103 void ModelNeutralMutableEntry::PutBaseVersion(int64 value) {
104 DCHECK(kernel_);
105 base_write_transaction_->TrackChangesTo(kernel_);
106 if (kernel_->ref(BASE_VERSION) != value) {
107 kernel_->put(BASE_VERSION, value);
108 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
112 void ModelNeutralMutableEntry::PutServerVersion(int64 value) {
113 DCHECK(kernel_);
114 base_write_transaction_->TrackChangesTo(kernel_);
115 if (kernel_->ref(SERVER_VERSION) != value) {
116 ScopedKernelLock lock(dir());
117 kernel_->put(SERVER_VERSION, value);
118 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
122 void ModelNeutralMutableEntry::PutServerMtime(base::Time value) {
123 DCHECK(kernel_);
124 base_write_transaction_->TrackChangesTo(kernel_);
125 if (kernel_->ref(SERVER_MTIME) != value) {
126 kernel_->put(SERVER_MTIME, value);
127 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
131 void ModelNeutralMutableEntry::PutServerCtime(base::Time value) {
132 DCHECK(kernel_);
133 base_write_transaction_->TrackChangesTo(kernel_);
134 if (kernel_->ref(SERVER_CTIME) != value) {
135 kernel_->put(SERVER_CTIME, value);
136 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
140 bool ModelNeutralMutableEntry::PutId(const Id& value) {
141 DCHECK(kernel_);
142 base_write_transaction_->TrackChangesTo(kernel_);
143 if (kernel_->ref(ID) != value) {
144 if (!dir()->ReindexId(base_write_transaction(), kernel_, value))
145 return false;
146 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
148 return true;
151 void ModelNeutralMutableEntry::PutServerParentId(const Id& value) {
152 DCHECK(kernel_);
153 base_write_transaction_->TrackChangesTo(kernel_);
155 if (kernel_->ref(SERVER_PARENT_ID) != value) {
156 kernel_->put(SERVER_PARENT_ID, value);
157 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
161 bool ModelNeutralMutableEntry::PutIsUnsynced(bool value) {
162 DCHECK(kernel_);
163 base_write_transaction_->TrackChangesTo(kernel_);
164 if (kernel_->ref(IS_UNSYNCED) != value) {
165 MetahandleSet* index = &dir()->kernel()->unsynced_metahandles;
167 ScopedKernelLock lock(dir());
168 if (value) {
169 if (!SyncAssert(index->insert(kernel_->ref(META_HANDLE)).second,
170 FROM_HERE,
171 "Could not insert",
172 base_write_transaction())) {
173 return false;
175 } else {
176 if (!SyncAssert(1U == index->erase(kernel_->ref(META_HANDLE)),
177 FROM_HERE,
178 "Entry Not succesfully erased",
179 base_write_transaction())) {
180 return false;
183 kernel_->put(IS_UNSYNCED, value);
184 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
186 return true;
189 bool ModelNeutralMutableEntry::PutIsUnappliedUpdate(bool value) {
190 DCHECK(kernel_);
191 base_write_transaction_->TrackChangesTo(kernel_);
192 if (kernel_->ref(IS_UNAPPLIED_UPDATE) != value) {
193 // Use kernel_->GetServerModelType() instead of
194 // GetServerModelType() as we may trigger some DCHECKs in the
195 // latter.
196 MetahandleSet* index = &dir()->kernel()->unapplied_update_metahandles[
197 kernel_->GetServerModelType()];
199 ScopedKernelLock lock(dir());
200 if (value) {
201 if (!SyncAssert(index->insert(kernel_->ref(META_HANDLE)).second,
202 FROM_HERE,
203 "Could not insert",
204 base_write_transaction())) {
205 return false;
207 } else {
208 if (!SyncAssert(1U == index->erase(kernel_->ref(META_HANDLE)),
209 FROM_HERE,
210 "Entry Not succesfully erased",
211 base_write_transaction())) {
212 return false;
215 kernel_->put(IS_UNAPPLIED_UPDATE, value);
216 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
218 return true;
221 void ModelNeutralMutableEntry::PutServerIsDir(bool value) {
222 DCHECK(kernel_);
223 base_write_transaction_->TrackChangesTo(kernel_);
224 bool old_value = kernel_->ref(SERVER_IS_DIR);
225 if (old_value != value) {
226 kernel_->put(SERVER_IS_DIR, value);
227 kernel_->mark_dirty(GetDirtyIndexHelper());
231 void ModelNeutralMutableEntry::PutServerIsDel(bool value) {
232 DCHECK(kernel_);
233 base_write_transaction_->TrackChangesTo(kernel_);
234 bool old_value = kernel_->ref(SERVER_IS_DEL);
235 if (old_value != value) {
236 kernel_->put(SERVER_IS_DEL, value);
237 kernel_->mark_dirty(GetDirtyIndexHelper());
240 if (!value || kernel_->ref(IS_UNAPPLIED_UPDATE)) {
241 // Update delete journal for existence status change on server side here
242 // instead of in PutIsDel() because IS_DEL may not be updated due to
243 // early returns when processing updates. And because
244 // UpdateDeleteJournalForServerDelete() checks for SERVER_IS_DEL, it has
245 // to be called on sync thread.
247 // Please note that the delete journal applies only to the deletions
248 // originating on the server side (hence the IS_UNAPPLIED_UPDATE check),
249 // but it still makes sense to remove the entry from the delete journal
250 // when it gets undeleted locally.
251 dir()->delete_journal()->UpdateDeleteJournalForServerDelete(
252 base_write_transaction(), old_value, *kernel_);
256 void ModelNeutralMutableEntry::PutServerNonUniqueName(
257 const std::string& value) {
258 DCHECK(kernel_);
259 base_write_transaction_->TrackChangesTo(kernel_);
261 if (kernel_->ref(SERVER_NON_UNIQUE_NAME) != value) {
262 kernel_->put(SERVER_NON_UNIQUE_NAME, value);
263 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
267 bool ModelNeutralMutableEntry::PutUniqueServerTag(const string& new_tag) {
268 if (new_tag == kernel_->ref(UNIQUE_SERVER_TAG)) {
269 return true;
272 base_write_transaction_->TrackChangesTo(kernel_);
273 ScopedKernelLock lock(dir());
274 // Make sure your new value is not in there already.
275 if (dir()->kernel()->server_tags_map.find(new_tag) !=
276 dir()->kernel()->server_tags_map.end()) {
277 DVLOG(1) << "Detected duplicate server tag";
278 return false;
280 dir()->kernel()->server_tags_map.erase(
281 kernel_->ref(UNIQUE_SERVER_TAG));
282 kernel_->put(UNIQUE_SERVER_TAG, new_tag);
283 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
284 if (!new_tag.empty()) {
285 dir()->kernel()->server_tags_map[new_tag] = kernel_;
288 return true;
291 bool ModelNeutralMutableEntry::PutUniqueClientTag(const string& new_tag) {
292 if (new_tag == kernel_->ref(UNIQUE_CLIENT_TAG)) {
293 return true;
296 base_write_transaction_->TrackChangesTo(kernel_);
297 ScopedKernelLock lock(dir());
298 // Make sure your new value is not in there already.
299 if (dir()->kernel()->client_tags_map.find(new_tag) !=
300 dir()->kernel()->client_tags_map.end()) {
301 DVLOG(1) << "Detected duplicate client tag";
302 return false;
304 dir()->kernel()->client_tags_map.erase(
305 kernel_->ref(UNIQUE_CLIENT_TAG));
306 kernel_->put(UNIQUE_CLIENT_TAG, new_tag);
307 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
308 if (!new_tag.empty()) {
309 dir()->kernel()->client_tags_map[new_tag] = kernel_;
312 return true;
315 void ModelNeutralMutableEntry::PutUniqueBookmarkTag(const std::string& tag) {
316 // This unique tag will eventually be used as the unique suffix when adjusting
317 // this bookmark's position. Let's make sure it's a valid suffix.
318 if (!UniquePosition::IsValidSuffix(tag)) {
319 NOTREACHED();
320 return;
323 if (!kernel_->ref(UNIQUE_BOOKMARK_TAG).empty() &&
324 tag != kernel_->ref(UNIQUE_BOOKMARK_TAG)) {
325 // There is only one scenario where our tag is expected to change. That
326 // scenario occurs when our current tag is a non-correct tag assigned during
327 // the UniquePosition migration.
328 std::string migration_generated_tag =
329 GenerateSyncableBookmarkHash(std::string(),
330 kernel_->ref(ID).GetServerId());
331 DCHECK_EQ(migration_generated_tag, kernel_->ref(UNIQUE_BOOKMARK_TAG));
334 kernel_->put(UNIQUE_BOOKMARK_TAG, tag);
335 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
338 void ModelNeutralMutableEntry::PutServerSpecifics(
339 const sync_pb::EntitySpecifics& value) {
340 DCHECK(kernel_);
341 CHECK(!value.password().has_client_only_encrypted_data());
342 base_write_transaction_->TrackChangesTo(kernel_);
343 // TODO(ncarter): This is unfortunately heavyweight. Can we do
344 // better?
345 if (kernel_->ref(SERVER_SPECIFICS).SerializeAsString() !=
346 value.SerializeAsString()) {
347 if (kernel_->ref(IS_UNAPPLIED_UPDATE)) {
348 // Remove ourselves from unapplied_update_metahandles with our
349 // old server type.
350 const ModelType old_server_type = kernel_->GetServerModelType();
351 const int64 metahandle = kernel_->ref(META_HANDLE);
352 size_t erase_count =
353 dir()->kernel()->unapplied_update_metahandles[old_server_type]
354 .erase(metahandle);
355 DCHECK_EQ(erase_count, 1u);
358 kernel_->put(SERVER_SPECIFICS, value);
359 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
361 if (kernel_->ref(IS_UNAPPLIED_UPDATE)) {
362 // Add ourselves back into unapplied_update_metahandles with our
363 // new server type.
364 const ModelType new_server_type = kernel_->GetServerModelType();
365 const int64 metahandle = kernel_->ref(META_HANDLE);
366 dir()->kernel()->unapplied_update_metahandles[new_server_type]
367 .insert(metahandle);
372 void ModelNeutralMutableEntry::PutBaseServerSpecifics(
373 const sync_pb::EntitySpecifics& value) {
374 DCHECK(kernel_);
375 CHECK(!value.password().has_client_only_encrypted_data());
376 base_write_transaction_->TrackChangesTo(kernel_);
377 // TODO(ncarter): This is unfortunately heavyweight. Can we do
378 // better?
379 if (kernel_->ref(BASE_SERVER_SPECIFICS).SerializeAsString()
380 != value.SerializeAsString()) {
381 kernel_->put(BASE_SERVER_SPECIFICS, value);
382 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
386 void ModelNeutralMutableEntry::PutServerUniquePosition(
387 const UniquePosition& value) {
388 DCHECK(kernel_);
389 base_write_transaction_->TrackChangesTo(kernel_);
390 if(!kernel_->ref(SERVER_UNIQUE_POSITION).Equals(value)) {
391 // We should never overwrite a valid position with an invalid one.
392 DCHECK(value.IsValid());
393 ScopedKernelLock lock(dir());
394 kernel_->put(SERVER_UNIQUE_POSITION, value);
395 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
399 void ModelNeutralMutableEntry::PutServerAttachmentMetadata(
400 const sync_pb::AttachmentMetadata& value) {
401 DCHECK(kernel_);
402 base_write_transaction_->TrackChangesTo(kernel_);
404 if (kernel_->ref(SERVER_ATTACHMENT_METADATA).SerializeAsString() !=
405 value.SerializeAsString()) {
406 kernel_->put(SERVER_ATTACHMENT_METADATA, value);
407 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
411 void ModelNeutralMutableEntry::PutSyncing(bool value) {
412 kernel_->put(SYNCING, value);
415 void ModelNeutralMutableEntry::PutDirtySync(bool value) {
416 DCHECK(!value || GetSyncing());
417 kernel_->put(DIRTY_SYNC, value);
420 void ModelNeutralMutableEntry::PutParentIdPropertyOnly(const Id& parent_id) {
421 base_write_transaction_->TrackChangesTo(kernel_);
422 dir()->ReindexParentId(base_write_transaction(), kernel_, parent_id);
423 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles);
426 void ModelNeutralMutableEntry::UpdateTransactionVersion(int64 value) {
427 ScopedKernelLock lock(dir());
428 kernel_->put(TRANSACTION_VERSION, value);
429 kernel_->mark_dirty(&(dir()->kernel()->dirty_metahandles));
432 ModelNeutralMutableEntry::ModelNeutralMutableEntry(BaseWriteTransaction* trans)
433 : Entry(trans), base_write_transaction_(trans) {}
435 MetahandleSet* ModelNeutralMutableEntry::GetDirtyIndexHelper() {
436 return &dir()->kernel()->dirty_metahandles;
439 } // namespace syncable
441 } // namespace syncer