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 // This is not a valid handle name, since all valid handles are 16 bytes long. |
| 18 const char kInvalidHandleName[] = "INVALIDHANDLENAME"; |
| 19 COMPILE_ASSERT(kBytesInHandle != arraysize(kInvalidHandleName), |
| 20 invalid_handle_name_must_differ_in_length_from_valid_handles); |
| 21 } // namespace |
| 22 |
| 23 AckHandle AckHandle::CreateUnique() { |
| 24 // This isn't a valid UUID, so we don't attempt to format it like one. |
| 25 uint8_t random_bytes[kBytesInHandle]; |
| 26 base::RandBytes(random_bytes, sizeof(random_bytes)); |
| 27 return AckHandle(base::HexEncode(random_bytes, sizeof(random_bytes))); |
13 } | 28 } |
14 | 29 |
15 scoped_ptr<base::Value> AckHandle::ToValue() const { | 30 AckHandle AckHandle::InvalidAckHandle() { |
16 return scoped_ptr<base::Value>(new DictionaryValue()); | 31 return AckHandle(kInvalidHandleName); |
17 } | 32 } |
18 | 33 |
19 bool AckHandle::ResetFromValue(const base::Value& value) { | 34 bool AckHandle::Equals(const AckHandle& other) const { |
20 return true; | 35 return state_ == other.state_; |
| 36 } |
| 37 |
| 38 scoped_ptr<base::DictionaryValue> AckHandle::ToValue() const { |
| 39 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
| 40 value->SetString("state", state_); |
| 41 return value.Pass(); |
| 42 } |
| 43 |
| 44 bool AckHandle::ResetFromValue(const base::DictionaryValue& value) { |
| 45 return value.GetString("state", &state_); |
| 46 } |
| 47 |
| 48 bool AckHandle::IsValid() const { |
| 49 return state_ != kInvalidHandleName; |
| 50 } |
| 51 |
| 52 AckHandle::AckHandle(const std::string& state) : state_(state) { |
| 53 } |
| 54 |
| 55 Invalidation::Invalidation() |
| 56 : ack_handle(AckHandle::InvalidAckHandle()) { |
21 } | 57 } |
22 | 58 |
23 bool Invalidation::Equals(const Invalidation& other) const { | 59 bool Invalidation::Equals(const Invalidation& other) const { |
24 return (payload == other.payload) && ack_handle.Equals(other.ack_handle); | 60 return (payload == other.payload) && ack_handle.Equals(other.ack_handle); |
25 } | 61 } |
26 | 62 |
27 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { | 63 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { |
28 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 64 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
29 value->SetString("payload", payload); | 65 value->SetString("payload", payload); |
30 value->Set("ackHandle", ack_handle.ToValue().release()); | 66 value->Set("ackHandle", ack_handle.ToValue().release()); |
31 return value.Pass(); | 67 return value.Pass(); |
32 } | 68 } |
33 | 69 |
34 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { | 70 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { |
35 const base::Value* ack_handle_value = NULL; | 71 const DictionaryValue* ack_handle_value = NULL; |
36 return | 72 return |
37 value.GetString("payload", &payload) && | 73 value.GetString("payload", &payload) && |
38 value.Get("ackHandle", &ack_handle_value) && | 74 value.GetDictionary("ackHandle", &ack_handle_value) && |
39 ack_handle.ResetFromValue(*ack_handle_value); | 75 ack_handle.ResetFromValue(*ack_handle_value); |
40 } | 76 } |
41 | 77 |
42 } // namespace syncer | 78 } // namespace syncer |
OLD | NEW |