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