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