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 // 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))); | |
28 } | |
29 | |
30 AckHandle AckHandle::InvalidAckHandle() { | |
31 return AckHandle(kInvalidHandleName); | |
32 } | |
33 | |
11 bool AckHandle::Equals(const AckHandle& other) const { | 34 bool AckHandle::Equals(const AckHandle& other) const { |
12 return true; | 35 return state_ == other.state_; |
13 } | 36 } |
14 | 37 |
15 scoped_ptr<base::Value> AckHandle::ToValue() const { | 38 scoped_ptr<base::Value> AckHandle::ToValue() const { |
16 return scoped_ptr<base::Value>(new DictionaryValue()); | 39 return scoped_ptr<base::Value>(new StringValue(state_)); |
akalin
2012/10/19 13:27:16
more future-proof to have a dictionary with a sing
dcheng
2012/10/19 19:38:11
Done.
| |
17 } | 40 } |
18 | 41 |
19 bool AckHandle::ResetFromValue(const base::Value& value) { | 42 bool AckHandle::ResetFromValue(const base::Value& value) { |
20 return true; | 43 // TODO(dcheng): Add debug mode sanity checking? |
44 return value.GetAsString(&state_); | |
45 } | |
46 | |
47 AckHandle::AckHandle(const std::string& state) : state_(state) { | |
48 } | |
49 | |
50 Invalidation::Invalidation() | |
51 : ack_handle(AckHandle::InvalidAckHandle()) { | |
21 } | 52 } |
22 | 53 |
23 bool Invalidation::Equals(const Invalidation& other) const { | 54 bool Invalidation::Equals(const Invalidation& other) const { |
24 return (payload == other.payload) && ack_handle.Equals(other.ack_handle); | 55 return (payload == other.payload) && ack_handle.Equals(other.ack_handle); |
25 } | 56 } |
26 | 57 |
27 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { | 58 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { |
28 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 59 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
29 value->SetString("payload", payload); | 60 value->SetString("payload", payload); |
30 value->Set("ackHandle", ack_handle.ToValue().release()); | 61 value->Set("ackHandle", ack_handle.ToValue().release()); |
31 return value.Pass(); | 62 return value.Pass(); |
32 } | 63 } |
33 | 64 |
34 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { | 65 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { |
35 const base::Value* ack_handle_value = NULL; | 66 const base::Value* ack_handle_value = NULL; |
36 return | 67 return |
37 value.GetString("payload", &payload) && | 68 value.GetString("payload", &payload) && |
38 value.Get("ackHandle", &ack_handle_value) && | 69 value.Get("ackHandle", &ack_handle_value) && |
39 ack_handle.ResetFromValue(*ack_handle_value); | 70 ack_handle.ResetFromValue(*ack_handle_value); |
40 } | 71 } |
41 | 72 |
42 } // namespace syncer | 73 } // namespace syncer |
OLD | NEW |