blob: 65951fb621f6bd12bc21fc26d21e67f3ab5e0dd8 [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
38
brettw@google.comc0f85582011-01-21 08:50:27 +090039bool TestSink::OnMessageReceived(const Message& msg) {
brettw@chromium.orgdb1259e2012-06-30 07:05:26 +090040 ObserverListBase<Listener>::Iterator it(filter_list_);
41 Listener* observer;
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090042 while ((observer = it.GetNext()) != NULL) {
43 if (observer->OnMessageReceived(msg))
44 return true;
45 }
46
47 // No filter handled the message, so store it.
brettw@google.comc0f85582011-01-21 08:50:27 +090048 messages_.push_back(Message(msg));
49 return true;
50}
51
52void TestSink::ClearMessages() {
53 messages_.clear();
54}
55
56const Message* TestSink::GetMessageAt(size_t index) const {
57 if (index >= messages_.size())
58 return NULL;
59 return &messages_[index];
60}
61
62const Message* TestSink::GetFirstMessageMatching(uint32 id) const {
63 for (size_t i = 0; i < messages_.size(); i++) {
64 if (messages_[i].type() == id)
65 return &messages_[i];
66 }
67 return NULL;
68}
69
70const Message* TestSink::GetUniqueMessageMatching(uint32 id) const {
71 size_t found_index = 0;
72 size_t found_count = 0;
73 for (size_t i = 0; i < messages_.size(); i++) {
74 if (messages_[i].type() == id) {
75 found_count++;
76 found_index = i;
77 }
78 }
79 if (found_count != 1)
80 return NULL; // Didn't find a unique one.
81 return &messages_[found_index];
82}
83
brettw@chromium.orgdb1259e2012-06-30 07:05:26 +090084void TestSink::AddFilter(Listener* filter) {
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090085 filter_list_.AddObserver(filter);
86}
87
brettw@chromium.orgdb1259e2012-06-30 07:05:26 +090088void TestSink::RemoveFilter(Listener* filter) {
bryner@chromium.org44ae58f2011-02-01 06:43:31 +090089 filter_list_.RemoveObserver(filter);
90}
91
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +090092#if defined(OS_POSIX) && !defined(OS_NACL)
93
94int TestSink::GetClientFileDescriptor() const {
95 NOTREACHED();
96 return -1;
97}
98
99int TestSink::TakeClientFileDescriptor() {
100 NOTREACHED();
101 return -1;
102}
103
104bool TestSink::AcceptsConnections() const {
105 NOTREACHED();
106 return false;
107}
108
109bool TestSink::HasAcceptedConnection() const {
110 NOTREACHED();
111 return false;
112}
113
114bool TestSink::GetPeerEuid(uid_t* peer_euid) const {
115 NOTREACHED();
116 return false;
117}
118
119void TestSink::ResetToAcceptingConnectionState() {
120 NOTREACHED();
121}
122
123#endif // defined(OS_POSIX) && !defined(OS_NACL)
124
brettw@google.comc0f85582011-01-21 08:50:27 +0900125} // namespace IPC