Get foreground tab on Android
[chromium-blink-merge.git] / sync / notifier / invalidation_util.cc
blob27acd38c8d491feaa1affac6d4cd770c753019c7
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"
7 #include <ostream>
8 #include <sstream>
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
23 namespace syncer {
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())
39 return true;
41 if (!a.is_unknown_version() && b.is_unknown_version())
42 return false;
44 if (a.is_unknown_version() && b.is_unknown_version())
45 return false;
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, &notification_type)) {
54 return false;
56 object_id->Init(ipc::invalidation::ObjectSource::CHROME_SYNC,
57 notification_type);
58 return true;
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());
71 return value.Pass();
74 bool ObjectIdFromValue(const base::DictionaryValue& value,
75 invalidation::ObjectId* out) {
76 *out = invalidation::ObjectId();
77 std::string name;
78 int source = 0;
79 if (!value.GetInteger("source", &source) ||
80 !value.GetString("name", &name)) {
81 return false;
83 *out = invalidation::ObjectId(source, name);
84 return true;
87 std::string ObjectIdToString(
88 const invalidation::ObjectId& object_id) {
89 scoped_ptr<base::DictionaryValue> value(ObjectIdToValue(object_id));
90 std::string str;
91 base::JSONWriter::Write(value.get(), &str);
92 return str;
95 ObjectIdSet ModelTypeSetToObjectIdSet(ModelTypeSet model_types) {
96 ObjectIdSet ids;
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();
101 continue;
103 ids.insert(model_type_as_id);
105 return ids;
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);
114 continue;
116 model_types.Put(model_type);
118 return model_types;
121 std::string InvalidationToString(
122 const invalidation::Invalidation& invalidation) {
123 std::stringstream ss;
124 ss << "{ ";
125 ss << "object_id: " << ObjectIdToString(invalidation.object_id()) << ", ";
126 ss << "version: " << invalidation.version();
127 ss << " }";
128 return ss.str();
131 } // namespace syncer