summaryrefslogtreecommitdiffstats
path: root/sync/engine/syncproto.h
blob: 0f4e21c85faf07380a77635e87d76e3a49dea2e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Wrappers to help us work with ids and protobuffers.

#ifndef SYNC_ENGINE_SYNCPROTO_H_
#define SYNC_ENGINE_SYNCPROTO_H_
#pragma once

#include "sync/internal_api/public/syncable/model_type.h"
#include "sync/protocol/sync.pb.h"
#include "sync/syncable/syncable_id.h"

namespace browser_sync {

template<class Base>
class IdWrapper : public Base {
 public:
  IdWrapper() {}
  explicit IdWrapper(const Base& other) : Base(other) {
  }
  syncable::Id id() const {
    return syncable::Id::CreateFromServerId(Base::id_string());
  }
  void set_id(const syncable::Id& id) {
    Base::set_id_string(id.GetServerId());
  }
};

// These wrapper classes contain no data, so their super classes can be cast to
// them directly.
class SyncEntity : public IdWrapper<sync_pb::SyncEntity> {
 public:
  SyncEntity() {}
  explicit SyncEntity(const sync_pb::SyncEntity& other)
      : IdWrapper<sync_pb::SyncEntity>(other) {
  }

  void set_parent_id(const syncable::Id& id) {
    set_parent_id_string(id.GetServerId());
  }
  syncable::Id parent_id() const {
    return syncable::Id::CreateFromServerId(parent_id_string());
  }
  void set_old_parent_id(const syncable::Id& id) {
    IdWrapper<sync_pb::SyncEntity>::set_old_parent_id(
      id.GetServerId());
  }
  syncable::Id old_parent_id() const {
    return syncable::Id::CreateFromServerId(
      sync_pb::SyncEntity::old_parent_id());
  }
  // Binary predicate helper to determine whether an Entity represents a folder
  // or non-folder object. Use this instead of checking these properties
  // directly, because the addition of bookmarks to the protobuf schema
  // makes the check slightly more tricky.
  bool IsFolder() const {
    return ((has_folder() && folder()) ||
            (has_bookmarkdata() && bookmarkdata().bookmark_folder()));
  }

  syncable::ModelType GetModelType() const {
    return syncable::GetModelType(*this);
  }
};

class CommitResponse_EntryResponse
    : public IdWrapper<sync_pb::CommitResponse_EntryResponse> {
};

class ClientToServerMessage : public sync_pb::ClientToServerMessage {
 public:
  ClientToServerMessage() {
    set_protocol_version(protocol_version());
  }
};

typedef sync_pb::CommitMessage CommitMessage;
typedef sync_pb::ClientToServerResponse ClientToServerResponse;
typedef sync_pb::CommitResponse CommitResponse;
typedef sync_pb::GetUpdatesResponse GetUpdatesResponse;
typedef sync_pb::GetUpdatesMessage GetUpdatesMessage;

}  // namespace browser_sync

#endif  // SYNC_ENGINE_SYNCPROTO_H_