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