Home | History | Annotate | Download | only in syncable
      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   GET_BY_SERVER_TAG
     39 };
     40 
     41 enum GetByHandle {
     42   GET_BY_HANDLE
     43 };
     44 
     45 class SYNC_EXPORT Entry {
     46  public:
     47   // After constructing, you must check good() to test whether the Get
     48   // succeeded.
     49   Entry(BaseTransaction* trans, GetByHandle, int64 handle);
     50   Entry(BaseTransaction* trans, GetById, const Id& id);
     51   Entry(BaseTransaction* trans, GetByServerTag, const std::string& tag);
     52   Entry(BaseTransaction* trans, GetByClientTag, const std::string& tag);
     53 
     54   bool good() const { return 0 != kernel_; }
     55 
     56   BaseTransaction* trans() const { return basetrans_; }
     57 
     58   // Field accessors.
     59   inline int64 Get(MetahandleField field) const {
     60     DCHECK(kernel_);
     61     return kernel_->ref(field);
     62   }
     63   inline Id Get(IdField field) const {
     64     DCHECK(kernel_);
     65     return kernel_->ref(field);
     66   }
     67   inline int64 Get(Int64Field field) const {
     68     DCHECK(kernel_);
     69     return kernel_->ref(field);
     70   }
     71   inline const base::Time& Get(TimeField field) const {
     72     DCHECK(kernel_);
     73     return kernel_->ref(field);
     74   }
     75   inline int64 Get(BaseVersion field) const {
     76     DCHECK(kernel_);
     77     return kernel_->ref(field);
     78   }
     79   inline bool Get(IndexedBitField field) const {
     80     DCHECK(kernel_);
     81     return kernel_->ref(field);
     82   }
     83   inline bool Get(IsDelField field) const {
     84     DCHECK(kernel_);
     85     return kernel_->ref(field);
     86   }
     87   inline bool Get(BitField field) const {
     88     DCHECK(kernel_);
     89     return kernel_->ref(field);
     90   }
     91   const std::string& Get(StringField field) const;
     92   inline const sync_pb::EntitySpecifics& Get(ProtoField field) const {
     93     DCHECK(kernel_);
     94     return kernel_->ref(field);
     95   }
     96   inline const UniquePosition& Get(UniquePositionField field) const {
     97     DCHECK(kernel_);
     98     return kernel_->ref(field);
     99   }
    100   inline bool Get(BitTemp field) const {
    101     DCHECK(kernel_);
    102     return kernel_->ref(field);
    103   }
    104 
    105   ModelType GetServerModelType() const;
    106   ModelType GetModelType() const;
    107 
    108   Id GetPredecessorId() const;
    109   Id GetSuccessorId() const;
    110   Id GetFirstChildId() const;
    111   int GetTotalNodeCount() const;
    112 
    113   int GetPositionIndex() const;
    114 
    115   // Returns a vector of this node's children's handles.
    116   // Clears |result| if there are no children.  If this node is of a type that
    117   // supports user-defined ordering then the resulting vector will be in the
    118   // proper order.
    119   void GetChildHandles(std::vector<int64>* result) const;
    120 
    121   inline bool ExistsOnClientBecauseNameIsNonEmpty() const {
    122     DCHECK(kernel_);
    123     return !kernel_->ref(NON_UNIQUE_NAME).empty();
    124   }
    125 
    126   inline bool IsRoot() const {
    127     DCHECK(kernel_);
    128     return kernel_->ref(ID).IsRoot();
    129   }
    130 
    131   // Returns true if this is an entry that is expected to maintain a certain
    132   // sort ordering relative to its siblings under the same parent.
    133   bool ShouldMaintainPosition() const;
    134 
    135   Directory* dir() const;
    136 
    137   const EntryKernel GetKernelCopy() const {
    138     return *kernel_;
    139   }
    140 
    141   // Dumps all entry info into a DictionaryValue and returns it.
    142   // Transfers ownership of the DictionaryValue to the caller.
    143   base::DictionaryValue* ToValue(Cryptographer* cryptographer) const;
    144 
    145  protected:  // Don't allow creation on heap, except by sync API wrappers.
    146   void* operator new(size_t size) { return (::operator new)(size); }
    147 
    148   inline explicit Entry(BaseTransaction* trans)
    149       : basetrans_(trans),
    150         kernel_(NULL) { }
    151 
    152  protected:
    153   BaseTransaction* const basetrans_;
    154 
    155   EntryKernel* kernel_;
    156 
    157  private:
    158   friend class Directory;
    159   friend class syncer::ReadNode;
    160   friend std::ostream& operator << (std::ostream& s, const Entry& e);
    161 
    162   DISALLOW_COPY_AND_ASSIGN(Entry);
    163 };
    164 
    165 std::ostream& operator<<(std::ostream& os, const Entry& entry);
    166 
    167 }  // namespace syncable
    168 }  // namespace syncer
    169 
    170 #endif  // SYNC_SYNCABLE_ENTRY_H_
    171