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 #ifndef COMPONENTS_SYNC_MODEL_SYNC_DATA_H_
6 #define COMPONENTS_SYNC_MODEL_SYNC_DATA_H_
7 
8 #include <stdint.h>
9 
10 #include <iosfwd>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "base/callback.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/time/time.h"
18 #include "components/sync/base/immutable.h"
19 #include "components/sync/base/model_type.h"
20 #include "components/sync/base/weak_handle.h"
21 
22 namespace sync_pb {
23 class EntitySpecifics;
24 class SyncEntity;
25 }  // namespace sync_pb
26 
27 namespace syncer {
28 
29 class ClientTagHash;
30 class SyncDataLocal;
31 class SyncDataRemote;
32 
33 // A light-weight container for immutable sync data. Pass-by-value and storage
34 // in STL containers are supported and encouraged if helpful.
35 class SyncData {
36  public:
37   // Creates an empty and invalid SyncData.
38   SyncData();
39   SyncData(const SyncData& other);
40   ~SyncData();
41 
42   // Default copy and assign welcome.
43 
44   // Helper methods for creating SyncData objects for local data.
45   //
46   // |sync_tag| Must be a string unique to this datatype and is used as a node
47   // identifier server-side.
48   //
49   // For deletes: |datatype| must specify the datatype who node is being
50   // deleted.
51   //
52   // For adds/updates: |specifics| must be valid and |non_unique_title| (can be
53   // the same as |sync_tag|) must be specfied.  Note: |non_unique_title| is
54   // primarily for debug purposes, and will be overwritten if the datatype is
55   // encrypted.
56   static SyncData CreateLocalDelete(const std::string& sync_tag,
57                                     ModelType datatype);
58   static SyncData CreateLocalData(const std::string& sync_tag,
59                                   const std::string& non_unique_title,
60                                   const sync_pb::EntitySpecifics& specifics);
61 
62   // Helper method for creating SyncData objects originating from the syncer.
63   static SyncData CreateRemoteData(sync_pb::EntitySpecifics specifics,
64                                    std::string client_tag_hash = std::string());
65 
66   // Whether this SyncData holds valid data. The only way to have a SyncData
67   // without valid data is to use the default constructor.
68   bool IsValid() const;
69 
70   // Return the datatype we're holding information about. Derived from the sync
71   // datatype specifics.
72   ModelType GetDataType() const;
73 
74   // Return the current sync datatype specifics.
75   const sync_pb::EntitySpecifics& GetSpecifics() const;
76 
77   // Return the non unique title (for debugging). Currently only set for data
78   // going TO the syncer, not from.
79   const std::string& GetTitle() const;
80 
81   // Whether this sync data is for local data or data coming from the syncer.
82   bool IsLocal() const;
83 
84   std::string ToString() const;
85 
86   // TODO(zea): Query methods for other sync properties: parent, successor, etc.
87 
88  protected:
89   // These data members are protected so derived types like SyncDataLocal and
90   // SyncDataRemote can access them.
91 
92   // Necessary since we forward-declare sync_pb::SyncEntity; see
93   // comments in immutable.h.
94   struct ImmutableSyncEntityTraits {
95     using Wrapper = sync_pb::SyncEntity*;
96 
97     static void InitializeWrapper(Wrapper* wrapper);
98 
99     static void DestroyWrapper(Wrapper* wrapper);
100 
101     static const sync_pb::SyncEntity& Unwrap(const Wrapper& wrapper);
102 
103     static sync_pb::SyncEntity* UnwrapMutable(Wrapper* wrapper);
104 
105     static void Swap(sync_pb::SyncEntity* t1, sync_pb::SyncEntity* t2);
106   };
107 
108   using ImmutableSyncEntity =
109       Immutable<sync_pb::SyncEntity, ImmutableSyncEntityTraits>;
110 
111   // The actual shared sync entity being held.
112   ImmutableSyncEntity immutable_entity_;
113 
114  private:
115   // Whether this SyncData represents a local change.
116   bool is_local_;
117 
118   // Whether this SyncData holds valid data.
119   bool is_valid_;
120 
121   // Clears |entity|.
122   SyncData(bool is_local_, sync_pb::SyncEntity* entity);
123 };
124 
125 // A SyncData going to the syncer.
126 class SyncDataLocal : public SyncData {
127  public:
128   // Construct a SyncDataLocal from a SyncData.
129   //
130   // |sync_data|'s IsLocal() must be true.
131   explicit SyncDataLocal(const SyncData& sync_data);
132   ~SyncDataLocal();
133 
134   // Return the value of the unique client tag. This is only set for data going
135   // TO the syncer, not coming from.
136   const std::string& GetTag() const;
137 };
138 
139 // A SyncData that comes from the syncer.
140 class SyncDataRemote : public SyncData {
141  public:
142   // Construct a SyncDataRemote from a SyncData.
143   //
144   // |sync_data|'s IsLocal() must be false.
145   explicit SyncDataRemote(const SyncData& sync_data);
146   ~SyncDataRemote();
147 
148   // Returns the tag hash value. May not always be present, in which case an
149   // empty string will be returned.
150   ClientTagHash GetClientTagHash() const;
151 };
152 
153 // gmock printer helper.
154 void PrintTo(const SyncData& sync_data, std::ostream* os);
155 
156 using SyncDataList = std::vector<SyncData>;
157 
158 }  // namespace syncer
159 
160 #endif  // COMPONENTS_SYNC_MODEL_SYNC_DATA_H_
161