summaryrefslogtreecommitdiff
path: root/chromium/jingle/notifier/listener/notification_defines.cc
blob: f7c8f8fd3ad5dd8fe90af742720f7a6c137efb87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "jingle/notifier/listener/notification_defines.h"

#include <cstddef>

#include "base/json/string_escape.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/values.h"

namespace notifier {

Subscription::Subscription() {}
Subscription::~Subscription() {}

bool Subscription::Equals(const Subscription& other) const {
  return channel == other.channel && from == other.from;
}

namespace {

template <typename T>
bool ListsEqual(const T& t1, const T& t2) {
  if (t1.size() != t2.size()) {
    return false;
  }
  for (size_t i = 0; i < t1.size(); ++i) {
    if (!t1[i].Equals(t2[i])) {
      return false;
    }
  }
  return true;
}

}  // namespace

bool SubscriptionListsEqual(const SubscriptionList& subscriptions1,
                            const SubscriptionList& subscriptions2) {
  return ListsEqual(subscriptions1, subscriptions2);
}

Recipient::Recipient() {}
Recipient::~Recipient() {}

bool Recipient::Equals(const Recipient& other) const {
  return to == other.to && user_specific_data == other.user_specific_data;
}

bool RecipientListsEqual(const RecipientList& recipients1,
                         const RecipientList& recipients2) {
  return ListsEqual(recipients1, recipients2);
}

Notification::Notification() {}
Notification::~Notification() {}

bool Notification::Equals(const Notification& other) const {
  return
      channel == other.channel &&
      data == other.data &&
      RecipientListsEqual(recipients, other.recipients);
}

std::string Notification::ToString() const {
  // |channel| or |data| could hold binary data, so convert all non-ASCII
  // characters to escape sequences.
  const std::string& printable_channel =
      base::EscapeBytesAsInvalidJSONString(channel, true /* put_in_quotes */);
  const std::string& printable_data =
      base::EscapeBytesAsInvalidJSONString(data, true /* put_in_quotes */);
  return
      "{ channel: " + printable_channel + ", data: " + printable_data + " }";
}

}  // namespace notifier