| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "remoting/host/clipboard.h" | 5 #include "remoting/host/clipboard.h" |
| 6 | 6 |
| 7 #include <X11/Xlib.h> | 7 #include <X11/Xlib.h> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #undef Status // Xlib.h #defines this, which breaks protobuf headers. | 10 #undef Status // Xlib.h #defines this, which breaks protobuf headers. |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/files/file_descriptor_watcher_posix.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 #include "remoting/host/linux/x_server_clipboard.h" | 16 #include "remoting/host/linux/x_server_clipboard.h" |
| 17 #include "remoting/proto/event.pb.h" | 17 #include "remoting/proto/event.pb.h" |
| 18 #include "remoting/protocol/clipboard_stub.h" | 18 #include "remoting/protocol/clipboard_stub.h" |
| 19 | 19 |
| 20 namespace remoting { | 20 namespace remoting { |
| 21 | 21 |
| 22 // This code is expected to be called on the desktop thread only. | 22 // This code is expected to be called on the desktop thread only. |
| 23 class ClipboardX11 : public Clipboard, | 23 class ClipboardX11 : public Clipboard { |
| 24 public base::MessageLoopForIO::Watcher { | |
| 25 public: | 24 public: |
| 26 ClipboardX11(); | 25 ClipboardX11(); |
| 27 ~ClipboardX11() override; | 26 ~ClipboardX11() override; |
| 28 | 27 |
| 29 // Clipboard interface. | 28 // Clipboard interface. |
| 30 void Start( | 29 void Start( |
| 31 std::unique_ptr<protocol::ClipboardStub> client_clipboard) override; | 30 std::unique_ptr<protocol::ClipboardStub> client_clipboard) override; |
| 32 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override; | 31 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override; |
| 33 | 32 |
| 34 // MessageLoopForIO::Watcher interface. | |
| 35 void OnFileCanReadWithoutBlocking(int fd) override; | |
| 36 void OnFileCanWriteWithoutBlocking(int fd) override; | |
| 37 | |
| 38 private: | 33 private: |
| 39 void OnClipboardChanged(const std::string& mime_type, | 34 void OnClipboardChanged(const std::string& mime_type, |
| 40 const std::string& data); | 35 const std::string& data); |
| 41 void PumpXEvents(); | 36 void PumpXEvents(); |
| 42 | 37 |
| 43 std::unique_ptr<protocol::ClipboardStub> client_clipboard_; | 38 std::unique_ptr<protocol::ClipboardStub> client_clipboard_; |
| 44 | 39 |
| 45 // Underlying X11 clipboard implementation. | 40 // Underlying X11 clipboard implementation. |
| 46 XServerClipboard x_server_clipboard_; | 41 XServerClipboard x_server_clipboard_; |
| 47 | 42 |
| 48 // Connection to the X server, used by |x_server_clipboard_|. This is created | 43 // Connection to the X server, used by |x_server_clipboard_|. This is created |
| 49 // and owned by this class. | 44 // and owned by this class. |
| 50 Display* display_; | 45 Display* display_; |
| 51 | 46 |
| 52 // Watcher used to handle X11 events from |display_|. | 47 // Watcher used to handle X11 events from |display_|. |
| 53 base::MessageLoopForIO::FileDescriptorWatcher x_connection_watcher_; | 48 std::unique_ptr<base::FileDescriptorWatcher::Controller> |
| 49 x_connection_watch_controller_; |
| 54 | 50 |
| 55 DISALLOW_COPY_AND_ASSIGN(ClipboardX11); | 51 DISALLOW_COPY_AND_ASSIGN(ClipboardX11); |
| 56 }; | 52 }; |
| 57 | 53 |
| 58 ClipboardX11::ClipboardX11() | 54 ClipboardX11::ClipboardX11() |
| 59 : display_(nullptr) { | 55 : display_(nullptr) { |
| 60 } | 56 } |
| 61 | 57 |
| 62 ClipboardX11::~ClipboardX11() { | 58 ClipboardX11::~ClipboardX11() { |
| 63 if (display_) | 59 if (display_) |
| 64 XCloseDisplay(display_); | 60 XCloseDisplay(display_); |
| 65 } | 61 } |
| 66 | 62 |
| 67 void ClipboardX11::Start( | 63 void ClipboardX11::Start( |
| 68 std::unique_ptr<protocol::ClipboardStub> client_clipboard) { | 64 std::unique_ptr<protocol::ClipboardStub> client_clipboard) { |
| 69 // TODO(lambroslambrou): Share the X connection with InputInjector. | 65 // TODO(lambroslambrou): Share the X connection with InputInjector. |
| 66 DCHECK(!display_); |
| 70 display_ = XOpenDisplay(nullptr); | 67 display_ = XOpenDisplay(nullptr); |
| 71 if (!display_) { | 68 if (!display_) { |
| 72 LOG(ERROR) << "Couldn't open X display"; | 69 LOG(ERROR) << "Couldn't open X display"; |
| 73 return; | 70 return; |
| 74 } | 71 } |
| 75 client_clipboard_.swap(client_clipboard); | 72 client_clipboard_.swap(client_clipboard); |
| 76 | 73 |
| 77 x_server_clipboard_.Init(display_, | 74 x_server_clipboard_.Init(display_, |
| 78 base::Bind(&ClipboardX11::OnClipboardChanged, | 75 base::Bind(&ClipboardX11::OnClipboardChanged, |
| 79 base::Unretained(this))); | 76 base::Unretained(this))); |
| 80 | 77 |
| 81 base::MessageLoopForIO::current()->WatchFileDescriptor( | 78 x_connection_watch_controller_ = base::FileDescriptorWatcher::WatchReadable( |
| 82 ConnectionNumber(display_), | 79 ConnectionNumber(display_), |
| 83 true, | 80 base::Bind(&ClipboardX11::PumpXEvents, base::Unretained(this))); |
| 84 base::MessageLoopForIO::WATCH_READ, | |
| 85 &x_connection_watcher_, | |
| 86 this); | |
| 87 PumpXEvents(); | 81 PumpXEvents(); |
| 88 } | 82 } |
| 89 | 83 |
| 90 void ClipboardX11::InjectClipboardEvent( | 84 void ClipboardX11::InjectClipboardEvent( |
| 91 const protocol::ClipboardEvent& event) { | 85 const protocol::ClipboardEvent& event) { |
| 92 x_server_clipboard_.SetClipboard(event.mime_type(), event.data()); | 86 x_server_clipboard_.SetClipboard(event.mime_type(), event.data()); |
| 93 } | 87 } |
| 94 | 88 |
| 95 void ClipboardX11::OnFileCanReadWithoutBlocking(int fd) { | |
| 96 PumpXEvents(); | |
| 97 } | |
| 98 | |
| 99 void ClipboardX11::OnFileCanWriteWithoutBlocking(int fd) { | |
| 100 } | |
| 101 | |
| 102 void ClipboardX11::OnClipboardChanged(const std::string& mime_type, | 89 void ClipboardX11::OnClipboardChanged(const std::string& mime_type, |
| 103 const std::string& data) { | 90 const std::string& data) { |
| 104 protocol::ClipboardEvent event; | 91 protocol::ClipboardEvent event; |
| 105 event.set_mime_type(mime_type); | 92 event.set_mime_type(mime_type); |
| 106 event.set_data(data); | 93 event.set_data(data); |
| 107 | 94 |
| 108 if (client_clipboard_.get()) { | 95 if (client_clipboard_.get()) { |
| 109 client_clipboard_->InjectClipboardEvent(event); | 96 client_clipboard_->InjectClipboardEvent(event); |
| 110 } | 97 } |
| 111 } | 98 } |
| 112 | 99 |
| 113 void ClipboardX11::PumpXEvents() { | 100 void ClipboardX11::PumpXEvents() { |
| 114 DCHECK(display_); | 101 DCHECK(display_); |
| 115 | 102 |
| 116 while (XPending(display_)) { | 103 while (XPending(display_)) { |
| 117 XEvent event; | 104 XEvent event; |
| 118 XNextEvent(display_, &event); | 105 XNextEvent(display_, &event); |
| 119 x_server_clipboard_.ProcessXEvent(&event); | 106 x_server_clipboard_.ProcessXEvent(&event); |
| 120 } | 107 } |
| 121 } | 108 } |
| 122 | 109 |
| 123 std::unique_ptr<Clipboard> Clipboard::Create() { | 110 std::unique_ptr<Clipboard> Clipboard::Create() { |
| 124 return base::WrapUnique(new ClipboardX11()); | 111 return base::WrapUnique(new ClipboardX11()); |
| 125 } | 112 } |
| 126 | 113 |
| 127 } // namespace remoting | 114 } // namespace remoting |
| OLD | NEW |