Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / sync / internal_api / write_transaction.cc
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/internal_api/public/write_transaction.h"
6
7 #include "sync/syncable/directory.h"
8 #include "sync/syncable/syncable_write_transaction.h"
9
10 namespace syncer {
11
12 //////////////////////////////////////////////////////////////////////////
13 // WriteTransaction member definitions
14 WriteTransaction::WriteTransaction(const tracked_objects::Location& from_here,
15                                    UserShare* share)
16     : BaseTransaction(share),
17       transaction_(NULL) {
18   transaction_ = new syncable::WriteTransaction(from_here, syncable::SYNCAPI,
19                                                 share->directory.get());
20 }
21
22 WriteTransaction::WriteTransaction(const tracked_objects::Location& from_here,
23                                    UserShare* share,
24                                    int64* new_model_version)
25     : BaseTransaction(share),
26       transaction_(NULL) {
27   transaction_ = new syncable::WriteTransaction(from_here,
28                                                 share->directory.get(),
29                                                 new_model_version);
30 }
31
32 WriteTransaction::~WriteTransaction() {
33   delete transaction_;
34 }
35
36 syncable::BaseTransaction* WriteTransaction::GetWrappedTrans() const {
37   return transaction_;
38 }
39
40 void WriteTransaction::SetDataTypeContext(
41     ModelType type,
42     syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status,
43     const std::string& context) {
44   DCHECK(ProtocolTypes().Has(type));
45   int field_number = GetSpecificsFieldNumberFromModelType(type);
46   sync_pb::DataTypeContext local_context;
47   GetDirectory()->GetDataTypeContext(transaction_,
48                                      type,
49                                      &local_context);
50   if (local_context.context() == context)
51     return;
52
53   if (!local_context.has_data_type_id())
54     local_context.set_data_type_id(field_number);
55
56   DCHECK_EQ(field_number, local_context.data_type_id());
57   DCHECK_GE(local_context.version(), 0);
58   local_context.set_version(local_context.version() + 1);
59   local_context.set_context(context);
60   GetDirectory()->SetDataTypeContext(transaction_,
61                                      type,
62                                      local_context);
63   if (refresh_status == syncer::SyncChangeProcessor::REFRESH_NEEDED) {
64     DVLOG(1) << "Forcing refresh of type " << ModelTypeToString(type);
65     // Clear the progress token from the progress markers. Preserve all other
66     // state, in case a GC directive was present.
67     sync_pb::DataTypeProgressMarker progress_marker;
68     GetDirectory()->GetDownloadProgress(type, &progress_marker);
69     progress_marker.clear_token();
70     GetDirectory()->SetDownloadProgress(type, progress_marker);
71
72     // Go through and reset the versions for all the synced entities of this
73     // data type.
74     GetDirectory()->ResetVersionsForType(transaction_, type);
75   }
76
77   // Note that it's possible for a GetUpdatesResponse that arrives immediately
78   // after the context update to override the cleared progress markers.
79   // TODO(zea): add a flag in the directory to prevent this from happening.
80   // See crbug.com/360280
81 }
82
83 }  // namespace syncer