OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/spy/spy.h" | 5 #include "mojo/spy/spy.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/threading/worker_pool.h" | 11 #include "base/threading/worker_pool.h" |
12 | 12 |
13 #include "mojo/public/cpp/system/core.h" | 13 #include "mojo/public/cpp/system/core.h" |
14 #include "mojo/service_manager/service_manager.h" | 14 #include "mojo/service_manager/service_manager.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const size_t kMessageBufSize = 2 * 1024; | 18 const size_t kMessageBufSize = 2 * 1024; |
19 const size_t kHandleBufSize = 64; | 19 const size_t kHandleBufSize = 64; |
20 | 20 |
| 21 void CloseHandles(MojoHandle* handles, size_t count) { |
| 22 for (size_t ix = 0; ix != count; ++count) |
| 23 MojoClose(handles[ix]); |
| 24 } |
| 25 |
21 // In charge of processing messages that flow over a | 26 // In charge of processing messages that flow over a |
22 // single message pipe. | 27 // single message pipe. |
23 class MessageProcessor : | 28 class MessageProcessor : |
24 public base::RefCountedThreadSafe<MessageProcessor> { | 29 public base::RefCountedThreadSafe<MessageProcessor> { |
25 public: | 30 public: |
26 | 31 |
27 MessageProcessor() | 32 MessageProcessor() |
28 : last_result_(MOJO_RESULT_OK), | 33 : last_result_(MOJO_RESULT_OK), |
29 bytes_transfered_(0) { | 34 bytes_transfered_(0) { |
30 | 35 |
31 message_count_[0] = 0; | 36 message_count_[0] = 0; |
32 message_count_[1] = 0; | 37 message_count_[1] = 0; |
33 handle_count_[0] = 0; | 38 handle_count_[0] = 0; |
34 handle_count_[1] = 0; | 39 handle_count_[1] = 0; |
35 } | 40 } |
36 | 41 |
37 void Start(mojo::ScopedMessagePipeHandle client, | 42 void Start(mojo::ScopedMessagePipeHandle client, |
38 mojo::ScopedMessagePipeHandle interceptor) { | 43 mojo::ScopedMessagePipeHandle interceptor) { |
39 std::vector<mojo::MessagePipeHandle> pipes; | 44 std::vector<mojo::MessagePipeHandle> pipes; |
40 pipes.push_back(client.get()); | 45 pipes.push_back(client.get()); |
41 pipes.push_back(interceptor.get()); | 46 pipes.push_back(interceptor.get()); |
42 std::vector<MojoWaitFlags> wait_flags; | 47 std::vector<MojoWaitFlags> wait_flags; |
43 wait_flags.push_back(MOJO_WAIT_FLAG_READABLE); | 48 wait_flags.push_back(MOJO_WAIT_FLAG_READABLE); |
44 wait_flags.push_back(MOJO_WAIT_FLAG_READABLE); | 49 wait_flags.push_back(MOJO_WAIT_FLAG_READABLE); |
45 | 50 |
46 scoped_ptr<char> mbuf(new char[kMessageBufSize]); | 51 scoped_ptr<char[]> mbuf(new char[kMessageBufSize]); |
47 scoped_ptr<MojoHandle> hbuf(new MojoHandle[kHandleBufSize]); | 52 scoped_ptr<MojoHandle[]> hbuf(new MojoHandle[kHandleBufSize]); |
48 | 53 |
49 // Main processing loop: | 54 // Main processing loop: |
50 // 1- Wait for an endpoint to have a message. | 55 // 1- Wait for an endpoint to have a message. |
51 // 2- Read the message | 56 // 2- Read the message |
52 // 3- Log data | 57 // 3- Log data |
53 // 4- Wait until the opposite port is ready for writting | 58 // 4- Wait until the opposite port is ready for writting |
54 // 4- Write the message to opposite port. | 59 // 4- Write the message to opposite port. |
55 | 60 |
56 for (;;) { | 61 for (;;) { |
57 int r = WaitMany(pipes, wait_flags, MOJO_DEADLINE_INDEFINITE); | 62 int r = WaitMany(pipes, wait_flags, MOJO_DEADLINE_INDEFINITE); |
(...skipping 22 matching lines...) Expand all Loading... |
80 | 85 |
81 mojo::MessagePipeHandle write_handle = (r == 0) ? pipes[1] : pipes[0]; | 86 mojo::MessagePipeHandle write_handle = (r == 0) ? pipes[1] : pipes[0]; |
82 if (!CheckResult(Wait(write_handle, | 87 if (!CheckResult(Wait(write_handle, |
83 MOJO_WAIT_FLAG_WRITABLE, | 88 MOJO_WAIT_FLAG_WRITABLE, |
84 MOJO_DEADLINE_INDEFINITE))) | 89 MOJO_DEADLINE_INDEFINITE))) |
85 break; | 90 break; |
86 | 91 |
87 if (!CheckResult(WriteMessageRaw(write_handle, | 92 if (!CheckResult(WriteMessageRaw(write_handle, |
88 mbuf.get(), bytes_read, | 93 mbuf.get(), bytes_read, |
89 hbuf.get(), handles_read, | 94 hbuf.get(), handles_read, |
90 MOJO_WRITE_MESSAGE_FLAG_NONE))) | 95 MOJO_WRITE_MESSAGE_FLAG_NONE))) { |
| 96 // On failure we own the handles. For now just close them. |
| 97 if (handles_read) |
| 98 CloseHandles(hbuf.get(), handles_read); |
91 break; | 99 break; |
| 100 } |
92 } | 101 } |
93 | |
94 } | 102 } |
95 | 103 |
96 private: | 104 private: |
97 friend class base::RefCountedThreadSafe<MessageProcessor>; | 105 friend class base::RefCountedThreadSafe<MessageProcessor>; |
98 virtual ~MessageProcessor() {} | 106 virtual ~MessageProcessor() {} |
99 | 107 |
100 bool CheckResult(MojoResult mr) { | 108 bool CheckResult(MojoResult mr) { |
101 if (mr == MOJO_RESULT_OK) | 109 if (mr == MOJO_RESULT_OK) |
102 return true; | 110 return true; |
103 last_result_ = mr; | 111 last_result_ = mr; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 Spy::Spy(mojo::ServiceManager* service_manager, const std::string& options) { | 159 Spy::Spy(mojo::ServiceManager* service_manager, const std::string& options) { |
152 service_manager->SetInterceptor(new SpyInterceptor()); | 160 service_manager->SetInterceptor(new SpyInterceptor()); |
153 } | 161 } |
154 | 162 |
155 Spy::~Spy(){ | 163 Spy::~Spy(){ |
156 // TODO(cpu): Do not leak the interceptor. Lifetime between the | 164 // TODO(cpu): Do not leak the interceptor. Lifetime between the |
157 // service_manager and the spy is still unclear hence the leak. | 165 // service_manager and the spy is still unclear hence the leak. |
158 } | 166 } |
159 | 167 |
160 } // namespace mojo | 168 } // namespace mojo |
OLD | NEW |