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"
22 void MutableEntry::Init(WriteTransaction
* trans
,
26 scoped_ptr
<EntryKernel
> kernel(new EntryKernel
);
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
,
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_
);
74 MutableEntry::MutableEntry(WriteTransaction
* trans
,
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
));
89 DCHECK(!ShouldMaintainPosition());
92 bool result
= trans
->directory()->InsertEntry(trans
, kernel_
);
96 MutableEntry::MutableEntry(WriteTransaction
* trans
, CreateNewUpdateItem
,
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
,
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
) {
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
) {
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
) {
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
) {
153 write_transaction()->TrackChangesTo(kernel_
);
154 if (kernel_
->ref(PARENT_ID
) != value
) {
155 PutParentIdPropertyOnly(value
);
157 if (!PutPredecessor(Id())) {
158 // TODO(lipalani) : Propagate the error to caller. crbug.com/100444.
165 void MutableEntry::PutIsDir(bool value
) {
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
) {
177 write_transaction()->TrackChangesTo(kernel_
);
178 if (value
== kernel_
->ref(IS_DEL
)) {
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
) {
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
) {
221 CHECK(!value
.password().has_client_only_encrypted_data());
222 write_transaction()->TrackChangesTo(kernel_
);
223 // TODO(ncarter): This is unfortunately heavyweight. Can we do
225 if (kernel_
->ref(SPECIFICS
).SerializeAsString() !=
226 value
.SerializeAsString()) {
227 kernel_
->put(SPECIFICS
, value
);
228 kernel_
->mark_dirty(&dir()->kernel()->dirty_metahandles
);
232 void MutableEntry::PutUniquePosition(const UniquePosition
& value
) {
234 write_transaction()->TrackChangesTo(kernel_
);
235 if(!kernel_
->ref(UNIQUE_POSITION
).Equals(value
)) {
236 // We should never overwrite a valid position with an invalid one.
237 DCHECK(value
.IsValid());
238 ScopedKernelLock
lock(dir());
239 ScopedParentChildIndexUpdater
updater(
240 lock
, kernel_
, &dir()->kernel()->parent_child_index
);
241 kernel_
->put(UNIQUE_POSITION
, value
);
242 kernel_
->mark_dirty(&dir()->kernel()->dirty_metahandles
);
246 bool MutableEntry::PutPredecessor(const Id
& predecessor_id
) {
247 if (predecessor_id
.IsNull()) {
248 dir()->PutPredecessor(kernel_
, NULL
);
250 MutableEntry
predecessor(write_transaction(), GET_BY_ID
, predecessor_id
);
251 if (!predecessor
.good())
253 dir()->PutPredecessor(kernel_
, predecessor
.kernel_
);
258 void MutableEntry::PutAttachmentMetadata(
259 const sync_pb::AttachmentMetadata
& attachment_metadata
) {
261 write_transaction()->TrackChangesTo(kernel_
);
262 if (kernel_
->ref(ATTACHMENT_METADATA
).SerializeAsString() !=
263 attachment_metadata
.SerializeAsString()) {
264 dir()->UpdateAttachmentIndex(GetMetahandle(),
265 kernel_
->ref(ATTACHMENT_METADATA
),
266 attachment_metadata
);
267 kernel_
->put(ATTACHMENT_METADATA
, attachment_metadata
);
268 kernel_
->mark_dirty(&dir()->kernel()->dirty_metahandles
);
272 void MutableEntry::MarkAttachmentAsOnServer(
273 const sync_pb::AttachmentIdProto
& attachment_id
) {
275 DCHECK(!attachment_id
.unique_id().empty());
276 write_transaction()->TrackChangesTo(kernel_
);
277 sync_pb::AttachmentMetadata
& attachment_metadata
=
278 kernel_
->mutable_ref(ATTACHMENT_METADATA
);
279 for (int i
= 0; i
< attachment_metadata
.record_size(); ++i
) {
280 sync_pb::AttachmentMetadataRecord
* record
=
281 attachment_metadata
.mutable_record(i
);
282 if (record
->id().unique_id() != attachment_id
.unique_id())
284 record
->set_is_on_server(true);
286 kernel_
->mark_dirty(&dir()->kernel()->dirty_metahandles
);
287 MarkForSyncing(this);
290 // This function sets only the flags needed to get this entry to sync.
291 bool MarkForSyncing(MutableEntry
* e
) {
292 DCHECK_NE(static_cast<MutableEntry
*>(NULL
), e
);
293 DCHECK(!e
->IsRoot()) << "We shouldn't mark a permanent object for syncing.";
294 if (!(e
->PutIsUnsynced(true)))
297 e
->PutDirtySync(true);
301 } // namespace syncable
302 } // namespace syncer