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_state.h" | 5 #include "sync/internal_api/public/base/invalidation_state.h" |
6 | 6 |
7 #include "base/rand_util.h" | |
7 #include "base/values.h" | 8 #include "base/values.h" |
8 | 9 |
9 namespace syncer { | 10 namespace syncer { |
10 | 11 |
12 namespace { | |
13 // Hopefully enough bytes for uniqueness. | |
14 const size_t kBytesInHandle = 16; | |
akalin
2012/09/17 23:59:54
include cstddef for size_t
dcheng
2012/09/25 22:35:06
Done.
| |
15 // This is not a valid handle name, since all valid handles are 16 bytes long. | |
16 const char kInvalidHandleName[] = "INVALIDHANDLENAME"; | |
17 COMPILE_ASSERT(kBytesInHandle != arraysize(kInvalidHandleName), | |
18 invalid_handle_name_must_differ_in_length_from_valid_handles); | |
19 } // namespace | |
20 | |
21 AckHandle AckHandle::CreateUnique() { | |
22 // We don't have a UUID generator, or else it'd be nice to use that instead. | |
23 // TODO(dcheng): Do we want to base64 encode this? | |
akalin
2012/09/17 23:59:54
i think it would be nice to have a readable ackhan
dcheng
2012/09/25 22:35:06
How about formatting it as an UUID? My original ra
akalin
2012/10/01 23:23:08
Yeah, the standard doesn't require keeping space f
| |
24 return AckHandle(base::RandBytesAsString(kBytesInHandle)); | |
25 } | |
26 | |
27 AckHandle AckHandle::InvalidAckHandle() { | |
28 return AckHandle(kInvalidHandleName); | |
29 } | |
30 | |
11 bool AckHandle::Equals(const AckHandle& other) const { | 31 bool AckHandle::Equals(const AckHandle& other) const { |
12 return true; | 32 return state_ == other.state_; |
13 } | 33 } |
14 | 34 |
15 scoped_ptr<base::Value> AckHandle::ToValue() const { | 35 scoped_ptr<base::Value> AckHandle::ToValue() const { |
akalin
2012/09/17 23:59:54
return a StringValue?
dcheng
2012/09/25 22:35:06
It's actually more convenient to keep using base::
akalin
2012/10/01 23:23:08
True. Fair enough.
| |
16 return scoped_ptr<base::Value>(new DictionaryValue()); | 36 return scoped_ptr<base::Value>(new StringValue(state_)); |
17 } | 37 } |
18 | 38 |
19 bool AckHandle::ResetFromValue(const base::Value& value) { | 39 bool AckHandle::ResetFromValue(const base::Value& value) { |
akalin
2012/09/17 23:59:54
take a StringValue?
dcheng
2012/09/25 22:35:06
See above.
| |
20 return true; | 40 // TODO(dcheng): Add debug mode sanity checking? |
41 return value.GetAsString(&state_); | |
42 } | |
43 | |
44 AckHandle::AckHandle(const std::string& state) : state_(state) { | |
45 } | |
46 | |
47 InvalidationState::InvalidationState() | |
48 : ack_handle(AckHandle::InvalidAckHandle()) { | |
21 } | 49 } |
22 | 50 |
23 bool InvalidationState::Equals(const InvalidationState& other) const { | 51 bool InvalidationState::Equals(const InvalidationState& other) const { |
24 return (payload == other.payload) && ack_handle.Equals(other.ack_handle); | 52 return (payload == other.payload) && ack_handle.Equals(other.ack_handle); |
25 } | 53 } |
26 | 54 |
27 scoped_ptr<base::DictionaryValue> InvalidationState::ToValue() const { | 55 scoped_ptr<base::DictionaryValue> InvalidationState::ToValue() const { |
28 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 56 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
29 value->SetString("payload", payload); | 57 value->SetString("payload", payload); |
30 value->Set("ackHandle", ack_handle.ToValue().release()); | 58 value->Set("ackHandle", ack_handle.ToValue().release()); |
31 return value.Pass(); | 59 return value.Pass(); |
32 } | 60 } |
33 | 61 |
34 bool InvalidationState::ResetFromValue(const base::DictionaryValue& value) { | 62 bool InvalidationState::ResetFromValue(const base::DictionaryValue& value) { |
35 const base::Value* ack_handle_value = NULL; | 63 const base::Value* ack_handle_value = NULL; |
36 return | 64 return |
37 value.GetString("payload", &payload) && | 65 value.GetString("payload", &payload) && |
38 value.Get("ackHandle", &ack_handle_value) && | 66 value.Get("ackHandle", &ack_handle_value) && |
39 ack_handle.ResetFromValue(*ack_handle_value); | 67 ack_handle.ResetFromValue(*ack_handle_value); |
40 } | 68 } |
41 | 69 |
42 } // namespace syncer | 70 } // namespace syncer |
OLD | NEW |