brettw@google.com | c0f8558 | 2011-01-21 08:50:27 +0900 | [diff] [blame] | 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 "ipc/ipc_message.h" |
| 8 | |
| 9 | namespace IPC { |
| 10 | |
| 11 | TestSink::TestSink() { |
| 12 | } |
| 13 | |
| 14 | TestSink::~TestSink() { |
| 15 | } |
| 16 | |
| 17 | bool TestSink::Send(Message* message) { |
| 18 | OnMessageReceived(*message); |
| 19 | delete message; |
| 20 | return true; |
| 21 | } |
| 22 | |
| 23 | bool TestSink::OnMessageReceived(const Message& msg) { |
bryner@chromium.org | 44ae58f | 2011-02-01 06:43:31 +0900 | [diff] [blame] | 24 | 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.com | c0f8558 | 2011-01-21 08:50:27 +0900 | [diff] [blame] | 32 | messages_.push_back(Message(msg)); |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | void TestSink::ClearMessages() { |
| 37 | messages_.clear(); |
| 38 | } |
| 39 | |
| 40 | const Message* TestSink::GetMessageAt(size_t index) const { |
| 41 | if (index >= messages_.size()) |
| 42 | return NULL; |
| 43 | return &messages_[index]; |
| 44 | } |
| 45 | |
| 46 | const 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 | |
| 54 | const 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.org | 44ae58f | 2011-02-01 06:43:31 +0900 | [diff] [blame] | 68 | void TestSink::AddFilter(Channel::Listener* filter) { |
| 69 | filter_list_.AddObserver(filter); |
| 70 | } |
| 71 | |
| 72 | void TestSink::RemoveFilter(Channel::Listener* filter) { |
| 73 | filter_list_.RemoveObserver(filter); |
| 74 | } |
| 75 | |
brettw@google.com | c0f8558 | 2011-01-21 08:50:27 +0900 | [diff] [blame] | 76 | } // namespace IPC |