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. 4 5 #include "ipc/ipc_test_sink.h" 6 7 #include <stddef.h> 8 #include <stdint.h> 9 10 #include "build/build_config.h" 11 #include "ipc/ipc_listener.h" 12 #include "ipc/ipc_message.h" 13 14 namespace IPC { 15 16 TestSink::TestSink() = default; 17 18 TestSink::~TestSink() = default; 19 Send(Message * message)20bool TestSink::Send(Message* message) { 21 OnMessageReceived(*message); 22 delete message; 23 return true; 24 } 25 Connect()26bool TestSink::Connect() { 27 NOTIMPLEMENTED(); 28 return false; 29 } 30 Close()31void TestSink::Close() { 32 NOTIMPLEMENTED(); 33 } 34 OnMessageReceived(const Message & msg)35bool TestSink::OnMessageReceived(const Message& msg) { 36 for (auto& observer : filter_list_) { 37 if (observer.OnMessageReceived(msg)) 38 return true; 39 } 40 41 // No filter handled the message, so store it. 42 messages_.push_back(Message(msg)); 43 return true; 44 } 45 ClearMessages()46void TestSink::ClearMessages() { 47 messages_.clear(); 48 } 49 GetMessageAt(size_t index) const50const Message* TestSink::GetMessageAt(size_t index) const { 51 if (index >= messages_.size()) 52 return NULL; 53 return &messages_[index]; 54 } 55 GetFirstMessageMatching(uint32_t id) const56const Message* TestSink::GetFirstMessageMatching(uint32_t id) const { 57 for (size_t i = 0; i < messages_.size(); i++) { 58 if (messages_[i].type() == id) 59 return &messages_[i]; 60 } 61 return NULL; 62 } 63 GetUniqueMessageMatching(uint32_t id) const64const Message* TestSink::GetUniqueMessageMatching(uint32_t id) const { 65 size_t found_index = 0; 66 size_t found_count = 0; 67 for (size_t i = 0; i < messages_.size(); i++) { 68 if (messages_[i].type() == id) { 69 found_count++; 70 found_index = i; 71 } 72 } 73 if (found_count != 1) 74 return NULL; // Didn't find a unique one. 75 return &messages_[found_index]; 76 } 77 AddFilter(Listener * filter)78void TestSink::AddFilter(Listener* filter) { 79 filter_list_.AddObserver(filter); 80 } 81 RemoveFilter(Listener * filter)82void TestSink::RemoveFilter(Listener* filter) { 83 filter_list_.RemoveObserver(filter); 84 } 85 86 } // namespace IPC 87