OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SYNC_API_SYNC_DATA_H_ | 5 #ifndef SYNC_API_SYNC_DATA_H_ |
6 #define SYNC_API_SYNC_DATA_H_ | 6 #define SYNC_API_SYNC_DATA_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback.h" | |
14 #include "base/stl_util.h" | |
13 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "sync/api/attachments/attachment.h" | |
17 #include "sync/api/attachments/attachment_service_proxy.h" | |
14 #include "sync/base/sync_export.h" | 18 #include "sync/base/sync_export.h" |
15 #include "sync/internal_api/public/base/model_type.h" | 19 #include "sync/internal_api/public/base/model_type.h" |
16 #include "sync/internal_api/public/util/immutable.h" | 20 #include "sync/internal_api/public/util/immutable.h" |
21 #include "sync/internal_api/public/util/weak_handle.h" | |
17 | 22 |
18 namespace sync_pb { | 23 namespace sync_pb { |
19 class EntitySpecifics; | 24 class EntitySpecifics; |
20 class SyncEntity; | 25 class SyncEntity; |
21 } // namespace sync_pb | 26 } // namespace sync_pb |
22 | 27 |
23 namespace syncer { | 28 namespace syncer { |
24 | 29 |
30 class AttachmentService; | |
31 | |
25 // A light-weight container for immutable sync data. Pass-by-value and storage | 32 // A light-weight container for immutable sync data. Pass-by-value and storage |
26 // in STL containers are supported and encouraged if helpful. | 33 // in STL containers are supported and encouraged if helpful. |
27 class SYNC_EXPORT SyncData { | 34 class SYNC_EXPORT SyncData { |
28 public: | 35 public: |
29 // Creates an empty and invalid SyncData. | 36 // Creates an empty and invalid SyncData. |
30 SyncData(); | 37 SyncData(); |
31 ~SyncData(); | 38 ~SyncData(); |
32 | 39 |
33 // Default copy and assign welcome. | 40 // Default copy and assign welcome. |
34 | 41 |
35 // Helper methods for creating SyncData objects for local data. | 42 // 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 | 43 // The sync tag must be a string unique to this datatype and is used as a node |
37 // identifier server-side. | 44 // identifier server-side. |
38 // For deletes: |datatype| must specify the datatype who node is being | 45 // For deletes: |datatype| must specify the datatype who node is being |
39 // deleted. | 46 // deleted. |
40 // For adds/updates: the specifics must be valid and the non-unique title (can | 47 // For adds/updates: the specifics must be valid and the non-unique title (can |
41 // be the same as sync tag) must be specfied. | 48 // be the same as sync tag) must be specfied. |
42 // Note: the non_unique_title is primarily for debug purposes, and will be | 49 // Note: the non_unique_title is primarily for debug purposes, and will be |
43 // overwritten if the datatype is encrypted. | 50 // overwritten if the datatype is encrypted. |
44 static SyncData CreateLocalDelete( | 51 static SyncData CreateLocalDelete( |
45 const std::string& sync_tag, | 52 const std::string& sync_tag, |
46 ModelType datatype); | 53 ModelType datatype); |
47 static SyncData CreateLocalData( | 54 static SyncData CreateLocalData( |
48 const std::string& sync_tag, | 55 const std::string& sync_tag, |
49 const std::string& non_unique_title, | 56 const std::string& non_unique_title, |
50 const sync_pb::EntitySpecifics& specifics); | 57 const sync_pb::EntitySpecifics& specifics); |
58 static SyncData CreateLocalDataWithAttachments( | |
59 const std::string& sync_tag, | |
60 const std::string& non_unique_title, | |
61 const sync_pb::EntitySpecifics& specifics, | |
62 const AttachmentList& attachments); | |
51 | 63 |
52 // Helper method for creating SyncData objects originating from the syncer. | 64 // Helper method for creating SyncData objects originating from the syncer. |
65 // | |
66 // TODO(maniscalco): Replace all calls to 3-arg CreateRemoteData with calls to | |
67 // the 5-arg version (bug 353296). | |
53 static SyncData CreateRemoteData( | 68 static SyncData CreateRemoteData( |
54 int64 id, | 69 int64 id, |
55 const sync_pb::EntitySpecifics& specifics, | 70 const sync_pb::EntitySpecifics& specifics, |
56 const base::Time& last_modified_time); | 71 const base::Time& last_modified_time, |
72 const AttachmentIdList& attachment_ids, | |
73 const syncer::AttachmentServiceProxy& attachment_service); | |
74 static SyncData CreateRemoteData(int64 id, | |
75 const sync_pb::EntitySpecifics& specifics, | |
76 const base::Time& last_modified_time); | |
57 | 77 |
58 // Whether this SyncData holds valid data. The only way to have a SyncData | 78 // Whether this SyncData holds valid data. The only way to have a SyncData |
59 // without valid data is to use the default constructor. | 79 // without valid data is to use the default constructor. |
60 bool IsValid() const; | 80 bool IsValid() const; |
61 | 81 |
62 // Return the datatype we're holding information about. Derived from the sync | 82 // Return the datatype we're holding information about. Derived from the sync |
63 // datatype specifics. | 83 // datatype specifics. |
64 ModelType GetDataType() const; | 84 ModelType GetDataType() const; |
65 | 85 |
66 // Return the current sync datatype specifics. | 86 // Return the current sync datatype specifics. |
(...skipping 13 matching lines...) Expand all Loading... | |
80 const base::Time& GetRemoteModifiedTime() const; | 100 const base::Time& GetRemoteModifiedTime() const; |
81 | 101 |
82 // Should only be called by sync code when IsLocal() is false. | 102 // Should only be called by sync code when IsLocal() is false. |
83 int64 GetRemoteId() const; | 103 int64 GetRemoteId() const; |
84 | 104 |
85 // Whether this sync data is for local data or data coming from the syncer. | 105 // Whether this sync data is for local data or data coming from the syncer. |
86 bool IsLocal() const; | 106 bool IsLocal() const; |
87 | 107 |
88 std::string ToString() const; | 108 std::string ToString() const; |
89 | 109 |
110 // Return a list of this SyncData's attachment ids. | |
111 // | |
112 // The attachments may or may not be present on this host. | |
tim (not reviewing)
2014/03/27 18:35:46
s/host/device to be more consistent with the termi
maniscalco
2014/03/27 21:17:31
Done.
| |
113 AttachmentIdList GetAttachmentIds() const; | |
114 | |
115 // Return a list of this SyncData's attachments. | |
tim (not reviewing)
2014/03/27 18:35:46
This is only ever the list of attachments intended
tim (not reviewing)
2014/03/27 19:16:44
Alternatively, there's the subclass + factory meth
maniscalco
2014/03/27 21:17:31
SGTM. Renamed to GetLocalAttachmentsForUpload. B
| |
116 // | |
117 // May only be called when IsLocal() is true. | |
118 const AttachmentList& GetAttachments() const; | |
119 | |
120 // Retrieve the attachments indentified by |attachment_ids|. Invoke |callback| | |
121 // with the requested attachments. | |
122 // | |
123 // May only be called when IsLocal() is false. | |
124 // | |
125 // |callback| will be invoked when the operation is complete (successfully or | |
126 // otherwise). | |
127 // | |
128 // Retrieving the requested attachments may require reading local storage or | |
129 // requesting the attachments from the network. | |
130 // | |
131 void GetOrDownloadAttachments( | |
132 const AttachmentIdList& attachment_ids, | |
133 const AttachmentService::GetOrDownloadCallback& callback); | |
134 | |
135 // Drop (delete from local storage) the attachments associated with this | |
136 // SyncData specified in |attachment_ids|. This method will not delete | |
137 // attachments from the server. | |
138 // | |
139 // May only be called when IsLocal() is false. | |
140 // | |
141 // |callback| will be invoked when the operation is complete (successfully or | |
142 // otherwise). | |
143 void DropAttachments(const AttachmentIdList& attachment_ids, | |
144 const AttachmentService::DropCallback& callback); | |
145 | |
90 // TODO(zea): Query methods for other sync properties: parent, successor, etc. | 146 // TODO(zea): Query methods for other sync properties: parent, successor, etc. |
91 | 147 |
92 private: | 148 private: |
93 // Necessary since we forward-declare sync_pb::SyncEntity; see | 149 // Necessary since we forward-declare sync_pb::SyncEntity; see |
94 // comments in immutable.h. | 150 // comments in immutable.h. |
95 struct ImmutableSyncEntityTraits { | 151 struct ImmutableSyncEntityTraits { |
96 typedef sync_pb::SyncEntity* Wrapper; | 152 typedef sync_pb::SyncEntity* Wrapper; |
97 | 153 |
98 static void InitializeWrapper(Wrapper* wrapper); | 154 static void InitializeWrapper(Wrapper* wrapper); |
99 | 155 |
100 static void DestroyWrapper(Wrapper* wrapper); | 156 static void DestroyWrapper(Wrapper* wrapper); |
101 | 157 |
102 static const sync_pb::SyncEntity& Unwrap(const Wrapper& wrapper); | 158 static const sync_pb::SyncEntity& Unwrap(const Wrapper& wrapper); |
103 | 159 |
104 static sync_pb::SyncEntity* UnwrapMutable(Wrapper* wrapper); | 160 static sync_pb::SyncEntity* UnwrapMutable(Wrapper* wrapper); |
105 | 161 |
106 static void Swap(sync_pb::SyncEntity* t1, sync_pb::SyncEntity* t2); | 162 static void Swap(sync_pb::SyncEntity* t1, sync_pb::SyncEntity* t2); |
107 }; | 163 }; |
108 | 164 |
109 typedef Immutable<sync_pb::SyncEntity, ImmutableSyncEntityTraits> | 165 typedef Immutable<sync_pb::SyncEntity, ImmutableSyncEntityTraits> |
110 ImmutableSyncEntity; | 166 ImmutableSyncEntity; |
111 | 167 |
112 // Clears |entity|. | 168 // Clears |entity| and |attachments|. |
113 SyncData(int64 id, | 169 SyncData( |
114 sync_pb::SyncEntity* entity, | 170 int64 id, |
115 const base::Time& remote_modification_time); | 171 sync_pb::SyncEntity* entity, |
172 AttachmentList* attachments, | |
173 const base::Time& remote_modification_time, | |
174 const syncer::AttachmentServiceProxy& attachment_service); | |
116 | 175 |
117 // Whether this SyncData holds valid data. | 176 // Whether this SyncData holds valid data. |
118 bool is_valid_; | 177 bool is_valid_; |
119 | 178 |
120 // Equal to kInvalidId iff this is local. | 179 // Equal to kInvalidId iff this is local. |
121 int64 id_; | 180 int64 id_; |
122 | 181 |
123 // This is only valid if IsLocal() is false, and may be null if the | 182 // This is only valid if IsLocal() is false, and may be null if the |
124 // SyncData represents a deleted item. | 183 // SyncData represents a deleted item. |
125 base::Time remote_modification_time_; | 184 base::Time remote_modification_time_; |
126 | 185 |
127 // The actual shared sync entity being held. | 186 // The actual shared sync entity being held. |
128 ImmutableSyncEntity immutable_entity_; | 187 ImmutableSyncEntity immutable_entity_; |
188 | |
189 Immutable<AttachmentList> attachments_; | |
190 | |
191 AttachmentServiceProxy attachment_service_; | |
129 }; | 192 }; |
130 | 193 |
131 // gmock printer helper. | 194 // gmock printer helper. |
132 void PrintTo(const SyncData& sync_data, std::ostream* os); | 195 void PrintTo(const SyncData& sync_data, std::ostream* os); |
133 | 196 |
134 typedef std::vector<SyncData> SyncDataList; | 197 typedef std::vector<SyncData> SyncDataList; |
135 | 198 |
136 } // namespace syncer | 199 } // namespace syncer |
137 | 200 |
138 #endif // SYNC_API_SYNC_DATA_H_ | 201 #endif // SYNC_API_SYNC_DATA_H_ |
OLD | NEW |