| 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> | |
| 8 | |
| 9 #include "base/bind.h" | 7 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 11 #include "base/location.h" | 9 #include "base/location.h" |
| 12 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" | |
| 14 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 16 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 17 #include "base/threading/worker_pool.h" | 14 #include "base/threading/worker_pool.h" |
| 18 | 15 |
| 19 #include "mojo/public/cpp/system/core.h" | 16 #include "mojo/public/cpp/system/core.h" |
| 20 #include "mojo/service_manager/service_manager.h" | 17 #include "mojo/service_manager/service_manager.h" |
| 21 #include "mojo/spy/public/spy.mojom.h" | |
| 22 #include "mojo/spy/spy_server_impl.h" | |
| 23 #include "mojo/spy/websocket_server.h" | 18 #include "mojo/spy/websocket_server.h" |
| 24 #include "url/gurl.h" | |
| 25 | 19 |
| 26 namespace { | 20 namespace { |
| 27 | 21 |
| 28 const size_t kMessageBufSize = 2 * 1024; | 22 const size_t kMessageBufSize = 2 * 1024; |
| 29 const size_t kHandleBufSize = 64; | 23 const size_t kHandleBufSize = 64; |
| 30 const int kDefaultWebSocketPort = 42424; | 24 const int kDefaultWebSocketPort = 42424; |
| 31 | 25 |
| 32 void CloseHandles(MojoHandle* handles, size_t count) { | 26 void CloseHandles(MojoHandle* handles, size_t count) { |
| 33 for (size_t ix = 0; ix != count; ++count) | 27 for (size_t ix = 0; ix != count; ++count) |
| 34 MojoClose(handles[ix]); | 28 MojoClose(handles[ix]); |
| 35 } | 29 } |
| 36 | 30 |
| 37 // In charge of processing messages that flow over a | 31 // In charge of processing messages that flow over a |
| 38 // single message pipe. | 32 // single message pipe. |
| 39 class MessageProcessor : | 33 class MessageProcessor : |
| 40 public base::RefCountedThreadSafe<MessageProcessor> { | 34 public base::RefCountedThreadSafe<MessageProcessor> { |
| 41 public: | 35 public: |
| 36 |
| 42 MessageProcessor() | 37 MessageProcessor() |
| 43 : last_result_(MOJO_RESULT_OK), | 38 : last_result_(MOJO_RESULT_OK), |
| 44 bytes_transfered_(0) { | 39 bytes_transfered_(0) { |
| 40 |
| 45 message_count_[0] = 0; | 41 message_count_[0] = 0; |
| 46 message_count_[1] = 0; | 42 message_count_[1] = 0; |
| 47 handle_count_[0] = 0; | 43 handle_count_[0] = 0; |
| 48 handle_count_[1] = 0; | 44 handle_count_[1] = 0; |
| 49 } | 45 } |
| 50 | 46 |
| 51 void Start(mojo::ScopedMessagePipeHandle client, | 47 void Start(mojo::ScopedMessagePipeHandle client, |
| 52 mojo::ScopedMessagePipeHandle interceptor) { | 48 mojo::ScopedMessagePipeHandle interceptor) { |
| 53 std::vector<mojo::MessagePipeHandle> pipes; | 49 std::vector<mojo::MessagePipeHandle> pipes; |
| 54 pipes.push_back(client.get()); | 50 pipes.push_back(client.get()); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 MOJO_WRITE_MESSAGE_FLAG_NONE))) { | 100 MOJO_WRITE_MESSAGE_FLAG_NONE))) { |
| 105 // On failure we own the handles. For now just close them. | 101 // On failure we own the handles. For now just close them. |
| 106 if (handles_read) | 102 if (handles_read) |
| 107 CloseHandles(hbuf.get(), handles_read); | 103 CloseHandles(hbuf.get(), handles_read); |
| 108 break; | 104 break; |
| 109 } | 105 } |
| 110 } | 106 } |
| 111 } | 107 } |
| 112 | 108 |
| 113 private: | 109 private: |
| 114 friend class base::RefCountedThreadSafe<MessageProcessor>; | 110 friend class base::RefCountedThreadSafe<MessageProcessor>; |
| 115 virtual ~MessageProcessor() {} | 111 virtual ~MessageProcessor() {} |
| 116 | 112 |
| 117 bool CheckResult(MojoResult mr) { | 113 bool CheckResult(MojoResult mr) { |
| 118 if (mr == MOJO_RESULT_OK) | 114 if (mr == MOJO_RESULT_OK) |
| 119 return true; | 115 return true; |
| 120 last_result_ = mr; | 116 last_result_ = mr; |
| 121 return false; | 117 return false; |
| 122 } | 118 } |
| 123 | 119 |
| 124 MojoResult last_result_; | 120 MojoResult last_result_; |
| 125 uint32_t bytes_transfered_; | 121 uint32_t bytes_transfered_; |
| 126 uint32_t message_count_[2]; | 122 uint32_t message_count_[2]; |
| 127 uint32_t handle_count_[2]; | 123 uint32_t handle_count_[2]; |
| 128 }; | 124 }; |
| 129 | 125 |
| 130 // In charge of intercepting access to the service manager. | 126 // In charge of intercepting access to the service manager. |
| 131 class SpyInterceptor : public mojo::ServiceManager::Interceptor { | 127 class SpyInterceptor : public mojo::ServiceManager::Interceptor { |
| 132 public: | |
| 133 explicit SpyInterceptor(scoped_refptr<mojo::SpyServerImpl> spy_server) | |
| 134 : spy_server_(spy_server), | |
| 135 proxy_(base::MessageLoopProxy::current()) { | |
| 136 } | |
| 137 | |
| 138 private: | 128 private: |
| 139 virtual mojo::ScopedMessagePipeHandle OnConnectToClient( | 129 virtual mojo::ScopedMessagePipeHandle OnConnectToClient( |
| 140 const GURL& url, mojo::ScopedMessagePipeHandle real_client) OVERRIDE { | 130 const GURL& url, mojo::ScopedMessagePipeHandle real_client) OVERRIDE { |
| 141 if (!MustIntercept(url)) | 131 if (!MustIntercept(url)) |
| 142 return real_client.Pass(); | 132 return real_client.Pass(); |
| 143 | 133 |
| 144 // You can get an invalid handle if the app (or service) is | 134 // You can get an invalid handle if the app (or service) is |
| 145 // created by unconventional means, for example the command line. | 135 // created by unconventional means, for example the command line. |
| 146 if (!real_client.is_valid()) | 136 if (!real_client.is_valid()) |
| 147 return real_client.Pass(); | 137 return real_client.Pass(); |
| 148 | 138 |
| 149 mojo::ScopedMessagePipeHandle faux_client; | 139 mojo::ScopedMessagePipeHandle faux_client; |
| 150 mojo::ScopedMessagePipeHandle interceptor; | 140 mojo::ScopedMessagePipeHandle interceptor; |
| 151 CreateMessagePipe(&faux_client, &interceptor); | 141 CreateMessagePipe(&faux_client, &interceptor); |
| 152 | 142 |
| 153 scoped_refptr<MessageProcessor> processor = new MessageProcessor(); | 143 scoped_refptr<MessageProcessor> processor = new MessageProcessor(); |
| 154 base::WorkerPool::PostTask( | 144 base::WorkerPool::PostTask( |
| 155 FROM_HERE, | 145 FROM_HERE, |
| 156 base::Bind(&MessageProcessor::Start, | 146 base::Bind(&MessageProcessor::Start, |
| 157 processor, | 147 processor, |
| 158 base::Passed(&real_client), base::Passed(&interceptor)), | 148 base::Passed(&real_client), base::Passed(&interceptor)), |
| 159 true); | 149 true); |
| 160 | 150 |
| 161 return faux_client.Pass(); | 151 return faux_client.Pass(); |
| 162 } | 152 } |
| 163 | 153 |
| 164 bool MustIntercept(const GURL& url) { | 154 bool MustIntercept(const GURL& url) { |
| 165 // TODO(cpu): manage who and when to intercept. | 155 // TODO(cpu): manage who and when to intercept. |
| 166 proxy_->PostTask( | |
| 167 FROM_HERE, | |
| 168 base::Bind(&mojo::SpyServerImpl::OnIntercept, spy_server_, url)); | |
| 169 return true; | 156 return true; |
| 170 } | 157 } |
| 171 | |
| 172 scoped_refptr<mojo::SpyServerImpl> spy_server_; | |
| 173 scoped_refptr<base::MessageLoopProxy> proxy_; | |
| 174 }; | 158 }; |
| 175 | 159 |
| 176 mojo::WebSocketServer* ws_server = NULL; | 160 spy::WebSocketServer* ws_server = NULL; |
| 177 | 161 |
| 178 void StartWebServer(int port, mojo::ScopedMessagePipeHandle pipe) { | 162 void StartServer(int port) { |
| 179 // TODO(cpu) figure out lifetime of the server. See Spy() dtor. | 163 // TODO(cpu) figure out lifetime of the server. See Spy() dtor. |
| 180 ws_server = new mojo::WebSocketServer(port, pipe.Pass()); | 164 ws_server = new spy::WebSocketServer(port); |
| 181 ws_server->Start(); | 165 ws_server->Start(); |
| 182 } | 166 } |
| 183 | 167 |
| 184 struct SpyOptions { | 168 struct SpyOptions { |
| 185 int websocket_port; | 169 int websocket_port; |
| 186 | 170 |
| 187 SpyOptions() | 171 SpyOptions() |
| 188 : websocket_port(kDefaultWebSocketPort) { | 172 : websocket_port(kDefaultWebSocketPort) { |
| 189 } | 173 } |
| 190 }; | 174 }; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 205 } | 189 } |
| 206 return spy_options; | 190 return spy_options; |
| 207 } | 191 } |
| 208 | 192 |
| 209 } // namespace | 193 } // namespace |
| 210 | 194 |
| 211 namespace mojo { | 195 namespace mojo { |
| 212 | 196 |
| 213 Spy::Spy(mojo::ServiceManager* service_manager, const std::string& options) { | 197 Spy::Spy(mojo::ServiceManager* service_manager, const std::string& options) { |
| 214 SpyOptions spy_options = ProcessOptions(options); | 198 SpyOptions spy_options = ProcessOptions(options); |
| 215 | |
| 216 spy_server_ = new SpyServerImpl(); | |
| 217 | |
| 218 // Start the tread what will accept commands from the frontend. | 199 // Start the tread what will accept commands from the frontend. |
| 219 control_thread_.reset(new base::Thread("mojo_spy_control_thread")); | 200 control_thread_.reset(new base::Thread("mojo_spy_control_thread")); |
| 220 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 201 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 221 control_thread_->StartWithOptions(thread_options); | 202 control_thread_->StartWithOptions(thread_options); |
| 222 control_thread_->message_loop_proxy()->PostTask( | 203 control_thread_->message_loop_proxy()->PostTask( |
| 223 FROM_HERE, base::Bind(&StartWebServer, | 204 FROM_HERE, base::Bind(&StartServer, spy_options.websocket_port)); |
| 224 spy_options.websocket_port, | |
| 225 base::Passed(spy_server_->ServerPipe()))); | |
| 226 | 205 |
| 227 // Start intercepting mojo services. | 206 // Start intercepting mojo services. |
| 228 service_manager->SetInterceptor(new SpyInterceptor(spy_server_)); | 207 service_manager->SetInterceptor(new SpyInterceptor()); |
| 229 } | 208 } |
| 230 | 209 |
| 231 Spy::~Spy() { | 210 Spy::~Spy(){ |
| 232 // TODO(cpu): Do not leak the interceptor. Lifetime between the | 211 // TODO(cpu): Do not leak the interceptor. Lifetime between the |
| 233 // service_manager and the spy is still unclear hence the leak. | 212 // service_manager and the spy is still unclear hence the leak. |
| 234 } | 213 } |
| 235 | 214 |
| 236 } // namespace mojo | 215 } // namespace mojo |
| OLD | NEW |