summaryrefslogtreecommitdiffstats
path: root/sync/internal_api/write_transaction.cc
blob: b2dfe87da1785038367047bd1f0bb05b0f7d5eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sync/internal_api/public/write_transaction.h"

#include "sync/syncable/directory.h"
#include "sync/syncable/syncable_write_transaction.h"

namespace syncer {

//////////////////////////////////////////////////////////////////////////
// WriteTransaction member definitions
WriteTransaction::WriteTransaction(const tracked_objects::Location& from_here,
                                   UserShare* share)
    : BaseTransaction(share),
      transaction_(NULL) {
  transaction_ = new syncable::WriteTransaction(from_here, syncable::SYNCAPI,
                                                share->directory.get());
}

WriteTransaction::WriteTransaction(const tracked_objects::Location& from_here,
                                   UserShare* share,
                                   int64* new_model_version)
    : BaseTransaction(share),
      transaction_(NULL) {
  transaction_ = new syncable::WriteTransaction(from_here,
                                                share->directory.get(),
                                                new_model_version);
}

WriteTransaction::~WriteTransaction() {
  delete transaction_;
}

syncable::BaseTransaction* WriteTransaction::GetWrappedTrans() const {
  return transaction_;
}

void WriteTransaction::SetDataTypeContext(
    ModelType type,
    syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status,
    const std::string& context) {
  DCHECK(ProtocolTypes().Has(type));
  int field_number = GetSpecificsFieldNumberFromModelType(type);
  sync_pb::DataTypeContext local_context;
  GetDirectory()->GetDataTypeContext(transaction_,
                                     type,
                                     &local_context);
  if (local_context.context() == context)
    return;

  if (!local_context.has_data_type_id())
    local_context.set_data_type_id(field_number);

  DCHECK_EQ(field_number, local_context.data_type_id());
  DCHECK_GE(local_context.version(), 0);
  local_context.set_version(local_context.version() + 1);
  local_context.set_context(context);
  GetDirectory()->SetDataTypeContext(transaction_,
                                     type,
                                     local_context);
  if (refresh_status == syncer::SyncChangeProcessor::REFRESH_NEEDED) {
    DVLOG(1) << "Forcing refresh of type " << ModelTypeToString(type);
    // Clear the progress token from the progress markers. Preserve all other
    // state, in case a GC directive was present.
    sync_pb::DataTypeProgressMarker progress_marker;
    GetDirectory()->GetDownloadProgress(type, &progress_marker);
    progress_marker.clear_token();
    GetDirectory()->SetDownloadProgress(type, progress_marker);

    // Go through and reset the versions for all the synced entities of this
    // data type.
    GetDirectory()->ResetVersionsForType(transaction_, type);
  }

  // Note that it's possible for a GetUpdatesResponse that arrives immediately
  // after the context update to override the cleared progress markers.
  // TODO(zea): add a flag in the directory to prevent this from happening.
  // See crbug.com/360280
}

}  // namespace syncer