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.
5 #include "remoting/host/clipboard.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/threading/platform_thread.h"
15 #include "base/win/message_window.h"
16 #include "base/win/scoped_hglobal.h"
17 #include "base/win/windows_version.h"
18 #include "remoting/base/constants.h"
19 #include "remoting/base/util.h"
20 #include "remoting/proto/event.pb.h"
21 #include "remoting/protocol/clipboard_stub.h"
25 // A scoper class that opens and closes the clipboard.
26 // This class was adapted from the ScopedClipboard class in
27 // ui/base/clipboard/clipboard_win.cc.
28 class ScopedClipboard
{
30 ScopedClipboard() : opened_(false) {
39 bool Init(HWND owner
) {
40 const int kMaxAttemptsToOpenClipboard
= 5;
41 const base::TimeDelta kSleepTimeBetweenAttempts
=
42 base::TimeDelta::FromMilliseconds(5);
49 // This code runs on the UI thread, so we can block only very briefly.
50 for (int attempt
= 0; attempt
< kMaxAttemptsToOpenClipboard
; ++attempt
) {
52 base::PlatformThread::Sleep(kSleepTimeBetweenAttempts
);
54 if (::OpenClipboard(owner
)) {
67 return ::EmptyClipboard();
70 void SetData(UINT uFormat
, HANDLE hMem
) {
75 // The caller must not close the handle that ::SetClipboardData returns.
76 ::SetClipboardData(uFormat
, hMem
);
79 // The caller must not free the handle. The caller should lock the handle,
80 // copy the clipboard data, and unlock the handle. All this must be done
81 // before this ScopedClipboard is destroyed.
82 HANDLE
GetData(UINT format
) {
87 return ::GetClipboardData(format
);
94 typedef BOOL (WINAPI AddClipboardFormatListenerFn
)(HWND
);
95 typedef BOOL (WINAPI RemoveClipboardFormatListenerFn
)(HWND
);
101 class ClipboardWin
: public Clipboard
{
106 scoped_ptr
<protocol::ClipboardStub
> client_clipboard
) OVERRIDE
;
107 virtual void InjectClipboardEvent(
108 const protocol::ClipboardEvent
& event
) OVERRIDE
;
109 virtual void Stop() OVERRIDE
;
112 void OnClipboardUpdate();
114 // Handles messages received by |window_|.
115 bool HandleMessage(UINT message
,
120 scoped_ptr
<protocol::ClipboardStub
> client_clipboard_
;
121 AddClipboardFormatListenerFn
* add_clipboard_format_listener_
;
122 RemoveClipboardFormatListenerFn
* remove_clipboard_format_listener_
;
124 // Used to subscribe to WM_CLIPBOARDUPDATE messages.
125 scoped_ptr
<base::win::MessageWindow
> window_
;
127 DISALLOW_COPY_AND_ASSIGN(ClipboardWin
);
130 ClipboardWin::ClipboardWin()
131 : add_clipboard_format_listener_(NULL
),
132 remove_clipboard_format_listener_(NULL
) {
135 void ClipboardWin::Start(
136 scoped_ptr
<protocol::ClipboardStub
> client_clipboard
) {
137 DCHECK(!add_clipboard_format_listener_
);
138 DCHECK(!remove_clipboard_format_listener_
);
141 client_clipboard_
.swap(client_clipboard
);
143 // user32.dll is statically linked.
144 HMODULE user32
= GetModuleHandle(L
"user32.dll");
147 add_clipboard_format_listener_
=
148 reinterpret_cast<AddClipboardFormatListenerFn
*>(
149 GetProcAddress(user32
, "AddClipboardFormatListener"));
150 if (add_clipboard_format_listener_
) {
151 remove_clipboard_format_listener_
=
152 reinterpret_cast<RemoveClipboardFormatListenerFn
*>(
153 GetProcAddress(user32
, "RemoveClipboardFormatListener"));
154 // If AddClipboardFormatListener() present, RemoveClipboardFormatListener()
155 // should be available too.
156 CHECK(remove_clipboard_format_listener_
);
158 LOG(WARNING
) << "AddClipboardFormatListener() is not available.";
161 window_
.reset(new base::win::MessageWindow());
162 if (!window_
->Create(base::Bind(&ClipboardWin::HandleMessage
,
163 base::Unretained(this)))) {
164 LOG(ERROR
) << "Couldn't create clipboard window.";
169 if (add_clipboard_format_listener_
) {
170 if (!(*add_clipboard_format_listener_
)(window_
->hwnd())) {
171 LOG(WARNING
) << "AddClipboardFormatListener() failed: " << GetLastError();
176 void ClipboardWin::Stop() {
177 client_clipboard_
.reset();
179 if (window_
&& remove_clipboard_format_listener_
)
180 (*remove_clipboard_format_listener_
)(window_
->hwnd());
185 void ClipboardWin::InjectClipboardEvent(
186 const protocol::ClipboardEvent
& event
) {
190 // Currently we only handle UTF-8 text.
191 if (event
.mime_type().compare(kMimeTypeTextUtf8
) != 0)
193 if (!StringIsUtf8(event
.data().c_str(), event
.data().length())) {
194 LOG(ERROR
) << "ClipboardEvent: data is not UTF-8 encoded.";
198 base::string16 text
= base::UTF8ToUTF16(ReplaceLfByCrLf(event
.data()));
200 ScopedClipboard clipboard
;
201 if (!clipboard
.Init(window_
->hwnd())) {
202 LOG(WARNING
) << "Couldn't open the clipboard.";
208 HGLOBAL text_global
=
209 ::GlobalAlloc(GMEM_MOVEABLE
, (text
.size() + 1) * sizeof(WCHAR
));
211 LOG(WARNING
) << "Couldn't allocate global memory.";
215 LPWSTR text_global_locked
=
216 reinterpret_cast<LPWSTR
>(::GlobalLock(text_global
));
217 memcpy(text_global_locked
, text
.data(), text
.size() * sizeof(WCHAR
));
218 text_global_locked
[text
.size()] = (WCHAR
)0;
219 ::GlobalUnlock(text_global
);
221 clipboard
.SetData(CF_UNICODETEXT
, text_global
);
224 void ClipboardWin::OnClipboardUpdate() {
227 if (::IsClipboardFormatAvailable(CF_UNICODETEXT
)) {
229 // Add a scope, so that we keep the clipboard open for as short a time as
232 ScopedClipboard clipboard
;
233 if (!clipboard
.Init(window_
->hwnd())) {
234 LOG(WARNING
) << "Couldn't open the clipboard." << GetLastError();
238 HGLOBAL text_global
= clipboard
.GetData(CF_UNICODETEXT
);
240 LOG(WARNING
) << "Couldn't get data from the clipboard: "
245 base::win::ScopedHGlobal
<WCHAR
*> text_lock(text_global
);
246 if (!text_lock
.get()) {
247 LOG(WARNING
) << "Couldn't lock clipboard data: " << GetLastError();
250 text
.assign(text_lock
.get());
253 protocol::ClipboardEvent event
;
254 event
.set_mime_type(kMimeTypeTextUtf8
);
255 event
.set_data(ReplaceCrLfByLf(base::UTF16ToUTF8(text
)));
257 if (client_clipboard_
.get()) {
258 client_clipboard_
->InjectClipboardEvent(event
);
263 bool ClipboardWin::HandleMessage(
264 UINT message
, WPARAM wparam
, LPARAM lparam
, LRESULT
* result
) {
265 if (message
== WM_CLIPBOARDUPDATE
) {
274 scoped_ptr
<Clipboard
> Clipboard::Create() {
275 return scoped_ptr
<Clipboard
>(new ClipboardWin());
278 } // namespace remoting