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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
17 #include "base/threading/worker_pool.h" | 18 #include "base/threading/worker_pool.h" |
18 | 19 #include "base/time/time.h" |
19 #include "mojo/public/cpp/system/core.h" | 20 #include "mojo/public/cpp/system/core.h" |
20 #include "mojo/service_manager/service_manager.h" | 21 #include "mojo/service_manager/service_manager.h" |
| 22 #include "mojo/spy/common.h" |
21 #include "mojo/spy/public/spy.mojom.h" | 23 #include "mojo/spy/public/spy.mojom.h" |
22 #include "mojo/spy/spy_server_impl.h" | 24 #include "mojo/spy/spy_server_impl.h" |
23 #include "mojo/spy/websocket_server.h" | 25 #include "mojo/spy/websocket_server.h" |
24 #include "url/gurl.h" | 26 #include "url/gurl.h" |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
| 30 mojo::WebSocketServer* ws_server = NULL; |
| 31 |
28 const size_t kMessageBufSize = 2 * 1024; | 32 const size_t kMessageBufSize = 2 * 1024; |
29 const size_t kHandleBufSize = 64; | 33 const size_t kHandleBufSize = 64; |
30 const int kDefaultWebSocketPort = 42424; | 34 const int kDefaultWebSocketPort = 42424; |
31 | 35 |
32 void CloseHandles(MojoHandle* handles, size_t count) { | 36 void CloseHandles(MojoHandle* handles, size_t count) { |
33 for (size_t ix = 0; ix != count; ++count) | 37 for (size_t ix = 0; ix != count; ++count) |
34 MojoClose(handles[ix]); | 38 MojoClose(handles[ix]); |
35 } | 39 } |
36 | 40 |
37 // In charge of processing messages that flow over a | 41 // In charge of processing messages that flow over a |
38 // single message pipe. | 42 // single message pipe. |
39 class MessageProcessor : | 43 class MessageProcessor : |
40 public base::RefCountedThreadSafe<MessageProcessor> { | 44 public base::RefCountedThreadSafe<MessageProcessor> { |
41 public: | 45 public: |
42 MessageProcessor() | 46 MessageProcessor(base::MessageLoopProxy* control_loop_proxy) |
43 : last_result_(MOJO_RESULT_OK), | 47 : last_result_(MOJO_RESULT_OK), |
44 bytes_transfered_(0) { | 48 bytes_transfered_(0), |
| 49 control_loop_proxy_(control_loop_proxy), |
| 50 service_vendor_message_pipe_received_(false) { |
45 message_count_[0] = 0; | 51 message_count_[0] = 0; |
46 message_count_[1] = 0; | 52 message_count_[1] = 0; |
47 handle_count_[0] = 0; | 53 handle_count_[0] = 0; |
48 handle_count_[1] = 0; | 54 handle_count_[1] = 0; |
49 } | 55 } |
50 | 56 |
51 void Start(mojo::ScopedMessagePipeHandle client, | 57 void Start(mojo::ScopedMessagePipeHandle client, |
52 mojo::ScopedMessagePipeHandle interceptor) { | 58 mojo::ScopedMessagePipeHandle interceptor, |
| 59 const GURL& url) { |
53 std::vector<mojo::MessagePipeHandle> pipes; | 60 std::vector<mojo::MessagePipeHandle> pipes; |
54 pipes.push_back(client.get()); | 61 pipes.push_back(client.get()); |
55 pipes.push_back(interceptor.get()); | 62 pipes.push_back(interceptor.get()); |
56 std::vector<MojoHandleSignals> handle_signals; | 63 std::vector<MojoHandleSignals> handle_signals; |
57 handle_signals.push_back(MOJO_HANDLE_SIGNAL_READABLE); | 64 handle_signals.push_back(MOJO_HANDLE_SIGNAL_READABLE); |
58 handle_signals.push_back(MOJO_HANDLE_SIGNAL_READABLE); | 65 handle_signals.push_back(MOJO_HANDLE_SIGNAL_READABLE); |
59 | 66 |
60 scoped_ptr<char[]> mbuf(new char[kMessageBufSize]); | 67 scoped_ptr<char[]> mbuf(new char[kMessageBufSize]); |
61 scoped_ptr<MojoHandle[]> hbuf(new MojoHandle[kHandleBufSize]); | 68 scoped_ptr<MojoHandle[]> hbuf(new MojoHandle[kHandleBufSize]); |
62 | 69 |
(...skipping 16 matching lines...) Expand all Loading... |
79 | 86 |
80 if (!CheckResult(ReadMessageRaw(pipes[r], | 87 if (!CheckResult(ReadMessageRaw(pipes[r], |
81 mbuf.get(), &bytes_read, | 88 mbuf.get(), &bytes_read, |
82 hbuf.get(), &handles_read, | 89 hbuf.get(), &handles_read, |
83 MOJO_READ_MESSAGE_FLAG_NONE))) | 90 MOJO_READ_MESSAGE_FLAG_NONE))) |
84 break; | 91 break; |
85 | 92 |
86 if (!bytes_read && !handles_read) | 93 if (!bytes_read && !handles_read) |
87 continue; | 94 continue; |
88 | 95 |
89 if (handles_read) | 96 if (handles_read) { |
90 handle_count_[r] += handles_read; | 97 handle_count_[r] += handles_read; |
91 | 98 |
| 99 // Intercept the first set of handles to message pipes with the |
| 100 // assumption that these would be used for vending mojo services. |
| 101 // TODO(ananta) |
| 102 // The above approach is hacky and could cause us to miss other message |
| 103 // pipes which could be exchanged between the client and the server. |
| 104 // Look into a cleaner way of identifying message pipe handles. |
| 105 if (!service_vendor_message_pipe_received_) { |
| 106 service_vendor_message_pipe_received_ = true; |
| 107 for (uint32_t i = 0; i < handles_read; i++) { |
| 108 mojo::ScopedMessagePipeHandle message_pipe_handle; |
| 109 message_pipe_handle.reset(mojo::MessagePipeHandle(hbuf[i])); |
| 110 |
| 111 mojo::ScopedMessagePipeHandle faux_client; |
| 112 mojo::ScopedMessagePipeHandle interceptor; |
| 113 CreateMessagePipe(NULL, &faux_client, &interceptor); |
| 114 |
| 115 base::WorkerPool::PostTask( |
| 116 FROM_HERE, |
| 117 base::Bind(&MessageProcessor::Start, |
| 118 this, |
| 119 base::Passed(&message_pipe_handle), |
| 120 base::Passed(&interceptor), |
| 121 url), |
| 122 true); |
| 123 hbuf.get()[i] = faux_client.release().value(); |
| 124 } |
| 125 } |
| 126 } |
92 ++message_count_[r]; | 127 ++message_count_[r]; |
93 bytes_transfered_ += bytes_read; | 128 bytes_transfered_ += bytes_read; |
94 | 129 |
| 130 LogMessageInfo(mbuf.get(), url); |
| 131 |
95 mojo::MessagePipeHandle write_handle = (r == 0) ? pipes[1] : pipes[0]; | 132 mojo::MessagePipeHandle write_handle = (r == 0) ? pipes[1] : pipes[0]; |
96 if (!CheckResult(Wait(write_handle, | 133 if (!CheckResult(Wait(write_handle, |
97 MOJO_HANDLE_SIGNAL_WRITABLE, | 134 MOJO_HANDLE_SIGNAL_WRITABLE, |
98 MOJO_DEADLINE_INDEFINITE))) | 135 MOJO_DEADLINE_INDEFINITE))) |
99 break; | 136 break; |
100 | 137 |
101 if (!CheckResult(WriteMessageRaw(write_handle, | 138 if (!CheckResult(WriteMessageRaw(write_handle, |
102 mbuf.get(), bytes_read, | 139 mbuf.get(), bytes_read, |
103 hbuf.get(), handles_read, | 140 hbuf.get(), handles_read, |
104 MOJO_WRITE_MESSAGE_FLAG_NONE))) { | 141 MOJO_WRITE_MESSAGE_FLAG_NONE))) { |
105 // On failure we own the handles. For now just close them. | 142 // On failure we own the handles. For now just close them. |
106 if (handles_read) | 143 if (handles_read) |
107 CloseHandles(hbuf.get(), handles_read); | 144 CloseHandles(hbuf.get(), handles_read); |
108 break; | 145 break; |
109 } | 146 } |
110 } | 147 } |
111 } | 148 } |
112 | 149 |
113 private: | 150 private: |
114 friend class base::RefCountedThreadSafe<MessageProcessor>; | 151 friend class base::RefCountedThreadSafe<MessageProcessor>; |
115 virtual ~MessageProcessor() {} | 152 virtual ~MessageProcessor() {} |
116 | 153 |
117 bool CheckResult(MojoResult mr) { | 154 bool CheckResult(MojoResult mr) { |
118 if (mr == MOJO_RESULT_OK) | 155 if (mr == MOJO_RESULT_OK) |
119 return true; | 156 return true; |
120 last_result_ = mr; | 157 last_result_ = mr; |
121 return false; | 158 return false; |
122 } | 159 } |
123 | 160 |
| 161 void LogInvalidMessage(const mojo::MojoMessageHeader& header) { |
| 162 LOG(ERROR) << "Invalid message: Number of Fields: " |
| 163 << header.num_fields |
| 164 << " Number of bytes: " |
| 165 << header.num_bytes |
| 166 << " Flags: " |
| 167 << header.flags; |
| 168 } |
| 169 |
| 170 // Validates the message as per the mojo spec. |
| 171 bool IsValidMessage(const mojo::MojoMessageHeader& header) { |
| 172 if (header.num_fields == 2) { |
| 173 if (header.num_bytes != sizeof(mojo::MojoMessageHeader)) { |
| 174 LogInvalidMessage(header); |
| 175 return false; |
| 176 } |
| 177 } else if (header.num_fields == 3) { |
| 178 if (header.num_bytes != sizeof(mojo::MojoRequestHeader)) { |
| 179 LogInvalidMessage(header); |
| 180 } |
| 181 } else if (header.num_fields > 3) { |
| 182 if (header.num_bytes < sizeof(mojo::MojoRequestHeader)) { |
| 183 LogInvalidMessage(header); |
| 184 return false; |
| 185 } |
| 186 } |
| 187 // These flags should be specified in request or response messages. |
| 188 if (header.num_fields < 3 && |
| 189 ((header.flags & mojo::kMessageExpectsResponse) || |
| 190 (header.flags & mojo::kMessageIsResponse))) { |
| 191 LOG(ERROR) << "Invalid request message."; |
| 192 LogInvalidMessage(header); |
| 193 return false; |
| 194 } |
| 195 // These flags are mutually exclusive. |
| 196 if ((header.flags & mojo::kMessageExpectsResponse) && |
| 197 (header.flags & mojo::kMessageIsResponse)) { |
| 198 LOG(ERROR) << "Invalid flags combination in request message."; |
| 199 LogInvalidMessage(header); |
| 200 return false; |
| 201 } |
| 202 return true; |
| 203 } |
| 204 |
| 205 void LogMessageInfo(void* data, const GURL& url) { |
| 206 mojo::MojoMessageData* message_data = |
| 207 reinterpret_cast<mojo::MojoMessageData*>(data); |
| 208 if (IsValidMessage(message_data->header)) { |
| 209 control_loop_proxy_->PostTask( |
| 210 FROM_HERE, |
| 211 base::Bind(&mojo::WebSocketServer::LogMessageInfo, |
| 212 base::Unretained(ws_server), |
| 213 message_data->header, url, base::Time::Now())); |
| 214 } |
| 215 } |
| 216 |
124 MojoResult last_result_; | 217 MojoResult last_result_; |
125 uint32_t bytes_transfered_; | 218 uint32_t bytes_transfered_; |
126 uint32_t message_count_[2]; | 219 uint32_t message_count_[2]; |
127 uint32_t handle_count_[2]; | 220 uint32_t handle_count_[2]; |
| 221 scoped_refptr<base::MessageLoopProxy> control_loop_proxy_; |
| 222 // This flag helps us intercept the first message pipe exchanged between |
| 223 // the client and the service vendor. |
| 224 bool service_vendor_message_pipe_received_; |
128 }; | 225 }; |
129 | 226 |
130 // In charge of intercepting access to the service manager. | 227 // In charge of intercepting access to the service manager. |
131 class SpyInterceptor : public mojo::ServiceManager::Interceptor { | 228 class SpyInterceptor : public mojo::ServiceManager::Interceptor { |
132 public: | 229 public: |
133 explicit SpyInterceptor(scoped_refptr<mojo::SpyServerImpl> spy_server) | 230 explicit SpyInterceptor(scoped_refptr<mojo::SpyServerImpl> spy_server, |
| 231 base::MessageLoopProxy* control_loop_proxy) |
134 : spy_server_(spy_server), | 232 : spy_server_(spy_server), |
135 proxy_(base::MessageLoopProxy::current()) { | 233 proxy_(base::MessageLoopProxy::current()), |
| 234 control_loop_proxy_(control_loop_proxy){ |
136 } | 235 } |
137 | 236 |
138 private: | 237 private: |
139 virtual mojo::ServiceProviderPtr OnConnectToClient( | 238 virtual mojo::ServiceProviderPtr OnConnectToClient( |
140 const GURL& url, mojo::ServiceProviderPtr real_client) OVERRIDE { | 239 const GURL& url, mojo::ServiceProviderPtr real_client) OVERRIDE { |
141 if (!MustIntercept(url)) | 240 if (!MustIntercept(url)) |
142 return real_client.Pass(); | 241 return real_client.Pass(); |
143 | 242 |
144 // You can get an invalid handle if the app (or service) is | 243 // You can get an invalid handle if the app (or service) is |
145 // created by unconventional means, for example the command line. | 244 // created by unconventional means, for example the command line. |
146 if (!real_client.get()) | 245 if (!real_client.get()) |
147 return real_client.Pass(); | 246 return real_client.Pass(); |
148 | 247 |
149 mojo::ScopedMessagePipeHandle faux_client; | 248 mojo::ScopedMessagePipeHandle faux_client; |
150 mojo::ScopedMessagePipeHandle interceptor; | 249 mojo::ScopedMessagePipeHandle interceptor; |
151 CreateMessagePipe(NULL, &faux_client, &interceptor); | 250 CreateMessagePipe(NULL, &faux_client, &interceptor); |
152 | 251 |
153 scoped_refptr<MessageProcessor> processor = new MessageProcessor(); | 252 scoped_refptr<MessageProcessor> processor = new MessageProcessor( |
| 253 control_loop_proxy_); |
154 mojo::ScopedMessagePipeHandle real_handle = real_client.PassMessagePipe(); | 254 mojo::ScopedMessagePipeHandle real_handle = real_client.PassMessagePipe(); |
155 base::WorkerPool::PostTask( | 255 base::WorkerPool::PostTask( |
156 FROM_HERE, | 256 FROM_HERE, |
157 base::Bind(&MessageProcessor::Start, | 257 base::Bind(&MessageProcessor::Start, |
158 processor, | 258 processor, |
159 base::Passed(&real_handle), base::Passed(&interceptor)), | 259 base::Passed(&real_handle), base::Passed(&interceptor), |
| 260 url), |
160 true); | 261 true); |
161 | 262 |
162 mojo::ServiceProviderPtr faux_provider; | 263 mojo::ServiceProviderPtr faux_provider; |
163 faux_provider.Bind(faux_client.Pass()); | 264 faux_provider.Bind(faux_client.Pass()); |
164 return faux_provider.Pass(); | 265 return faux_provider.Pass(); |
165 } | 266 } |
166 | 267 |
167 bool MustIntercept(const GURL& url) { | 268 bool MustIntercept(const GURL& url) { |
168 // TODO(cpu): manage who and when to intercept. | 269 // TODO(cpu): manage who and when to intercept. |
169 proxy_->PostTask( | 270 proxy_->PostTask( |
170 FROM_HERE, | 271 FROM_HERE, |
171 base::Bind(&mojo::SpyServerImpl::OnIntercept, spy_server_, url)); | 272 base::Bind(&mojo::SpyServerImpl::OnIntercept, spy_server_, url)); |
172 return true; | 273 return true; |
173 } | 274 } |
174 | 275 |
175 scoped_refptr<mojo::SpyServerImpl> spy_server_; | 276 scoped_refptr<mojo::SpyServerImpl> spy_server_; |
176 scoped_refptr<base::MessageLoopProxy> proxy_; | 277 scoped_refptr<base::MessageLoopProxy> proxy_; |
| 278 scoped_refptr<base::MessageLoopProxy> control_loop_proxy_; |
177 }; | 279 }; |
178 | 280 |
179 mojo::WebSocketServer* ws_server = NULL; | |
180 | |
181 void StartWebServer(int port, mojo::ScopedMessagePipeHandle pipe) { | 281 void StartWebServer(int port, mojo::ScopedMessagePipeHandle pipe) { |
182 // TODO(cpu) figure out lifetime of the server. See Spy() dtor. | 282 // TODO(cpu) figure out lifetime of the server. See Spy() dtor. |
183 ws_server = new mojo::WebSocketServer(port, pipe.Pass()); | 283 ws_server = new mojo::WebSocketServer(port, pipe.Pass()); |
184 ws_server->Start(); | 284 ws_server->Start(); |
185 } | 285 } |
186 | 286 |
187 struct SpyOptions { | 287 struct SpyOptions { |
188 int websocket_port; | 288 int websocket_port; |
189 | 289 |
190 SpyOptions() | 290 SpyOptions() |
(...skipping 30 matching lines...) Expand all Loading... |
221 // Start the tread what will accept commands from the frontend. | 321 // Start the tread what will accept commands from the frontend. |
222 control_thread_.reset(new base::Thread("mojo_spy_control_thread")); | 322 control_thread_.reset(new base::Thread("mojo_spy_control_thread")); |
223 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 323 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
224 control_thread_->StartWithOptions(thread_options); | 324 control_thread_->StartWithOptions(thread_options); |
225 control_thread_->message_loop_proxy()->PostTask( | 325 control_thread_->message_loop_proxy()->PostTask( |
226 FROM_HERE, base::Bind(&StartWebServer, | 326 FROM_HERE, base::Bind(&StartWebServer, |
227 spy_options.websocket_port, | 327 spy_options.websocket_port, |
228 base::Passed(spy_server_->ServerPipe()))); | 328 base::Passed(spy_server_->ServerPipe()))); |
229 | 329 |
230 // Start intercepting mojo services. | 330 // Start intercepting mojo services. |
231 service_manager->SetInterceptor(new SpyInterceptor(spy_server_)); | 331 service_manager->SetInterceptor(new SpyInterceptor( |
| 332 spy_server_, control_thread_->message_loop_proxy())); |
232 } | 333 } |
233 | 334 |
234 Spy::~Spy() { | 335 Spy::~Spy() { |
235 // TODO(cpu): Do not leak the interceptor. Lifetime between the | 336 // TODO(cpu): Do not leak the interceptor. Lifetime between the |
236 // service_manager and the spy is still unclear hence the leak. | 337 // service_manager and the spy is still unclear hence the leak. |
237 } | 338 } |
238 | 339 |
239 } // namespace mojo | 340 } // namespace mojo |
OLD | NEW |