| 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 "components/invalidation/invalidation.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "sync/internal_api/public/base/ack_handler.h" | 13 #include "components/invalidation/ack_handler.h" |
| 14 #include "sync/internal_api/public/base/invalidation_util.h" | 14 #include "components/invalidation/invalidation_util.h" |
| 15 | 15 |
| 16 namespace syncer { | 16 namespace syncer { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 const char kObjectIdKey[] = "objectId"; | 19 const char kObjectIdKey[] = "objectId"; |
| 20 const char kIsUnknownVersionKey[] = "isUnknownVersion"; | 20 const char kIsUnknownVersionKey[] = "isUnknownVersion"; |
| 21 const char kVersionKey[] = "version"; | 21 const char kVersionKey[] = "version"; |
| 22 const char kPayloadKey[] = "payload"; | 22 const char kPayloadKey[] = "payload"; |
| 23 const int64 kInvalidVersion = -1; | 23 const int64 kInvalidVersion = -1; |
| 24 } | 24 } |
| 25 | 25 |
| 26 Invalidation Invalidation::Init( | 26 Invalidation Invalidation::Init(const invalidation::ObjectId& id, |
| 27 const invalidation::ObjectId& id, | 27 int64 version, |
| 28 int64 version, | 28 const std::string& payload) { |
| 29 const std::string& payload) { | |
| 30 return Invalidation(id, false, version, payload, AckHandle::CreateUnique()); | 29 return Invalidation(id, false, version, payload, AckHandle::CreateUnique()); |
| 31 } | 30 } |
| 32 | 31 |
| 33 Invalidation Invalidation::InitUnknownVersion( | 32 Invalidation Invalidation::InitUnknownVersion( |
| 34 const invalidation::ObjectId& id) { | 33 const invalidation::ObjectId& id) { |
| 35 return Invalidation(id, true, kInvalidVersion, | 34 return Invalidation( |
| 36 std::string(), AckHandle::CreateUnique()); | 35 id, true, kInvalidVersion, std::string(), AckHandle::CreateUnique()); |
| 37 } | 36 } |
| 38 | 37 |
| 39 Invalidation Invalidation::InitFromDroppedInvalidation( | 38 Invalidation Invalidation::InitFromDroppedInvalidation( |
| 40 const Invalidation& dropped) { | 39 const Invalidation& dropped) { |
| 41 return Invalidation(dropped.id_, true, kInvalidVersion, | 40 return Invalidation( |
| 42 std::string(), dropped.ack_handle_); | 41 dropped.id_, true, kInvalidVersion, std::string(), dropped.ack_handle_); |
| 43 } | 42 } |
| 44 | 43 |
| 45 scoped_ptr<Invalidation> Invalidation::InitFromValue( | 44 scoped_ptr<Invalidation> Invalidation::InitFromValue( |
| 46 const base::DictionaryValue& value) { | 45 const base::DictionaryValue& value) { |
| 47 invalidation::ObjectId id; | 46 invalidation::ObjectId id; |
| 48 | 47 |
| 49 const base::DictionaryValue* object_id_dict; | 48 const base::DictionaryValue* object_id_dict; |
| 50 if (!value.GetDictionary(kObjectIdKey, &object_id_dict) | 49 if (!value.GetDictionary(kObjectIdKey, &object_id_dict) || |
| 51 || !ObjectIdFromValue(*object_id_dict, &id)) { | 50 !ObjectIdFromValue(*object_id_dict, &id)) { |
| 52 DLOG(WARNING) << "Failed to parse id"; | 51 DLOG(WARNING) << "Failed to parse id"; |
| 53 return scoped_ptr<Invalidation>(); | 52 return scoped_ptr<Invalidation>(); |
| 54 } | 53 } |
| 55 bool is_unknown_version; | 54 bool is_unknown_version; |
| 56 if (!value.GetBoolean(kIsUnknownVersionKey, &is_unknown_version)) { | 55 if (!value.GetBoolean(kIsUnknownVersionKey, &is_unknown_version)) { |
| 57 DLOG(WARNING) << "Failed to parse is_unknown_version flag"; | 56 DLOG(WARNING) << "Failed to parse is_unknown_version flag"; |
| 58 return scoped_ptr<Invalidation>(); | 57 return scoped_ptr<Invalidation>(); |
| 59 } | 58 } |
| 60 if (is_unknown_version) { | 59 if (is_unknown_version) { |
| 61 return scoped_ptr<Invalidation>(new Invalidation( | 60 return scoped_ptr<Invalidation>(new Invalidation( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 78 return scoped_ptr<Invalidation>(); | 77 return scoped_ptr<Invalidation>(); |
| 79 } | 78 } |
| 80 return scoped_ptr<Invalidation>(new Invalidation( | 79 return scoped_ptr<Invalidation>(new Invalidation( |
| 81 id, | 80 id, |
| 82 false, | 81 false, |
| 83 version, | 82 version, |
| 84 payload, | 83 payload, |
| 85 AckHandle::CreateUnique())); | 84 AckHandle::CreateUnique())); |
| 86 } | 85 } |
| 87 | 86 |
| 88 Invalidation::~Invalidation() {} | 87 Invalidation::~Invalidation() { |
| 88 } |
| 89 | 89 |
| 90 invalidation::ObjectId Invalidation::object_id() const { | 90 invalidation::ObjectId Invalidation::object_id() const { |
| 91 return id_; | 91 return id_; |
| 92 } | 92 } |
| 93 | 93 |
| 94 bool Invalidation::is_unknown_version() const { | 94 bool Invalidation::is_unknown_version() const { |
| 95 return is_unknown_version_; | 95 return is_unknown_version_; |
| 96 } | 96 } |
| 97 | 97 |
| 98 int64 Invalidation::version() const { | 98 int64 Invalidation::version() const { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 112 void Invalidation::set_ack_handler(syncer::WeakHandle<AckHandler> handler) { | 112 void Invalidation::set_ack_handler(syncer::WeakHandle<AckHandler> handler) { |
| 113 ack_handler_ = handler; | 113 ack_handler_ = handler; |
| 114 } | 114 } |
| 115 | 115 |
| 116 bool Invalidation::SupportsAcknowledgement() const { | 116 bool Invalidation::SupportsAcknowledgement() const { |
| 117 return ack_handler_.IsInitialized(); | 117 return ack_handler_.IsInitialized(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void Invalidation::Acknowledge() const { | 120 void Invalidation::Acknowledge() const { |
| 121 if (SupportsAcknowledgement()) { | 121 if (SupportsAcknowledgement()) { |
| 122 ack_handler_.Call(FROM_HERE, | 122 ack_handler_.Call(FROM_HERE, &AckHandler::Acknowledge, id_, ack_handle_); |
| 123 &AckHandler::Acknowledge, | |
| 124 id_, | |
| 125 ack_handle_); | |
| 126 } | 123 } |
| 127 } | 124 } |
| 128 | 125 |
| 129 void Invalidation::Drop() { | 126 void Invalidation::Drop() { |
| 130 if (SupportsAcknowledgement()) { | 127 if (SupportsAcknowledgement()) { |
| 131 ack_handler_.Call(FROM_HERE, | 128 ack_handler_.Call(FROM_HERE, &AckHandler::Drop, id_, ack_handle_); |
| 132 &AckHandler::Drop, | |
| 133 id_, | |
| 134 ack_handle_); | |
| 135 } | 129 } |
| 136 } | 130 } |
| 137 | 131 |
| 138 bool Invalidation::Equals(const Invalidation& other) const { | 132 bool Invalidation::Equals(const Invalidation& other) const { |
| 139 return id_ == other.id_ | 133 return id_ == other.id_ && is_unknown_version_ == other.is_unknown_version_ && |
| 140 && is_unknown_version_ == other.is_unknown_version_ | 134 version_ == other.version_ && payload_ == other.payload_; |
| 141 && version_ == other.version_ | |
| 142 && payload_ == other.payload_; | |
| 143 } | 135 } |
| 144 | 136 |
| 145 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { | 137 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const { |
| 146 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | 138 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| 147 value->Set(kObjectIdKey, ObjectIdToValue(id_).release()); | 139 value->Set(kObjectIdKey, ObjectIdToValue(id_).release()); |
| 148 if (is_unknown_version_) { | 140 if (is_unknown_version_) { |
| 149 value->SetBoolean(kIsUnknownVersionKey, true); | 141 value->SetBoolean(kIsUnknownVersionKey, true); |
| 150 } else { | 142 } else { |
| 151 value->SetBoolean(kIsUnknownVersionKey, false); | 143 value->SetBoolean(kIsUnknownVersionKey, false); |
| 152 value->SetString(kVersionKey, base::Int64ToString(version_)); | 144 value->SetString(kVersionKey, base::Int64ToString(version_)); |
| 153 value->SetString(kPayloadKey, payload_); | 145 value->SetString(kPayloadKey, payload_); |
| 154 } | 146 } |
| 155 return value.Pass(); | 147 return value.Pass(); |
| 156 } | 148 } |
| 157 | 149 |
| 158 std::string Invalidation::ToString() const { | 150 std::string Invalidation::ToString() const { |
| 159 std::string output; | 151 std::string output; |
| 160 JSONStringValueSerializer serializer(&output); | 152 JSONStringValueSerializer serializer(&output); |
| 161 serializer.set_pretty_print(true); | 153 serializer.set_pretty_print(true); |
| 162 serializer.Serialize(*ToValue().get()); | 154 serializer.Serialize(*ToValue().get()); |
| 163 return output; | 155 return output; |
| 164 } | 156 } |
| 165 | 157 |
| 166 Invalidation::Invalidation( | 158 Invalidation::Invalidation(const invalidation::ObjectId& id, |
| 167 const invalidation::ObjectId& id, | 159 bool is_unknown_version, |
| 168 bool is_unknown_version, | 160 int64 version, |
| 169 int64 version, | 161 const std::string& payload, |
| 170 const std::string& payload, | 162 AckHandle ack_handle) |
| 171 AckHandle ack_handle) | 163 : id_(id), |
| 172 : id_(id), | 164 is_unknown_version_(is_unknown_version), |
| 173 is_unknown_version_(is_unknown_version), | 165 version_(version), |
| 174 version_(version), | 166 payload_(payload), |
| 175 payload_(payload), | 167 ack_handle_(ack_handle) { |
| 176 ack_handle_(ack_handle) {} | 168 } |
| 177 | 169 |
| 178 } // namespace syncer | 170 } // namespace syncer |
| OLD | NEW |