Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(817)

Side by Side Diff: sync/internal_api/public/base/invalidation.cc

Issue 23754021: Invalidation trickles mega-patch (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/dropped_invalidation_tracker.h"
14 #include "sync/notifier/invalidation_util.h"
11 15
12 namespace syncer { 16 namespace syncer {
13 17
14 namespace { 18 namespace {
15 // Hopefully enough bytes for uniqueness. 19 const char kObjectIdKey[] = "objectId";
16 const size_t kBytesInHandle = 16; 20 const char kIsUnknownVersionKey[] = "isUnknownVersion";
17 } // namespace 21 const char kVersionKey[] = "version";
18 22 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 } 23 }
26 24
27 AckHandle AckHandle::InvalidAckHandle() { 25 Invalidation Invalidation::Init(
28 return AckHandle(std::string(), base::Time()); 26 const invalidation::ObjectId& id,
27 int64 version,
28 const std::string& payload) {
29 return Invalidation(id, false, version, payload, AckHandle::CreateUnique());
29 } 30 }
30 31
31 bool AckHandle::Equals(const AckHandle& other) const { 32 Invalidation Invalidation::InitUnknownVersion(
32 return state_ == other.state_ && timestamp_ == other.timestamp_; 33 const invalidation::ObjectId& id) {
34 return Invalidation(id, true, -1, "", AckHandle::CreateUnique());
33 } 35 }
34 36
35 scoped_ptr<base::DictionaryValue> AckHandle::ToValue() const { 37 Invalidation Invalidation::InitFromDroppedInvalidation(
36 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); 38 const Invalidation& dropped) {
37 value->SetString("state", state_); 39 return Invalidation(dropped.id_, true, -1, "", dropped.ack_handle_);
38 value->SetString("timestamp",
39 base::Int64ToString(timestamp_.ToInternalValue()));
40 return value.Pass();
41 } 40 }
42 41
43 bool AckHandle::ResetFromValue(const base::DictionaryValue& value) { 42 Invalidation::Invalidation()
44 if (!value.GetString("state", &state_)) 43 : ack_handle_(AckHandle::InvalidAckHandle()) {}
45 return false; 44
46 std::string timestamp_as_string; 45 Invalidation::~Invalidation() {}
47 if (!value.GetString("timestamp", &timestamp_as_string)) 46
48 return false; 47 invalidation::ObjectId Invalidation::GetObjectId() const {
49 int64 timestamp_value; 48 return id_;
50 if (!base::StringToInt64(timestamp_as_string, &timestamp_value))
51 return false;
52 timestamp_ = base::Time::FromInternalValue(timestamp_value);
53 return true;
54 } 49 }
55 50
56 bool AckHandle::IsValid() const { 51 bool Invalidation::IsUnknownVersion() const {
57 return !state_.empty(); 52 return is_unknown_version_;
58 } 53 }
59 54
60 AckHandle::AckHandle(const std::string& state, base::Time timestamp) 55 int64 Invalidation::GetVersion() const {
61 : state_(state), timestamp_(timestamp) { 56 DCHECK(!is_unknown_version_);
57 return version_;
62 } 58 }
63 59
64 AckHandle::~AckHandle() { 60 const std::string& Invalidation::GetPayload() const {
61 DCHECK(!is_unknown_version_);
62 return payload_;
65 } 63 }
66 64
67 const int64 Invalidation::kUnknownVersion = -1; 65 const AckHandle& Invalidation::GetAckHandle() const {
68 66 return ack_handle_;
69 Invalidation::Invalidation()
70 : version(kUnknownVersion), ack_handle(AckHandle::InvalidAckHandle()) {
71 } 67 }
72 68
73 Invalidation::~Invalidation() { 69 void Invalidation::SetAckHandler(syncer::WeakHandle<AckHandler> handler) {
70 ack_handler_ = handler;
74 } 71 }
75 72
76 bool Invalidation::Equals(const Invalidation& other) const { 73 bool Invalidation::SupportsAcknowledgement() const {
77 return (version == other.version) && (payload == other.payload) && 74 return ack_handler_.IsInitialized();
78 ack_handle.Equals(other.ack_handle); 75 }
76
77 void Invalidation::Acknowledge() const {
78 if (SupportsAcknowledgement()) {
79 ack_handler_.Call(FROM_HERE,
80 &AckHandler::Acknowledge,
81 id_,
82 ack_handle_);
83 }
84 }
85
86 void Invalidation::Drop(DroppedInvalidationTracker* tracker) const {
87 DCHECK(tracker->GetObjectId() == GetObjectId());
88 tracker->Drop(ack_handler_, ack_handle_);
89 }
90
91 bool Invalidation::operator==(const Invalidation& other) const {
92 return id_ == other.id_
93 && is_unknown_version_ == other.is_unknown_version_
94 && version_ == other.version_
95 && payload_ == other.payload_;
79 } 96 }
80 97
81 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { 98 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const {
82 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); 99 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
83 value->SetString("version", base::Int64ToString(version)); 100 value->Set(kObjectIdKey, ObjectIdToValue(id_).release());
84 value->SetString("payload", payload); 101 if (is_unknown_version_) {
85 value->Set("ackHandle", ack_handle.ToValue().release()); 102 value->SetBoolean(kIsUnknownVersionKey, true);
103 } else {
104 value->SetBoolean(kIsUnknownVersionKey, false);
105 value->SetString(kVersionKey, base::Int64ToString(version_));
106 value->SetString(kPayloadKey, payload_);
107 }
86 return value.Pass(); 108 return value.Pass();
87 } 109 }
88 110
89 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) { 111 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) {
90 const base::DictionaryValue* ack_handle_value = NULL; 112 const base::DictionaryValue* object_id_dict;
91 std::string version_as_string; 113 if (!value.GetDictionary(kObjectIdKey, &object_id_dict)
92 if (value.GetString("version", &version_as_string)) { 114 || !ObjectIdFromValue(*object_id_dict, &id_)) {
93 if (!base::StringToInt64(version_as_string, &version)) 115 DLOG(WARNING) << "Failed to parse id";
116 return false;
117 }
118 if (!value.GetBoolean(kIsUnknownVersionKey, &is_unknown_version_)) {
119 DLOG(WARNING) << "Failed to parse is_unknown_version flag";
120 return false;
121 }
122 if (is_unknown_version_) {
123 version_ = -1;
124 payload_ = "";
125 } else {
126 std::string version_as_string;
127 if (!value.GetString(kVersionKey, &version_as_string)
128 || !base::StringToInt64(version_as_string, &version_)) {
129 DLOG(WARNING) << "Failed to parse version";
94 return false; 130 return false;
95 } else { 131 }
96 version = kUnknownVersion; 132 if (!value.GetString(kPayloadKey, &payload_)) {
133 DLOG(WARNING) << "Failed to parse payload";
134 return false;
135 }
97 } 136 }
98 return 137 return true;
99 value.GetString("payload", &payload) &&
100 value.GetDictionary("ackHandle", &ack_handle_value) &&
101 ack_handle.ResetFromValue(*ack_handle_value);
102 } 138 }
103 139
140 std::string Invalidation::ToString() const {
141 std::string output;
142 JSONStringValueSerializer serializer(&output);
143 serializer.set_pretty_print(true);
144 serializer.Serialize(*ToValue().get());
145 return output;
146 }
147
148 Invalidation::Invalidation(
149 const invalidation::ObjectId& id,
150 bool is_unknown_version,
151 int64 version,
152 const std::string& payload,
153 AckHandle ack_handle)
154 : id_(id),
155 is_unknown_version_(is_unknown_version),
156 version_(version),
157 payload_(payload),
158 ack_handle_(ack_handle) {}
159
104 } // namespace syncer 160 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/internal_api/public/base/invalidation.h ('k') | sync/internal_api/public/base/invalidation_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698