Update V8 to version 4.6.55.
[chromium-blink-merge.git] / sync / syncable / mutable_entry.cc
blob7c42b48e74dfb1694e70bf5662e944bb46366043
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 if (kernel_->ref(LOCAL_EXTERNAL_ID) != value) {
126 write_transaction()->TrackChangesTo(kernel_);
127 ScopedKernelLock lock(dir());
128 kernel_->put(LOCAL_EXTERNAL_ID, value);
129 MarkDirty();
133 void MutableEntry::PutMtime(base::Time value) {
134 DCHECK(kernel_);
135 if (kernel_->ref(MTIME) != value) {
136 write_transaction()->TrackChangesTo(kernel_);
137 kernel_->put(MTIME, value);
138 MarkDirty();
142 void MutableEntry::PutCtime(base::Time value) {
143 DCHECK(kernel_);
144 if (kernel_->ref(CTIME) != value) {
145 write_transaction()->TrackChangesTo(kernel_);
146 kernel_->put(CTIME, value);
147 MarkDirty();
151 void MutableEntry::PutParentId(const Id& value) {
152 DCHECK(kernel_);
153 if (kernel_->ref(PARENT_ID) != value) {
154 write_transaction()->TrackChangesTo(kernel_);
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 if (kernel_->ref(IS_DIR) != value) {
168 write_transaction()->TrackChangesTo(kernel_);
169 kernel_->put(IS_DIR, value);
170 MarkDirty();
174 void MutableEntry::PutIsDel(bool value) {
175 DCHECK(kernel_);
176 if (value == kernel_->ref(IS_DEL)) {
177 return;
180 write_transaction()->TrackChangesTo(kernel_);
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 MarkDirty();
209 void MutableEntry::PutNonUniqueName(const std::string& value) {
210 DCHECK(kernel_);
211 if (kernel_->ref(NON_UNIQUE_NAME) != value) {
212 write_transaction()->TrackChangesTo(kernel_);
213 kernel_->put(NON_UNIQUE_NAME, value);
214 MarkDirty();
218 void MutableEntry::PutSpecifics(const sync_pb::EntitySpecifics& value) {
219 DCHECK(kernel_);
220 CHECK(!value.password().has_client_only_encrypted_data());
221 // TODO(ncarter): This is unfortunately heavyweight. Can we do
222 // better?
223 const std::string& serialized_value = value.SerializeAsString();
224 if (serialized_value != kernel_->ref(SPECIFICS).SerializeAsString()) {
225 write_transaction()->TrackChangesTo(kernel_);
226 // Check for potential sharing - SPECIFICS is often
227 // copied from SERVER_SPECIFICS.
228 if (serialized_value ==
229 kernel_->ref(SERVER_SPECIFICS).SerializeAsString()) {
230 kernel_->copy(SERVER_SPECIFICS, SPECIFICS);
231 } else {
232 kernel_->put(SPECIFICS, value);
234 MarkDirty();
238 void MutableEntry::PutUniquePosition(const UniquePosition& value) {
239 DCHECK(kernel_);
240 if(!kernel_->ref(UNIQUE_POSITION).Equals(value)) {
241 write_transaction()->TrackChangesTo(kernel_);
242 // We should never overwrite a valid position with an invalid one.
243 DCHECK(value.IsValid());
244 ScopedKernelLock lock(dir());
245 ScopedParentChildIndexUpdater updater(
246 lock, kernel_, &dir()->kernel()->parent_child_index);
247 kernel_->put(UNIQUE_POSITION, value);
248 MarkDirty();
252 bool MutableEntry::PutPredecessor(const Id& predecessor_id) {
253 if (predecessor_id.IsNull()) {
254 dir()->PutPredecessor(kernel_, NULL);
255 } else {
256 MutableEntry predecessor(write_transaction(), GET_BY_ID, predecessor_id);
257 if (!predecessor.good())
258 return false;
259 dir()->PutPredecessor(kernel_, predecessor.kernel_);
260 // No need to make the entry dirty here because setting the predecessor
261 // results in PutUniquePosition call (which might or might not make the
262 // entry dirty).
264 return true;
267 void MutableEntry::PutAttachmentMetadata(
268 const sync_pb::AttachmentMetadata& value) {
269 DCHECK(kernel_);
270 const std::string& serialized_value = value.SerializeAsString();
271 if (serialized_value !=
272 kernel_->ref(ATTACHMENT_METADATA).SerializeAsString()) {
273 write_transaction()->TrackChangesTo(kernel_);
274 dir()->UpdateAttachmentIndex(GetMetahandle(),
275 kernel_->ref(ATTACHMENT_METADATA), value);
276 // Check for potential sharing - ATTACHMENT_METADATA is often
277 // copied from SERVER_ATTACHMENT_METADATA.
278 if (serialized_value ==
279 kernel_->ref(SERVER_ATTACHMENT_METADATA).SerializeAsString()) {
280 kernel_->copy(SERVER_ATTACHMENT_METADATA, ATTACHMENT_METADATA);
281 } else {
282 kernel_->put(ATTACHMENT_METADATA, value);
284 MarkDirty();
288 void MutableEntry::MarkAttachmentAsOnServer(
289 const sync_pb::AttachmentIdProto& attachment_id) {
290 DCHECK(kernel_);
291 DCHECK(!attachment_id.unique_id().empty());
292 write_transaction()->TrackChangesTo(kernel_);
293 sync_pb::AttachmentMetadata attachment_metadata =
294 kernel_->ref(ATTACHMENT_METADATA);
295 for (int i = 0; i < attachment_metadata.record_size(); ++i) {
296 sync_pb::AttachmentMetadataRecord* record =
297 attachment_metadata.mutable_record(i);
298 if (record->id().unique_id() != attachment_id.unique_id())
299 continue;
300 record->set_is_on_server(true);
302 kernel_->put(ATTACHMENT_METADATA, attachment_metadata);
303 MarkDirty();
304 MarkForSyncing(this);
307 // This function sets only the flags needed to get this entry to sync.
308 bool MarkForSyncing(MutableEntry* e) {
309 DCHECK_NE(static_cast<MutableEntry*>(NULL), e);
310 DCHECK(!e->IsRoot()) << "We shouldn't mark a permanent object for syncing.";
311 if (!(e->PutIsUnsynced(true)))
312 return false;
313 if (e->GetSyncing())
314 e->PutDirtySync(true);
315 return true;
318 } // namespace syncable
319 } // namespace syncer