[email protected] | aa166d0 | 2012-12-11 23:47:42 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 895a1e5 | 2012-05-15 02:50:12 | [diff] [blame] | 5 | #ifndef SYNC_API_SYNC_DATA_H_ |
| 6 | #define SYNC_API_SYNC_DATA_H_ |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 7 | |
[email protected] | 2d05d148 | 2011-09-23 00:34:36 | [diff] [blame] | 8 | #include <iosfwd> |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
[email protected] | 0f9842d61 | 2011-09-20 14:26:55 | [diff] [blame] | 12 | #include "base/basictypes.h" |
[email protected] | aa166d0 | 2012-12-11 23:47:42 | [diff] [blame] | 13 | #include "sync/base/sync_export.h" |
[email protected] | 002d3919 | 2012-07-03 01:30:56 | [diff] [blame] | 14 | #include "sync/internal_api/public/base/model_type.h" |
[email protected] | 406203d | 2012-06-17 01:07:19 | [diff] [blame] | 15 | #include "sync/internal_api/public/util/immutable.h" |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 16 | |
| 17 | namespace sync_pb { |
| 18 | class EntitySpecifics; |
| 19 | class SyncEntity; |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 20 | } // namespace sync_pb |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 21 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 22 | namespace syncer { |
[email protected] | cb02f61 | 2012-06-27 03:15:50 | [diff] [blame] | 23 | |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 24 | // A light-weight container for immutable sync data. Pass-by-value and storage |
| 25 | // in STL containers are supported and encouraged if helpful. |
[email protected] | aa166d0 | 2012-12-11 23:47:42 | [diff] [blame] | 26 | class SYNC_EXPORT SyncData { |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 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. |
[email protected] | 9871081 | 2011-10-25 21:11:38 | [diff] [blame] | 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. |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 41 | // Note: the non_unique_title is primarily for debug purposes, and will be |
| 42 | // overwritten if the datatype is encrypted. |
[email protected] | 9871081 | 2011-10-25 21:11:38 | [diff] [blame] | 43 | static SyncData CreateLocalDelete( |
| 44 | const std::string& sync_tag, |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 45 | ModelType datatype); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 46 | static SyncData CreateLocalData( |
| 47 | const std::string& sync_tag, |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 48 | const std::string& non_unique_title, |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 49 | const sync_pb::EntitySpecifics& specifics); |
| 50 | |
[email protected] | 9871081 | 2011-10-25 21:11:38 | [diff] [blame] | 51 | // Helper method for creating SyncData objects originating from the syncer. |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 52 | static SyncData CreateRemoteData( |
[email protected] | 0f9842d61 | 2011-09-20 14:26:55 | [diff] [blame] | 53 | int64 id, const sync_pb::EntitySpecifics& specifics); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 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; |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 58 | |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 59 | // Return the datatype we're holding information about. Derived from the sync |
| 60 | // datatype specifics. |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 61 | ModelType GetDataType() const; |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 62 | |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 63 | // Return the current sync datatype specifics. |
| 64 | const sync_pb::EntitySpecifics& GetSpecifics() const; |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 65 | |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 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; |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 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 | |
[email protected] | 0f9842d61 | 2011-09-20 14:26:55 | [diff] [blame] | 74 | // Should only be called by sync code when IsLocal() is false. |
| 75 | int64 GetRemoteId() const; |
| 76 | |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 77 | // Whether this sync data is for local data or data coming from the syncer. |
| 78 | bool IsLocal() const; |
| 79 | |
[email protected] | 2d05d148 | 2011-09-23 00:34:36 | [diff] [blame] | 80 | std::string ToString() const; |
| 81 | |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 82 | // TODO(zea): Query methods for other sync properties: parent, successor, etc. |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 83 | |
| 84 | private: |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 85 | // Necessary since we forward-declare sync_pb::SyncEntity; see |
| 86 | // comments in immutable.h. |
| 87 | struct ImmutableSyncEntityTraits { |
| 88 | typedef sync_pb::SyncEntity* Wrapper; |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 89 | |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 90 | static void InitializeWrapper(Wrapper* wrapper); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 91 | |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 92 | static void DestroyWrapper(Wrapper* wrapper); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 93 | |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 94 | static const sync_pb::SyncEntity& Unwrap(const Wrapper& wrapper); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 95 | |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 96 | static sync_pb::SyncEntity* UnwrapMutable(Wrapper* wrapper); |
| 97 | |
| 98 | static void Swap(sync_pb::SyncEntity* t1, sync_pb::SyncEntity* t2); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 99 | }; |
| 100 | |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 101 | typedef Immutable<sync_pb::SyncEntity, ImmutableSyncEntityTraits> |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 102 | ImmutableSyncEntity; |
| 103 | |
| 104 | // Clears |entity|. |
[email protected] | 0f9842d61 | 2011-09-20 14:26:55 | [diff] [blame] | 105 | SyncData(int64 id, sync_pb::SyncEntity* entity); |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 106 | |
| 107 | // Whether this SyncData holds valid data. |
| 108 | bool is_valid_; |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 109 | |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 110 | // Equal to kInvalidId iff this is local. |
[email protected] | 0f9842d61 | 2011-09-20 14:26:55 | [diff] [blame] | 111 | int64 id_; |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 112 | |
| 113 | // The actual shared sync entity being held. |
| 114 | ImmutableSyncEntity immutable_entity_; |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 115 | }; |
| 116 | |
[email protected] | 2d05d148 | 2011-09-23 00:34:36 | [diff] [blame] | 117 | // gmock printer helper. |
| 118 | void PrintTo(const SyncData& sync_data, std::ostream* os); |
| 119 | |
[email protected] | 2ee9fa2 | 2013-03-06 22:43:00 | [diff] [blame^] | 120 | typedef std::vector<SyncData> SyncDataList; |
| 121 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 122 | } // namespace syncer |
[email protected] | cb02f61 | 2012-06-27 03:15:50 | [diff] [blame] | 123 | |
[email protected] | 895a1e5 | 2012-05-15 02:50:12 | [diff] [blame] | 124 | #endif // SYNC_API_SYNC_DATA_H_ |