| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "sync/notifier/invalidation_util.h" | |
| 6 | |
| 7 #include <ostream> | |
| 8 #include <sstream> | |
| 9 | |
| 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" | |
| 16 | |
| 17 namespace invalidation { | |
| 18 void PrintTo(const invalidation::ObjectId& id, std::ostream* os) { | |
| 19 *os << syncer::ObjectIdToString(id); | |
| 20 } | |
| 21 } // namespace invalidation | |
| 22 | |
| 23 namespace syncer { | |
| 24 | |
| 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()); | |
| 29 } | |
| 30 | |
| 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()); | |
| 37 | |
| 38 if (a.is_unknown_version() && !b.is_unknown_version()) | |
| 39 return true; | |
| 40 | |
| 41 if (!a.is_unknown_version() && b.is_unknown_version()) | |
| 42 return false; | |
| 43 | |
| 44 if (a.is_unknown_version() && b.is_unknown_version()) | |
| 45 return false; | |
| 46 | |
| 47 return a.version() < b.version(); | |
| 48 } | |
| 49 | |
| 50 bool RealModelTypeToObjectId(ModelType model_type, | |
| 51 invalidation::ObjectId* object_id) { | |
| 52 std::string notification_type; | |
| 53 if (!RealModelTypeToNotificationType(model_type, ¬ification_type)) { | |
| 54 return false; | |
| 55 } | |
| 56 object_id->Init(ipc::invalidation::ObjectSource::CHROME_SYNC, | |
| 57 notification_type); | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 bool ObjectIdToRealModelType(const invalidation::ObjectId& object_id, | |
| 62 ModelType* model_type) { | |
| 63 return NotificationTypeToRealModelType(object_id.name(), model_type); | |
| 64 } | |
| 65 | |
| 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(); | |
| 72 } | |
| 73 | |
| 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; | |
| 82 } | |
| 83 *out = invalidation::ObjectId(source, name); | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 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; | |
| 93 } | |
| 94 | |
| 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; | |
| 102 } | |
| 103 ids.insert(model_type_as_id); | |
| 104 } | |
| 105 return ids; | |
| 106 } | |
| 107 | |
| 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; | |
| 115 } | |
| 116 model_types.Put(model_type); | |
| 117 } | |
| 118 return model_types; | |
| 119 } | |
| 120 | |
| 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(); | |
| 129 } | |
| 130 | |
| 131 } // namespace syncer | |
| OLD | NEW |