This is similar to https://codereview.chromium.org/328423002/ where we started archiv...
[chromium-blink-merge.git] / ipc / ipc_test_sink.cc
blobb1a21bfd4807a35b8409cc225ea89c05f91a8f2f
1 // 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.
5 #include "ipc/ipc_test_sink.h"
7 #include "ipc/ipc_listener.h"
8 #include "ipc/ipc_message.h"
10 namespace IPC {
12 TestSink::TestSink() {
15 TestSink::~TestSink() {
18 bool TestSink::Send(Message* message) {
19 OnMessageReceived(*message);
20 delete message;
21 return true;
24 bool TestSink::Connect() {
25 NOTIMPLEMENTED();
26 return false;
29 void TestSink::Close() {
30 NOTIMPLEMENTED();
33 base::ProcessId TestSink::GetPeerPID() const {
34 NOTIMPLEMENTED();
35 return base::ProcessId();
38 base::ProcessId TestSink::GetSelfPID() const {
39 NOTIMPLEMENTED();
40 return base::ProcessId();
43 ChannelHandle TestSink::TakePipeHandle() {
44 NOTIMPLEMENTED();
45 return ChannelHandle();
48 bool TestSink::OnMessageReceived(const Message& msg) {
49 ObserverListBase<Listener>::Iterator it(filter_list_);
50 Listener* observer;
51 while ((observer = it.GetNext()) != NULL) {
52 if (observer->OnMessageReceived(msg))
53 return true;
56 // No filter handled the message, so store it.
57 messages_.push_back(Message(msg));
58 return true;
61 void TestSink::ClearMessages() {
62 messages_.clear();
65 const Message* TestSink::GetMessageAt(size_t index) const {
66 if (index >= messages_.size())
67 return NULL;
68 return &messages_[index];
71 const 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];
76 return NULL;
79 const 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;
88 if (found_count != 1)
89 return NULL; // Didn't find a unique one.
90 return &messages_[found_index];
93 void TestSink::AddFilter(Listener* filter) {
94 filter_list_.AddObserver(filter);
97 void TestSink::RemoveFilter(Listener* filter) {
98 filter_list_.RemoveObserver(filter);
101 #if defined(OS_POSIX) && !defined(OS_NACL)
103 int TestSink::GetClientFileDescriptor() const {
104 NOTREACHED();
105 return -1;
108 int TestSink::TakeClientFileDescriptor() {
109 NOTREACHED();
110 return -1;
113 #endif // defined(OS_POSIX) && !defined(OS_NACL)
115 } // namespace IPC