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 | |
tfarina@chromium.org | beba292 | 2012-11-18 04:20:05 +0900 | [diff] [blame] | 7 | #include "ipc/ipc_listener.h" |
brettw@google.com | c0f8558 | 2011-01-21 08:50:27 +0900 | [diff] [blame] | 8 | #include "ipc/ipc_message.h" |
| 9 | |
| 10 | namespace IPC { |
| 11 | |
| 12 | TestSink::TestSink() { |
| 13 | } |
| 14 | |
| 15 | TestSink::~TestSink() { |
| 16 | } |
| 17 | |
| 18 | bool TestSink::Send(Message* message) { |
| 19 | OnMessageReceived(*message); |
| 20 | delete message; |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | bool TestSink::OnMessageReceived(const Message& msg) { |
brettw@chromium.org | db1259e | 2012-06-30 07:05:26 +0900 | [diff] [blame] | 25 | ObserverListBase<Listener>::Iterator it(filter_list_); |
| 26 | Listener* observer; |
bryner@chromium.org | 44ae58f | 2011-02-01 06:43:31 +0900 | [diff] [blame] | 27 | while ((observer = it.GetNext()) != NULL) { |
| 28 | if (observer->OnMessageReceived(msg)) |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | // No filter handled the message, so store it. |
brettw@google.com | c0f8558 | 2011-01-21 08:50:27 +0900 | [diff] [blame] | 33 | messages_.push_back(Message(msg)); |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | void TestSink::ClearMessages() { |
| 38 | messages_.clear(); |
| 39 | } |
| 40 | |
| 41 | const Message* TestSink::GetMessageAt(size_t index) const { |
| 42 | if (index >= messages_.size()) |
| 43 | return NULL; |
| 44 | return &messages_[index]; |
| 45 | } |
| 46 | |
| 47 | const Message* TestSink::GetFirstMessageMatching(uint32 id) const { |
| 48 | for (size_t i = 0; i < messages_.size(); i++) { |
| 49 | if (messages_[i].type() == id) |
| 50 | return &messages_[i]; |
| 51 | } |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | const Message* TestSink::GetUniqueMessageMatching(uint32 id) const { |
| 56 | size_t found_index = 0; |
| 57 | size_t found_count = 0; |
| 58 | for (size_t i = 0; i < messages_.size(); i++) { |
| 59 | if (messages_[i].type() == id) { |
| 60 | found_count++; |
| 61 | found_index = i; |
| 62 | } |
| 63 | } |
| 64 | if (found_count != 1) |
| 65 | return NULL; // Didn't find a unique one. |
| 66 | return &messages_[found_index]; |
| 67 | } |
| 68 | |
brettw@chromium.org | db1259e | 2012-06-30 07:05:26 +0900 | [diff] [blame] | 69 | void TestSink::AddFilter(Listener* filter) { |
bryner@chromium.org | 44ae58f | 2011-02-01 06:43:31 +0900 | [diff] [blame] | 70 | filter_list_.AddObserver(filter); |
| 71 | } |
| 72 | |
brettw@chromium.org | db1259e | 2012-06-30 07:05:26 +0900 | [diff] [blame] | 73 | void TestSink::RemoveFilter(Listener* filter) { |
bryner@chromium.org | 44ae58f | 2011-02-01 06:43:31 +0900 | [diff] [blame] | 74 | filter_list_.RemoveObserver(filter); |
| 75 | } |
| 76 | |
brettw@google.com | c0f8558 | 2011-01-21 08:50:27 +0900 | [diff] [blame] | 77 | } // namespace IPC |