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 |
| 14 namespace { |
| 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 random_bytes[kBytesInHandle]; |
| 22 base::RandBytes(random_bytes, sizeof(random_bytes)); |
| 23 return AckHandle(base::HexEncode(random_bytes, sizeof(random_bytes)), |
| 24 base::Time::Now()); |
| 25 } |
| 26 |
| 27 AckHandle AckHandle::InvalidAckHandle() { |
| 28 return AckHandle(std::string(), base::Time()); |
| 29 } |
| 30 |
11 bool AckHandle::Equals(const AckHandle& other) const { | 31 bool AckHandle::Equals(const AckHandle& other) const { |
| 32 return state_ == other.state_ && timestamp_ == other.timestamp_; |
| 33 } |
| 34 |
| 35 scoped_ptr<base::DictionaryValue> AckHandle::ToValue() const { |
| 36 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
| 37 value->SetString("state", state_); |
| 38 value->SetString("timestamp", |
| 39 base::Int64ToString(timestamp_.ToInternalValue())); |
| 40 return value.Pass(); |
| 41 } |
| 42 |
| 43 bool AckHandle::ResetFromValue(const base::DictionaryValue& value) { |
| 44 if (!value.GetString("state", &state_)) |
| 45 return false; |
| 46 std::string timestamp_as_string; |
| 47 if (!value.GetString("timestamp", ×tamp_as_string)) |
| 48 return false; |
| 49 int64 timestamp_value; |
| 50 if (!base::StringToInt64(timestamp_as_string, ×tamp_value)) |
| 51 return false; |
| 52 timestamp_ = base::Time::FromInternalValue(timestamp_value); |
12 return true; | 53 return true; |
13 } | 54 } |
14 | 55 |
15 scoped_ptr<base::Value> AckHandle::ToValue() const { | 56 bool AckHandle::IsValid() const { |
16 return scoped_ptr<base::Value>(new DictionaryValue()); | 57 return !state_.empty(); |
17 } | 58 } |
18 | 59 |
19 bool AckHandle::ResetFromValue(const base::Value& value) { | 60 AckHandle::AckHandle(const std::string& state, base::Time timestamp) |
20 return true; | 61 : state_(state), timestamp_(timestamp) { |
| 62 } |
| 63 |
| 64 Invalidation::Invalidation() |
| 65 : ack_handle(AckHandle::InvalidAckHandle()) { |
21 } | 66 } |
22 | 67 |
23 bool Invalidation::Equals(const Invalidation& other) const { | 68 bool Invalidation::Equals(const Invalidation& other) const { |
24 return (payload == other.payload) && ack_handle.Equals(other.ack_handle); | 69 return (payload == other.payload) && ack_handle.Equals(other.ack_handle); |
25 } | 70 } |
26 | 71 |
27 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { | 72 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { |
28 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 73 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
29 value->SetString("payload", payload); | 74 value->SetString("payload", payload); |
30 value->Set("ackHandle", ack_handle.ToValue().release()); | 75 value->Set("ackHandle", ack_handle.ToValue().release()); |
31 return value.Pass(); | 76 return value.Pass(); |
32 } | 77 } |
33 | 78 |
34 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { | 79 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { |
35 const base::Value* ack_handle_value = NULL; | 80 const DictionaryValue* ack_handle_value = NULL; |
36 return | 81 return |
37 value.GetString("payload", &payload) && | 82 value.GetString("payload", &payload) && |
38 value.Get("ackHandle", &ack_handle_value) && | 83 value.GetDictionary("ackHandle", &ack_handle_value) && |
39 ack_handle.ResetFromValue(*ack_handle_value); | 84 ack_handle.ResetFromValue(*ack_handle_value); |
40 } | 85 } |
41 | 86 |
42 } // namespace syncer | 87 } // namespace syncer |
OLD | NEW |