OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/syncable/mutable_entry.h" | 5 #include "sync/syncable/mutable_entry.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "sync/internal_api/public/base/unique_position.h" | 8 #include "sync/internal_api/public/base/unique_position.h" |
9 #include "sync/syncable/directory.h" | 9 #include "sync/syncable/directory.h" |
10 #include "sync/syncable/scoped_kernel_lock.h" | 10 #include "sync/syncable/scoped_kernel_lock.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 write_transaction_(trans) { | 115 write_transaction_(trans) { |
116 } | 116 } |
117 | 117 |
118 MutableEntry::MutableEntry(WriteTransaction* trans, GetTypeRoot, ModelType type) | 118 MutableEntry::MutableEntry(WriteTransaction* trans, GetTypeRoot, ModelType type) |
119 : ModelNeutralMutableEntry(trans, GET_TYPE_ROOT, type), | 119 : ModelNeutralMutableEntry(trans, GET_TYPE_ROOT, type), |
120 write_transaction_(trans) { | 120 write_transaction_(trans) { |
121 } | 121 } |
122 | 122 |
123 void MutableEntry::PutLocalExternalId(int64 value) { | 123 void MutableEntry::PutLocalExternalId(int64 value) { |
124 DCHECK(kernel_); | 124 DCHECK(kernel_); |
125 write_transaction()->TrackChangesTo(kernel_); | |
126 if (kernel_->ref(LOCAL_EXTERNAL_ID) != value) { | 125 if (kernel_->ref(LOCAL_EXTERNAL_ID) != value) { |
| 126 write_transaction()->TrackChangesTo(kernel_); |
127 ScopedKernelLock lock(dir()); | 127 ScopedKernelLock lock(dir()); |
128 kernel_->put(LOCAL_EXTERNAL_ID, value); | 128 kernel_->put(LOCAL_EXTERNAL_ID, value); |
129 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); | 129 MarkDirty(); |
130 } | 130 } |
131 } | 131 } |
132 | 132 |
133 void MutableEntry::PutMtime(base::Time value) { | 133 void MutableEntry::PutMtime(base::Time value) { |
134 DCHECK(kernel_); | 134 DCHECK(kernel_); |
135 write_transaction()->TrackChangesTo(kernel_); | |
136 if (kernel_->ref(MTIME) != value) { | 135 if (kernel_->ref(MTIME) != value) { |
| 136 write_transaction()->TrackChangesTo(kernel_); |
137 kernel_->put(MTIME, value); | 137 kernel_->put(MTIME, value); |
138 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); | 138 MarkDirty(); |
139 } | 139 } |
140 } | 140 } |
141 | 141 |
142 void MutableEntry::PutCtime(base::Time value) { | 142 void MutableEntry::PutCtime(base::Time value) { |
143 DCHECK(kernel_); | 143 DCHECK(kernel_); |
144 write_transaction()->TrackChangesTo(kernel_); | |
145 if (kernel_->ref(CTIME) != value) { | 144 if (kernel_->ref(CTIME) != value) { |
| 145 write_transaction()->TrackChangesTo(kernel_); |
146 kernel_->put(CTIME, value); | 146 kernel_->put(CTIME, value); |
147 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); | 147 MarkDirty(); |
148 } | 148 } |
149 } | 149 } |
150 | 150 |
151 void MutableEntry::PutParentId(const Id& value) { | 151 void MutableEntry::PutParentId(const Id& value) { |
152 DCHECK(kernel_); | 152 DCHECK(kernel_); |
153 write_transaction()->TrackChangesTo(kernel_); | |
154 if (kernel_->ref(PARENT_ID) != value) { | 153 if (kernel_->ref(PARENT_ID) != value) { |
| 154 write_transaction()->TrackChangesTo(kernel_); |
155 PutParentIdPropertyOnly(value); | 155 PutParentIdPropertyOnly(value); |
156 if (!GetIsDel()) { | 156 if (!GetIsDel()) { |
157 if (!PutPredecessor(Id())) { | 157 if (!PutPredecessor(Id())) { |
158 // TODO(lipalani) : Propagate the error to caller. crbug.com/100444. | 158 // TODO(lipalani) : Propagate the error to caller. crbug.com/100444. |
159 NOTREACHED(); | 159 NOTREACHED(); |
160 } | 160 } |
161 } | 161 } |
162 } | 162 } |
163 } | 163 } |
164 | 164 |
165 void MutableEntry::PutIsDir(bool value) { | 165 void MutableEntry::PutIsDir(bool value) { |
166 DCHECK(kernel_); | 166 DCHECK(kernel_); |
167 write_transaction()->TrackChangesTo(kernel_); | 167 if (kernel_->ref(IS_DIR) != value) { |
168 bool old_value = kernel_->ref(IS_DIR); | 168 write_transaction()->TrackChangesTo(kernel_); |
169 if (old_value != value) { | |
170 kernel_->put(IS_DIR, value); | 169 kernel_->put(IS_DIR, value); |
171 kernel_->mark_dirty(GetDirtyIndexHelper()); | 170 MarkDirty(); |
172 } | 171 } |
173 } | 172 } |
174 | 173 |
175 void MutableEntry::PutIsDel(bool value) { | 174 void MutableEntry::PutIsDel(bool value) { |
176 DCHECK(kernel_); | 175 DCHECK(kernel_); |
177 write_transaction()->TrackChangesTo(kernel_); | |
178 if (value == kernel_->ref(IS_DEL)) { | 176 if (value == kernel_->ref(IS_DEL)) { |
179 return; | 177 return; |
180 } | 178 } |
| 179 |
| 180 write_transaction()->TrackChangesTo(kernel_); |
181 if (value) { | 181 if (value) { |
182 // If the server never knew about this item and it's deleted then we don't | 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: | 183 // need to keep it around. Unsetting IS_UNSYNCED will: |
184 // - Ensure that the item is never committed to the server. | 184 // - Ensure that the item is never committed to the server. |
185 // - Allow any items with the same UNIQUE_CLIENT_TAG created on other | 185 // - Allow any items with the same UNIQUE_CLIENT_TAG created on other |
186 // clients to override this entry. | 186 // clients to override this entry. |
187 // - Let us delete this entry permanently when we next restart sync - see | 187 // - Let us delete this entry permanently when we next restart sync - see |
188 // DirectoryBackingStore::SafeToPurgeOnLoading. | 188 // DirectoryBackingStore::SafeToPurgeOnLoading. |
189 // This will avoid crbug.com/125381. | 189 // This will avoid crbug.com/125381. |
190 // Note: do not unset IsUnsynced if the syncer has started but not yet | 190 // Note: do not unset IsUnsynced if the syncer has started but not yet |
191 // finished committing this entity. | 191 // finished committing this entity. |
192 if (!GetId().ServerKnows() && !GetSyncing()) { | 192 if (!GetId().ServerKnows() && !GetSyncing()) { |
193 PutIsUnsynced(false); | 193 PutIsUnsynced(false); |
194 } | 194 } |
195 } | 195 } |
196 | 196 |
197 { | 197 { |
198 ScopedKernelLock lock(dir()); | 198 ScopedKernelLock lock(dir()); |
199 // Some indices don't include deleted items and must be updated | 199 // Some indices don't include deleted items and must be updated |
200 // upon a value change. | 200 // upon a value change. |
201 ScopedParentChildIndexUpdater updater(lock, kernel_, | 201 ScopedParentChildIndexUpdater updater(lock, kernel_, |
202 &dir()->kernel()->parent_child_index); | 202 &dir()->kernel()->parent_child_index); |
203 | 203 |
204 kernel_->put(IS_DEL, value); | 204 kernel_->put(IS_DEL, value); |
205 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); | 205 MarkDirty(); |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 void MutableEntry::PutNonUniqueName(const std::string& value) { | 209 void MutableEntry::PutNonUniqueName(const std::string& value) { |
210 DCHECK(kernel_); | 210 DCHECK(kernel_); |
211 write_transaction()->TrackChangesTo(kernel_); | |
212 | |
213 if (kernel_->ref(NON_UNIQUE_NAME) != value) { | 211 if (kernel_->ref(NON_UNIQUE_NAME) != value) { |
| 212 write_transaction()->TrackChangesTo(kernel_); |
214 kernel_->put(NON_UNIQUE_NAME, value); | 213 kernel_->put(NON_UNIQUE_NAME, value); |
215 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); | 214 MarkDirty(); |
216 } | 215 } |
217 } | 216 } |
218 | 217 |
219 void MutableEntry::PutSpecifics(const sync_pb::EntitySpecifics& value) { | 218 void MutableEntry::PutSpecifics(const sync_pb::EntitySpecifics& value) { |
220 DCHECK(kernel_); | 219 DCHECK(kernel_); |
221 CHECK(!value.password().has_client_only_encrypted_data()); | 220 CHECK(!value.password().has_client_only_encrypted_data()); |
222 write_transaction()->TrackChangesTo(kernel_); | |
223 // TODO(ncarter): This is unfortunately heavyweight. Can we do | 221 // TODO(ncarter): This is unfortunately heavyweight. Can we do |
224 // better? | 222 // better? |
225 const std::string& serialized_value = value.SerializeAsString(); | 223 const std::string& serialized_value = value.SerializeAsString(); |
226 if (serialized_value != kernel_->ref(SPECIFICS).SerializeAsString()) { | 224 if (serialized_value != kernel_->ref(SPECIFICS).SerializeAsString()) { |
| 225 write_transaction()->TrackChangesTo(kernel_); |
227 // Check for potential sharing - SPECIFICS is often | 226 // Check for potential sharing - SPECIFICS is often |
228 // copied from SERVER_SPECIFICS. | 227 // copied from SERVER_SPECIFICS. |
229 if (serialized_value == | 228 if (serialized_value == |
230 kernel_->ref(SERVER_SPECIFICS).SerializeAsString()) { | 229 kernel_->ref(SERVER_SPECIFICS).SerializeAsString()) { |
231 kernel_->copy(SERVER_SPECIFICS, SPECIFICS); | 230 kernel_->copy(SERVER_SPECIFICS, SPECIFICS); |
232 } else { | 231 } else { |
233 kernel_->put(SPECIFICS, value); | 232 kernel_->put(SPECIFICS, value); |
234 } | 233 } |
235 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); | 234 MarkDirty(); |
236 } | 235 } |
237 } | 236 } |
238 | 237 |
239 void MutableEntry::PutUniquePosition(const UniquePosition& value) { | 238 void MutableEntry::PutUniquePosition(const UniquePosition& value) { |
240 DCHECK(kernel_); | 239 DCHECK(kernel_); |
241 write_transaction()->TrackChangesTo(kernel_); | |
242 if(!kernel_->ref(UNIQUE_POSITION).Equals(value)) { | 240 if(!kernel_->ref(UNIQUE_POSITION).Equals(value)) { |
| 241 write_transaction()->TrackChangesTo(kernel_); |
243 // We should never overwrite a valid position with an invalid one. | 242 // We should never overwrite a valid position with an invalid one. |
244 DCHECK(value.IsValid()); | 243 DCHECK(value.IsValid()); |
245 ScopedKernelLock lock(dir()); | 244 ScopedKernelLock lock(dir()); |
246 ScopedParentChildIndexUpdater updater( | 245 ScopedParentChildIndexUpdater updater( |
247 lock, kernel_, &dir()->kernel()->parent_child_index); | 246 lock, kernel_, &dir()->kernel()->parent_child_index); |
248 kernel_->put(UNIQUE_POSITION, value); | 247 kernel_->put(UNIQUE_POSITION, value); |
249 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); | 248 MarkDirty(); |
250 } | 249 } |
251 } | 250 } |
252 | 251 |
253 bool MutableEntry::PutPredecessor(const Id& predecessor_id) { | 252 bool MutableEntry::PutPredecessor(const Id& predecessor_id) { |
254 if (predecessor_id.IsNull()) { | 253 if (predecessor_id.IsNull()) { |
255 dir()->PutPredecessor(kernel_, NULL); | 254 dir()->PutPredecessor(kernel_, NULL); |
256 } else { | 255 } else { |
257 MutableEntry predecessor(write_transaction(), GET_BY_ID, predecessor_id); | 256 MutableEntry predecessor(write_transaction(), GET_BY_ID, predecessor_id); |
258 if (!predecessor.good()) | 257 if (!predecessor.good()) |
259 return false; | 258 return false; |
260 dir()->PutPredecessor(kernel_, predecessor.kernel_); | 259 dir()->PutPredecessor(kernel_, predecessor.kernel_); |
| 260 DCHECK(kernel_->is_dirty()); |
261 } | 261 } |
262 return true; | 262 return true; |
263 } | 263 } |
264 | 264 |
265 void MutableEntry::PutAttachmentMetadata( | 265 void MutableEntry::PutAttachmentMetadata( |
266 const sync_pb::AttachmentMetadata& value) { | 266 const sync_pb::AttachmentMetadata& value) { |
267 DCHECK(kernel_); | 267 DCHECK(kernel_); |
268 write_transaction()->TrackChangesTo(kernel_); | |
269 const std::string& serialized_value = value.SerializeAsString(); | 268 const std::string& serialized_value = value.SerializeAsString(); |
270 if (serialized_value != | 269 if (serialized_value != |
271 kernel_->ref(ATTACHMENT_METADATA).SerializeAsString()) { | 270 kernel_->ref(ATTACHMENT_METADATA).SerializeAsString()) { |
| 271 write_transaction()->TrackChangesTo(kernel_); |
272 dir()->UpdateAttachmentIndex(GetMetahandle(), | 272 dir()->UpdateAttachmentIndex(GetMetahandle(), |
273 kernel_->ref(ATTACHMENT_METADATA), value); | 273 kernel_->ref(ATTACHMENT_METADATA), value); |
274 // Check for potential sharing - ATTACHMENT_METADATA is often | 274 // Check for potential sharing - ATTACHMENT_METADATA is often |
275 // copied from SERVER_ATTACHMENT_METADATA. | 275 // copied from SERVER_ATTACHMENT_METADATA. |
276 if (serialized_value == | 276 if (serialized_value == |
277 kernel_->ref(SERVER_ATTACHMENT_METADATA).SerializeAsString()) { | 277 kernel_->ref(SERVER_ATTACHMENT_METADATA).SerializeAsString()) { |
278 kernel_->copy(SERVER_ATTACHMENT_METADATA, ATTACHMENT_METADATA); | 278 kernel_->copy(SERVER_ATTACHMENT_METADATA, ATTACHMENT_METADATA); |
279 } else { | 279 } else { |
280 kernel_->put(ATTACHMENT_METADATA, value); | 280 kernel_->put(ATTACHMENT_METADATA, value); |
281 } | 281 } |
282 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); | 282 MarkDirty(); |
283 } | 283 } |
284 } | 284 } |
285 | 285 |
286 void MutableEntry::MarkAttachmentAsOnServer( | 286 void MutableEntry::MarkAttachmentAsOnServer( |
287 const sync_pb::AttachmentIdProto& attachment_id) { | 287 const sync_pb::AttachmentIdProto& attachment_id) { |
288 DCHECK(kernel_); | 288 DCHECK(kernel_); |
289 DCHECK(!attachment_id.unique_id().empty()); | 289 DCHECK(!attachment_id.unique_id().empty()); |
290 write_transaction()->TrackChangesTo(kernel_); | 290 write_transaction()->TrackChangesTo(kernel_); |
291 sync_pb::AttachmentMetadata attachment_metadata = | 291 sync_pb::AttachmentMetadata attachment_metadata = |
292 kernel_->ref(ATTACHMENT_METADATA); | 292 kernel_->ref(ATTACHMENT_METADATA); |
293 for (int i = 0; i < attachment_metadata.record_size(); ++i) { | 293 for (int i = 0; i < attachment_metadata.record_size(); ++i) { |
294 sync_pb::AttachmentMetadataRecord* record = | 294 sync_pb::AttachmentMetadataRecord* record = |
295 attachment_metadata.mutable_record(i); | 295 attachment_metadata.mutable_record(i); |
296 if (record->id().unique_id() != attachment_id.unique_id()) | 296 if (record->id().unique_id() != attachment_id.unique_id()) |
297 continue; | 297 continue; |
298 record->set_is_on_server(true); | 298 record->set_is_on_server(true); |
299 } | 299 } |
300 kernel_->put(ATTACHMENT_METADATA, attachment_metadata); | 300 kernel_->put(ATTACHMENT_METADATA, attachment_metadata); |
301 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); | 301 MarkDirty(); |
302 MarkForSyncing(this); | 302 MarkForSyncing(this); |
303 } | 303 } |
304 | 304 |
305 // This function sets only the flags needed to get this entry to sync. | 305 // This function sets only the flags needed to get this entry to sync. |
306 bool MarkForSyncing(MutableEntry* e) { | 306 bool MarkForSyncing(MutableEntry* e) { |
307 DCHECK_NE(static_cast<MutableEntry*>(NULL), e); | 307 DCHECK_NE(static_cast<MutableEntry*>(NULL), e); |
308 DCHECK(!e->IsRoot()) << "We shouldn't mark a permanent object for syncing."; | 308 DCHECK(!e->IsRoot()) << "We shouldn't mark a permanent object for syncing."; |
309 if (!(e->PutIsUnsynced(true))) | 309 if (!(e->PutIsUnsynced(true))) |
310 return false; | 310 return false; |
311 if (e->GetSyncing()) | 311 if (e->GetSyncing()) |
312 e->PutDirtySync(true); | 312 e->PutDirtySync(true); |
313 return true; | 313 return true; |
314 } | 314 } |
315 | 315 |
316 } // namespace syncable | 316 } // namespace syncable |
317 } // namespace syncer | 317 } // namespace syncer |
OLD | NEW |