OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "sync/internal_api/public/base/invalidation.h" | 5 #include "sync/internal_api/public/base/invalidation.h" |
6 | 6 |
| 7 #include <cstddef> |
| 8 #include "base/rand_util.h" |
| 9 #include "base/string_number_conversions.h" |
7 #include "base/values.h" | 10 #include "base/values.h" |
8 | 11 |
9 namespace syncer { | 12 namespace syncer { |
10 | 13 |
11 bool AckHandle::Equals(const AckHandle& other) const { | 14 namespace { |
12 return true; | 15 // Hopefully enough bytes for uniqueness. |
| 16 const size_t kBytesInHandle = 16; |
| 17 } // namespace |
| 18 |
| 19 AckHandle AckHandle::CreateUnique() { |
| 20 // This isn't a valid UUID, so we don't attempt to format it like one. |
| 21 uint8_t random_bytes[kBytesInHandle]; |
| 22 base::RandBytes(random_bytes, sizeof(random_bytes)); |
| 23 return AckHandle(base::HexEncode(random_bytes, sizeof(random_bytes))); |
13 } | 24 } |
14 | 25 |
15 scoped_ptr<base::Value> AckHandle::ToValue() const { | 26 AckHandle AckHandle::InvalidAckHandle() { |
16 return scoped_ptr<base::Value>(new DictionaryValue()); | 27 return AckHandle(std::string()); |
17 } | 28 } |
18 | 29 |
19 bool AckHandle::ResetFromValue(const base::Value& value) { | 30 bool AckHandle::Equals(const AckHandle& other) const { |
20 return true; | 31 return state_ == other.state_; |
| 32 } |
| 33 |
| 34 scoped_ptr<base::DictionaryValue> AckHandle::ToValue() const { |
| 35 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
| 36 value->SetString("state", state_); |
| 37 return value.Pass(); |
| 38 } |
| 39 |
| 40 bool AckHandle::ResetFromValue(const base::DictionaryValue& value) { |
| 41 return value.GetString("state", &state_); |
| 42 } |
| 43 |
| 44 bool AckHandle::IsValid() const { |
| 45 return !state_.empty(); |
| 46 } |
| 47 |
| 48 AckHandle::AckHandle(const std::string& state) : state_(state) { |
| 49 } |
| 50 |
| 51 Invalidation::Invalidation() |
| 52 : ack_handle(AckHandle::InvalidAckHandle()) { |
21 } | 53 } |
22 | 54 |
23 bool Invalidation::Equals(const Invalidation& other) const { | 55 bool Invalidation::Equals(const Invalidation& other) const { |
24 return (payload == other.payload) && ack_handle.Equals(other.ack_handle); | 56 return (payload == other.payload) && ack_handle.Equals(other.ack_handle); |
25 } | 57 } |
26 | 58 |
27 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { | 59 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { |
28 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 60 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
29 value->SetString("payload", payload); | 61 value->SetString("payload", payload); |
30 value->Set("ackHandle", ack_handle.ToValue().release()); | 62 value->Set("ackHandle", ack_handle.ToValue().release()); |
31 return value.Pass(); | 63 return value.Pass(); |
32 } | 64 } |
33 | 65 |
34 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { | 66 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { |
35 const base::Value* ack_handle_value = NULL; | 67 const DictionaryValue* ack_handle_value = NULL; |
36 return | 68 return |
37 value.GetString("payload", &payload) && | 69 value.GetString("payload", &payload) && |
38 value.Get("ackHandle", &ack_handle_value) && | 70 value.GetDictionary("ackHandle", &ack_handle_value) && |
39 ack_handle.ResetFromValue(*ack_handle_value); | 71 ack_handle.ResetFromValue(*ack_handle_value); |
40 } | 72 } |
41 | 73 |
42 } // namespace syncer | 74 } // namespace syncer |
OLD | NEW |