blob: 44042390727c75455a5758e16cb55c1be521610a [file] [log] [blame]
brettw@google.comc0f85582011-01-21 08:50:27 +09001// Copyright (c) 2011 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 "ipc/ipc_test_sink.h"
6
7#include "ipc/ipc_message.h"
8
9namespace IPC {
10
11TestSink::TestSink() {
12}
13
14TestSink::~TestSink() {
15}
16
17bool TestSink::Send(Message* message) {
18 OnMessageReceived(*message);
19 delete message;
20 return true;
21}
22
23bool TestSink::OnMessageReceived(const Message& msg) {
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090024 ObserverListBase<Channel::Listener>::Iterator it(filter_list_);
25 Channel::Listener* observer;
26 while ((observer = it.GetNext()) != NULL) {
27 if (observer->OnMessageReceived(msg))
28 return true;
29 }
30
31 // No filter handled the message, so store it.
brettw@google.comc0f85582011-01-21 08:50:27 +090032 messages_.push_back(Message(msg));
33 return true;
34}
35
36void TestSink::ClearMessages() {
37 messages_.clear();
38}
39
40const Message* TestSink::GetMessageAt(size_t index) const {
41 if (index >= messages_.size())
42 return NULL;
43 return &messages_[index];
44}
45
46const Message* TestSink::GetFirstMessageMatching(uint32 id) const {
47 for (size_t i = 0; i < messages_.size(); i++) {
48 if (messages_[i].type() == id)
49 return &messages_[i];
50 }
51 return NULL;
52}
53
54const Message* TestSink::GetUniqueMessageMatching(uint32 id) const {
55 size_t found_index = 0;
56 size_t found_count = 0;
57 for (size_t i = 0; i < messages_.size(); i++) {
58 if (messages_[i].type() == id) {
59 found_count++;
60 found_index = i;
61 }
62 }
63 if (found_count != 1)
64 return NULL; // Didn't find a unique one.
65 return &messages_[found_index];
66}
67
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090068void TestSink::AddFilter(Channel::Listener* filter) {
69 filter_list_.AddObserver(filter);
70}
71
72void TestSink::RemoveFilter(Channel::Listener* filter) {
73 filter_list_.RemoveObserver(filter);
74}
75
brettw@google.comc0f85582011-01-21 08:50:27 +090076} // namespace IPC