OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "sync/api/attachments/attachment_id.h" | 5 #include "sync/api/attachments/attachment_id.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "sync/protocol/sync.pb.h" |
9 | 10 |
10 namespace syncer { | 11 namespace syncer { |
11 | 12 |
| 13 void AttachmentId::ImmutableAttachmentIdProtoTraits::InitializeWrapper( |
| 14 Wrapper* wrapper) { |
| 15 *wrapper = new sync_pb::AttachmentIdProto(); |
| 16 } |
| 17 |
| 18 void AttachmentId::ImmutableAttachmentIdProtoTraits::DestroyWrapper( |
| 19 Wrapper* wrapper) { |
| 20 delete *wrapper; |
| 21 } |
| 22 |
| 23 const sync_pb::AttachmentIdProto& |
| 24 AttachmentId::ImmutableAttachmentIdProtoTraits::Unwrap(const Wrapper& wrapper) { |
| 25 return *wrapper; |
| 26 } |
| 27 |
| 28 sync_pb::AttachmentIdProto* |
| 29 AttachmentId::ImmutableAttachmentIdProtoTraits::UnwrapMutable( |
| 30 Wrapper* wrapper) { |
| 31 return *wrapper; |
| 32 } |
| 33 |
| 34 void AttachmentId::ImmutableAttachmentIdProtoTraits::Swap( |
| 35 sync_pb::AttachmentIdProto* t1, |
| 36 sync_pb::AttachmentIdProto* t2) { |
| 37 t1->Swap(t2); |
| 38 } |
| 39 |
12 AttachmentId::~AttachmentId() {} | 40 AttachmentId::~AttachmentId() {} |
13 | 41 |
14 bool AttachmentId::operator==(const AttachmentId& other) const { | 42 bool AttachmentId::operator==(const AttachmentId& other) const { |
15 return unique_id_ == other.unique_id_; | 43 return proto_.Get().unique_id() == other.proto_.Get().unique_id(); |
16 } | 44 } |
17 | 45 |
18 bool AttachmentId::operator!=(const AttachmentId& other) const { | 46 bool AttachmentId::operator!=(const AttachmentId& other) const { |
19 return !operator==(other); | 47 return !operator==(other); |
20 } | 48 } |
21 | 49 |
22 bool AttachmentId::operator<(const AttachmentId& other) const { | 50 bool AttachmentId::operator<(const AttachmentId& other) const { |
23 return unique_id_ < other.unique_id_; | 51 return proto_.Get().unique_id() < other.proto_.Get().unique_id(); |
24 } | 52 } |
25 | 53 |
26 // Static. | 54 // Static. |
27 AttachmentId AttachmentId::Create() { | 55 AttachmentId AttachmentId::Create() { |
28 // Only requirement here is that this id must be globally unique. | 56 // Only requirement here is that this id must be globally unique. |
29 // TODO(maniscalco): Consider making this base64 encoded. | 57 // TODO(maniscalco): Consider making this base64 encoded. |
30 return AttachmentId(base::RandBytesAsString(16)); | 58 sync_pb::AttachmentIdProto proto; |
| 59 proto.set_unique_id(base::RandBytesAsString(16)); |
| 60 return AttachmentId(&proto); |
31 } | 61 } |
32 | 62 |
33 AttachmentId::AttachmentId(const std::string& unique_id) | 63 // Static. |
34 : unique_id_(unique_id) { | 64 AttachmentId AttachmentId::CreateFromProto( |
35 DCHECK(!unique_id_.empty()); | 65 const sync_pb::AttachmentIdProto& proto) { |
| 66 sync_pb::AttachmentIdProto copy_of_proto(proto); |
| 67 return AttachmentId(©_of_proto); |
36 } | 68 } |
37 | 69 |
| 70 const sync_pb::AttachmentIdProto& AttachmentId::GetProto() const { |
| 71 return proto_.Get(); |
| 72 } |
| 73 |
| 74 AttachmentId::AttachmentId(sync_pb::AttachmentIdProto* proto) |
| 75 : proto_(proto) {} |
| 76 |
38 } // namespace syncer | 77 } // namespace syncer |
OLD | NEW |