blob: b1a21bfd4807a35b8409cc225ea89c05f91a8f2f [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
tfarina@chromium.orgbeba2922012-11-18 04:20:05 +09007#include "ipc/ipc_listener.h"
brettw@google.comc0f85582011-01-21 08:50:27 +09008#include "ipc/ipc_message.h"
9
10namespace IPC {
11
12TestSink::TestSink() {
13}
14
15TestSink::~TestSink() {
16}
17
18bool TestSink::Send(Message* message) {
19 OnMessageReceived(*message);
20 delete message;
21 return true;
22}
23
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +090024bool TestSink::Connect() {
25 NOTIMPLEMENTED();
26 return false;
27}
28
29void TestSink::Close() {
30 NOTIMPLEMENTED();
31}
32
33base::ProcessId TestSink::GetPeerPID() const {
34 NOTIMPLEMENTED();
35 return base::ProcessId();
36}
37
morrita@chromium.org15996aa2014-08-05 08:44:17 +090038base::ProcessId TestSink::GetSelfPID() const {
39 NOTIMPLEMENTED();
40 return base::ProcessId();
41}
42
43ChannelHandle TestSink::TakePipeHandle() {
44 NOTIMPLEMENTED();
45 return ChannelHandle();
46}
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +090047
brettw@google.comc0f85582011-01-21 08:50:27 +090048bool TestSink::OnMessageReceived(const Message& msg) {
brettw@chromium.orgdb1259e2012-06-30 07:05:26 +090049 ObserverListBase<Listener>::Iterator it(filter_list_);
50 Listener* observer;
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090051 while ((observer = it.GetNext()) != NULL) {
52 if (observer->OnMessageReceived(msg))
53 return true;
54 }
55
56 // No filter handled the message, so store it.
brettw@google.comc0f85582011-01-21 08:50:27 +090057 messages_.push_back(Message(msg));
58 return true;
59}
60
61void TestSink::ClearMessages() {
62 messages_.clear();
63}
64
65const Message* TestSink::GetMessageAt(size_t index) const {
66 if (index >= messages_.size())
67 return NULL;
68 return &messages_[index];
69}
70
71const Message* TestSink::GetFirstMessageMatching(uint32 id) const {
72 for (size_t i = 0; i < messages_.size(); i++) {
73 if (messages_[i].type() == id)
74 return &messages_[i];
75 }
76 return NULL;
77}
78
79const Message* TestSink::GetUniqueMessageMatching(uint32 id) const {
80 size_t found_index = 0;
81 size_t found_count = 0;
82 for (size_t i = 0; i < messages_.size(); i++) {
83 if (messages_[i].type() == id) {
84 found_count++;
85 found_index = i;
86 }
87 }
88 if (found_count != 1)
89 return NULL; // Didn't find a unique one.
90 return &messages_[found_index];
91}
92
brettw@chromium.orgdb1259e2012-06-30 07:05:26 +090093void TestSink::AddFilter(Listener* filter) {
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090094 filter_list_.AddObserver(filter);
95}
96
brettw@chromium.orgdb1259e2012-06-30 07:05:26 +090097void TestSink::RemoveFilter(Listener* filter) {
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090098 filter_list_.RemoveObserver(filter);
99}
100
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +0900101#if defined(OS_POSIX) && !defined(OS_NACL)
102
103int TestSink::GetClientFileDescriptor() const {
104 NOTREACHED();
105 return -1;
106}
107
108int TestSink::TakeClientFileDescriptor() {
109 NOTREACHED();
110 return -1;
111}
112
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +0900113#endif // defined(OS_POSIX) && !defined(OS_NACL)
114
brettw@google.comc0f85582011-01-21 08:50:27 +0900115} // namespace IPC