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 SYNC_SYNCABLE_ENTRY_H_ | |
6 #define SYNC_SYNCABLE_ENTRY_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/macros.h" | |
15 #include "sync/base/sync_export.h" | |
16 #include "sync/syncable/entry_kernel.h" | |
17 | |
18 namespace syncer { | |
19 class Cryptographer; | |
20 class ReadNode; | |
21 | |
22 namespace syncable { | |
23 | |
24 class Directory; | |
25 class BaseTransaction; | |
26 | |
27 // A read-only meta entry | |
28 // Instead of: | |
29 // Entry e = transaction.GetById(id); | |
30 // use: | |
31 // Entry e(transaction, GET_BY_ID, id); | |
32 // | |
33 // Why? The former would require a copy constructor, and it would be difficult | |
34 // to enforce that an entry never outlived its transaction if there were a copy | |
35 // constructor. | |
36 enum GetById { | |
37 GET_BY_ID | |
38 }; | |
39 | |
40 enum GetByClientTag { | |
41 GET_BY_CLIENT_TAG | |
42 }; | |
43 | |
44 enum GetByServerTag { | |
45 // Server tagged items are deprecated for all types but bookmarks. | |
46 GET_BY_SERVER_TAG | |
47 }; | |
48 | |
49 enum GetTypeRoot { | |
50 GET_TYPE_ROOT | |
51 }; | |
52 | |
53 enum GetByHandle { | |
54 GET_BY_HANDLE | |
55 }; | |
56 | |
57 class SYNC_EXPORT Entry { | |
58 public: | |
59 // After constructing, you must check good() to test whether the Get | |
60 // succeeded. | |
61 Entry(BaseTransaction* trans, GetByHandle, int64_t handle); | |
62 Entry(BaseTransaction* trans, GetById, const Id& id); | |
63 Entry(BaseTransaction* trans, GetTypeRoot, ModelType type); | |
64 Entry(BaseTransaction* trans, GetByClientTag, const std::string& tag); | |
65 | |
66 // This lookup function is deprecated. All types except bookmarks can use | |
67 // the GetTypeRoot variant instead. | |
68 Entry(BaseTransaction* trans, GetByServerTag, const std::string& tag); | |
69 | |
70 bool good() const { return 0 != kernel_; } | |
71 | |
72 BaseTransaction* trans() const { return basetrans_; } | |
73 | |
74 // Field accessors. | |
75 int64_t GetMetahandle() const { | |
76 DCHECK(kernel_); | |
77 return kernel_->ref(META_HANDLE); | |
78 } | |
79 | |
80 int64_t GetBaseVersion() const { | |
81 DCHECK(kernel_); | |
82 return kernel_->ref(BASE_VERSION); | |
83 } | |
84 | |
85 int64_t GetServerVersion() const { | |
86 DCHECK(kernel_); | |
87 return kernel_->ref(SERVER_VERSION); | |
88 } | |
89 | |
90 int64_t GetLocalExternalId() const { | |
91 DCHECK(kernel_); | |
92 return kernel_->ref(LOCAL_EXTERNAL_ID); | |
93 } | |
94 | |
95 int64_t GetTransactionVersion() const { | |
96 DCHECK(kernel_); | |
97 return kernel_->ref(TRANSACTION_VERSION); | |
98 } | |
99 | |
100 const base::Time& GetMtime() const { | |
101 DCHECK(kernel_); | |
102 return kernel_->ref(MTIME); | |
103 } | |
104 | |
105 const base::Time& GetServerMtime() const { | |
106 DCHECK(kernel_); | |
107 return kernel_->ref(SERVER_MTIME); | |
108 } | |
109 | |
110 const base::Time& GetCtime() const { | |
111 DCHECK(kernel_); | |
112 return kernel_->ref(CTIME); | |
113 } | |
114 | |
115 const base::Time& GetServerCtime() const { | |
116 DCHECK(kernel_); | |
117 return kernel_->ref(SERVER_CTIME); | |
118 } | |
119 | |
120 const Id& GetId() const { | |
121 DCHECK(kernel_); | |
122 return kernel_->ref(ID); | |
123 } | |
124 | |
125 const Id& GetParentId() const { | |
126 DCHECK(kernel_); | |
127 return kernel_->ref(PARENT_ID); | |
128 } | |
129 | |
130 Id GetServerParentId() const { | |
131 DCHECK(kernel_); | |
132 return kernel_->ref(SERVER_PARENT_ID); | |
133 } | |
134 | |
135 bool GetIsUnsynced() const { | |
136 DCHECK(kernel_); | |
137 return kernel_->ref(IS_UNSYNCED); | |
138 } | |
139 | |
140 bool GetIsUnappliedUpdate() const { | |
141 DCHECK(kernel_); | |
142 return kernel_->ref(IS_UNAPPLIED_UPDATE); | |
143 } | |
144 | |
145 bool GetIsDel() const { | |
146 DCHECK(kernel_); | |
147 return kernel_->ref(IS_DEL); | |
148 } | |
149 | |
150 bool GetIsDir() const { | |
151 DCHECK(kernel_); | |
152 return kernel_->ref(IS_DIR); | |
153 } | |
154 | |
155 bool GetServerIsDir() const { | |
156 DCHECK(kernel_); | |
157 return kernel_->ref(SERVER_IS_DIR); | |
158 } | |
159 | |
160 bool GetServerIsDel() const { | |
161 DCHECK(kernel_); | |
162 return kernel_->ref(SERVER_IS_DEL); | |
163 } | |
164 | |
165 const std::string& GetNonUniqueName() const { | |
166 DCHECK(kernel_); | |
167 return kernel_->ref(NON_UNIQUE_NAME); | |
168 } | |
169 | |
170 const std::string& GetServerNonUniqueName() const { | |
171 DCHECK(kernel_); | |
172 return kernel_->ref(SERVER_NON_UNIQUE_NAME); | |
173 } | |
174 | |
175 const std::string& GetUniqueServerTag() const { | |
176 DCHECK(kernel_); | |
177 return kernel_->ref(UNIQUE_SERVER_TAG); | |
178 } | |
179 | |
180 const std::string& GetUniqueClientTag() const { | |
181 DCHECK(kernel_); | |
182 return kernel_->ref(UNIQUE_CLIENT_TAG); | |
183 } | |
184 | |
185 const std::string& GetUniqueBookmarkTag() const { | |
186 DCHECK(kernel_); | |
187 return kernel_->ref(UNIQUE_BOOKMARK_TAG); | |
188 } | |
189 | |
190 const sync_pb::EntitySpecifics& GetSpecifics() const { | |
191 DCHECK(kernel_); | |
192 return kernel_->ref(SPECIFICS); | |
193 } | |
194 | |
195 const sync_pb::EntitySpecifics& GetServerSpecifics() const { | |
196 DCHECK(kernel_); | |
197 return kernel_->ref(SERVER_SPECIFICS); | |
198 } | |
199 | |
200 const sync_pb::EntitySpecifics& GetBaseServerSpecifics() const { | |
201 DCHECK(kernel_); | |
202 return kernel_->ref(BASE_SERVER_SPECIFICS); | |
203 } | |
204 | |
205 const UniquePosition& GetServerUniquePosition() const { | |
206 DCHECK(kernel_); | |
207 return kernel_->ref(SERVER_UNIQUE_POSITION); | |
208 } | |
209 | |
210 const UniquePosition& GetUniquePosition() const { | |
211 DCHECK(kernel_); | |
212 return kernel_->ref(UNIQUE_POSITION); | |
213 } | |
214 | |
215 const sync_pb::AttachmentMetadata& GetAttachmentMetadata() const { | |
216 DCHECK(kernel_); | |
217 return kernel_->ref(ATTACHMENT_METADATA); | |
218 } | |
219 | |
220 const sync_pb::AttachmentMetadata& GetServerAttachmentMetadata() const { | |
221 DCHECK(kernel_); | |
222 return kernel_->ref(SERVER_ATTACHMENT_METADATA); | |
223 } | |
224 | |
225 bool GetSyncing() const; | |
226 bool GetDirtySync() const; | |
227 | |
228 ModelType GetServerModelType() const; | |
229 ModelType GetModelType() const; | |
230 | |
231 Id GetPredecessorId() const; | |
232 Id GetSuccessorId() const; | |
233 Id GetFirstChildId() const; | |
234 int GetTotalNodeCount() const; | |
235 | |
236 int GetPositionIndex() const; | |
237 | |
238 // Returns a vector of this node's children's handles. | |
239 // Clears |result| if there are no children. If this node is of a type that | |
240 // supports user-defined ordering then the resulting vector will be in the | |
241 // proper order. | |
242 void GetChildHandles(std::vector<int64_t>* result) const; | |
243 | |
244 inline bool ExistsOnClientBecauseNameIsNonEmpty() const { | |
245 DCHECK(kernel_); | |
246 return !kernel_->ref(NON_UNIQUE_NAME).empty(); | |
247 } | |
248 | |
249 inline bool IsRoot() const { | |
250 DCHECK(kernel_); | |
251 return kernel_->ref(ID).IsRoot(); | |
252 } | |
253 | |
254 // Returns true if this is an entry that is expected to maintain a certain | |
255 // sort ordering relative to its siblings under the same parent. | |
256 bool ShouldMaintainPosition() const; | |
257 | |
258 // Returns true if this is an entry that is expected to maintain hierarchy. | |
259 // ie. Whether or not the PARENT_ID field contains useful information. | |
260 bool ShouldMaintainHierarchy() const; | |
261 | |
262 Directory* dir() const; | |
263 | |
264 const EntryKernel GetKernelCopy() const { | |
265 return *kernel_; | |
266 } | |
267 | |
268 // Dumps all entry info into a DictionaryValue and returns it. | |
269 // Transfers ownership of the DictionaryValue to the caller. | |
270 base::DictionaryValue* ToValue(Cryptographer* cryptographer) const; | |
271 | |
272 protected: // Don't allow creation on heap, except by sync API wrappers. | |
273 void* operator new(size_t size) { return (::operator new)(size); } | |
274 | |
275 inline explicit Entry(BaseTransaction* trans) | |
276 : basetrans_(trans), | |
277 kernel_(NULL) { } | |
278 | |
279 protected: | |
280 BaseTransaction* const basetrans_; | |
281 | |
282 EntryKernel* kernel_; | |
283 | |
284 private: | |
285 friend class Directory; | |
286 friend class syncer::ReadNode; | |
287 friend std::ostream& operator << (std::ostream& s, const Entry& e); | |
288 | |
289 DISALLOW_COPY_AND_ASSIGN(Entry); | |
290 }; | |
291 | |
292 std::ostream& operator<<(std::ostream& os, const Entry& entry); | |
293 | |
294 } // namespace syncable | |
295 } // namespace syncer | |
296 | |
297 #endif // SYNC_SYNCABLE_ENTRY_H_ | |
OLD | NEW |