[email protected] | c9ce60b | 2012-12-19 23:16:08 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[email protected] | 67192b4 | 2011-08-17 23:38:30 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 406203d | 2012-06-17 01:07:19 | [diff] [blame] | 5 | #include "sync/internal_api/public/write_transaction.h" |
[email protected] | 67192b4 | 2011-08-17 23:38:30 | [diff] [blame] | 6 | |
avi | 4856ced0 | 2015-12-22 03:14:10 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | d7f9c93 | 2014-04-04 21:10:21 | [diff] [blame] | 9 | #include "sync/syncable/directory.h" |
[email protected] | 0ae5be3 | 2014-05-20 06:14:54 | [diff] [blame] | 10 | #include "sync/syncable/mutable_entry.h" |
[email protected] | c9ce60b | 2012-12-19 23:16:08 | [diff] [blame] | 11 | #include "sync/syncable/syncable_write_transaction.h" |
[email protected] | 67192b4 | 2011-08-17 23:38:30 | [diff] [blame] | 12 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 13 | namespace syncer { |
[email protected] | 67192b4 | 2011-08-17 23:38:30 | [diff] [blame] | 14 | |
| 15 | ////////////////////////////////////////////////////////////////////////// |
| 16 | // WriteTransaction member definitions |
| 17 | WriteTransaction::WriteTransaction(const tracked_objects::Location& from_here, |
| 18 | UserShare* share) |
| 19 | : BaseTransaction(share), |
| 20 | transaction_(NULL) { |
| 21 | transaction_ = new syncable::WriteTransaction(from_here, syncable::SYNCAPI, |
[email protected] | 30aebeb9 | 2012-02-28 05:27:15 | [diff] [blame] | 22 | share->directory.get()); |
[email protected] | 67192b4 | 2011-08-17 23:38:30 | [diff] [blame] | 23 | } |
| 24 | |
[email protected] | fc290d6 | 2012-11-10 05:20:11 | [diff] [blame] | 25 | WriteTransaction::WriteTransaction(const tracked_objects::Location& from_here, |
| 26 | UserShare* share, |
avi | 4856ced0 | 2015-12-22 03:14:10 | [diff] [blame] | 27 | int64_t* new_model_version) |
| 28 | : BaseTransaction(share), transaction_(NULL) { |
[email protected] | fc290d6 | 2012-11-10 05:20:11 | [diff] [blame] | 29 | transaction_ = new syncable::WriteTransaction(from_here, |
| 30 | share->directory.get(), |
| 31 | new_model_version); |
| 32 | } |
| 33 | |
[email protected] | 67192b4 | 2011-08-17 23:38:30 | [diff] [blame] | 34 | WriteTransaction::~WriteTransaction() { |
| 35 | delete transaction_; |
| 36 | } |
| 37 | |
| 38 | syncable::BaseTransaction* WriteTransaction::GetWrappedTrans() const { |
| 39 | return transaction_; |
| 40 | } |
| 41 | |
[email protected] | 694ffab8 | 2014-04-09 00:17:36 | [diff] [blame] | 42 | void WriteTransaction::SetDataTypeContext( |
| 43 | ModelType type, |
| 44 | syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status, |
| 45 | const std::string& context) { |
| 46 | DCHECK(ProtocolTypes().Has(type)); |
| 47 | int field_number = GetSpecificsFieldNumberFromModelType(type); |
[email protected] | d7f9c93 | 2014-04-04 21:10:21 | [diff] [blame] | 48 | sync_pb::DataTypeContext local_context; |
| 49 | GetDirectory()->GetDataTypeContext(transaction_, |
| 50 | type, |
| 51 | &local_context); |
| 52 | if (local_context.context() == context) |
| 53 | return; |
| 54 | |
[email protected] | 694ffab8 | 2014-04-09 00:17:36 | [diff] [blame] | 55 | if (!local_context.has_data_type_id()) |
| 56 | local_context.set_data_type_id(field_number); |
| 57 | |
| 58 | DCHECK_EQ(field_number, local_context.data_type_id()); |
[email protected] | d7f9c93 | 2014-04-04 21:10:21 | [diff] [blame] | 59 | DCHECK_GE(local_context.version(), 0); |
| 60 | local_context.set_version(local_context.version() + 1); |
| 61 | local_context.set_context(context); |
| 62 | GetDirectory()->SetDataTypeContext(transaction_, |
| 63 | type, |
| 64 | local_context); |
[email protected] | 694ffab8 | 2014-04-09 00:17:36 | [diff] [blame] | 65 | if (refresh_status == syncer::SyncChangeProcessor::REFRESH_NEEDED) { |
| 66 | DVLOG(1) << "Forcing refresh of type " << ModelTypeToString(type); |
| 67 | // Clear the progress token from the progress markers. Preserve all other |
| 68 | // state, in case a GC directive was present. |
| 69 | sync_pb::DataTypeProgressMarker progress_marker; |
| 70 | GetDirectory()->GetDownloadProgress(type, &progress_marker); |
| 71 | progress_marker.clear_token(); |
| 72 | GetDirectory()->SetDownloadProgress(type, progress_marker); |
| 73 | |
| 74 | // Go through and reset the versions for all the synced entities of this |
| 75 | // data type. |
| 76 | GetDirectory()->ResetVersionsForType(transaction_, type); |
| 77 | } |
| 78 | |
| 79 | // Note that it's possible for a GetUpdatesResponse that arrives immediately |
| 80 | // after the context update to override the cleared progress markers. |
| 81 | // TODO(zea): add a flag in the directory to prevent this from happening. |
| 82 | // See crbug.com/360280 |
[email protected] | d7f9c93 | 2014-04-04 21:10:21 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | 8ac09b21 | 2014-07-17 06:47:44 | [diff] [blame] | 85 | void WriteTransaction::UpdateEntriesMarkAttachmentAsOnServer( |
[email protected] | 0ae5be3 | 2014-05-20 06:14:54 | [diff] [blame] | 86 | const AttachmentId& attachment_id) { |
| 87 | syncable::Directory::Metahandles handles; |
| 88 | GetDirectory()->GetMetahandlesByAttachmentId( |
| 89 | transaction_, attachment_id.GetProto(), &handles); |
| 90 | for (syncable::Directory::Metahandles::iterator iter = handles.begin(); |
| 91 | iter != handles.end(); |
| 92 | ++iter) { |
| 93 | syncable::MutableEntry entry(transaction_, syncable::GET_BY_HANDLE, *iter); |
[email protected] | 8ac09b21 | 2014-07-17 06:47:44 | [diff] [blame] | 94 | entry.MarkAttachmentAsOnServer(attachment_id.GetProto()); |
[email protected] | 0ae5be3 | 2014-05-20 06:14:54 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 98 | } // namespace syncer |