blob: 316609c3386d762a3ae6bd17cc1491073e7ae7b4 [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
16TestSink::TestSink() {
17}
18
19TestSink::~TestSink() {
20}
21
[email protected]3ff2a102011-01-20 23:50:2722bool TestSink::Send(Message* message) {
[email protected]90b721e62010-04-05 17:35:0123 OnMessageReceived(*message);
24 delete message;
25 return true;
26}
27
[email protected]2f60c9b2014-06-06 20:13:5128bool TestSink::Connect() {
29 NOTIMPLEMENTED();
30 return false;
31}
32
33void TestSink::Close() {
34 NOTIMPLEMENTED();
35}
36
37base::ProcessId TestSink::GetPeerPID() const {
38 NOTIMPLEMENTED();
39 return base::ProcessId();
40}
41
[email protected]64860882014-08-04 23:44:1742base::ProcessId TestSink::GetSelfPID() const {
43 NOTIMPLEMENTED();
44 return base::ProcessId();
45}
46
[email protected]6db8d9902010-12-24 08:36:2547bool TestSink::OnMessageReceived(const Message& msg) {
brettw5a1613dc2015-06-02 05:34:4348 base::ObserverListBase<Listener>::Iterator it(&filter_list_);
[email protected]b7f59e822012-06-29 22:05:2649 Listener* observer;
[email protected]9f9db892011-01-31 21:43:3150 while ((observer = it.GetNext()) != NULL) {
51 if (observer->OnMessageReceived(msg))
52 return true;
53 }
54
55 // No filter handled the message, so store it.
[email protected]445623e62010-03-25 23:20:2456 messages_.push_back(Message(msg));
[email protected]6db8d9902010-12-24 08:36:2557 return true;
[email protected]a16ed65e2009-02-14 01:35:2758}
59
60void TestSink::ClearMessages() {
61 messages_.clear();
62}
63
64const Message* TestSink::GetMessageAt(size_t index) const {
65 if (index >= messages_.size())
66 return NULL;
67 return &messages_[index];
68}
69
tfarina10a5c062015-09-04 18:47:5770const Message* TestSink::GetFirstMessageMatching(uint32_t id) const {
[email protected]a16ed65e2009-02-14 01:35:2771 for (size_t i = 0; i < messages_.size(); i++) {
72 if (messages_[i].type() == id)
73 return &messages_[i];
74 }
75 return NULL;
76}
77
tfarina10a5c062015-09-04 18:47:5778const Message* TestSink::GetUniqueMessageMatching(uint32_t id) const {
[email protected]a16ed65e2009-02-14 01:35:2779 size_t found_index = 0;
80 size_t found_count = 0;
81 for (size_t i = 0; i < messages_.size(); i++) {
82 if (messages_[i].type() == id) {
83 found_count++;
84 found_index = i;
85 }
86 }
87 if (found_count != 1)
88 return NULL; // Didn't find a unique one.
89 return &messages_[found_index];
90}
91
[email protected]b7f59e822012-06-29 22:05:2692void TestSink::AddFilter(Listener* filter) {
[email protected]9f9db892011-01-31 21:43:3193 filter_list_.AddObserver(filter);
94}
95
[email protected]b7f59e822012-06-29 22:05:2696void TestSink::RemoveFilter(Listener* filter) {
[email protected]9f9db892011-01-31 21:43:3197 filter_list_.RemoveObserver(filter);
98}
99
[email protected]2f60c9b2014-06-06 20:13:51100#if defined(OS_POSIX) && !defined(OS_NACL)
101
102int TestSink::GetClientFileDescriptor() const {
103 NOTREACHED();
104 return -1;
105}
106
morritaa409ccc2014-10-20 23:53:25107base::ScopedFD TestSink::TakeClientFileDescriptor() {
[email protected]2f60c9b2014-06-06 20:13:51108 NOTREACHED();
morritaa409ccc2014-10-20 23:53:25109 return base::ScopedFD();
[email protected]2f60c9b2014-06-06 20:13:51110}
111
[email protected]2f60c9b2014-06-06 20:13:51112#endif // defined(OS_POSIX) && !defined(OS_NACL)
113
[email protected]a16ed65e2009-02-14 01:35:27114} // namespace IPC