| 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" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 } | 39 } |
| 40 | 40 |
| 41 // In charge of processing messages that flow over a | 41 // In charge of processing messages that flow over a |
| 42 // single message pipe. | 42 // single message pipe. |
| 43 class MessageProcessor : | 43 class MessageProcessor : |
| 44 public base::RefCountedThreadSafe<MessageProcessor> { | 44 public base::RefCountedThreadSafe<MessageProcessor> { |
| 45 public: | 45 public: |
| 46 MessageProcessor(base::MessageLoopProxy* control_loop_proxy) | 46 MessageProcessor(base::MessageLoopProxy* control_loop_proxy) |
| 47 : last_result_(MOJO_RESULT_OK), | 47 : last_result_(MOJO_RESULT_OK), |
| 48 bytes_transfered_(0), | 48 bytes_transfered_(0), |
| 49 control_loop_proxy_(control_loop_proxy), | 49 control_loop_proxy_(control_loop_proxy) { |
| 50 service_vendor_message_pipe_received_(false) { | |
| 51 message_count_[0] = 0; | 50 message_count_[0] = 0; |
| 52 message_count_[1] = 0; | 51 message_count_[1] = 0; |
| 53 handle_count_[0] = 0; | 52 handle_count_[0] = 0; |
| 54 handle_count_[1] = 0; | 53 handle_count_[1] = 0; |
| 55 } | 54 } |
| 56 | 55 |
| 57 void Start(mojo::ScopedMessagePipeHandle client, | 56 void Start(mojo::ScopedMessagePipeHandle client, |
| 58 mojo::ScopedMessagePipeHandle interceptor, | 57 mojo::ScopedMessagePipeHandle interceptor, |
| 59 const GURL& url) { | 58 const GURL& url) { |
| 60 std::vector<mojo::MessagePipeHandle> pipes; | 59 std::vector<mojo::MessagePipeHandle> pipes; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 89 hbuf.get(), &handles_read, | 88 hbuf.get(), &handles_read, |
| 90 MOJO_READ_MESSAGE_FLAG_NONE))) | 89 MOJO_READ_MESSAGE_FLAG_NONE))) |
| 91 break; | 90 break; |
| 92 | 91 |
| 93 if (!bytes_read && !handles_read) | 92 if (!bytes_read && !handles_read) |
| 94 continue; | 93 continue; |
| 95 | 94 |
| 96 if (handles_read) { | 95 if (handles_read) { |
| 97 handle_count_[r] += handles_read; | 96 handle_count_[r] += handles_read; |
| 98 | 97 |
| 99 // Intercept the first set of handles to message pipes with the | 98 // Intercept message pipes which are returned via the ReadMessageRaw |
| 100 // assumption that these would be used for vending mojo services. | 99 // call |
| 101 // TODO(ananta) | 100 for (uint32_t i = 0; i < handles_read; i++) { |
| 102 // The above approach is hacky and could cause us to miss other message | 101 // Hack to determine if a handle is a message pipe. |
| 103 // pipes which could be exchanged between the client and the server. | 102 // TODO(ananta) |
| 104 // Look into a cleaner way of identifying message pipe handles. | 103 // We should have an API which given a handle returns additional |
| 105 if (!service_vendor_message_pipe_received_) { | 104 // information about the handle which includes its type, etc. |
| 106 service_vendor_message_pipe_received_ = true; | 105 if (MojoReadMessage(hbuf[i], NULL, NULL, NULL, NULL, |
| 107 for (uint32_t i = 0; i < handles_read; i++) { | 106 MOJO_READ_MESSAGE_FLAG_NONE) != |
| 107 MOJO_RESULT_INVALID_ARGUMENT) { |
| 108 mojo::ScopedMessagePipeHandle message_pipe_handle; | 108 mojo::ScopedMessagePipeHandle message_pipe_handle; |
| 109 message_pipe_handle.reset(mojo::MessagePipeHandle(hbuf[i])); | 109 message_pipe_handle.reset(mojo::MessagePipeHandle(hbuf[i])); |
| 110 | 110 |
| 111 mojo::ScopedMessagePipeHandle faux_client; | 111 mojo::ScopedMessagePipeHandle faux_client; |
| 112 mojo::ScopedMessagePipeHandle interceptor; | 112 mojo::ScopedMessagePipeHandle interceptor; |
| 113 CreateMessagePipe(NULL, &faux_client, &interceptor); | 113 CreateMessagePipe(NULL, &faux_client, &interceptor); |
| 114 | 114 |
| 115 base::WorkerPool::PostTask( | 115 base::WorkerPool::PostTask( |
| 116 FROM_HERE, | 116 FROM_HERE, |
| 117 base::Bind(&MessageProcessor::Start, | 117 base::Bind(&MessageProcessor::Start, |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 base::Unretained(ws_server), | 212 base::Unretained(ws_server), |
| 213 message_data->header, url, base::Time::Now())); | 213 message_data->header, url, base::Time::Now())); |
| 214 } | 214 } |
| 215 } | 215 } |
| 216 | 216 |
| 217 MojoResult last_result_; | 217 MojoResult last_result_; |
| 218 uint32_t bytes_transfered_; | 218 uint32_t bytes_transfered_; |
| 219 uint32_t message_count_[2]; | 219 uint32_t message_count_[2]; |
| 220 uint32_t handle_count_[2]; | 220 uint32_t handle_count_[2]; |
| 221 scoped_refptr<base::MessageLoopProxy> control_loop_proxy_; | 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_; | |
| 225 }; | 222 }; |
| 226 | 223 |
| 227 // In charge of intercepting access to the service manager. | 224 // In charge of intercepting access to the service manager. |
| 228 class SpyInterceptor : public mojo::ServiceManager::Interceptor { | 225 class SpyInterceptor : public mojo::ServiceManager::Interceptor { |
| 229 public: | 226 public: |
| 230 explicit SpyInterceptor(scoped_refptr<mojo::SpyServerImpl> spy_server, | 227 explicit SpyInterceptor(scoped_refptr<mojo::SpyServerImpl> spy_server, |
| 231 base::MessageLoopProxy* control_loop_proxy) | 228 base::MessageLoopProxy* control_loop_proxy) |
| 232 : spy_server_(spy_server), | 229 : spy_server_(spy_server), |
| 233 proxy_(base::MessageLoopProxy::current()), | 230 proxy_(base::MessageLoopProxy::current()), |
| 234 control_loop_proxy_(control_loop_proxy){ | 231 control_loop_proxy_(control_loop_proxy){ |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 service_manager->SetInterceptor(new SpyInterceptor( | 328 service_manager->SetInterceptor(new SpyInterceptor( |
| 332 spy_server_, control_thread_->message_loop_proxy())); | 329 spy_server_, control_thread_->message_loop_proxy())); |
| 333 } | 330 } |
| 334 | 331 |
| 335 Spy::~Spy() { | 332 Spy::~Spy() { |
| 336 // TODO(cpu): Do not leak the interceptor. Lifetime between the | 333 // TODO(cpu): Do not leak the interceptor. Lifetime between the |
| 337 // service_manager and the spy is still unclear hence the leak. | 334 // service_manager and the spy is still unclear hence the leak. |
| 338 } | 335 } |
| 339 | 336 |
| 340 } // namespace mojo | 337 } // namespace mojo |
| OLD | NEW |