1 // Copyright (c) 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.
5 #include "sync/notifier/invalidation_util.h"
10 #include "base/json/json_writer.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h"
13 #include "google/cacheinvalidation/include/types.h"
14 #include "google/cacheinvalidation/types.pb.h"
15 #include "sync/internal_api/public/base/invalidation.h"
17 namespace invalidation
{
18 void PrintTo(const invalidation::ObjectId
& id
, std::ostream
* os
) {
19 *os
<< syncer::ObjectIdToString(id
);
21 } // namespace invalidation
25 bool ObjectIdLessThan::operator()(const invalidation::ObjectId
& lhs
,
26 const invalidation::ObjectId
& rhs
) const {
27 return (lhs
.source() < rhs
.source()) ||
28 (lhs
.source() == rhs
.source() && lhs
.name() < rhs
.name());
31 bool InvalidationVersionLessThan::operator()(
32 const Invalidation
& a
,
33 const Invalidation
& b
) const {
34 DCHECK(a
.object_id() == b
.object_id())
35 << "a: " << ObjectIdToString(a
.object_id()) << ", "
36 << "b: " << ObjectIdToString(a
.object_id());
38 if (a
.is_unknown_version() && !b
.is_unknown_version())
41 if (!a
.is_unknown_version() && b
.is_unknown_version())
44 if (a
.is_unknown_version() && b
.is_unknown_version())
47 return a
.version() < b
.version();
50 bool RealModelTypeToObjectId(ModelType model_type
,
51 invalidation::ObjectId
* object_id
) {
52 std::string notification_type
;
53 if (!RealModelTypeToNotificationType(model_type
, ¬ification_type
)) {
56 object_id
->Init(ipc::invalidation::ObjectSource::CHROME_SYNC
,
61 bool ObjectIdToRealModelType(const invalidation::ObjectId
& object_id
,
62 ModelType
* model_type
) {
63 return NotificationTypeToRealModelType(object_id
.name(), model_type
);
66 scoped_ptr
<base::DictionaryValue
> ObjectIdToValue(
67 const invalidation::ObjectId
& object_id
) {
68 scoped_ptr
<base::DictionaryValue
> value(new base::DictionaryValue());
69 value
->SetInteger("source", object_id
.source());
70 value
->SetString("name", object_id
.name());
74 bool ObjectIdFromValue(const base::DictionaryValue
& value
,
75 invalidation::ObjectId
* out
) {
76 *out
= invalidation::ObjectId();
79 if (!value
.GetInteger("source", &source
) ||
80 !value
.GetString("name", &name
)) {
83 *out
= invalidation::ObjectId(source
, name
);
87 std::string
ObjectIdToString(
88 const invalidation::ObjectId
& object_id
) {
89 scoped_ptr
<base::DictionaryValue
> value(ObjectIdToValue(object_id
));
91 base::JSONWriter::Write(value
.get(), &str
);
95 ObjectIdSet
ModelTypeSetToObjectIdSet(ModelTypeSet model_types
) {
97 for (ModelTypeSet::Iterator it
= model_types
.First(); it
.Good(); it
.Inc()) {
98 invalidation::ObjectId model_type_as_id
;
99 if (!RealModelTypeToObjectId(it
.Get(), &model_type_as_id
)) {
100 DLOG(WARNING
) << "Invalid model type " << it
.Get();
103 ids
.insert(model_type_as_id
);
108 ModelTypeSet
ObjectIdSetToModelTypeSet(const ObjectIdSet
& ids
) {
109 ModelTypeSet model_types
;
110 for (ObjectIdSet::const_iterator it
= ids
.begin(); it
!= ids
.end(); ++it
) {
111 ModelType model_type
;
112 if (!ObjectIdToRealModelType(*it
, &model_type
)) {
113 DLOG(WARNING
) << "Invalid object ID " << ObjectIdToString(*it
);
116 model_types
.Put(model_type
);
121 std::string
InvalidationToString(
122 const invalidation::Invalidation
& invalidation
) {
123 std::stringstream ss
;
125 ss
<< "object_id: " << ObjectIdToString(invalidation
.object_id()) << ", ";
126 ss
<< "version: " << invalidation
.version();
131 } // namespace syncer