blob: 33e41a8abc0b01b00acc4229867582008a8ca8e8 [file] [log] [blame]
[email protected]aa166d02012-12-11 23:47:421// Copyright 2012 The Chromium Authors. All rights reserved.
[email protected]52c788232011-05-23 22:51:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]895a1e52012-05-15 02:50:125#ifndef SYNC_API_SYNC_DATA_H_
6#define SYNC_API_SYNC_DATA_H_
[email protected]52c788232011-05-23 22:51:337
[email protected]2d05d1482011-09-23 00:34:368#include <iosfwd>
[email protected]52c788232011-05-23 22:51:339#include <string>
10#include <vector>
11
[email protected]0f9842d612011-09-20 14:26:5512#include "base/basictypes.h"
[email protected]aa166d02012-12-11 23:47:4213#include "sync/base/sync_export.h"
[email protected]002d39192012-07-03 01:30:5614#include "sync/internal_api/public/base/model_type.h"
[email protected]406203d2012-06-17 01:07:1915#include "sync/internal_api/public/util/immutable.h"
[email protected]52c788232011-05-23 22:51:3316
17namespace sync_pb {
18class EntitySpecifics;
19class SyncEntity;
[email protected]fb16d7f92011-09-16 04:55:3920} // namespace sync_pb
[email protected]52c788232011-05-23 22:51:3321
[email protected]65f173552012-06-28 22:43:5822namespace syncer {
[email protected]cb02f612012-06-27 03:15:5023
[email protected]52c788232011-05-23 22:51:3324// 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]aa166d02012-12-11 23:47:4226class SYNC_EXPORT SyncData {
[email protected]52c788232011-05-23 22:51:3327 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]98710812011-10-25 21:11:3835 // 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]729eae682011-06-17 18:24:5941 // Note: the non_unique_title is primarily for debug purposes, and will be
42 // overwritten if the datatype is encrypted.
[email protected]98710812011-10-25 21:11:3843 static SyncData CreateLocalDelete(
44 const std::string& sync_tag,
[email protected]d45f0d92012-07-20 17:25:4145 ModelType datatype);
[email protected]52c788232011-05-23 22:51:3346 static SyncData CreateLocalData(
47 const std::string& sync_tag,
[email protected]729eae682011-06-17 18:24:5948 const std::string& non_unique_title,
[email protected]52c788232011-05-23 22:51:3349 const sync_pb::EntitySpecifics& specifics);
50
[email protected]98710812011-10-25 21:11:3851 // Helper method for creating SyncData objects originating from the syncer.
[email protected]52c788232011-05-23 22:51:3352 static SyncData CreateRemoteData(
[email protected]0f9842d612011-09-20 14:26:5553 int64 id, const sync_pb::EntitySpecifics& specifics);
[email protected]52c788232011-05-23 22:51:3354
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]729eae682011-06-17 18:24:5958
[email protected]52c788232011-05-23 22:51:3359 // Return the datatype we're holding information about. Derived from the sync
60 // datatype specifics.
[email protected]d45f0d92012-07-20 17:25:4161 ModelType GetDataType() const;
[email protected]729eae682011-06-17 18:24:5962
[email protected]52c788232011-05-23 22:51:3363 // Return the current sync datatype specifics.
64 const sync_pb::EntitySpecifics& GetSpecifics() const;
[email protected]729eae682011-06-17 18:24:5965
[email protected]52c788232011-05-23 22:51:3366 // 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]729eae682011-06-17 18:24:5969
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]0f9842d612011-09-20 14:26:5574 // Should only be called by sync code when IsLocal() is false.
75 int64 GetRemoteId() const;
76
[email protected]52c788232011-05-23 22:51:3377 // Whether this sync data is for local data or data coming from the syncer.
78 bool IsLocal() const;
79
[email protected]2d05d1482011-09-23 00:34:3680 std::string ToString() const;
81
[email protected]729eae682011-06-17 18:24:5982 // TODO(zea): Query methods for other sync properties: parent, successor, etc.
[email protected]52c788232011-05-23 22:51:3383
84 private:
[email protected]fb16d7f92011-09-16 04:55:3985 // 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]52c788232011-05-23 22:51:3389
[email protected]fb16d7f92011-09-16 04:55:3990 static void InitializeWrapper(Wrapper* wrapper);
[email protected]52c788232011-05-23 22:51:3391
[email protected]fb16d7f92011-09-16 04:55:3992 static void DestroyWrapper(Wrapper* wrapper);
[email protected]52c788232011-05-23 22:51:3393
[email protected]fb16d7f92011-09-16 04:55:3994 static const sync_pb::SyncEntity& Unwrap(const Wrapper& wrapper);
[email protected]52c788232011-05-23 22:51:3395
[email protected]fb16d7f92011-09-16 04:55:3996 static sync_pb::SyncEntity* UnwrapMutable(Wrapper* wrapper);
97
98 static void Swap(sync_pb::SyncEntity* t1, sync_pb::SyncEntity* t2);
[email protected]52c788232011-05-23 22:51:3399 };
100
[email protected]d45f0d92012-07-20 17:25:41101 typedef Immutable<sync_pb::SyncEntity, ImmutableSyncEntityTraits>
[email protected]fb16d7f92011-09-16 04:55:39102 ImmutableSyncEntity;
103
104 // Clears |entity|.
[email protected]0f9842d612011-09-20 14:26:55105 SyncData(int64 id, sync_pb::SyncEntity* entity);
[email protected]fb16d7f92011-09-16 04:55:39106
107 // Whether this SyncData holds valid data.
108 bool is_valid_;
[email protected]52c788232011-05-23 22:51:33109
[email protected]d45f0d92012-07-20 17:25:41110 // Equal to kInvalidId iff this is local.
[email protected]0f9842d612011-09-20 14:26:55111 int64 id_;
[email protected]fb16d7f92011-09-16 04:55:39112
113 // The actual shared sync entity being held.
114 ImmutableSyncEntity immutable_entity_;
[email protected]52c788232011-05-23 22:51:33115};
116
[email protected]2d05d1482011-09-23 00:34:36117// gmock printer helper.
118void PrintTo(const SyncData& sync_data, std::ostream* os);
119
[email protected]2ee9fa22013-03-06 22:43:00120typedef std::vector<SyncData> SyncDataList;
121
[email protected]65f173552012-06-28 22:43:58122} // namespace syncer
[email protected]cb02f612012-06-27 03:15:50123
[email protected]895a1e52012-05-15 02:50:12124#endif // SYNC_API_SYNC_DATA_H_