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> |
| 8 |
| 9 #include "base/bind.h" |
7 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "base/message_pump_libevent.h" |
| 13 #include "base/threading/thread.h" |
| 14 #include "remoting/host/x_server_clipboard.h" |
| 15 #include "remoting/proto/event.pb.h" |
| 16 #include "remoting/protocol/clipboard_stub.h" |
8 | 17 |
9 namespace remoting { | 18 namespace remoting { |
10 | 19 |
11 class ClipboardLinux : public Clipboard { | 20 class ClipboardLinux : public Clipboard, base::MessagePumpLibevent::Watcher { |
12 public: | 21 public: |
13 ClipboardLinux(); | 22 ClipboardLinux(); |
| 23 ~ClipboardLinux(); |
14 | 24 |
15 // Must be called on the UI thread. | 25 // Must be called on the UI thread. |
16 virtual void Start( | 26 virtual void Start( |
17 scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE; | 27 scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE; |
18 virtual void InjectClipboardEvent( | 28 virtual void InjectClipboardEvent( |
19 const protocol::ClipboardEvent& event) OVERRIDE; | 29 const protocol::ClipboardEvent& event) OVERRIDE; |
20 virtual void Stop() OVERRIDE; | 30 virtual void Stop() OVERRIDE; |
21 | 31 |
22 private: | 32 private: |
| 33 void OnClipboardChanged(const std::string& mime_type, |
| 34 const std::string& data); |
| 35 void PumpEvents(); |
| 36 |
| 37 // base::MessagePumpLibevent::Watcher interface |
| 38 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 39 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} |
| 40 |
| 41 scoped_ptr<protocol::ClipboardStub> client_clipboard_; |
| 42 |
| 43 XServerClipboard x_server_clipboard_; |
| 44 Display* display_; |
| 45 base::Thread x11_thread_; |
| 46 |
| 47 base::MessagePumpLibevent::FileDescriptorWatcher x_connection_watcher_; |
| 48 |
23 DISALLOW_COPY_AND_ASSIGN(ClipboardLinux); | 49 DISALLOW_COPY_AND_ASSIGN(ClipboardLinux); |
24 }; | 50 }; |
25 | 51 |
| 52 ClipboardLinux::ClipboardLinux() |
| 53 : display_(NULL), |
| 54 x11_thread_("Clipboard X11 thread") { |
| 55 x11_thread_.StartWithOptions(base::Thread::Options(MessageLoop::TYPE_IO, 0)); |
| 56 } |
| 57 |
| 58 ClipboardLinux::~ClipboardLinux() { |
| 59 Stop(); |
| 60 x11_thread_.Stop(); |
| 61 } |
| 62 |
26 void ClipboardLinux::Start( | 63 void ClipboardLinux::Start( |
27 scoped_ptr<protocol::ClipboardStub> client_clipboard) { | 64 scoped_ptr<protocol::ClipboardStub> client_clipboard) { |
28 NOTIMPLEMENTED(); | 65 if (!x11_thread_.message_loop_proxy()->BelongsToCurrentThread()) { |
| 66 // base::Unretained is safe, since the clipboard thread is owned by this |
| 67 // object. |
| 68 x11_thread_.message_loop_proxy()->PostTask( |
| 69 FROM_HERE, |
| 70 base::Bind(&ClipboardLinux::Start, base::Unretained(this), |
| 71 base::Passed(client_clipboard.Pass()))); |
| 72 return; |
| 73 } |
| 74 |
| 75 display_ = XOpenDisplay(NULL); |
| 76 if (!display_) { |
| 77 LOG(ERROR) << "Couldn't open X display"; |
| 78 return; |
| 79 } |
| 80 client_clipboard_.swap(client_clipboard); |
| 81 |
| 82 // base::Unretained is safe, since the clipboard thread is owned by this |
| 83 // object, and this dtor resets |callback_|, so that no more clipboard |
| 84 // changes are processed while stopping the thread. |
| 85 x_server_clipboard_.Init(display_, |
| 86 base::Bind(&ClipboardLinux::OnClipboardChanged, |
| 87 base::Unretained(this))); |
| 88 |
| 89 MessageLoopForIO::current()->WatchFileDescriptor( |
| 90 ConnectionNumber(display_), true, MessageLoopForIO::WATCH_READ, |
| 91 &x_connection_watcher_, this); |
29 } | 92 } |
30 | 93 |
31 void ClipboardLinux::InjectClipboardEvent( | 94 void ClipboardLinux::InjectClipboardEvent( |
32 const protocol::ClipboardEvent& event) { | 95 const protocol::ClipboardEvent& event) { |
33 NOTIMPLEMENTED(); | 96 if (!x11_thread_.message_loop_proxy()->BelongsToCurrentThread()) { |
| 97 x11_thread_.message_loop_proxy()->PostTask( |
| 98 FROM_HERE, |
| 99 base::Bind(&ClipboardLinux::InjectClipboardEvent, |
| 100 base::Unretained(this), |
| 101 event)); |
| 102 return; |
| 103 } |
| 104 |
| 105 x_server_clipboard_.SetClipboard(event.mime_type(), event.data()); |
34 } | 106 } |
35 | 107 |
36 void ClipboardLinux::Stop() { | 108 void ClipboardLinux::Stop() { |
37 NOTIMPLEMENTED(); | 109 if (!x11_thread_.message_loop_proxy()->BelongsToCurrentThread()) { |
| 110 x11_thread_.message_loop_proxy()->PostTask( |
| 111 FROM_HERE, |
| 112 base::Bind(&ClipboardLinux::Stop, base::Unretained(this))); |
| 113 return; |
| 114 } |
| 115 |
| 116 client_clipboard_.reset(); |
| 117 x_connection_watcher_.StopWatchingFileDescriptor(); |
| 118 |
| 119 if (display_) { |
| 120 XCloseDisplay(display_); |
| 121 display_ = NULL; |
| 122 } |
| 123 } |
| 124 |
| 125 void ClipboardLinux::OnClipboardChanged(const std::string& mime_type, |
| 126 const std::string& data) { |
| 127 protocol::ClipboardEvent event; |
| 128 event.set_mime_type(mime_type); |
| 129 event.set_data(data); |
| 130 |
| 131 if (client_clipboard_.get()) { |
| 132 client_clipboard_->InjectClipboardEvent(event); |
| 133 } |
| 134 } |
| 135 |
| 136 void ClipboardLinux::PumpEvents() { |
| 137 DCHECK(display_); |
| 138 |
| 139 while (XPending(display_)) { |
| 140 XEvent event; |
| 141 XNextEvent(display_, &event); |
| 142 x_server_clipboard_.ProcessXEvent(&event); |
| 143 } |
| 144 } |
| 145 |
| 146 void ClipboardLinux::OnFileCanReadWithoutBlocking(int fd) { |
| 147 PumpEvents(); |
38 } | 148 } |
39 | 149 |
40 scoped_ptr<Clipboard> Clipboard::Create() { | 150 scoped_ptr<Clipboard> Clipboard::Create() { |
41 return scoped_ptr<Clipboard>(new ClipboardLinux()); | 151 return scoped_ptr<Clipboard>(new ClipboardLinux()); |
42 } | 152 } |
43 | 153 |
44 } // namespace remoting | 154 } // namespace remoting |
OLD | NEW |