[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] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 13 | #include "base/callback.h" |
[email protected] | 8e064af | 2014-04-10 23:40:09 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 15 | #include "base/stl_util.h" |
[email protected] | 7d91f64 | 2013-07-15 22:29:29 | [diff] [blame] | 16 | #include "base/time/time.h" |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 17 | #include "sync/api/attachments/attachment.h" |
| 18 | #include "sync/api/attachments/attachment_service_proxy.h" |
[email protected] | aa166d0 | 2012-12-11 23:47:42 | [diff] [blame] | 19 | #include "sync/base/sync_export.h" |
[email protected] | 002d3919 | 2012-07-03 01:30:56 | [diff] [blame] | 20 | #include "sync/internal_api/public/base/model_type.h" |
[email protected] | 406203d | 2012-06-17 01:07:19 | [diff] [blame] | 21 | #include "sync/internal_api/public/util/immutable.h" |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 22 | #include "sync/internal_api/public/util/weak_handle.h" |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 23 | |
| 24 | namespace sync_pb { |
| 25 | class EntitySpecifics; |
| 26 | class SyncEntity; |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 27 | } // namespace sync_pb |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 28 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 29 | namespace syncer { |
[email protected] | cb02f61 | 2012-06-27 03:15:50 | [diff] [blame] | 30 | |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 31 | class AttachmentService; |
[email protected] | a76fa79 | 2014-04-03 04:14:28 | [diff] [blame] | 32 | class SyncDataLocal; |
| 33 | class SyncDataRemote; |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 34 | |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 35 | // A light-weight container for immutable sync data. Pass-by-value and storage |
| 36 | // in STL containers are supported and encouraged if helpful. |
[email protected] | aa166d0 | 2012-12-11 23:47:42 | [diff] [blame] | 37 | class SYNC_EXPORT SyncData { |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 38 | public: |
| 39 | // Creates an empty and invalid SyncData. |
| 40 | SyncData(); |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 41 | ~SyncData(); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 42 | |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 43 | // Default copy and assign welcome. |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 44 | |
| 45 | // Helper methods for creating SyncData objects for local data. |
[email protected] | 706d41d | 2014-05-02 19:00:11 | [diff] [blame] | 46 | // |
| 47 | // |sync_tag| Must be a string unique to this datatype and is used as a node |
[email protected] | 9871081 | 2011-10-25 21:11:38 | [diff] [blame] | 48 | // identifier server-side. |
[email protected] | 706d41d | 2014-05-02 19:00:11 | [diff] [blame] | 49 | // |
[email protected] | 9871081 | 2011-10-25 21:11:38 | [diff] [blame] | 50 | // For deletes: |datatype| must specify the datatype who node is being |
| 51 | // deleted. |
[email protected] | 706d41d | 2014-05-02 19:00:11 | [diff] [blame] | 52 | // |
| 53 | // For adds/updates: |specifics| must be valid and |non_unique_title| (can be |
| 54 | // the same as |sync_tag|) must be specfied. Note: |non_unique_title| is |
| 55 | // primarily for debug purposes, and will be overwritten if the datatype is |
| 56 | // encrypted. |
| 57 | // |
| 58 | // For data with attachments: |attachments| must not contain duplicates. |
[email protected] | 9871081 | 2011-10-25 21:11:38 | [diff] [blame] | 59 | static SyncData CreateLocalDelete( |
| 60 | const std::string& sync_tag, |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 61 | ModelType datatype); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 62 | static SyncData CreateLocalData( |
| 63 | const std::string& sync_tag, |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 64 | const std::string& non_unique_title, |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 65 | const sync_pb::EntitySpecifics& specifics); |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 66 | static SyncData CreateLocalDataWithAttachments( |
| 67 | const std::string& sync_tag, |
| 68 | const std::string& non_unique_title, |
| 69 | const sync_pb::EntitySpecifics& specifics, |
| 70 | const AttachmentList& attachments); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 71 | |
[email protected] | 9871081 | 2011-10-25 21:11:38 | [diff] [blame] | 72 | // Helper method for creating SyncData objects originating from the syncer. |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 73 | static SyncData CreateRemoteData( |
[email protected] | 7d91f64 | 2013-07-15 22:29:29 | [diff] [blame] | 74 | int64 id, |
| 75 | const sync_pb::EntitySpecifics& specifics, |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 76 | const base::Time& last_modified_time, |
| 77 | const AttachmentIdList& attachment_ids, |
| 78 | const syncer::AttachmentServiceProxy& attachment_service); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 79 | |
| 80 | // Whether this SyncData holds valid data. The only way to have a SyncData |
| 81 | // without valid data is to use the default constructor. |
| 82 | bool IsValid() const; |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 83 | |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 84 | // Return the datatype we're holding information about. Derived from the sync |
| 85 | // datatype specifics. |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 86 | ModelType GetDataType() const; |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 87 | |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 88 | // Return the current sync datatype specifics. |
| 89 | const sync_pb::EntitySpecifics& GetSpecifics() const; |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 90 | |
[email protected] | a76fa79 | 2014-04-03 04:14:28 | [diff] [blame] | 91 | // Return the non unique title (for debugging). Currently only set for data |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 92 | // going TO the syncer, not from. |
| 93 | const std::string& GetTitle() const; |
| 94 | |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 95 | // Whether this sync data is for local data or data coming from the syncer. |
| 96 | bool IsLocal() const; |
| 97 | |
[email protected] | 2d05d148 | 2011-09-23 00:34:36 | [diff] [blame] | 98 | std::string ToString() const; |
| 99 | |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 100 | // Return a list of this SyncData's attachment ids. |
| 101 | // |
| 102 | // The attachments may or may not be present on this device. |
| 103 | AttachmentIdList GetAttachmentIds() const; |
| 104 | |
[email protected] | 729eae68 | 2011-06-17 18:24:59 | [diff] [blame] | 105 | // TODO(zea): Query methods for other sync properties: parent, successor, etc. |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 106 | |
[email protected] | a76fa79 | 2014-04-03 04:14:28 | [diff] [blame] | 107 | protected: |
| 108 | // These data members are protected so derived types like SyncDataLocal and |
| 109 | // SyncDataRemote can access them. |
| 110 | |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 111 | // Necessary since we forward-declare sync_pb::SyncEntity; see |
| 112 | // comments in immutable.h. |
| 113 | struct ImmutableSyncEntityTraits { |
| 114 | typedef sync_pb::SyncEntity* Wrapper; |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 115 | |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 116 | static void InitializeWrapper(Wrapper* wrapper); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 117 | |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 118 | static void DestroyWrapper(Wrapper* wrapper); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 119 | |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 120 | static const sync_pb::SyncEntity& Unwrap(const Wrapper& wrapper); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 121 | |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 122 | static sync_pb::SyncEntity* UnwrapMutable(Wrapper* wrapper); |
| 123 | |
| 124 | static void Swap(sync_pb::SyncEntity* t1, sync_pb::SyncEntity* t2); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 125 | }; |
| 126 | |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 127 | typedef Immutable<sync_pb::SyncEntity, ImmutableSyncEntityTraits> |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 128 | ImmutableSyncEntity; |
| 129 | |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 130 | // Equal to kInvalidId iff this is local. |
[email protected] | 0f9842d61 | 2011-09-20 14:26:55 | [diff] [blame] | 131 | int64 id_; |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 132 | |
[email protected] | a76fa79 | 2014-04-03 04:14:28 | [diff] [blame] | 133 | // This may be null if the SyncData represents a deleted item. |
[email protected] | 7d91f64 | 2013-07-15 22:29:29 | [diff] [blame] | 134 | base::Time remote_modification_time_; |
| 135 | |
[email protected] | fb16d7f9 | 2011-09-16 04:55:39 | [diff] [blame] | 136 | // The actual shared sync entity being held. |
| 137 | ImmutableSyncEntity immutable_entity_; |
[email protected] | 1069f58 | 2014-04-01 05:04:46 | [diff] [blame] | 138 | |
| 139 | Immutable<AttachmentList> attachments_; |
| 140 | |
| 141 | AttachmentServiceProxy attachment_service_; |
[email protected] | a76fa79 | 2014-04-03 04:14:28 | [diff] [blame] | 142 | |
| 143 | private: |
| 144 | // Whether this SyncData holds valid data. |
| 145 | bool is_valid_; |
| 146 | |
| 147 | // Clears |entity| and |attachments|. |
| 148 | SyncData(int64 id, |
| 149 | sync_pb::SyncEntity* entity, |
| 150 | AttachmentList* attachments, |
| 151 | const base::Time& remote_modification_time, |
| 152 | const syncer::AttachmentServiceProxy& attachment_service); |
| 153 | }; |
| 154 | |
| 155 | // A SyncData going to the syncer. |
| 156 | class SYNC_EXPORT SyncDataLocal : public SyncData { |
| 157 | public: |
| 158 | // Construct a SyncDataLocal from a SyncData. |
| 159 | // |
| 160 | // |sync_data|'s IsLocal() must be true. |
| 161 | explicit SyncDataLocal(const SyncData& sync_data); |
| 162 | ~SyncDataLocal(); |
| 163 | |
| 164 | // Return a list of this SyncData's attachments. |
| 165 | const AttachmentList& GetLocalAttachmentsForUpload() const; |
| 166 | |
| 167 | // Return the value of the unique client tag. This is only set for data going |
| 168 | // TO the syncer, not coming from. |
| 169 | const std::string& GetTag() const; |
| 170 | }; |
| 171 | |
| 172 | // A SyncData that comes from the syncer. |
| 173 | class SYNC_EXPORT SyncDataRemote : public SyncData { |
| 174 | public: |
| 175 | // Construct a SyncDataRemote from a SyncData. |
| 176 | // |
| 177 | // |sync_data|'s IsLocal() must be false. |
| 178 | explicit SyncDataRemote(const SyncData& sync_data); |
| 179 | ~SyncDataRemote(); |
| 180 | |
| 181 | // Return the last motification time according to the server. This may be null |
| 182 | // if the SyncData represents a deleted item. |
| 183 | const base::Time& GetModifiedTime() const; |
| 184 | |
| 185 | int64 GetId() const; |
| 186 | |
| 187 | // Retrieve the attachments indentified by |attachment_ids|. Invoke |
| 188 | // |callback| with the requested attachments. |
| 189 | // |
| 190 | // |callback| will be invoked when the operation is complete (successfully |
| 191 | // or otherwise). |
| 192 | // |
| 193 | // Retrieving the requested attachments may require reading local storage or |
| 194 | // requesting the attachments from the network. |
| 195 | // |
| 196 | void GetOrDownloadAttachments( |
| 197 | const AttachmentIdList& attachment_ids, |
| 198 | const AttachmentService::GetOrDownloadCallback& callback); |
| 199 | |
| 200 | // Drop (delete from local storage) the attachments associated with this |
| 201 | // SyncData specified in |attachment_ids|. This method will not delete |
| 202 | // attachments from the server. |
| 203 | // |
| 204 | // |callback| will be invoked when the operation is complete (successfully |
| 205 | // or otherwise). |
| 206 | void DropAttachments(const AttachmentIdList& attachment_ids, |
| 207 | const AttachmentService::DropCallback& callback); |
[email protected] | 52c78823 | 2011-05-23 22:51:33 | [diff] [blame] | 208 | }; |
| 209 | |
[email protected] | 2d05d148 | 2011-09-23 00:34:36 | [diff] [blame] | 210 | // gmock printer helper. |
| 211 | void PrintTo(const SyncData& sync_data, std::ostream* os); |
| 212 | |
[email protected] | 2ee9fa2 | 2013-03-06 22:43:00 | [diff] [blame] | 213 | typedef std::vector<SyncData> SyncDataList; |
| 214 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 215 | } // namespace syncer |
[email protected] | cb02f61 | 2012-06-27 03:15:50 | [diff] [blame] | 216 | |
[email protected] | 895a1e5 | 2012-05-15 02:50:12 | [diff] [blame] | 217 | #endif // SYNC_API_SYNC_DATA_H_ |