Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Side by Side Diff: trunk/src/mojo/spy/spy.cc

Issue 374243003: Revert 281910 "Revert 272463 "Revert 272458 "Adding more guts to..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « trunk/src/mojo/spy/spy.h ('k') | trunk/src/mojo/spy/spy_server_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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::ServiceProviderPtr OnConnectToClient( 129 virtual mojo::ServiceProviderPtr OnConnectToClient(
140 const GURL& url, mojo::ServiceProviderPtr real_client) OVERRIDE { 130 const GURL& url, mojo::ServiceProviderPtr 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.get()) 136 if (!real_client.get())
147 return real_client.Pass(); 137 return real_client.Pass();
(...skipping 11 matching lines...) Expand all
159 base::Passed(&real_handle), base::Passed(&interceptor)), 149 base::Passed(&real_handle), base::Passed(&interceptor)),
160 true); 150 true);
161 151
162 mojo::ServiceProviderPtr faux_provider; 152 mojo::ServiceProviderPtr faux_provider;
163 faux_provider.Bind(faux_client.Pass()); 153 faux_provider.Bind(faux_client.Pass());
164 return faux_provider.Pass(); 154 return faux_provider.Pass();
165 } 155 }
166 156
167 bool MustIntercept(const GURL& url) { 157 bool MustIntercept(const GURL& url) {
168 // TODO(cpu): manage who and when to intercept. 158 // TODO(cpu): manage who and when to intercept.
169 proxy_->PostTask(
170 FROM_HERE,
171 base::Bind(&mojo::SpyServerImpl::OnIntercept, spy_server_, url));
172 return true; 159 return true;
173 } 160 }
174
175 scoped_refptr<mojo::SpyServerImpl> spy_server_;
176 scoped_refptr<base::MessageLoopProxy> proxy_;
177 }; 161 };
178 162
179 mojo::WebSocketServer* ws_server = NULL; 163 spy::WebSocketServer* ws_server = NULL;
180 164
181 void StartWebServer(int port, mojo::ScopedMessagePipeHandle pipe) { 165 void StartServer(int port) {
182 // TODO(cpu) figure out lifetime of the server. See Spy() dtor. 166 // TODO(cpu) figure out lifetime of the server. See Spy() dtor.
183 ws_server = new mojo::WebSocketServer(port, pipe.Pass()); 167 ws_server = new spy::WebSocketServer(port);
184 ws_server->Start(); 168 ws_server->Start();
185 } 169 }
186 170
187 struct SpyOptions { 171 struct SpyOptions {
188 int websocket_port; 172 int websocket_port;
189 173
190 SpyOptions() 174 SpyOptions()
191 : websocket_port(kDefaultWebSocketPort) { 175 : websocket_port(kDefaultWebSocketPort) {
192 } 176 }
193 }; 177 };
(...skipping 14 matching lines...) Expand all
208 } 192 }
209 return spy_options; 193 return spy_options;
210 } 194 }
211 195
212 } // namespace 196 } // namespace
213 197
214 namespace mojo { 198 namespace mojo {
215 199
216 Spy::Spy(mojo::ServiceManager* service_manager, const std::string& options) { 200 Spy::Spy(mojo::ServiceManager* service_manager, const std::string& options) {
217 SpyOptions spy_options = ProcessOptions(options); 201 SpyOptions spy_options = ProcessOptions(options);
218
219 spy_server_ = new SpyServerImpl();
220
221 // Start the tread what will accept commands from the frontend. 202 // Start the tread what will accept commands from the frontend.
222 control_thread_.reset(new base::Thread("mojo_spy_control_thread")); 203 control_thread_.reset(new base::Thread("mojo_spy_control_thread"));
223 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); 204 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0);
224 control_thread_->StartWithOptions(thread_options); 205 control_thread_->StartWithOptions(thread_options);
225 control_thread_->message_loop_proxy()->PostTask( 206 control_thread_->message_loop_proxy()->PostTask(
226 FROM_HERE, base::Bind(&StartWebServer, 207 FROM_HERE, base::Bind(&StartServer, spy_options.websocket_port));
227 spy_options.websocket_port,
228 base::Passed(spy_server_->ServerPipe())));
229 208
230 // Start intercepting mojo services. 209 // Start intercepting mojo services.
231 service_manager->SetInterceptor(new SpyInterceptor(spy_server_)); 210 service_manager->SetInterceptor(new SpyInterceptor());
232 } 211 }
233 212
234 Spy::~Spy() { 213 Spy::~Spy(){
235 // TODO(cpu): Do not leak the interceptor. Lifetime between the 214 // TODO(cpu): Do not leak the interceptor. Lifetime between the
236 // service_manager and the spy is still unclear hence the leak. 215 // service_manager and the spy is still unclear hence the leak.
237 } 216 }
238 217
239 } // namespace mojo 218 } // namespace mojo
OLDNEW
« no previous file with comments | « trunk/src/mojo/spy/spy.h ('k') | trunk/src/mojo/spy/spy_server_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698