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..c7552c5e03d6a0b455cc6da61877f67202be4d7b 100644 |
| --- a/remoting/host/clipboard_linux.cc |
| +++ b/remoting/host/clipboard_linux.cc |
| @@ -4,37 +4,119 @@ |
| #include "remoting/host/clipboard.h" |
| +#include <X11/Xlib.h> |
| + |
| +#include "base/bind.h" |
| #include "base/logging.h" |
| +#include "base/message_loop.h" |
| +#include "remoting/host/linux/x_server_clipboard.h" |
| +#include "remoting/proto/event.pb.h" |
| +#include "remoting/protocol/clipboard_stub.h" |
| namespace remoting { |
| -class ClipboardLinux : public Clipboard { |
| +// This code is expected to be used on the desktop thread only. |
|
Wez
2012/09/17 22:13:29
nit: "used" is vague; do we expect to be _called_
Lambros
2012/09/25 22:42:39
Changed to "called". This comment was specificall
|
| +class ClipboardLinux : public Clipboard, |
| + public MessageLoopForIO::Watcher { |
| public: |
| ClipboardLinux(); |
| + virtual ~ClipboardLinux(); |
| - // Must be called on the UI thread. |
| + // Clipboard interface. |
| virtual void Start( |
| scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE; |
|
Wez
2012/09/17 22:13:29
nit: I think Start & Stop should be StartClipboard
Lambros
2012/09/25 22:42:39
Agreed, but that should be a separate CL as it aff
|
| virtual void InjectClipboardEvent( |
| const protocol::ClipboardEvent& event) OVERRIDE; |
| virtual void Stop() OVERRIDE; |
| + // MessageLoopForIO::Watcher interface. |
| + virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| + virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
| + |
| private: |
| + void OnClipboardChanged(const std::string& mime_type, |
| + const std::string& data); |
| + void PumpEvents(); |
|
Wez
2012/09/17 22:13:29
nit: PumpXEvents?
Lambros
2012/09/25 22:42:39
Done.
|
| + |
| + scoped_ptr<protocol::ClipboardStub> client_clipboard_; |
| + |
| + XServerClipboard x_server_clipboard_; |
| + Display* display_; |
| + |
| + MessageLoopForIO::FileDescriptorWatcher x_connection_watcher_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(ClipboardLinux); |
| }; |
| +ClipboardLinux::ClipboardLinux() |
| + : display_(NULL) { |
| +} |
| + |
| +ClipboardLinux::~ClipboardLinux() { |
| + Stop(); |
| +} |
| + |
| void ClipboardLinux::Start( |
| scoped_ptr<protocol::ClipboardStub> client_clipboard) { |
| - NOTIMPLEMENTED(); |
| + // TODO(lambroslambrou): Share the X connection with EventExecutor. |
| + display_ = XOpenDisplay(NULL); |
| + if (!display_) { |
| + LOG(ERROR) << "Couldn't open X display"; |
| + return; |
| + } |
| + client_clipboard_.swap(client_clipboard); |
| + |
| + 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(); |
| + x_server_clipboard_.SetClipboard(event.mime_type(), event.data()); |
| } |
| void ClipboardLinux::Stop() { |
| - NOTIMPLEMENTED(); |
| + client_clipboard_.reset(); |
| + x_connection_watcher_.StopWatchingFileDescriptor(); |
| + |
| + if (display_) { |
| + XCloseDisplay(display_); |
| + display_ = NULL; |
| + } |
| +} |
| + |
| +void ClipboardLinux::OnFileCanReadWithoutBlocking(int fd) { |
| + PumpEvents(); |
| +} |
| + |
| +void ClipboardLinux::OnFileCanWriteWithoutBlocking(int fd) { |
| +} |
| + |
| +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); |
| + } |
| } |
| scoped_ptr<Clipboard> Clipboard::Create() { |