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_); |
125 if (kernel_->ref(LOCAL_EXTERNAL_ID) != value) { | 126 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 MarkDirty(); | 129 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); |
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_); |
135 if (kernel_->ref(MTIME) != value) { | 136 if (kernel_->ref(MTIME) != value) { |
136 write_transaction()->TrackChangesTo(kernel_); | |
137 kernel_->put(MTIME, value); | 137 kernel_->put(MTIME, value); |
138 MarkDirty(); | 138 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); |
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_); |
144 if (kernel_->ref(CTIME) != value) { | 145 if (kernel_->ref(CTIME) != value) { |
145 write_transaction()->TrackChangesTo(kernel_); | |
146 kernel_->put(CTIME, value); | 146 kernel_->put(CTIME, value); |
147 MarkDirty(); | 147 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); |
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_); |
153 if (kernel_->ref(PARENT_ID) != value) { | 154 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 if (kernel_->ref(IS_DIR) != value) { | 167 write_transaction()->TrackChangesTo(kernel_); |
168 write_transaction()->TrackChangesTo(kernel_); | 168 bool old_value = kernel_->ref(IS_DIR); |
| 169 if (old_value != value) { |
169 kernel_->put(IS_DIR, value); | 170 kernel_->put(IS_DIR, value); |
170 MarkDirty(); | 171 kernel_->mark_dirty(GetDirtyIndexHelper()); |
171 } | 172 } |
172 } | 173 } |
173 | 174 |
174 void MutableEntry::PutIsDel(bool value) { | 175 void MutableEntry::PutIsDel(bool value) { |
175 DCHECK(kernel_); | 176 DCHECK(kernel_); |
| 177 write_transaction()->TrackChangesTo(kernel_); |
176 if (value == kernel_->ref(IS_DEL)) { | 178 if (value == kernel_->ref(IS_DEL)) { |
177 return; | 179 return; |
178 } | 180 } |
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 MarkDirty(); | 205 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); |
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 |
211 if (kernel_->ref(NON_UNIQUE_NAME) != value) { | 213 if (kernel_->ref(NON_UNIQUE_NAME) != value) { |
212 write_transaction()->TrackChangesTo(kernel_); | |
213 kernel_->put(NON_UNIQUE_NAME, value); | 214 kernel_->put(NON_UNIQUE_NAME, value); |
214 MarkDirty(); | 215 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); |
215 } | 216 } |
216 } | 217 } |
217 | 218 |
218 void MutableEntry::PutSpecifics(const sync_pb::EntitySpecifics& value) { | 219 void MutableEntry::PutSpecifics(const sync_pb::EntitySpecifics& value) { |
219 DCHECK(kernel_); | 220 DCHECK(kernel_); |
220 CHECK(!value.password().has_client_only_encrypted_data()); | 221 CHECK(!value.password().has_client_only_encrypted_data()); |
| 222 write_transaction()->TrackChangesTo(kernel_); |
221 // TODO(ncarter): This is unfortunately heavyweight. Can we do | 223 // TODO(ncarter): This is unfortunately heavyweight. Can we do |
222 // better? | 224 // better? |
223 const std::string& serialized_value = value.SerializeAsString(); | 225 const std::string& serialized_value = value.SerializeAsString(); |
224 if (serialized_value != kernel_->ref(SPECIFICS).SerializeAsString()) { | 226 if (serialized_value != kernel_->ref(SPECIFICS).SerializeAsString()) { |
225 write_transaction()->TrackChangesTo(kernel_); | |
226 // Check for potential sharing - SPECIFICS is often | 227 // Check for potential sharing - SPECIFICS is often |
227 // copied from SERVER_SPECIFICS. | 228 // copied from SERVER_SPECIFICS. |
228 if (serialized_value == | 229 if (serialized_value == |
229 kernel_->ref(SERVER_SPECIFICS).SerializeAsString()) { | 230 kernel_->ref(SERVER_SPECIFICS).SerializeAsString()) { |
230 kernel_->copy(SERVER_SPECIFICS, SPECIFICS); | 231 kernel_->copy(SERVER_SPECIFICS, SPECIFICS); |
231 } else { | 232 } else { |
232 kernel_->put(SPECIFICS, value); | 233 kernel_->put(SPECIFICS, value); |
233 } | 234 } |
234 MarkDirty(); | 235 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); |
235 } | 236 } |
236 } | 237 } |
237 | 238 |
238 void MutableEntry::PutUniquePosition(const UniquePosition& value) { | 239 void MutableEntry::PutUniquePosition(const UniquePosition& value) { |
239 DCHECK(kernel_); | 240 DCHECK(kernel_); |
| 241 write_transaction()->TrackChangesTo(kernel_); |
240 if(!kernel_->ref(UNIQUE_POSITION).Equals(value)) { | 242 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 // We should never overwrite a valid position with an invalid one. |
243 DCHECK(value.IsValid()); | 244 DCHECK(value.IsValid()); |
244 ScopedKernelLock lock(dir()); | 245 ScopedKernelLock lock(dir()); |
245 ScopedParentChildIndexUpdater updater( | 246 ScopedParentChildIndexUpdater updater( |
246 lock, kernel_, &dir()->kernel()->parent_child_index); | 247 lock, kernel_, &dir()->kernel()->parent_child_index); |
247 kernel_->put(UNIQUE_POSITION, value); | 248 kernel_->put(UNIQUE_POSITION, value); |
248 MarkDirty(); | 249 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); |
249 } | 250 } |
250 } | 251 } |
251 | 252 |
252 bool MutableEntry::PutPredecessor(const Id& predecessor_id) { | 253 bool MutableEntry::PutPredecessor(const Id& predecessor_id) { |
253 if (predecessor_id.IsNull()) { | 254 if (predecessor_id.IsNull()) { |
254 dir()->PutPredecessor(kernel_, NULL); | 255 dir()->PutPredecessor(kernel_, NULL); |
255 } else { | 256 } else { |
256 MutableEntry predecessor(write_transaction(), GET_BY_ID, predecessor_id); | 257 MutableEntry predecessor(write_transaction(), GET_BY_ID, predecessor_id); |
257 if (!predecessor.good()) | 258 if (!predecessor.good()) |
258 return false; | 259 return false; |
259 dir()->PutPredecessor(kernel_, predecessor.kernel_); | 260 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_); |
268 const std::string& serialized_value = value.SerializeAsString(); | 269 const std::string& serialized_value = value.SerializeAsString(); |
269 if (serialized_value != | 270 if (serialized_value != |
270 kernel_->ref(ATTACHMENT_METADATA).SerializeAsString()) { | 271 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 MarkDirty(); | 282 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); |
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 MarkDirty(); | 301 kernel_->mark_dirty(&dir()->kernel()->dirty_metahandles); |
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 |