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

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

Issue 400073003: Finish decoupling sync and invalidations (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix GN Created 6 years, 5 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "sync/internal_api/public/base/invalidation.h"
6
7 #include <cstddef>
8
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/rand_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "sync/internal_api/public/base/ack_handler.h"
14 #include "sync/internal_api/public/base/invalidation_util.h"
15
16 namespace syncer {
17
18 namespace {
19 const char kObjectIdKey[] = "objectId";
20 const char kIsUnknownVersionKey[] = "isUnknownVersion";
21 const char kVersionKey[] = "version";
22 const char kPayloadKey[] = "payload";
23 const int64 kInvalidVersion = -1;
24 }
25
26 Invalidation Invalidation::Init(
27 const invalidation::ObjectId& id,
28 int64 version,
29 const std::string& payload) {
30 return Invalidation(id, false, version, payload, AckHandle::CreateUnique());
31 }
32
33 Invalidation Invalidation::InitUnknownVersion(
34 const invalidation::ObjectId& id) {
35 return Invalidation(id, true, kInvalidVersion,
36 std::string(), AckHandle::CreateUnique());
37 }
38
39 Invalidation Invalidation::InitFromDroppedInvalidation(
40 const Invalidation& dropped) {
41 return Invalidation(dropped.id_, true, kInvalidVersion,
42 std::string(), dropped.ack_handle_);
43 }
44
45 scoped_ptr<Invalidation> Invalidation::InitFromValue(
46 const base::DictionaryValue& value) {
47 invalidation::ObjectId id;
48
49 const base::DictionaryValue* object_id_dict;
50 if (!value.GetDictionary(kObjectIdKey, &object_id_dict)
51 || !ObjectIdFromValue(*object_id_dict, &id)) {
52 DLOG(WARNING) << "Failed to parse id";
53 return scoped_ptr<Invalidation>();
54 }
55 bool is_unknown_version;
56 if (!value.GetBoolean(kIsUnknownVersionKey, &is_unknown_version)) {
57 DLOG(WARNING) << "Failed to parse is_unknown_version flag";
58 return scoped_ptr<Invalidation>();
59 }
60 if (is_unknown_version) {
61 return scoped_ptr<Invalidation>(new Invalidation(
62 id,
63 true,
64 kInvalidVersion,
65 std::string(),
66 AckHandle::CreateUnique()));
67 }
68 int64 version = 0;
69 std::string version_as_string;
70 if (!value.GetString(kVersionKey, &version_as_string)
71 || !base::StringToInt64(version_as_string, &version)) {
72 DLOG(WARNING) << "Failed to parse version";
73 return scoped_ptr<Invalidation>();
74 }
75 std::string payload;
76 if (!value.GetString(kPayloadKey, &payload)) {
77 DLOG(WARNING) << "Failed to parse payload";
78 return scoped_ptr<Invalidation>();
79 }
80 return scoped_ptr<Invalidation>(new Invalidation(
81 id,
82 false,
83 version,
84 payload,
85 AckHandle::CreateUnique()));
86 }
87
88 Invalidation::~Invalidation() {}
89
90 invalidation::ObjectId Invalidation::object_id() const {
91 return id_;
92 }
93
94 bool Invalidation::is_unknown_version() const {
95 return is_unknown_version_;
96 }
97
98 int64 Invalidation::version() const {
99 DCHECK(!is_unknown_version_);
100 return version_;
101 }
102
103 const std::string& Invalidation::payload() const {
104 DCHECK(!is_unknown_version_);
105 return payload_;
106 }
107
108 const AckHandle& Invalidation::ack_handle() const {
109 return ack_handle_;
110 }
111
112 void Invalidation::set_ack_handler(syncer::WeakHandle<AckHandler> handler) {
113 ack_handler_ = handler;
114 }
115
116 bool Invalidation::SupportsAcknowledgement() const {
117 return ack_handler_.IsInitialized();
118 }
119
120 void Invalidation::Acknowledge() const {
121 if (SupportsAcknowledgement()) {
122 ack_handler_.Call(FROM_HERE,
123 &AckHandler::Acknowledge,
124 id_,
125 ack_handle_);
126 }
127 }
128
129 void Invalidation::Drop() {
130 if (SupportsAcknowledgement()) {
131 ack_handler_.Call(FROM_HERE,
132 &AckHandler::Drop,
133 id_,
134 ack_handle_);
135 }
136 }
137
138 bool Invalidation::Equals(const Invalidation& other) const {
139 return id_ == other.id_
140 && is_unknown_version_ == other.is_unknown_version_
141 && version_ == other.version_
142 && payload_ == other.payload_;
143 }
144
145 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const {
146 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
147 value->Set(kObjectIdKey, ObjectIdToValue(id_).release());
148 if (is_unknown_version_) {
149 value->SetBoolean(kIsUnknownVersionKey, true);
150 } else {
151 value->SetBoolean(kIsUnknownVersionKey, false);
152 value->SetString(kVersionKey, base::Int64ToString(version_));
153 value->SetString(kPayloadKey, payload_);
154 }
155 return value.Pass();
156 }
157
158 std::string Invalidation::ToString() const {
159 std::string output;
160 JSONStringValueSerializer serializer(&output);
161 serializer.set_pretty_print(true);
162 serializer.Serialize(*ToValue().get());
163 return output;
164 }
165
166 Invalidation::Invalidation(
167 const invalidation::ObjectId& id,
168 bool is_unknown_version,
169 int64 version,
170 const std::string& payload,
171 AckHandle ack_handle)
172 : id_(id),
173 is_unknown_version_(is_unknown_version),
174 version_(version),
175 payload_(payload),
176 ack_handle_(ack_handle) {}
177
178 } // 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