sync_data.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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_API_SYNC_DATA_H_ 6#define SYNC_API_SYNC_DATA_H_ 7 8#include <iosfwd> 9#include <string> 10#include <vector> 11 12#include "base/basictypes.h" 13#include "sync/base/sync_export.h" 14#include "sync/internal_api/public/base/model_type.h" 15#include "sync/internal_api/public/util/immutable.h" 16 17namespace sync_pb { 18class EntitySpecifics; 19class SyncEntity; 20} // namespace sync_pb 21 22namespace syncer { 23 24// A light-weight container for immutable sync data. Pass-by-value and storage 25// in STL containers are supported and encouraged if helpful. 26class SYNC_EXPORT SyncData { 27 public: 28 // Creates an empty and invalid SyncData. 29 SyncData(); 30 ~SyncData(); 31 32 // Default copy and assign welcome. 33 34 // Helper methods for creating SyncData objects for local data. 35 // The sync tag must be a string unique to this datatype and is used as a node 36 // identifier server-side. 37 // For deletes: |datatype| must specify the datatype who node is being 38 // deleted. 39 // For adds/updates: the specifics must be valid and the non-unique title (can 40 // be the same as sync tag) must be specfied. 41 // Note: the non_unique_title is primarily for debug purposes, and will be 42 // overwritten if the datatype is encrypted. 43 static SyncData CreateLocalDelete( 44 const std::string& sync_tag, 45 ModelType datatype); 46 static SyncData CreateLocalData( 47 const std::string& sync_tag, 48 const std::string& non_unique_title, 49 const sync_pb::EntitySpecifics& specifics); 50 51 // Helper method for creating SyncData objects originating from the syncer. 52 static SyncData CreateRemoteData( 53 int64 id, const sync_pb::EntitySpecifics& specifics); 54 55 // Whether this SyncData holds valid data. The only way to have a SyncData 56 // without valid data is to use the default constructor. 57 bool IsValid() const; 58 59 // Return the datatype we're holding information about. Derived from the sync 60 // datatype specifics. 61 ModelType GetDataType() const; 62 63 // Return the current sync datatype specifics. 64 const sync_pb::EntitySpecifics& GetSpecifics() const; 65 66 // Returns the value of the unique client tag. This is only set for data going 67 // TO the syncer, not coming from. 68 const std::string& GetTag() const; 69 70 // Returns the non unique title (for debugging). Currently only set for data 71 // going TO the syncer, not from. 72 const std::string& GetTitle() const; 73 74 // Should only be called by sync code when IsLocal() is false. 75 int64 GetRemoteId() const; 76 77 // Whether this sync data is for local data or data coming from the syncer. 78 bool IsLocal() const; 79 80 std::string ToString() const; 81 82 // TODO(zea): Query methods for other sync properties: parent, successor, etc. 83 84 private: 85 // Necessary since we forward-declare sync_pb::SyncEntity; see 86 // comments in immutable.h. 87 struct ImmutableSyncEntityTraits { 88 typedef sync_pb::SyncEntity* Wrapper; 89 90 static void InitializeWrapper(Wrapper* wrapper); 91 92 static void DestroyWrapper(Wrapper* wrapper); 93 94 static const sync_pb::SyncEntity& Unwrap(const Wrapper& wrapper); 95 96 static sync_pb::SyncEntity* UnwrapMutable(Wrapper* wrapper); 97 98 static void Swap(sync_pb::SyncEntity* t1, sync_pb::SyncEntity* t2); 99 }; 100 101 typedef Immutable<sync_pb::SyncEntity, ImmutableSyncEntityTraits> 102 ImmutableSyncEntity; 103 104 // Clears |entity|. 105 SyncData(int64 id, sync_pb::SyncEntity* entity); 106 107 // Whether this SyncData holds valid data. 108 bool is_valid_; 109 110 // Equal to kInvalidId iff this is local. 111 int64 id_; 112 113 // The actual shared sync entity being held. 114 ImmutableSyncEntity immutable_entity_; 115}; 116 117// gmock printer helper. 118void PrintTo(const SyncData& sync_data, std::ostream* os); 119 120typedef std::vector<SyncData> SyncDataList; 121 122} // namespace syncer 123 124#endif // SYNC_API_SYNC_DATA_H_ 125