Chromium Code Reviews| 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/strings/string_number_conversions.h" | |
| 12 #include "base/strings/string_split.h" | |
| 13 #include "base/threading/thread.h" | |
| 11 #include "base/threading/worker_pool.h" | 14 #include "base/threading/worker_pool.h" |
| 12 | 15 |
| 13 #include "mojo/public/cpp/system/core.h" | 16 #include "mojo/public/cpp/system/core.h" |
| 14 #include "mojo/service_manager/service_manager.h" | 17 #include "mojo/service_manager/service_manager.h" |
| 18 #include "mojo/spy/websocket_server.h" | |
| 15 | 19 |
| 16 namespace { | 20 namespace { |
| 17 | 21 |
| 18 const size_t kMessageBufSize = 2 * 1024; | 22 const size_t kMessageBufSize = 2 * 1024; |
| 19 const size_t kHandleBufSize = 64; | 23 const size_t kHandleBufSize = 64; |
| 24 const int kDefaultWebSocketPort = 42424; | |
| 20 | 25 |
| 21 void CloseHandles(MojoHandle* handles, size_t count) { | 26 void CloseHandles(MojoHandle* handles, size_t count) { |
| 22 for (size_t ix = 0; ix != count; ++count) | 27 for (size_t ix = 0; ix != count; ++count) |
| 23 MojoClose(handles[ix]); | 28 MojoClose(handles[ix]); |
| 24 } | 29 } |
| 25 | 30 |
| 26 // In charge of processing messages that flow over a | 31 // In charge of processing messages that flow over a |
| 27 // single message pipe. | 32 // single message pipe. |
| 28 class MessageProcessor : | 33 class MessageProcessor : |
| 29 public base::RefCountedThreadSafe<MessageProcessor> { | 34 public base::RefCountedThreadSafe<MessageProcessor> { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 | 125 |
| 121 // In charge of intercepting access to the service manager. | 126 // In charge of intercepting access to the service manager. |
| 122 class SpyInterceptor : public mojo::ServiceManager::Interceptor { | 127 class SpyInterceptor : public mojo::ServiceManager::Interceptor { |
| 123 private: | 128 private: |
| 124 virtual mojo::ScopedMessagePipeHandle OnConnectToClient( | 129 virtual mojo::ScopedMessagePipeHandle OnConnectToClient( |
| 125 const GURL& url, mojo::ScopedMessagePipeHandle real_client) OVERRIDE { | 130 const GURL& url, mojo::ScopedMessagePipeHandle real_client) OVERRIDE { |
| 126 if (!MustIntercept(url)) | 131 if (!MustIntercept(url)) |
| 127 return real_client.Pass(); | 132 return real_client.Pass(); |
| 128 | 133 |
| 129 // 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 |
| 130 // by unconventional means, for example the command line. | 135 // created by unconventional means, for example the command line. |
| 131 if (!real_client.is_valid()) | 136 if (!real_client.is_valid()) |
| 132 return real_client.Pass(); | 137 return real_client.Pass(); |
| 133 | 138 |
| 134 mojo::ScopedMessagePipeHandle faux_client; | 139 mojo::ScopedMessagePipeHandle faux_client; |
| 135 mojo::ScopedMessagePipeHandle interceptor; | 140 mojo::ScopedMessagePipeHandle interceptor; |
| 136 CreateMessagePipe(&faux_client, &interceptor); | 141 CreateMessagePipe(&faux_client, &interceptor); |
| 137 | 142 |
| 138 scoped_refptr<MessageProcessor> processor = new MessageProcessor(); | 143 scoped_refptr<MessageProcessor> processor = new MessageProcessor(); |
| 139 base::WorkerPool::PostTask( | 144 base::WorkerPool::PostTask( |
| 140 FROM_HERE, | 145 FROM_HERE, |
| 141 base::Bind(&MessageProcessor::Start, | 146 base::Bind(&MessageProcessor::Start, |
| 142 processor, | 147 processor, |
| 143 base::Passed(&real_client), base::Passed(&interceptor)), | 148 base::Passed(&real_client), base::Passed(&interceptor)), |
| 144 true); | 149 true); |
| 145 | 150 |
| 146 return faux_client.Pass(); | 151 return faux_client.Pass(); |
| 147 } | 152 } |
| 148 | 153 |
| 149 bool MustIntercept(const GURL& url) { | 154 bool MustIntercept(const GURL& url) { |
| 150 // TODO(cpu): manage who and when to intercept. | 155 // TODO(cpu): manage who and when to intercept. |
| 151 return true; | 156 return true; |
| 152 } | 157 } |
| 153 }; | 158 }; |
| 154 | 159 |
| 160 spy::WebSocketServer* ws_server = NULL; | |
| 161 | |
| 162 void StartServer(int port) { | |
| 163 // TODO(cpu) figure out lifetime of the server. See Spy() dtor. | |
| 164 ws_server = new spy::WebSocketServer(port); | |
| 165 ws_server->Start(); | |
| 166 } | |
| 167 | |
| 168 struct SpyOptions { | |
| 169 int websocket_port; | |
| 170 | |
| 171 SpyOptions() | |
| 172 : websocket_port(kDefaultWebSocketPort) { | |
|
darin (slow to review)
2014/04/29 06:03:58
nit: indentation
cpu_(ooo_6.6-7.5)
2014/04/29 22:31:16
Done.
| |
| 173 } | |
| 174 }; | |
| 175 | |
| 176 SpyOptions ProcessOptions(const std::string& options) { | |
| 177 SpyOptions spy_options; | |
| 178 if (options.empty()) | |
| 179 return spy_options; | |
| 180 base::StringPairs kv_pairs; | |
| 181 base::SplitStringIntoKeyValuePairs(options, ':',',', &kv_pairs); | |
|
darin (slow to review)
2014/04/29 06:03:58
nit: add space after ':',
cpu_(ooo_6.6-7.5)
2014/04/29 22:31:16
Done.
| |
| 182 base::StringPairs::iterator it = kv_pairs.begin(); | |
| 183 for (; it != kv_pairs.end(); ++it) { | |
| 184 if (it->first == "port") { | |
| 185 int port; | |
| 186 if (base::StringToInt(it->second, &port)) | |
| 187 spy_options.websocket_port = port; | |
| 188 } | |
| 189 } | |
| 190 return spy_options; | |
| 191 } | |
| 192 | |
| 155 } // namespace | 193 } // namespace |
| 156 | 194 |
| 157 namespace mojo { | 195 namespace mojo { |
| 158 | 196 |
| 159 Spy::Spy(mojo::ServiceManager* service_manager, const std::string& options) { | 197 Spy::Spy(mojo::ServiceManager* service_manager, const std::string& options) { |
| 198 SpyOptions spy_options = ProcessOptions(options); | |
| 199 // Start the tread what will accept commands from the frontend. | |
| 200 control_thread_.reset(new base::Thread("spy_control_thread")); | |
|
darin (slow to review)
2014/04/29 06:03:58
nit: add "mojo" to the prefix of this thread name?
cpu_(ooo_6.6-7.5)
2014/04/29 22:31:16
Done.
| |
| 201 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | |
| 202 control_thread_->StartWithOptions(thread_options); | |
| 203 control_thread_->message_loop_proxy()->PostTask( | |
| 204 FROM_HERE, base::Bind(&StartServer, spy_options.websocket_port)); | |
| 205 | |
| 206 // Start intercepting mojo services. | |
| 160 service_manager->SetInterceptor(new SpyInterceptor()); | 207 service_manager->SetInterceptor(new SpyInterceptor()); |
| 161 } | 208 } |
| 162 | 209 |
| 163 Spy::~Spy(){ | 210 Spy::~Spy(){ |
| 164 // TODO(cpu): Do not leak the interceptor. Lifetime between the | 211 // TODO(cpu): Do not leak the interceptor. Lifetime between the |
| 165 // service_manager and the spy is still unclear hence the leak. | 212 // service_manager and the spy is still unclear hence the leak. |
| 166 } | 213 } |
| 167 | 214 |
| 168 } // namespace mojo | 215 } // namespace mojo |
| OLD | NEW |