Chromium Code Reviews| Index: remoting/host/clipboard_linux.cc |
| diff --git a/remoting/host/clipboard_linux.cc b/remoting/host/clipboard_linux.cc |
| index 6a1729167fccf9aeea4617c719d28d037e3290a2..ed4c5a3c1c8c166d6faf71ae69b801fc0a64e386 100644 |
| --- a/remoting/host/clipboard_linux.cc |
| +++ b/remoting/host/clipboard_linux.cc |
| @@ -4,13 +4,23 @@ |
| #include "remoting/host/clipboard.h" |
| +#include <X11/Xlib.h> |
| + |
| +#include "base/bind.h" |
| #include "base/logging.h" |
| +#include "base/message_loop.h" |
| +#include "base/message_pump_libevent.h" |
| +#include "base/threading/thread.h" |
| +#include "remoting/host/x_server_clipboard.h" |
| +#include "remoting/proto/event.pb.h" |
| +#include "remoting/protocol/clipboard_stub.h" |
| namespace remoting { |
| -class ClipboardLinux : public Clipboard { |
| +class ClipboardLinux : public Clipboard, base::MessagePumpLibevent::Watcher { |
| public: |
| ClipboardLinux(); |
| + ~ClipboardLinux(); |
|
Sergey Ulanov
2012/09/12 01:00:08
virtual
Lambros
2012/09/12 23:42:32
Done.
|
| // Must be called on the UI thread. |
| virtual void Start( |
| @@ -20,21 +30,122 @@ class ClipboardLinux : public Clipboard { |
| virtual void Stop() OVERRIDE; |
| private: |
| + void OnClipboardChanged(const std::string& mime_type, |
| + const std::string& data); |
| + void PumpEvents(); |
| + |
| + // base::MessagePumpLibevent::Watcher interface |
| + virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| + virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} |
|
Sergey Ulanov
2012/09/12 01:00:08
Better not to inline implementation of this method
Wez
2012/09/12 04:52:53
NOTREACHED seems overkill for a notification handl
Sergey Ulanov
2012/09/12 18:39:13
heh, really?
http://codereview.chromium.org/109070
Lambros
2012/09/12 23:42:32
Done. Chrome has many examples both with and with
|
| + |
| + scoped_ptr<protocol::ClipboardStub> client_clipboard_; |
| + |
| + XServerClipboard x_server_clipboard_; |
| + Display* display_; |
| + base::Thread x11_thread_; |
|
Sergey Ulanov
2012/09/12 01:00:08
Do we really need to create a new thread just for
Lambros
2012/09/12 23:42:32
Sharing the X connection and thread with EventExec
Sergey Ulanov
2012/09/14 00:29:56
main_task_runner that is passed to EventExecutorLi
Lambros
2012/09/14 02:09:18
Done.
|
| + |
| + base::MessagePumpLibevent::FileDescriptorWatcher x_connection_watcher_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(ClipboardLinux); |
| }; |
| +ClipboardLinux::ClipboardLinux() |
| + : display_(NULL), |
| + x11_thread_("Clipboard X11 thread") { |
| + x11_thread_.StartWithOptions(base::Thread::Options(MessageLoop::TYPE_IO, 0)); |
| +} |
| + |
| +ClipboardLinux::~ClipboardLinux() { |
| + Stop(); |
| + x11_thread_.Stop(); |
| +} |
| + |
| void ClipboardLinux::Start( |
| scoped_ptr<protocol::ClipboardStub> client_clipboard) { |
| - NOTIMPLEMENTED(); |
| + if (!x11_thread_.message_loop_proxy()->BelongsToCurrentThread()) { |
| + // base::Unretained is safe, since the clipboard thread is owned by this |
| + // object. |
| + x11_thread_.message_loop_proxy()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&ClipboardLinux::Start, base::Unretained(this), |
| + base::Passed(client_clipboard.Pass()))); |
| + return; |
| + } |
| + |
| + display_ = XOpenDisplay(NULL); |
| + if (!display_) { |
| + LOG(ERROR) << "Couldn't open X display"; |
| + return; |
| + } |
| + client_clipboard_.swap(client_clipboard); |
| + |
| + // base::Unretained is safe, since the clipboard thread is owned by this |
| + // object, and this dtor resets |callback_|, so that no more clipboard |
| + // changes are processed while stopping the thread. |
| + x_server_clipboard_.Init(display_, |
| + base::Bind(&ClipboardLinux::OnClipboardChanged, |
| + base::Unretained(this))); |
| + |
| + MessageLoopForIO::current()->WatchFileDescriptor( |
| + ConnectionNumber(display_), true, MessageLoopForIO::WATCH_READ, |
| + &x_connection_watcher_, this); |
| + PumpEvents(); |
| } |
| void ClipboardLinux::InjectClipboardEvent( |
| const protocol::ClipboardEvent& event) { |
| - NOTIMPLEMENTED(); |
| + if (!x11_thread_.message_loop_proxy()->BelongsToCurrentThread()) { |
| + x11_thread_.message_loop_proxy()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&ClipboardLinux::InjectClipboardEvent, |
| + base::Unretained(this), |
| + event)); |
| + return; |
| + } |
| + |
| + x_server_clipboard_.SetClipboard(event.mime_type(), event.data()); |
| } |
| void ClipboardLinux::Stop() { |
| - NOTIMPLEMENTED(); |
| + if (!x11_thread_.message_loop_proxy()->BelongsToCurrentThread()) { |
| + x11_thread_.message_loop_proxy()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&ClipboardLinux::Stop, base::Unretained(this))); |
| + return; |
| + } |
| + |
| + client_clipboard_.reset(); |
| + x_connection_watcher_.StopWatchingFileDescriptor(); |
| + |
| + if (display_) { |
| + XCloseDisplay(display_); |
| + display_ = NULL; |
| + } |
| +} |
| + |
| +void ClipboardLinux::OnClipboardChanged(const std::string& mime_type, |
| + const std::string& data) { |
| + protocol::ClipboardEvent event; |
| + event.set_mime_type(mime_type); |
| + event.set_data(data); |
| + |
| + if (client_clipboard_.get()) { |
| + client_clipboard_->InjectClipboardEvent(event); |
| + } |
| +} |
| + |
| +void ClipboardLinux::PumpEvents() { |
| + DCHECK(display_); |
| + |
| + while (XPending(display_)) { |
| + XEvent event; |
| + XNextEvent(display_, &event); |
| + x_server_clipboard_.ProcessXEvent(&event); |
| + } |
| +} |
| + |
| +void ClipboardLinux::OnFileCanReadWithoutBlocking(int fd) { |
| + PumpEvents(); |
| } |
| scoped_ptr<Clipboard> Clipboard::Create() { |