sync_change.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include "components/sync/model/sync_change.h"
  5. #include <ostream>
  6. #include "base/notreached.h"
  7. namespace syncer {
  8. SyncChange::SyncChange(const base::Location& from_here,
  9. SyncChangeType change_type,
  10. const SyncData& sync_data)
  11. : location_(from_here), change_type_(change_type), sync_data_(sync_data) {
  12. DCHECK(IsValid()) << " from " << from_here.ToString();
  13. }
  14. SyncChange::~SyncChange() = default;
  15. bool SyncChange::IsValid() const {
  16. // TODO(crbug.com/1152824): This implementation could be simplified if the
  17. // public API provides guarantees around when it returns false.
  18. if (!sync_data_.IsValid()) {
  19. return false;
  20. }
  21. if (!IsRealDataType(sync_data_.GetDataType())) {
  22. return false;
  23. }
  24. // Changes must always have a unique tag.
  25. if (sync_data_.GetClientTagHash().value().empty()) {
  26. return false;
  27. }
  28. return true;
  29. }
  30. SyncChange::SyncChangeType SyncChange::change_type() const {
  31. return change_type_;
  32. }
  33. SyncData SyncChange::sync_data() const {
  34. return sync_data_;
  35. }
  36. base::Location SyncChange::location() const {
  37. return location_;
  38. }
  39. // static
  40. std::string SyncChange::ChangeTypeToString(SyncChangeType change_type) {
  41. switch (change_type) {
  42. case ACTION_ADD:
  43. return "ACTION_ADD";
  44. case ACTION_UPDATE:
  45. return "ACTION_UPDATE";
  46. case ACTION_DELETE:
  47. return "ACTION_DELETE";
  48. }
  49. }
  50. std::string SyncChange::ToString() const {
  51. return "{ " + location_.ToString() +
  52. ", changeType: " + ChangeTypeToString(change_type_) +
  53. ", syncData: " + sync_data_.ToString() + "}";
  54. }
  55. void PrintTo(const SyncChange& sync_change, std::ostream* os) {
  56. *os << sync_change.ToString();
  57. }
  58. } // namespace syncer