| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/notifier/invalidation_util.h" | 5 #include "sync/notifier/invalidation_util.h" |
| 6 | 6 |
| 7 #include <ostream> | 7 #include <ostream> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 | 9 |
| 10 #include "base/json/json_writer.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/values.h" |
| 10 #include "google/cacheinvalidation/include/types.h" | 13 #include "google/cacheinvalidation/include/types.h" |
| 11 #include "google/cacheinvalidation/types.pb.h" | 14 #include "google/cacheinvalidation/types.pb.h" |
| 12 | 15 |
| 13 namespace invalidation { | 16 namespace invalidation { |
| 14 void PrintTo(const invalidation::ObjectId& id, std::ostream* os) { | 17 void PrintTo(const invalidation::ObjectId& id, std::ostream* os) { |
| 15 *os << syncer::ObjectIdToString(id); | 18 *os << syncer::ObjectIdToString(id); |
| 16 } | 19 } |
| 17 } // namespace invalidation | 20 } // namespace invalidation |
| 18 | 21 |
| 19 namespace syncer { | 22 namespace syncer { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 33 object_id->Init(ipc::invalidation::ObjectSource::CHROME_SYNC, | 36 object_id->Init(ipc::invalidation::ObjectSource::CHROME_SYNC, |
| 34 notification_type); | 37 notification_type); |
| 35 return true; | 38 return true; |
| 36 } | 39 } |
| 37 | 40 |
| 38 bool ObjectIdToRealModelType(const invalidation::ObjectId& object_id, | 41 bool ObjectIdToRealModelType(const invalidation::ObjectId& object_id, |
| 39 ModelType* model_type) { | 42 ModelType* model_type) { |
| 40 return NotificationTypeToRealModelType(object_id.name(), model_type); | 43 return NotificationTypeToRealModelType(object_id.name(), model_type); |
| 41 } | 44 } |
| 42 | 45 |
| 46 scoped_ptr<base::DictionaryValue> ObjectIdToValue( |
| 47 const invalidation::ObjectId& object_id) { |
| 48 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| 49 value->SetInteger("source", object_id.source()); |
| 50 value->SetString("name", object_id.name()); |
| 51 return value.Pass(); |
| 52 } |
| 53 |
| 54 bool ObjectIdFromValue(const base::DictionaryValue& value, |
| 55 invalidation::ObjectId* out) { |
| 56 *out = invalidation::ObjectId(); |
| 57 std::string name; |
| 58 int source = 0; |
| 59 if (!value.GetInteger("source", &source) || |
| 60 !value.GetString("name", &name)) { |
| 61 return false; |
| 62 } |
| 63 *out = invalidation::ObjectId(source, name); |
| 64 return true; |
| 65 } |
| 66 |
| 67 std::string ObjectIdToString( |
| 68 const invalidation::ObjectId& object_id) { |
| 69 scoped_ptr<base::DictionaryValue> value(ObjectIdToValue(object_id)); |
| 70 std::string str; |
| 71 base::JSONWriter::Write(value.get(), &str); |
| 72 return str; |
| 73 } |
| 74 |
| 43 ObjectIdSet ModelTypeSetToObjectIdSet(const ModelTypeSet& model_types) { | 75 ObjectIdSet ModelTypeSetToObjectIdSet(const ModelTypeSet& model_types) { |
| 44 ObjectIdSet ids; | 76 ObjectIdSet ids; |
| 45 for (ModelTypeSet::Iterator it = model_types.First(); it.Good(); it.Inc()) { | 77 for (ModelTypeSet::Iterator it = model_types.First(); it.Good(); it.Inc()) { |
| 46 invalidation::ObjectId model_type_as_id; | 78 invalidation::ObjectId model_type_as_id; |
| 47 if (!RealModelTypeToObjectId(it.Get(), &model_type_as_id)) { | 79 if (!RealModelTypeToObjectId(it.Get(), &model_type_as_id)) { |
| 48 DLOG(WARNING) << "Invalid model type " << it.Get(); | 80 DLOG(WARNING) << "Invalid model type " << it.Get(); |
| 49 continue; | 81 continue; |
| 50 } | 82 } |
| 51 ids.insert(model_type_as_id); | 83 ids.insert(model_type_as_id); |
| 52 } | 84 } |
| 53 return ids; | 85 return ids; |
| 54 } | 86 } |
| 55 | 87 |
| 56 ModelTypeSet ObjectIdSetToModelTypeSet(const ObjectIdSet& ids) { | 88 ModelTypeSet ObjectIdSetToModelTypeSet(const ObjectIdSet& ids) { |
| 57 ModelTypeSet model_types; | 89 ModelTypeSet model_types; |
| 58 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | 90 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { |
| 59 ModelType model_type; | 91 ModelType model_type; |
| 60 if (!ObjectIdToRealModelType(*it, &model_type)) { | 92 if (!ObjectIdToRealModelType(*it, &model_type)) { |
| 61 DLOG(WARNING) << "Invalid object ID " << ObjectIdToString(*it); | 93 DLOG(WARNING) << "Invalid object ID " << ObjectIdToString(*it); |
| 62 continue; | 94 continue; |
| 63 } | 95 } |
| 64 model_types.Put(model_type); | 96 model_types.Put(model_type); |
| 65 } | 97 } |
| 66 return model_types; | 98 return model_types; |
| 67 } | 99 } |
| 68 | 100 |
| 69 std::string ObjectIdToString( | |
| 70 const invalidation::ObjectId& object_id) { | |
| 71 std::stringstream ss; | |
| 72 ss << "{ "; | |
| 73 ss << "name: " << object_id.name() << ", "; | |
| 74 ss << "source: " << object_id.source(); | |
| 75 ss << " }"; | |
| 76 return ss.str(); | |
| 77 } | |
| 78 | |
| 79 std::string InvalidationToString( | 101 std::string InvalidationToString( |
| 80 const invalidation::Invalidation& invalidation) { | 102 const invalidation::Invalidation& invalidation) { |
| 81 std::stringstream ss; | 103 std::stringstream ss; |
| 82 ss << "{ "; | 104 ss << "{ "; |
| 83 ss << "object_id: " << ObjectIdToString(invalidation.object_id()) << ", "; | 105 ss << "object_id: " << ObjectIdToString(invalidation.object_id()) << ", "; |
| 84 ss << "version: " << invalidation.version(); | 106 ss << "version: " << invalidation.version(); |
| 85 ss << " }"; | 107 ss << " }"; |
| 86 return ss.str(); | 108 return ss.str(); |
| 87 } | 109 } |
| 88 | 110 |
| 89 } // namespace syncer | 111 } // namespace syncer |
| OLD | NEW |