[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 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 | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #include "remoting/host/clipboard.h" | ||||
6 | |||||
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 7 | #include "base/memory/ptr_util.h" |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 8 | |
9 | #include "base/bind.h" | ||||
fdoray | cd80c41 | 2016-10-03 17:22:41 | [diff] [blame] | 10 | #include "base/files/file_descriptor_watcher_posix.h" |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 11 | #include "base/logging.h" |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 13 | #include "remoting/host/linux/x_server_clipboard.h" |
14 | #include "remoting/proto/event.pb.h" | ||||
15 | #include "remoting/protocol/clipboard_stub.h" | ||||
Daniel Bratell | b40fc33 | 2017-12-04 10:28:21 | [diff] [blame] | 16 | #include "ui/gfx/x/x11.h" |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 17 | |
18 | namespace remoting { | ||||
19 | |||||
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 20 | // This code is expected to be called on the desktop thread only. |
fdoray | cd80c41 | 2016-10-03 17:22:41 | [diff] [blame] | 21 | class ClipboardX11 : public Clipboard { |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 22 | public: |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 23 | ClipboardX11(); |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 24 | ~ClipboardX11() override; |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 25 | |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 26 | // Clipboard interface. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 27 | void Start( |
28 | std::unique_ptr<protocol::ClipboardStub> client_clipboard) override; | ||||
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 29 | void InjectClipboardEvent(const protocol::ClipboardEvent& event) override; |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 30 | |
31 | private: | ||||
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 32 | void OnClipboardChanged(const std::string& mime_type, |
33 | const std::string& data); | ||||
34 | void PumpXEvents(); | ||||
35 | |||||
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 36 | std::unique_ptr<protocol::ClipboardStub> client_clipboard_; |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 37 | |
[email protected] | e2123b5 | 2012-11-17 14:19:49 | [diff] [blame] | 38 | // Underlying X11 clipboard implementation. |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 39 | XServerClipboard x_server_clipboard_; |
[email protected] | e2123b5 | 2012-11-17 14:19:49 | [diff] [blame] | 40 | |
41 | // Connection to the X server, used by |x_server_clipboard_|. This is created | ||||
42 | // and owned by this class. | ||||
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 43 | Display* display_; |
44 | |||||
[email protected] | e2123b5 | 2012-11-17 14:19:49 | [diff] [blame] | 45 | // Watcher used to handle X11 events from |display_|. |
fdoray | cd80c41 | 2016-10-03 17:22:41 | [diff] [blame] | 46 | std::unique_ptr<base::FileDescriptorWatcher::Controller> |
47 | x_connection_watch_controller_; | ||||
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 48 | |
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 49 | DISALLOW_COPY_AND_ASSIGN(ClipboardX11); |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 50 | }; |
51 | |||||
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 52 | ClipboardX11::ClipboardX11() |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 53 | : display_(nullptr) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 54 | } |
55 | |||||
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 56 | ClipboardX11::~ClipboardX11() { |
Wez | 72c6fc6 | 2019-02-08 20:04:47 | [diff] [blame^] | 57 | x_connection_watch_controller_ = nullptr; |
sergeyu | 45f92d83 | 2015-02-19 01:24:19 | [diff] [blame] | 58 | if (display_) |
59 | XCloseDisplay(display_); | ||||
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 60 | } |
61 | |||||
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 62 | void ClipboardX11::Start( |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 63 | std::unique_ptr<protocol::ClipboardStub> client_clipboard) { |
[email protected] | b0b72f11 | 2013-03-24 03:42:42 | [diff] [blame] | 64 | // TODO(lambroslambrou): Share the X connection with InputInjector. |
fdoray | cd80c41 | 2016-10-03 17:22:41 | [diff] [blame] | 65 | DCHECK(!display_); |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 66 | display_ = XOpenDisplay(nullptr); |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 67 | if (!display_) { |
68 | LOG(ERROR) << "Couldn't open X display"; | ||||
69 | return; | ||||
70 | } | ||||
71 | client_clipboard_.swap(client_clipboard); | ||||
72 | |||||
73 | x_server_clipboard_.Init(display_, | ||||
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 74 | base::Bind(&ClipboardX11::OnClipboardChanged, |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 75 | base::Unretained(this))); |
76 | |||||
fdoray | cd80c41 | 2016-10-03 17:22:41 | [diff] [blame] | 77 | x_connection_watch_controller_ = base::FileDescriptorWatcher::WatchReadable( |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 78 | ConnectionNumber(display_), |
fdoray | cd80c41 | 2016-10-03 17:22:41 | [diff] [blame] | 79 | base::Bind(&ClipboardX11::PumpXEvents, base::Unretained(this))); |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 80 | PumpXEvents(); |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 81 | } |
82 | |||||
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 83 | void ClipboardX11::InjectClipboardEvent( |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 84 | const protocol::ClipboardEvent& event) { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 85 | x_server_clipboard_.SetClipboard(event.mime_type(), event.data()); |
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 86 | } |
87 | |||||
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 88 | void ClipboardX11::OnClipboardChanged(const std::string& mime_type, |
89 | const std::string& data) { | ||||
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 90 | protocol::ClipboardEvent event; |
91 | event.set_mime_type(mime_type); | ||||
92 | event.set_data(data); | ||||
93 | |||||
94 | if (client_clipboard_.get()) { | ||||
95 | client_clipboard_->InjectClipboardEvent(event); | ||||
96 | } | ||||
97 | } | ||||
98 | |||||
[email protected] | c9c94bad | 2013-04-29 17:45:12 | [diff] [blame] | 99 | void ClipboardX11::PumpXEvents() { |
[email protected] | 070ffb7 | 2012-09-27 00:24:49 | [diff] [blame] | 100 | DCHECK(display_); |
101 | |||||
102 | while (XPending(display_)) { | ||||
103 | XEvent event; | ||||
104 | XNextEvent(display_, &event); | ||||
105 | x_server_clipboard_.ProcessXEvent(&event); | ||||
106 | } | ||||
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 107 | } |
108 | |||||
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 109 | std::unique_ptr<Clipboard> Clipboard::Create() { |
110 | return base::WrapUnique(new ClipboardX11()); | ||||
[email protected] | 6d17db9 | 2012-05-11 17:03:14 | [diff] [blame] | 111 | } |
112 | |||||
113 | } // namespace remoting |