sync_data.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #ifndef COMPONENTS_SYNC_MODEL_SYNC_DATA_H_
  5. #define COMPONENTS_SYNC_MODEL_SYNC_DATA_H_
  6. #include <iosfwd>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/scoped_refptr.h"
  10. #include "components/sync/base/client_tag_hash.h"
  11. #include "components/sync/base/model_type.h"
  12. namespace sync_pb {
  13. class EntitySpecifics;
  14. } // namespace sync_pb
  15. namespace syncer {
  16. // A light-weight container for immutable sync data. Pass-by-value and storage
  17. // in STL containers are supported and encouraged if helpful.
  18. class SyncData {
  19. public:
  20. // Creates an empty and invalid SyncData.
  21. SyncData();
  22. // Copyable and movable, all cheap.
  23. SyncData(const SyncData& other);
  24. SyncData(SyncData&& other);
  25. SyncData& operator=(const SyncData& other);
  26. SyncData& operator=(SyncData&& other);
  27. ~SyncData();
  28. // Helper methods for creating SyncData objects for local data.
  29. //
  30. // |client_tag_unhashed| Must be a non-empty string unique to this entity and
  31. // is used (in hashed form) as a node identifier server-side.
  32. //
  33. // For deletes: |datatype| must specify the datatype who node is being
  34. // deleted.
  35. //
  36. // For adds/updates: |specifics| must be valid and |non_unique_title| (can be
  37. // the same as |client_tag_unhashed|) must be specfied. Note:
  38. // |non_unique_title| is primarily for debug purposes, and will be overwritten
  39. // if the datatype is encrypted.
  40. static SyncData CreateLocalDelete(const std::string& client_tag_unhashed,
  41. ModelType datatype);
  42. static SyncData CreateLocalData(const std::string& client_tag_unhashed,
  43. const std::string& non_unique_title,
  44. const sync_pb::EntitySpecifics& specifics);
  45. // Helper method for creating SyncData objects originating from the syncer.
  46. static SyncData CreateRemoteData(sync_pb::EntitySpecifics specifics,
  47. const ClientTagHash& client_tag_hash);
  48. // Whether this SyncData holds valid data. An instance can be invalid either
  49. // if the default value was used upon construction or if an instance was moved
  50. // away.
  51. bool IsValid() const;
  52. // Return the datatype we're holding information about. Derived from the sync
  53. // datatype specifics.
  54. ModelType GetDataType() const;
  55. // Return the value of the unique client tag hash.
  56. ClientTagHash GetClientTagHash() const;
  57. // Return the current sync datatype specifics.
  58. const sync_pb::EntitySpecifics& GetSpecifics() const;
  59. // Return the non unique title (for debugging). Currently only set for data
  60. // going TO the syncer, not from.
  61. const std::string& GetTitle() const;
  62. std::string ToString() const;
  63. private:
  64. // Forward-declared to avoid includes for EntitySpecifics.
  65. struct InternalData;
  66. // Null if data invalid, i.e. default constructor used or moved-away instance.
  67. scoped_refptr<InternalData> ptr_;
  68. explicit SyncData(scoped_refptr<InternalData> ptr);
  69. };
  70. // gmock printer helper.
  71. void PrintTo(const SyncData& sync_data, std::ostream* os);
  72. using SyncDataList = std::vector<SyncData>;
  73. } // namespace syncer
  74. #endif // COMPONENTS_SYNC_MODEL_SYNC_DATA_H_