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