Commit 0b077e3f authored by cpu's avatar cpu Committed by Commit bot

remove websocket server dependency from the mojo spy

So the core of the mojo spy can travel to new, exciting destinations.

BUG=none

Review URL: https://codereview.chromium.org/648573002

Cr-Commit-Position: refs/heads/master@{#299036}
parent 97236cef
...@@ -219,6 +219,9 @@ void Context::Init() { ...@@ -219,6 +219,9 @@ void Context::Init() {
spy_.reset( spy_.reset(
new mojo::Spy(&application_manager_, new mojo::Spy(&application_manager_,
command_line->GetSwitchValueASCII(switches::kSpy))); command_line->GetSwitchValueASCII(switches::kSpy)));
// TODO(cpu): the spy can snoop, but can't tell anybody until
// the Spy::WebSocketDelegate is implemented. In the original repo this
// was implemented by src\mojo\spy\websocket_server.h and .cc.
} }
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
......
...@@ -22,13 +22,10 @@ ...@@ -22,13 +22,10 @@
#include "mojo/spy/common.h" #include "mojo/spy/common.h"
#include "mojo/spy/public/spy.mojom.h" #include "mojo/spy/public/spy.mojom.h"
#include "mojo/spy/spy_server_impl.h" #include "mojo/spy/spy_server_impl.h"
#include "mojo/spy/websocket_server.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace { namespace {
mojo::WebSocketServer* ws_server = NULL;
const size_t kMessageBufSize = 2 * 1024; const size_t kMessageBufSize = 2 * 1024;
const size_t kHandleBufSize = 64; const size_t kHandleBufSize = 64;
const int kDefaultWebSocketPort = 42424; const int kDefaultWebSocketPort = 42424;
...@@ -43,9 +40,11 @@ void CloseHandles(MojoHandle* handles, size_t count) { ...@@ -43,9 +40,11 @@ void CloseHandles(MojoHandle* handles, size_t count) {
class MessageProcessor : class MessageProcessor :
public base::RefCountedThreadSafe<MessageProcessor> { public base::RefCountedThreadSafe<MessageProcessor> {
public: public:
MessageProcessor(base::MessageLoopProxy* control_loop_proxy) MessageProcessor(mojo::Spy::WebSocketDelegate* websocket_delegate,
base::MessageLoopProxy* control_loop_proxy)
: last_result_(MOJO_RESULT_OK), : last_result_(MOJO_RESULT_OK),
bytes_transfered_(0), bytes_transfered_(0),
websocket_delegate_(websocket_delegate),
control_loop_proxy_(control_loop_proxy) { control_loop_proxy_(control_loop_proxy) {
message_count_[0] = 0; message_count_[0] = 0;
message_count_[1] = 0; message_count_[1] = 0;
...@@ -115,10 +114,10 @@ class MessageProcessor : ...@@ -115,10 +114,10 @@ class MessageProcessor :
base::WorkerPool::PostTask( base::WorkerPool::PostTask(
FROM_HERE, FROM_HERE,
base::Bind(&MessageProcessor::Start, base::Bind(&MessageProcessor::Start,
this, this,
base::Passed(&message_pipe_handle), base::Passed(&message_pipe_handle),
base::Passed(&interceptor), base::Passed(&interceptor),
url), url),
true); true);
hbuf.get()[i] = faux_client.release().value(); hbuf.get()[i] = faux_client.release().value();
} }
...@@ -203,14 +202,19 @@ class MessageProcessor : ...@@ -203,14 +202,19 @@ class MessageProcessor :
} }
void LogMessageInfo(void* data, const GURL& url) { void LogMessageInfo(void* data, const GURL& url) {
if (!websocket_delegate_)
return;
mojo::MojoMessageData* message_data = mojo::MojoMessageData* message_data =
reinterpret_cast<mojo::MojoMessageData*>(data); reinterpret_cast<mojo::MojoMessageData*>(data);
if (IsValidMessage(message_data->header)) { if (IsValidMessage(message_data->header)) {
control_loop_proxy_->PostTask( control_loop_proxy_->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(&mojo::WebSocketServer::LogMessageInfo, base::Bind(&mojo::Spy::WebSocketDelegate::OnMessage,
base::Unretained(ws_server), base::Unretained(websocket_delegate_),
message_data->header, url, base::Time::Now())); message_data,
url,
base::Time::Now()));
} }
} }
...@@ -218,6 +222,7 @@ class MessageProcessor : ...@@ -218,6 +222,7 @@ class MessageProcessor :
uint32_t bytes_transfered_; uint32_t bytes_transfered_;
uint32_t message_count_[2]; uint32_t message_count_[2];
uint32_t handle_count_[2]; uint32_t handle_count_[2];
mojo::Spy::WebSocketDelegate* websocket_delegate_;
scoped_refptr<base::MessageLoopProxy> control_loop_proxy_; scoped_refptr<base::MessageLoopProxy> control_loop_proxy_;
}; };
...@@ -226,10 +231,12 @@ class SpyInterceptor : public mojo::ApplicationManager::Interceptor { ...@@ -226,10 +231,12 @@ class SpyInterceptor : public mojo::ApplicationManager::Interceptor {
public: public:
explicit SpyInterceptor( explicit SpyInterceptor(
scoped_refptr<mojo::SpyServerImpl> spy_server, scoped_refptr<mojo::SpyServerImpl> spy_server,
const scoped_refptr<base::MessageLoopProxy>& control_loop_proxy) const scoped_refptr<base::MessageLoopProxy>& control_loop_proxy,
mojo::Spy::WebSocketDelegate* websocket_delegate)
: spy_server_(spy_server), : spy_server_(spy_server),
proxy_(base::MessageLoopProxy::current()), proxy_(base::MessageLoopProxy::current()),
control_loop_proxy_(control_loop_proxy) {} control_loop_proxy_(control_loop_proxy),
websocket_delegate_(websocket_delegate) {}
private: private:
virtual mojo::ServiceProviderPtr OnConnectToClient( virtual mojo::ServiceProviderPtr OnConnectToClient(
...@@ -247,7 +254,7 @@ class SpyInterceptor : public mojo::ApplicationManager::Interceptor { ...@@ -247,7 +254,7 @@ class SpyInterceptor : public mojo::ApplicationManager::Interceptor {
CreateMessagePipe(NULL, &faux_client, &interceptor); CreateMessagePipe(NULL, &faux_client, &interceptor);
scoped_refptr<MessageProcessor> processor = scoped_refptr<MessageProcessor> processor =
new MessageProcessor(control_loop_proxy_.get()); new MessageProcessor(websocket_delegate_, control_loop_proxy_.get());
mojo::ScopedMessagePipeHandle real_handle = real_client.PassMessagePipe(); mojo::ScopedMessagePipeHandle real_handle = real_client.PassMessagePipe();
base::WorkerPool::PostTask( base::WorkerPool::PostTask(
FROM_HERE, FROM_HERE,
...@@ -273,13 +280,12 @@ class SpyInterceptor : public mojo::ApplicationManager::Interceptor { ...@@ -273,13 +280,12 @@ class SpyInterceptor : public mojo::ApplicationManager::Interceptor {
scoped_refptr<mojo::SpyServerImpl> spy_server_; scoped_refptr<mojo::SpyServerImpl> spy_server_;
scoped_refptr<base::MessageLoopProxy> proxy_; scoped_refptr<base::MessageLoopProxy> proxy_;
scoped_refptr<base::MessageLoopProxy> control_loop_proxy_; scoped_refptr<base::MessageLoopProxy> control_loop_proxy_;
mojo::Spy::WebSocketDelegate* websocket_delegate_;
}; };
void StartWebServer(int port, mojo::ScopedMessagePipeHandle pipe) { } // namespace
// TODO(cpu) figure out lifetime of the server. See Spy() dtor.
ws_server = new mojo::WebSocketServer(port, pipe.Pass()); namespace mojo {
ws_server->Start();
}
struct SpyOptions { struct SpyOptions {
int websocket_port; int websocket_port;
...@@ -306,28 +312,18 @@ SpyOptions ProcessOptions(const std::string& options) { ...@@ -306,28 +312,18 @@ SpyOptions ProcessOptions(const std::string& options) {
return spy_options; return spy_options;
} }
} // namespace
namespace mojo {
Spy::Spy(mojo::ApplicationManager* application_manager, Spy::Spy(mojo::ApplicationManager* application_manager,
const std::string& options) { const std::string& options)
SpyOptions spy_options = ProcessOptions(options); : websocket_delegate_(NULL),
application_manager_(application_manager),
spy_options_(NULL) {
spy_options_ = new SpyOptions(ProcessOptions(options));
spy_server_ = new SpyServerImpl(); spy_server_ = new SpyServerImpl();
// Start the tread what will accept commands from the frontend. // Start the tread what will accept commands from the frontend.
control_thread_.reset(new base::Thread("mojo_spy_control_thread")); control_thread_.reset(new base::Thread("mojo_spy_control_thread"));
base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0);
control_thread_->StartWithOptions(thread_options); control_thread_->StartWithOptions(thread_options);
control_thread_->message_loop_proxy()->PostTask(
FROM_HERE, base::Bind(&StartWebServer,
spy_options.websocket_port,
base::Passed(spy_server_->ServerPipe())));
// Start intercepting mojo services.
application_manager->SetInterceptor(
new SpyInterceptor(spy_server_, control_thread_->message_loop_proxy()));
} }
Spy::~Spy() { Spy::~Spy() {
...@@ -335,4 +331,19 @@ Spy::~Spy() { ...@@ -335,4 +331,19 @@ Spy::~Spy() {
// application_manager and the spy is still unclear hence the leak. // application_manager and the spy is still unclear hence the leak.
} }
void Spy::SetWebSocketDelegate(WebSocketDelegate* websocket_delegate) {
DCHECK(websocket_delegate_ == NULL);
websocket_delegate_ = websocket_delegate;
control_thread_->message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(&WebSocketDelegate::Start,
base::Unretained(websocket_delegate_),
spy_options_->websocket_port,
base::Passed(spy_server_->ServerPipe())));
// Start intercepting mojo services.
application_manager_->SetInterceptor(new SpyInterceptor(
spy_server_, control_thread_->message_loop_proxy(), websocket_delegate_));
}
} // namespace mojo } // namespace mojo
...@@ -8,8 +8,12 @@ ...@@ -8,8 +8,12 @@
#include <string> #include <string>
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "mojo/spy/common.h"
#include "url/gurl.h"
namespace base { namespace base {
class Time;
class Thread; class Thread;
} }
...@@ -17,6 +21,7 @@ namespace mojo { ...@@ -17,6 +21,7 @@ namespace mojo {
class ApplicationManager; class ApplicationManager;
class SpyServerImpl; class SpyServerImpl;
struct SpyOptions;
// mojo::Spy is a troubleshooting and debugging aid. It helps tracking // mojo::Spy is a troubleshooting and debugging aid. It helps tracking
// the mojo system core activities like messages, service creation, etc. // the mojo system core activities like messages, service creation, etc.
...@@ -29,14 +34,30 @@ class SpyServerImpl; ...@@ -29,14 +34,30 @@ class SpyServerImpl;
// //
class Spy { class Spy {
public: public:
// Interface for the shell-provided websocket server.
class WebSocketDelegate {
public:
virtual void Start(int port, mojo::ScopedMessagePipeHandle server_pipe) = 0;
virtual void OnMessage(mojo::MojoMessageData* data,
const GURL& url,
const base::Time& time) = 0;
};
Spy(mojo::ApplicationManager* application_manager, Spy(mojo::ApplicationManager* application_manager,
const std::string& options); const std::string& options);
~Spy(); ~Spy();
// non-owning reference to the websocket server.
void SetWebSocketDelegate(WebSocketDelegate* websocket_delegate);
private: private:
scoped_refptr<SpyServerImpl> spy_server_; scoped_refptr<SpyServerImpl> spy_server_;
// This thread runs the code that talks to the frontend. // This thread runs the code that talks to the frontend.
scoped_ptr<base::Thread> control_thread_; scoped_ptr<base::Thread> control_thread_;
// The delegate is in charge of talking to the html frontend.
WebSocketDelegate* websocket_delegate_;
ApplicationManager* application_manager_;
SpyOptions* spy_options_;
}; };
} // namespace mojo } // namespace mojo
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment