invalidation_util.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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 "chrome/browser/sync/notifier/invalidation_util.h"
6
7#include <sstream>
8
9namespace sync_notifier {
10
11void RunAndDeleteClosure(invalidation::Closure* task) {
12  task->Run();
13  delete task;
14}
15
16bool RealModelTypeToObjectId(syncable::ModelType model_type,
17                             invalidation::ObjectId* object_id) {
18  std::string notification_type;
19  if (!syncable::RealModelTypeToNotificationType(
20          model_type, &notification_type)) {
21    return false;
22  }
23  object_id->mutable_name()->set_string_value(notification_type);
24  object_id->set_source(invalidation::ObjectId::CHROME_SYNC);
25  return true;
26}
27
28bool ObjectIdToRealModelType(const invalidation::ObjectId& object_id,
29                             syncable::ModelType* model_type) {
30  return
31      syncable::NotificationTypeToRealModelType(
32          object_id.name().string_value(), model_type);
33}
34
35std::string ObjectIdToString(
36    const invalidation::ObjectId& object_id) {
37  std::stringstream ss;
38  ss << "{ ";
39  ss << "name: " << object_id.name().string_value() << ", ";
40  ss << "source: " << object_id.source();
41  ss << " }";
42  return ss.str();
43}
44
45std::string StatusToString(
46    const invalidation::Status& status) {
47  std::stringstream ss;
48  ss << "{ ";
49  ss << "code: " << status.code() << ", ";
50  ss << "description: " << status.description();
51  ss << " }";
52  return ss.str();
53}
54
55std::string InvalidationToString(
56    const invalidation::Invalidation& invalidation) {
57  std::stringstream ss;
58  ss << "{ ";
59  ss << "object_id: " << ObjectIdToString(invalidation.object_id()) << ", ";
60  ss << "version: " << invalidation.version() << ", ";
61  ss << "components: { ";
62  const invalidation::ComponentStampLog& component_stamp_log =
63      invalidation.component_stamp_log();
64  for (int i = 0; i < component_stamp_log.stamp_size(); ++i) {
65    const invalidation::ComponentStamp& component_stamp =
66        component_stamp_log.stamp(i);
67    ss << "component: " << component_stamp.component() << ", ";
68    ss << "time: " << component_stamp.time() << ", ";
69  }
70  ss << " }";
71  ss << " }";
72  return ss.str();
73}
74
75std::string RegistrationUpdateToString(
76    const invalidation::RegistrationUpdate& update) {
77  std::stringstream ss;
78  ss << "{ ";
79  ss << "type: " << update.type() << ", ";
80  ss << "object_id: " << ObjectIdToString(update.object_id()) << ", ";
81  ss << "version: " << update.version() << ", ";
82  ss << "sequence_number: " << update.sequence_number();
83  ss << " }";
84  return ss.str();
85}
86
87std::string RegistrationUpdateResultToString(
88    const invalidation::RegistrationUpdateResult& update_result) {
89  std::stringstream ss;
90  ss << "{ ";
91  ss << "operation: "
92     << RegistrationUpdateToString(update_result.operation()) << ", ";
93  ss << "status: " << StatusToString(update_result.status());
94  ss << " }";
95  return ss.str();
96}
97
98}  // namespace sync_notifier
99