[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 | |||||
Chris Watkins | 2d879af | 2017-11-30 02:11:59 | [diff] [blame] | 16 | TestSink::TestSink() = default; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 17 | |
Chris Watkins | 2d879af | 2017-11-30 02:11:59 | [diff] [blame] | 18 | TestSink::~TestSink() = default; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 19 | |
[email protected] | 3ff2a10 | 2011-01-20 23:50:27 | [diff] [blame] | 20 | bool TestSink::Send(Message* message) { |
[email protected] | 90b721e6 | 2010-04-05 17:35:01 | [diff] [blame] | 21 | OnMessageReceived(*message); |
22 | delete message; | ||||
23 | return true; | ||||
24 | } | ||||
25 | |||||
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 26 | bool TestSink::Connect() { |
27 | NOTIMPLEMENTED(); | ||||
28 | return false; | ||||
29 | } | ||||
30 | |||||
31 | void TestSink::Close() { | ||||
32 | NOTIMPLEMENTED(); | ||||
33 | } | ||||
34 | |||||
[email protected] | 6db8d990 | 2010-12-24 08:36:25 | [diff] [blame] | 35 | bool TestSink::OnMessageReceived(const Message& msg) { |
dcheng | 6b787f9 | 2016-10-12 23:54:47 | [diff] [blame] | 36 | for (auto& observer : filter_list_) { |
37 | if (observer.OnMessageReceived(msg)) | ||||
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 38 | return true; |
39 | } | ||||
40 | |||||
41 | // No filter handled the message, so store it. | ||||
[email protected] | 445623e6 | 2010-03-25 23:20:24 | [diff] [blame] | 42 | messages_.push_back(Message(msg)); |
[email protected] | 6db8d990 | 2010-12-24 08:36:25 | [diff] [blame] | 43 | return true; |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 44 | } |
45 | |||||
46 | void TestSink::ClearMessages() { | ||||
47 | messages_.clear(); | ||||
48 | } | ||||
49 | |||||
50 | const Message* TestSink::GetMessageAt(size_t index) const { | ||||
51 | if (index >= messages_.size()) | ||||
52 | return NULL; | ||||
53 | return &messages_[index]; | ||||
54 | } | ||||
55 | |||||
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 56 | const Message* TestSink::GetFirstMessageMatching(uint32_t id) const { |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 57 | 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 | |||||
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 64 | const Message* TestSink::GetUniqueMessageMatching(uint32_t id) const { |
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 65 | 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] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 78 | void TestSink::AddFilter(Listener* filter) { |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 79 | filter_list_.AddObserver(filter); |
80 | } | ||||
81 | |||||
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 82 | void TestSink::RemoveFilter(Listener* filter) { |
[email protected] | 9f9db89 | 2011-01-31 21:43:31 | [diff] [blame] | 83 | filter_list_.RemoveObserver(filter); |
84 | } | ||||
85 | |||||
[email protected] | a16ed65e | 2009-02-14 01:35:27 | [diff] [blame] | 86 | } // namespace IPC |