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.
5 #ifndef SYNC_API_SYNC_DATA_H_
6 #define SYNC_API_SYNC_DATA_H_
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"
19 class EntitySpecifics;
21 } // namespace sync_pb
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 {
29 // Creates an empty and invalid SyncData.
33 // Default copy and assign welcome.
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
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,
47 static SyncData CreateLocalData(
48 const std::string& sync_tag,
49 const std::string& non_unique_title,
50 const sync_pb::EntitySpecifics& specifics);
52 // Helper method for creating SyncData objects originating from the syncer.
53 static SyncData CreateRemoteData(
55 const sync_pb::EntitySpecifics& specifics,
56 const base::Time& last_modified_time);
58 // Whether this SyncData holds valid data. The only way to have a SyncData
59 // without valid data is to use the default constructor.
62 // Return the datatype we're holding information about. Derived from the sync
63 // datatype specifics.
64 ModelType GetDataType() const;
66 // Return the current sync datatype specifics.
67 const sync_pb::EntitySpecifics& GetSpecifics() const;
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;
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;
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;
82 // Should only be called by sync code when IsLocal() is false.
83 int64 GetRemoteId() const;
85 // Whether this sync data is for local data or data coming from the syncer.
88 std::string ToString() const;
90 // TODO(zea): Query methods for other sync properties: parent, successor, etc.
93 // Necessary since we forward-declare sync_pb::SyncEntity; see
94 // comments in immutable.h.
95 struct ImmutableSyncEntityTraits {
96 typedef sync_pb::SyncEntity* Wrapper;
98 static void InitializeWrapper(Wrapper* wrapper);
100 static void DestroyWrapper(Wrapper* wrapper);
102 static const sync_pb::SyncEntity& Unwrap(const Wrapper& wrapper);
104 static sync_pb::SyncEntity* UnwrapMutable(Wrapper* wrapper);
106 static void Swap(sync_pb::SyncEntity* t1, sync_pb::SyncEntity* t2);
109 typedef Immutable<sync_pb::SyncEntity, ImmutableSyncEntityTraits>
114 sync_pb::SyncEntity* entity,
115 const base::Time& remote_modification_time);
117 // Whether this SyncData holds valid data.
120 // Equal to kInvalidId iff this is local.
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_;
127 // The actual shared sync entity being held.
128 ImmutableSyncEntity immutable_entity_;
131 // gmock printer helper.
132 void PrintTo(const SyncData& sync_data, std::ostream* os);
134 typedef std::vector<SyncData> SyncDataList;
136 } // namespace syncer
138 #endif // SYNC_API_SYNC_DATA_H_