Reland 189a2ed4d209231d517b0e64a722722dead3f17a Fixes HasEventListener check in Exten...
[chromium-blink-merge.git] / jingle / notifier / listener / notification_defines.cc
blobf7c8f8fd3ad5dd8fe90af742720f7a6c137efb87
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 "jingle/notifier/listener/notification_defines.h"
7 #include <cstddef>
9 #include "base/json/string_escape.h"
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "base/values.h"
14 namespace notifier {
16 Subscription::Subscription() {}
17 Subscription::~Subscription() {}
19 bool Subscription::Equals(const Subscription& other) const {
20 return channel == other.channel && from == other.from;
23 namespace {
25 template <typename T>
26 bool ListsEqual(const T& t1, const T& t2) {
27 if (t1.size() != t2.size()) {
28 return false;
30 for (size_t i = 0; i < t1.size(); ++i) {
31 if (!t1[i].Equals(t2[i])) {
32 return false;
35 return true;
38 } // namespace
40 bool SubscriptionListsEqual(const SubscriptionList& subscriptions1,
41 const SubscriptionList& subscriptions2) {
42 return ListsEqual(subscriptions1, subscriptions2);
45 Recipient::Recipient() {}
46 Recipient::~Recipient() {}
48 bool Recipient::Equals(const Recipient& other) const {
49 return to == other.to && user_specific_data == other.user_specific_data;
52 bool RecipientListsEqual(const RecipientList& recipients1,
53 const RecipientList& recipients2) {
54 return ListsEqual(recipients1, recipients2);
57 Notification::Notification() {}
58 Notification::~Notification() {}
60 bool Notification::Equals(const Notification& other) const {
61 return
62 channel == other.channel &&
63 data == other.data &&
64 RecipientListsEqual(recipients, other.recipients);
67 std::string Notification::ToString() const {
68 // |channel| or |data| could hold binary data, so convert all non-ASCII
69 // characters to escape sequences.
70 const std::string& printable_channel =
71 base::EscapeBytesAsInvalidJSONString(channel, true /* put_in_quotes */);
72 const std::string& printable_data =
73 base::EscapeBytesAsInvalidJSONString(data, true /* put_in_quotes */);
74 return
75 "{ channel: " + printable_channel + ", data: " + printable_data + " }";
78 } // namespace notifier