blob: 070fe1235d138a90554ffa87c7e5caab5d94e126 [file] [log] [blame]
[email protected]3ff2a102011-01-20 23:50:271// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]a16ed65e2009-02-14 01:35:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]3ff2a102011-01-20 23:50:275#include "ipc/ipc_test_sink.h"
6
[email protected]0ee990682012-11-17 19:20:057#include "ipc/ipc_listener.h"
[email protected]a83d42292010-08-17 22:51:108#include "ipc/ipc_message.h"
[email protected]a16ed65e2009-02-14 01:35:279
10namespace IPC {
11
12TestSink::TestSink() {
13}
14
15TestSink::~TestSink() {
16}
17
[email protected]3ff2a102011-01-20 23:50:2718bool TestSink::Send(Message* message) {
[email protected]90b721e62010-04-05 17:35:0119 OnMessageReceived(*message);
20 delete message;
21 return true;
22}
23
[email protected]6db8d9902010-12-24 08:36:2524bool TestSink::OnMessageReceived(const Message& msg) {
[email protected]b7f59e822012-06-29 22:05:2625 ObserverListBase<Listener>::Iterator it(filter_list_);
26 Listener* observer;
[email protected]9f9db892011-01-31 21:43:3127 while ((observer = it.GetNext()) != NULL) {
28 if (observer->OnMessageReceived(msg))
29 return true;
30 }
31
32 // No filter handled the message, so store it.
[email protected]445623e62010-03-25 23:20:2433 messages_.push_back(Message(msg));
[email protected]6db8d9902010-12-24 08:36:2534 return true;
[email protected]a16ed65e2009-02-14 01:35:2735}
36
37void TestSink::ClearMessages() {
38 messages_.clear();
39}
40
41const Message* TestSink::GetMessageAt(size_t index) const {
42 if (index >= messages_.size())
43 return NULL;
44 return &messages_[index];
45}
46
[email protected]168ae922009-12-04 18:08:4547const Message* TestSink::GetFirstMessageMatching(uint32 id) const {
[email protected]a16ed65e2009-02-14 01:35:2748 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
[email protected]168ae922009-12-04 18:08:4555const Message* TestSink::GetUniqueMessageMatching(uint32 id) const {
[email protected]a16ed65e2009-02-14 01:35:2756 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
[email protected]b7f59e822012-06-29 22:05:2669void TestSink::AddFilter(Listener* filter) {
[email protected]9f9db892011-01-31 21:43:3170 filter_list_.AddObserver(filter);
71}
72
[email protected]b7f59e822012-06-29 22:05:2673void TestSink::RemoveFilter(Listener* filter) {
[email protected]9f9db892011-01-31 21:43:3174 filter_list_.RemoveObserver(filter);
75}
76
[email protected]a16ed65e2009-02-14 01:35:2777} // namespace IPC