1 // Copyright 2014 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.
5 #include "components/invalidation/invalidation.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/location.h"
12 #include "base/rand_util.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/values.h"
15 #include "components/invalidation/ack_handler.h"
16 #include "components/invalidation/invalidation_util.h"
21 const char kObjectIdKey
[] = "objectId";
22 const char kIsUnknownVersionKey
[] = "isUnknownVersion";
23 const char kVersionKey
[] = "version";
24 const char kPayloadKey
[] = "payload";
25 const int64 kInvalidVersion
= -1;
28 Invalidation
Invalidation::Init(const invalidation::ObjectId
& id
,
30 const std::string
& payload
) {
31 return Invalidation(id
, false, version
, payload
, AckHandle::CreateUnique());
34 Invalidation
Invalidation::InitUnknownVersion(
35 const invalidation::ObjectId
& id
) {
37 id
, true, kInvalidVersion
, std::string(), AckHandle::CreateUnique());
40 Invalidation
Invalidation::InitFromDroppedInvalidation(
41 const Invalidation
& dropped
) {
43 dropped
.id_
, true, kInvalidVersion
, std::string(), dropped
.ack_handle_
);
46 scoped_ptr
<Invalidation
> Invalidation::InitFromValue(
47 const base::DictionaryValue
& value
) {
48 invalidation::ObjectId id
;
50 const base::DictionaryValue
* object_id_dict
;
51 if (!value
.GetDictionary(kObjectIdKey
, &object_id_dict
) ||
52 !ObjectIdFromValue(*object_id_dict
, &id
)) {
53 DLOG(WARNING
) << "Failed to parse id";
54 return scoped_ptr
<Invalidation
>();
56 bool is_unknown_version
;
57 if (!value
.GetBoolean(kIsUnknownVersionKey
, &is_unknown_version
)) {
58 DLOG(WARNING
) << "Failed to parse is_unknown_version flag";
59 return scoped_ptr
<Invalidation
>();
61 if (is_unknown_version
) {
62 return scoped_ptr
<Invalidation
>(new Invalidation(
67 AckHandle::CreateUnique()));
70 std::string version_as_string
;
71 if (!value
.GetString(kVersionKey
, &version_as_string
)
72 || !base::StringToInt64(version_as_string
, &version
)) {
73 DLOG(WARNING
) << "Failed to parse version";
74 return scoped_ptr
<Invalidation
>();
77 if (!value
.GetString(kPayloadKey
, &payload
)) {
78 DLOG(WARNING
) << "Failed to parse payload";
79 return scoped_ptr
<Invalidation
>();
81 return scoped_ptr
<Invalidation
>(new Invalidation(
86 AckHandle::CreateUnique()));
89 Invalidation::~Invalidation() {
92 invalidation::ObjectId
Invalidation::object_id() const {
96 bool Invalidation::is_unknown_version() const {
97 return is_unknown_version_
;
100 int64
Invalidation::version() const {
101 DCHECK(!is_unknown_version_
);
105 const std::string
& Invalidation::payload() const {
106 DCHECK(!is_unknown_version_
);
110 const AckHandle
& Invalidation::ack_handle() const {
114 void Invalidation::SetAckHandler(
115 base::WeakPtr
<AckHandler
> handler
,
116 scoped_refptr
<base::SequencedTaskRunner
> handler_task_runner
) {
117 ack_handler_
= handler
;
118 ack_handler_task_runner_
= handler_task_runner
;
121 bool Invalidation::SupportsAcknowledgement() const {
122 return !!ack_handler_task_runner_
.get();
125 void Invalidation::Acknowledge() const {
126 if (SupportsAcknowledgement()) {
127 ack_handler_task_runner_
->PostTask(
129 base::Bind(&AckHandler::Acknowledge
, ack_handler_
, id_
, ack_handle_
));
133 void Invalidation::Drop() {
134 if (SupportsAcknowledgement()) {
135 ack_handler_task_runner_
->PostTask(
137 base::Bind(&AckHandler::Drop
, ack_handler_
, id_
, ack_handle_
));
141 bool Invalidation::Equals(const Invalidation
& other
) const {
142 return id_
== other
.id_
&& is_unknown_version_
== other
.is_unknown_version_
&&
143 version_
== other
.version_
&& payload_
== other
.payload_
;
146 scoped_ptr
<base::DictionaryValue
> Invalidation::ToValue() const {
147 scoped_ptr
<base::DictionaryValue
> value(new base::DictionaryValue());
148 value
->Set(kObjectIdKey
, ObjectIdToValue(id_
).release());
149 if (is_unknown_version_
) {
150 value
->SetBoolean(kIsUnknownVersionKey
, true);
152 value
->SetBoolean(kIsUnknownVersionKey
, false);
153 value
->SetString(kVersionKey
, base::Int64ToString(version_
));
154 value
->SetString(kPayloadKey
, payload_
);
159 std::string
Invalidation::ToString() const {
161 JSONStringValueSerializer
serializer(&output
);
162 serializer
.set_pretty_print(true);
163 serializer
.Serialize(*ToValue().get());
167 Invalidation::Invalidation(const invalidation::ObjectId
& id
,
168 bool is_unknown_version
,
170 const std::string
& payload
,
171 AckHandle ack_handle
)
173 is_unknown_version_(is_unknown_version
),
176 ack_handle_(ack_handle
) {
179 } // namespace syncer