blob: 6aa80785a06be24223e0c4c3904dab20bc66c0aa [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
avi246998d82015-12-22 02:39:047#include <stddef.h>
8#include <stdint.h>
9
10#include "build/build_config.h"
[email protected]0ee990682012-11-17 19:20:0511#include "ipc/ipc_listener.h"
[email protected]a83d42292010-08-17 22:51:1012#include "ipc/ipc_message.h"
[email protected]a16ed65e2009-02-14 01:35:2713
14namespace IPC {
15
Chris Watkins2d879af2017-11-30 02:11:5916TestSink::TestSink() = default;
[email protected]a16ed65e2009-02-14 01:35:2717
Chris Watkins2d879af2017-11-30 02:11:5918TestSink::~TestSink() = default;
[email protected]a16ed65e2009-02-14 01:35:2719
[email protected]3ff2a102011-01-20 23:50:2720bool TestSink::Send(Message* message) {
[email protected]90b721e62010-04-05 17:35:0121 OnMessageReceived(*message);
22 delete message;
23 return true;
24}
25
[email protected]2f60c9b2014-06-06 20:13:5126bool TestSink::Connect() {
27 NOTIMPLEMENTED();
28 return false;
29}
30
31void TestSink::Close() {
32 NOTIMPLEMENTED();
33}
34
[email protected]6db8d9902010-12-24 08:36:2535bool TestSink::OnMessageReceived(const Message& msg) {
dcheng6b787f92016-10-12 23:54:4736 for (auto& observer : filter_list_) {
37 if (observer.OnMessageReceived(msg))
[email protected]9f9db892011-01-31 21:43:3138 return true;
39 }
40
41 // No filter handled the message, so store it.
[email protected]445623e62010-03-25 23:20:2442 messages_.push_back(Message(msg));
[email protected]6db8d9902010-12-24 08:36:2543 return true;
[email protected]a16ed65e2009-02-14 01:35:2744}
45
46void TestSink::ClearMessages() {
47 messages_.clear();
48}
49
50const Message* TestSink::GetMessageAt(size_t index) const {
51 if (index >= messages_.size())
52 return NULL;
53 return &messages_[index];
54}
55
tfarina10a5c062015-09-04 18:47:5756const Message* TestSink::GetFirstMessageMatching(uint32_t id) const {
[email protected]a16ed65e2009-02-14 01:35:2757 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
tfarina10a5c062015-09-04 18:47:5764const Message* TestSink::GetUniqueMessageMatching(uint32_t id) const {
[email protected]a16ed65e2009-02-14 01:35:2765 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
[email protected]b7f59e822012-06-29 22:05:2678void TestSink::AddFilter(Listener* filter) {
[email protected]9f9db892011-01-31 21:43:3179 filter_list_.AddObserver(filter);
80}
81
[email protected]b7f59e822012-06-29 22:05:2682void TestSink::RemoveFilter(Listener* filter) {
[email protected]9f9db892011-01-31 21:43:3183 filter_list_.RemoveObserver(filter);
84}
85
[email protected]a16ed65e2009-02-14 01:35:2786} // namespace IPC