1// Copyright (c) 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/api/sync_change.h"
6
7#include <ostream>
8
9namespace syncer {
10
11SyncChange::SyncChange() : change_type_(ACTION_INVALID) {
12}
13
14SyncChange::SyncChange(
15    const tracked_objects::Location& from_here,
16    SyncChangeType change_type,
17    const SyncData& sync_data)
18    : location_(from_here),
19      change_type_(change_type),
20      sync_data_(sync_data) {
21  DCHECK(IsValid());
22}
23
24SyncChange::~SyncChange() {}
25
26bool SyncChange::IsValid() const {
27  if (change_type_ == ACTION_INVALID || !sync_data_.IsValid())
28    return false;
29
30  // Data from the syncer must always have valid specifics.
31  if (!sync_data_.IsLocal())
32    return IsRealDataType(sync_data_.GetDataType());
33
34  // Local changes must always have a tag and specify a valid datatype.
35  if (SyncDataLocal(sync_data_).GetTag().empty() ||
36      !IsRealDataType(sync_data_.GetDataType())) {
37    return false;
38  }
39
40  // Adds and updates must have a non-unique-title.
41  if (change_type_ == ACTION_ADD || change_type_ == ACTION_UPDATE)
42    return (!sync_data_.GetTitle().empty());
43
44  return true;
45}
46
47SyncChange::SyncChangeType SyncChange::change_type() const {
48  return change_type_;
49}
50
51SyncData SyncChange::sync_data() const {
52  return sync_data_;
53}
54
55tracked_objects::Location SyncChange::location() const {
56  return location_;
57}
58
59// static
60std::string SyncChange::ChangeTypeToString(SyncChangeType change_type) {
61  switch (change_type) {
62    case ACTION_INVALID:
63      return "ACTION_INVALID";
64    case ACTION_ADD:
65      return "ACTION_ADD";
66    case ACTION_UPDATE:
67      return "ACTION_UPDATE";
68    case ACTION_DELETE:
69      return "ACTION_DELETE";
70    default:
71      NOTREACHED();
72  }
73  return std::string();
74}
75
76std::string SyncChange::ToString() const {
77  return "{ " + location_.ToString() + ", changeType: " +
78      ChangeTypeToString(change_type_) + ", syncData: " +
79      sync_data_.ToString() + "}";
80}
81
82void PrintTo(const SyncChange& sync_change, std::ostream* os) {
83  *os << sync_change.ToString();
84}
85
86}  // namespace syncer
87