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