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/threading/thread.h" |
| 12 #include "base/time.h" |
| 13 #include "base/timer.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 { |
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 scoped_ptr<protocol::ClipboardStub> client_clipboard_; |
| 38 |
| 39 XServerClipboard x_server_clipboard_; |
| 40 Display* display_; |
| 41 base::Thread x11_thread_; |
| 42 base::RepeatingTimer<ClipboardLinux> event_timer_; |
| 43 |
23 DISALLOW_COPY_AND_ASSIGN(ClipboardLinux); | 44 DISALLOW_COPY_AND_ASSIGN(ClipboardLinux); |
24 }; | 45 }; |
25 | 46 |
| 47 ClipboardLinux::ClipboardLinux() : x11_thread_("Clipboard X11 thread") { |
| 48 x11_thread_.Start(); |
| 49 } |
| 50 |
| 51 ClipboardLinux::~ClipboardLinux() { |
| 52 Stop(); |
| 53 x11_thread_.Stop(); |
| 54 } |
| 55 |
26 void ClipboardLinux::Start( | 56 void ClipboardLinux::Start( |
27 scoped_ptr<protocol::ClipboardStub> client_clipboard) { | 57 scoped_ptr<protocol::ClipboardStub> client_clipboard) { |
28 NOTIMPLEMENTED(); | 58 if (!x11_thread_.message_loop_proxy()->BelongsToCurrentThread()) { |
| 59 // base::Unretained is safe, since the clipboard thread is owned by this |
| 60 // object. |
| 61 x11_thread_.message_loop_proxy()->PostTask( |
| 62 FROM_HERE, |
| 63 base::Bind(&ClipboardLinux::Start, base::Unretained(this), |
| 64 base::Passed(client_clipboard.Pass()))); |
| 65 return; |
| 66 } |
| 67 |
| 68 LOG(ERROR) << "*** Start"; |
| 69 |
| 70 display_ = XOpenDisplay(NULL); |
| 71 if (!display_) { |
| 72 LOG(ERROR) << "Couldn't open X display"; |
| 73 return; |
| 74 } |
| 75 client_clipboard_.swap(client_clipboard); |
| 76 |
| 77 // base::Unretained is safe, since the clipboard thread is owned by this |
| 78 // object, and this dtor resets |callback_|, so that no more clipboard |
| 79 // changes are processed while stopping the thread. |
| 80 x_server_clipboard_.Init(display_, |
| 81 base::Bind(&ClipboardLinux::OnClipboardChanged, |
| 82 base::Unretained(this))); |
| 83 |
| 84 event_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this, |
| 85 &ClipboardLinux::PumpEvents); |
29 } | 86 } |
30 | 87 |
31 void ClipboardLinux::InjectClipboardEvent( | 88 void ClipboardLinux::InjectClipboardEvent( |
32 const protocol::ClipboardEvent& event) { | 89 const protocol::ClipboardEvent& event) { |
33 NOTIMPLEMENTED(); | 90 if (!x11_thread_.message_loop_proxy()->BelongsToCurrentThread()) { |
| 91 x11_thread_.message_loop_proxy()->PostTask( |
| 92 FROM_HERE, |
| 93 base::Bind(&ClipboardLinux::InjectClipboardEvent, |
| 94 base::Unretained(this), |
| 95 event)); |
| 96 return; |
| 97 } |
| 98 |
| 99 LOG(ERROR) << "*** InjectClipboardEvent('" << event.mime_type() |
| 100 << "', '" << event.data() << "')"; |
| 101 x_server_clipboard_.SetClipboard(event.mime_type(), event.data()); |
34 } | 102 } |
35 | 103 |
36 void ClipboardLinux::Stop() { | 104 void ClipboardLinux::Stop() { |
37 NOTIMPLEMENTED(); | 105 if (!x11_thread_.message_loop_proxy()->BelongsToCurrentThread()) { |
| 106 x11_thread_.message_loop_proxy()->PostTask( |
| 107 FROM_HERE, |
| 108 base::Bind(&ClipboardLinux::Stop, base::Unretained(this))); |
| 109 return; |
| 110 } |
| 111 |
| 112 client_clipboard_.reset(); |
| 113 event_timer_.Stop(); |
| 114 if (display_) { |
| 115 XCloseDisplay(display_); |
| 116 display_ = NULL; |
| 117 } |
| 118 } |
| 119 |
| 120 void ClipboardLinux::OnClipboardChanged(const std::string& mime_type, |
| 121 const std::string& data) { |
| 122 protocol::ClipboardEvent event; |
| 123 event.set_mime_type(mime_type); |
| 124 event.set_data(data); |
| 125 |
| 126 if (client_clipboard_.get()) { |
| 127 client_clipboard_->InjectClipboardEvent(event); |
| 128 } |
| 129 } |
| 130 |
| 131 void ClipboardLinux::PumpEvents() { |
| 132 DCHECK(display_); |
| 133 |
| 134 while (XPending(display_)) { |
| 135 XEvent event; |
| 136 XNextEvent(display_, &event); |
| 137 x_server_clipboard_.ProcessXEvent(&event); |
| 138 } |
38 } | 139 } |
39 | 140 |
40 scoped_ptr<Clipboard> Clipboard::Create() { | 141 scoped_ptr<Clipboard> Clipboard::Create() { |
41 return scoped_ptr<Clipboard>(new ClipboardLinux()); | 142 return scoped_ptr<Clipboard>(new ClipboardLinux()); |
42 } | 143 } |
43 | 144 |
44 } // namespace remoting | 145 } // namespace remoting |
OLD | NEW |