OLD | NEW |
| (Empty) |
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_API_SYNC_DATA_H_ | |
6 #define COMPONENTS_SYNC_API_SYNC_DATA_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <iosfwd> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/callback.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "base/time/time.h" | |
17 #include "components/sync/api/attachments/attachment_id.h" | |
18 #include "components/sync/api/attachments/attachment_service_proxy.h" | |
19 #include "components/sync/base/immutable.h" | |
20 #include "components/sync/base/model_type.h" | |
21 #include "components/sync/base/weak_handle.h" | |
22 | |
23 namespace sync_pb { | |
24 class EntitySpecifics; | |
25 class SyncEntity; | |
26 } // namespace sync_pb | |
27 | |
28 namespace syncer { | |
29 | |
30 class AttachmentService; | |
31 class SyncDataLocal; | |
32 class SyncDataRemote; | |
33 | |
34 // A light-weight container for immutable sync data. Pass-by-value and storage | |
35 // in STL containers are supported and encouraged if helpful. | |
36 class SyncData { | |
37 public: | |
38 // Creates an empty and invalid SyncData. | |
39 SyncData(); | |
40 SyncData(const SyncData& other); | |
41 ~SyncData(); | |
42 | |
43 // Default copy and assign welcome. | |
44 | |
45 // Helper methods for creating SyncData objects for local data. | |
46 // | |
47 // |sync_tag| Must be a string unique to this datatype and is used as a node | |
48 // identifier server-side. | |
49 // | |
50 // For deletes: |datatype| must specify the datatype who node is being | |
51 // deleted. | |
52 // | |
53 // For adds/updates: |specifics| must be valid and |non_unique_title| (can be | |
54 // the same as |sync_tag|) must be specfied. Note: |non_unique_title| is | |
55 // primarily for debug purposes, and will be overwritten if the datatype is | |
56 // encrypted. | |
57 // | |
58 // For data with attachments: |attachment_ids| must not contain duplicates. | |
59 static SyncData CreateLocalDelete(const std::string& sync_tag, | |
60 ModelType datatype); | |
61 static SyncData CreateLocalData(const std::string& sync_tag, | |
62 const std::string& non_unique_title, | |
63 const sync_pb::EntitySpecifics& specifics); | |
64 static SyncData CreateLocalDataWithAttachments( | |
65 const std::string& sync_tag, | |
66 const std::string& non_unique_title, | |
67 const sync_pb::EntitySpecifics& specifics, | |
68 const AttachmentIdList& attachment_ids); | |
69 | |
70 // Helper method for creating SyncData objects originating from the syncer. | |
71 static SyncData CreateRemoteData( | |
72 int64_t id, | |
73 const sync_pb::EntitySpecifics& specifics, | |
74 const base::Time& last_modified_time, | |
75 const AttachmentIdList& attachment_ids, | |
76 const AttachmentServiceProxy& attachment_service, | |
77 const std::string& client_tag_hash = std::string()); | |
78 | |
79 // Whether this SyncData holds valid data. The only way to have a SyncData | |
80 // without valid data is to use the default constructor. | |
81 bool IsValid() const; | |
82 | |
83 // Return the datatype we're holding information about. Derived from the sync | |
84 // datatype specifics. | |
85 ModelType GetDataType() const; | |
86 | |
87 // Return the current sync datatype specifics. | |
88 const sync_pb::EntitySpecifics& GetSpecifics() const; | |
89 | |
90 // Return the non unique title (for debugging). Currently only set for data | |
91 // going TO the syncer, not from. | |
92 const std::string& GetTitle() const; | |
93 | |
94 // Whether this sync data is for local data or data coming from the syncer. | |
95 bool IsLocal() const; | |
96 | |
97 std::string ToString() const; | |
98 | |
99 // Return a list of this SyncData's attachment ids. | |
100 // | |
101 // The attachments may or may not be present on this device. | |
102 AttachmentIdList GetAttachmentIds() const; | |
103 | |
104 // TODO(zea): Query methods for other sync properties: parent, successor, etc. | |
105 | |
106 protected: | |
107 // These data members are protected so derived types like SyncDataLocal and | |
108 // SyncDataRemote can access them. | |
109 | |
110 // Necessary since we forward-declare sync_pb::SyncEntity; see | |
111 // comments in immutable.h. | |
112 struct ImmutableSyncEntityTraits { | |
113 typedef sync_pb::SyncEntity* Wrapper; | |
114 | |
115 static void InitializeWrapper(Wrapper* wrapper); | |
116 | |
117 static void DestroyWrapper(Wrapper* wrapper); | |
118 | |
119 static const sync_pb::SyncEntity& Unwrap(const Wrapper& wrapper); | |
120 | |
121 static sync_pb::SyncEntity* UnwrapMutable(Wrapper* wrapper); | |
122 | |
123 static void Swap(sync_pb::SyncEntity* t1, sync_pb::SyncEntity* t2); | |
124 }; | |
125 | |
126 typedef Immutable<sync_pb::SyncEntity, ImmutableSyncEntityTraits> | |
127 ImmutableSyncEntity; | |
128 | |
129 // Equal to kInvalidId iff this is local. | |
130 int64_t id_; | |
131 | |
132 // This may be null if the SyncData represents a deleted item. | |
133 base::Time remote_modification_time_; | |
134 | |
135 // The actual shared sync entity being held. | |
136 ImmutableSyncEntity immutable_entity_; | |
137 | |
138 AttachmentServiceProxy attachment_service_; | |
139 | |
140 private: | |
141 // Whether this SyncData holds valid data. | |
142 bool is_valid_; | |
143 | |
144 // Clears |entity| and |attachments|. | |
145 SyncData(int64_t id, | |
146 sync_pb::SyncEntity* entity, | |
147 const base::Time& remote_modification_time, | |
148 const AttachmentServiceProxy& attachment_service); | |
149 }; | |
150 | |
151 // A SyncData going to the syncer. | |
152 class SyncDataLocal : public SyncData { | |
153 public: | |
154 // Construct a SyncDataLocal from a SyncData. | |
155 // | |
156 // |sync_data|'s IsLocal() must be true. | |
157 explicit SyncDataLocal(const SyncData& sync_data); | |
158 ~SyncDataLocal(); | |
159 | |
160 // Return the value of the unique client tag. This is only set for data going | |
161 // TO the syncer, not coming from. | |
162 const std::string& GetTag() const; | |
163 }; | |
164 | |
165 // A SyncData that comes from the syncer. | |
166 class SyncDataRemote : public SyncData { | |
167 public: | |
168 // Construct a SyncDataRemote from a SyncData. | |
169 // | |
170 // |sync_data|'s IsLocal() must be false. | |
171 explicit SyncDataRemote(const SyncData& sync_data); | |
172 ~SyncDataRemote(); | |
173 | |
174 // Return the last motification time according to the server. This may be null | |
175 // if the SyncData represents a deleted item. | |
176 const base::Time& GetModifiedTime() const; | |
177 | |
178 int64_t GetId() const; | |
179 | |
180 // Returns the tag hash value. May not always be present, in which case an | |
181 // empty string will be returned. | |
182 const std::string& GetClientTagHash() const; | |
183 | |
184 // Retrieve the attachments indentified by |attachment_ids|. Invoke | |
185 // |callback| with the requested attachments. | |
186 // | |
187 // |callback| will be invoked when the operation is complete (successfully | |
188 // or otherwise). | |
189 // | |
190 // Retrieving the requested attachments may require reading local storage or | |
191 // requesting the attachments from the network. | |
192 // | |
193 void GetOrDownloadAttachments( | |
194 const AttachmentIdList& attachment_ids, | |
195 const AttachmentService::GetOrDownloadCallback& callback); | |
196 }; | |
197 | |
198 // gmock printer helper. | |
199 void PrintTo(const SyncData& sync_data, std::ostream* os); | |
200 | |
201 typedef std::vector<SyncData> SyncDataList; | |
202 | |
203 } // namespace syncer | |
204 | |
205 #endif // COMPONENTS_SYNC_API_SYNC_DATA_H_ | |
OLD | NEW |