Fix bug in load time stats.
[chromium-blink-merge.git] / sync / notifier / invalidation_util.cc
blobb10e2b498b2d29d204c4a82ee84ed4e6a29c1b90
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 <sstream>
9 #include "google/cacheinvalidation/include/types.h"
10 #include "google/cacheinvalidation/types.pb.h"
12 namespace syncer {
14 bool ObjectIdLessThan::operator()(const invalidation::ObjectId& lhs,
15 const invalidation::ObjectId& rhs) const {
16 return (lhs.source() < rhs.source()) ||
17 (lhs.source() == rhs.source() && lhs.name() < rhs.name());
20 bool RealModelTypeToObjectId(ModelType model_type,
21 invalidation::ObjectId* object_id) {
22 std::string notification_type;
23 if (!RealModelTypeToNotificationType(model_type, &notification_type)) {
24 return false;
26 object_id->Init(ipc::invalidation::ObjectSource::CHROME_SYNC,
27 notification_type);
28 return true;
31 bool ObjectIdToRealModelType(const invalidation::ObjectId& object_id,
32 ModelType* model_type) {
33 return NotificationTypeToRealModelType(object_id.name(), model_type);
36 ObjectIdSet ModelTypeSetToObjectIdSet(const ModelTypeSet& model_types) {
37 ObjectIdSet ids;
38 for (ModelTypeSet::Iterator it = model_types.First(); it.Good(); it.Inc()) {
39 invalidation::ObjectId model_type_as_id;
40 if (!RealModelTypeToObjectId(it.Get(), &model_type_as_id)) {
41 DLOG(WARNING) << "Invalid model type " << it.Get();
42 continue;
44 ids.insert(model_type_as_id);
46 return ids;
49 ModelTypeSet ObjectIdSetToModelTypeSet(const ObjectIdSet& ids) {
50 ModelTypeSet model_types;
51 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) {
52 ModelType model_type;
53 if (!ObjectIdToRealModelType(*it, &model_type)) {
54 DLOG(WARNING) << "Invalid object ID " << ObjectIdToString(*it);
55 continue;
57 model_types.Put(model_type);
59 return model_types;
62 std::string ObjectIdToString(
63 const invalidation::ObjectId& object_id) {
64 std::stringstream ss;
65 ss << "{ ";
66 ss << "name: " << object_id.name() << ", ";
67 ss << "source: " << object_id.source();
68 ss << " }";
69 return ss.str();
72 std::string InvalidationToString(
73 const invalidation::Invalidation& invalidation) {
74 std::stringstream ss;
75 ss << "{ ";
76 ss << "object_id: " << ObjectIdToString(invalidation.object_id()) << ", ";
77 ss << "version: " << invalidation.version();
78 ss << " }";
79 return ss.str();
82 } // namespace syncer