Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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> | 7 #include <cstddef> |
| 8 | |
| 9 #include "base/json/json_string_value_serializer.h" | |
| 8 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "sync/notifier/invalidation_util.h" | |
| 11 | 14 |
| 12 namespace syncer { | 15 namespace syncer { |
| 13 | 16 |
| 14 namespace { | 17 namespace { |
| 15 // Hopefully enough bytes for uniqueness. | 18 const char kObjectIdKey[] = "objectId"; |
| 16 const size_t kBytesInHandle = 16; | 19 const char kIsUnknownVersionKey[] = "isUnknownVersion"; |
| 17 } // namespace | 20 const char kVersionKey[] = "version"; |
| 18 | 21 const char kPayloadKey[] = "payload"; |
| 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 } | 22 } |
| 26 | 23 |
| 27 AckHandle AckHandle::InvalidAckHandle() { | 24 Invalidation Invalidation::Init( |
| 28 return AckHandle(std::string(), base::Time()); | 25 const invalidation::ObjectId& id, |
| 26 int64 version, | |
| 27 const std::string& payload) { | |
| 28 return Invalidation(id, false, version, payload, AckHandle::CreateUnique()); | |
| 29 } | 29 } |
| 30 | 30 |
| 31 bool AckHandle::Equals(const AckHandle& other) const { | 31 Invalidation Invalidation::InitUnknownVersion( |
| 32 return state_ == other.state_ && timestamp_ == other.timestamp_; | 32 const invalidation::ObjectId& id) { |
| 33 return Invalidation(id, true, -1, "", AckHandle::CreateUnique()); | |
|
tim (not reviewing)
2013/09/20 21:53:46
std::string vs ""
rlarocque
2013/09/23 18:38:19
Done.
| |
| 33 } | 34 } |
| 34 | 35 |
| 35 scoped_ptr<base::DictionaryValue> AckHandle::ToValue() const { | 36 Invalidation::Invalidation() |
| 36 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | 37 : ack_handle_(AckHandle::InvalidAckHandle()) {} |
| 37 value->SetString("state", state_); | 38 |
| 38 value->SetString("timestamp", | 39 Invalidation::~Invalidation() {} |
| 39 base::Int64ToString(timestamp_.ToInternalValue())); | 40 |
| 40 return value.Pass(); | 41 invalidation::ObjectId Invalidation::GetObjectId() const { |
|
tim (not reviewing)
2013/09/20 21:53:46
style guide would have this called id(). Or objec
rlarocque
2013/09/23 18:38:19
True. Lately, I've been reluctant to follow that
| |
| 42 return id_; | |
| 41 } | 43 } |
| 42 | 44 |
| 43 bool AckHandle::ResetFromValue(const base::DictionaryValue& value) { | 45 bool Invalidation::IsUnknownVersion() const { |
| 44 if (!value.GetString("state", &state_)) | 46 return is_unknown_version_; |
| 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); | |
| 53 return true; | |
| 54 } | 47 } |
| 55 | 48 |
| 56 bool AckHandle::IsValid() const { | 49 int64 Invalidation::GetVersion() const { |
| 57 return !state_.empty(); | 50 DCHECK(!is_unknown_version_); |
|
tim (not reviewing)
2013/09/20 21:53:46
should be named version()... this is logically jus
rlarocque
2013/09/23 18:38:19
Done.
| |
| 51 return version_; | |
| 58 } | 52 } |
| 59 | 53 |
| 60 AckHandle::AckHandle(const std::string& state, base::Time timestamp) | 54 const std::string& Invalidation::GetPayload() const { |
| 61 : state_(state), timestamp_(timestamp) { | 55 DCHECK(!is_unknown_version_); |
| 56 return payload_; | |
| 62 } | 57 } |
| 63 | 58 |
| 64 AckHandle::~AckHandle() { | 59 const AckHandle& Invalidation::GetAckHandle() const { |
| 60 return ack_handle_; | |
| 65 } | 61 } |
| 66 | 62 |
| 67 const int64 Invalidation::kUnknownVersion = -1; | 63 void Invalidation::SetAckHandle(const AckHandle& ack_handle) { |
| 68 | 64 ack_handle_ = ack_handle; |
| 69 Invalidation::Invalidation() | |
| 70 : version(kUnknownVersion), ack_handle(AckHandle::InvalidAckHandle()) { | |
| 71 } | 65 } |
| 72 | 66 |
| 73 Invalidation::~Invalidation() { | 67 bool Invalidation::operator==(const Invalidation& other) const { |
| 74 } | 68 return id_ == other.id_ |
| 75 | 69 && is_unknown_version_ == other.is_unknown_version_ |
| 76 bool Invalidation::Equals(const Invalidation& other) const { | 70 && version_ == other.version_ |
| 77 return (version == other.version) && (payload == other.payload) && | 71 && payload_ == other.payload_; |
|
tim (not reviewing)
2013/09/20 21:53:46
You're not including ack_handle_, was that intenti
rlarocque
2013/09/23 18:38:19
It was more or less intentional. The decision wil
tim (not reviewing)
2013/09/24 21:16:54
That makes sense. However in general, and especia
rlarocque
2013/09/25 00:40:00
Fixed. I had to add some boilerplate to get some
| |
| 78 ack_handle.Equals(other.ack_handle); | |
| 79 } | 72 } |
| 80 | 73 |
| 81 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { | 74 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { |
| 82 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | 75 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| 83 value->SetString("version", base::Int64ToString(version)); | 76 value->Set(kObjectIdKey, ObjectIdToValue(id_).release()); |
| 84 value->SetString("payload", payload); | 77 if (is_unknown_version_) { |
| 85 value->Set("ackHandle", ack_handle.ToValue().release()); | 78 value->SetBoolean(kIsUnknownVersionKey, true); |
| 79 } else { | |
| 80 value->SetBoolean(kIsUnknownVersionKey, false); | |
| 81 value->SetString(kVersionKey, base::Int64ToString(version_)); | |
| 82 value->SetString(kPayloadKey, payload_); | |
| 83 } | |
| 86 return value.Pass(); | 84 return value.Pass(); |
| 87 } | 85 } |
| 88 | 86 |
| 89 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { | 87 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { |
| 90 const base::DictionaryValue* ack_handle_value = NULL; | 88 const base::DictionaryValue* object_id_dict; |
| 91 std::string version_as_string; | 89 if (!value.GetDictionary(kObjectIdKey, &object_id_dict) |
| 92 if (value.GetString("version", &version_as_string)) { | 90 || !ObjectIdFromValue(*object_id_dict, &id_)) { |
| 93 if (!base::StringToInt64(version_as_string, &version)) | 91 DLOG(WARNING) << "Failed to parse id"; |
| 92 return false; | |
| 93 } | |
| 94 if (!value.GetBoolean(kIsUnknownVersionKey, &is_unknown_version_)) { | |
| 95 DLOG(WARNING) << "Failed to parse is_unknown_version flag"; | |
| 96 return false; | |
| 97 } | |
| 98 if (is_unknown_version_) { | |
| 99 version_ = -1; | |
| 100 payload_ = ""; | |
|
tim (not reviewing)
2013/09/20 21:53:46
std::string
rlarocque
2013/09/23 18:38:19
Done.
| |
| 101 } else { | |
| 102 std::string version_as_string; | |
| 103 if (!value.GetString(kVersionKey, &version_as_string) | |
| 104 || !base::StringToInt64(version_as_string, &version_)) { | |
| 105 DLOG(WARNING) << "Failed to parse version"; | |
| 94 return false; | 106 return false; |
| 95 } else { | 107 } |
| 96 version = kUnknownVersion; | 108 if (!value.GetString(kPayloadKey, &payload_)) { |
| 109 DLOG(WARNING) << "Failed to parse payload"; | |
| 110 return false; | |
| 111 } | |
| 97 } | 112 } |
| 98 return | 113 return true; |
| 99 value.GetString("payload", &payload) && | |
| 100 value.GetDictionary("ackHandle", &ack_handle_value) && | |
| 101 ack_handle.ResetFromValue(*ack_handle_value); | |
| 102 } | 114 } |
| 103 | 115 |
| 116 std::string Invalidation::ToString() const { | |
| 117 std::string output; | |
| 118 JSONStringValueSerializer serializer(&output); | |
| 119 serializer.set_pretty_print(true); | |
| 120 serializer.Serialize(*ToValue().get()); | |
| 121 return output; | |
| 122 } | |
| 123 | |
| 124 Invalidation::Invalidation( | |
| 125 const invalidation::ObjectId& id, | |
| 126 bool is_unknown_version, | |
| 127 int64 version, | |
| 128 const std::string& payload, | |
| 129 AckHandle ack_handle) | |
| 130 : id_(id), | |
| 131 is_unknown_version_(is_unknown_version), | |
| 132 version_(version), | |
| 133 payload_(payload), | |
| 134 ack_handle_(ack_handle) {} | |
| 135 | |
| 104 } // namespace syncer | 136 } // namespace syncer |
| OLD | NEW |