[email protected] | 3ff2a10 | 2011-01-20 23:50:27 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 3ff2a10 | 2011-01-20 23:50:27 | [diff] [blame] | 5 | #include "ipc/ipc_test_sink.h" |
6 | |||||
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
8 | #include <stdint.h> | ||||
9 | |||||
10 | #include "build/build_config.h" | ||||
[email protected] | 0ee99068 | 2012-11-17 19:20:05 | [diff] [blame] | 11 | #include "ipc/ipc_listener.h" |
[email protected] | a83d4229 | 2010-08-17 22:51:10 | [diff] [blame] | 12 | #include "ipc/ipc_message.h" |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 13 | |
14 | namespace IPC { | ||||
15 | |||||
16 | TestSink::TestSink() { | ||||
17 | } | ||||
18 | |||||
19 | TestSink::~TestSink() { | ||||
20 | } | ||||
21 | |||||
[email protected] | 3ff2a10 | 2011-01-20 23:50:27 | [diff] [blame] | 22 | bool TestSink::Send(Message* message) { |
[email protected] | 90b721e6 | 2010-04-05 17:35:01 | [diff] [blame] | 23 | OnMessageReceived(*message); |
24 | delete message; | ||||
25 | return true; | ||||
26 | } | ||||
27 | |||||
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 28 | bool TestSink::Connect() { |
29 | NOTIMPLEMENTED(); | ||||
30 | return false; | ||||
31 | } | ||||
32 | |||||
33 | void TestSink::Close() { | ||||
34 | NOTIMPLEMENTED(); | ||||
35 | } | ||||
36 | |||||
37 | base::ProcessId TestSink::GetPeerPID() const { | ||||
38 | NOTIMPLEMENTED(); | ||||
39 | return base::ProcessId(); | ||||
40 | } | ||||
41 | |||||
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 42 | base::ProcessId TestSink::GetSelfPID() const { |
43 | NOTIMPLEMENTED(); | ||||
44 | return base::ProcessId(); | ||||
45 | } | ||||
46 | |||||
[email protected] | 6db8d990 | 2010-12-24 08:36:25 | [diff] [blame] | 47 | bool TestSink::OnMessageReceived(const Message& msg) { |
brettw | 5a1613dc | 2015-06-02 05:34:43 | [diff] [blame] | 48 | base::ObserverListBase<Listener>::Iterator it(&filter_list_); |
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 49 | Listener* observer; |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 50 | 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] | 445623e6 | 2010-03-25 23:20:24 | [diff] [blame] | 56 | messages_.push_back(Message(msg)); |
[email protected] | 6db8d990 | 2010-12-24 08:36:25 | [diff] [blame] | 57 | return true; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 58 | } |
59 | |||||
60 | void TestSink::ClearMessages() { | ||||
61 | messages_.clear(); | ||||
62 | } | ||||
63 | |||||
64 | const Message* TestSink::GetMessageAt(size_t index) const { | ||||
65 | if (index >= messages_.size()) | ||||
66 | return NULL; | ||||
67 | return &messages_[index]; | ||||
68 | } | ||||
69 | |||||
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 70 | const Message* TestSink::GetFirstMessageMatching(uint32_t id) const { |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 71 | 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 | |||||
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 78 | const Message* TestSink::GetUniqueMessageMatching(uint32_t id) const { |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 79 | 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] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 92 | void TestSink::AddFilter(Listener* filter) { |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 93 | filter_list_.AddObserver(filter); |
94 | } | ||||
95 | |||||
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 96 | void TestSink::RemoveFilter(Listener* filter) { |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 97 | filter_list_.RemoveObserver(filter); |
98 | } | ||||
99 | |||||
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 100 | #if defined(OS_POSIX) && !defined(OS_NACL) |
101 | |||||
102 | int TestSink::GetClientFileDescriptor() const { | ||||
103 | NOTREACHED(); | ||||
104 | return -1; | ||||
105 | } | ||||
106 | |||||
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 107 | base::ScopedFD TestSink::TakeClientFileDescriptor() { |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 108 | NOTREACHED(); |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 109 | return base::ScopedFD(); |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 110 | } |
111 | |||||
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 112 | #endif // defined(OS_POSIX) && !defined(OS_NACL) |
113 | |||||
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 114 | } // namespace IPC |