| 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 <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
| 13 #include "base/string16.h" | 13 #include "base/string16.h" |
| 14 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "base/win/scoped_hglobal.h" | 16 #include "base/win/scoped_hglobal.h" |
| 17 #include "base/win/windows_version.h" | 17 #include "base/win/windows_version.h" |
| 18 #include "base/win/wrapped_window_proc.h" | 18 #include "base/win/wrapped_window_proc.h" |
| 19 #include "remoting/base/constants.h" | 19 #include "remoting/base/constants.h" |
| 20 #include "remoting/proto/event.pb.h" | 20 #include "remoting/proto/event.pb.h" |
| 21 #include "remoting/protocol/clipboard_stub.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 const WCHAR kWindowClassName[] = L"clipboardWindowClass"; | 25 const WCHAR kWindowClassName[] = L"clipboardWindowClass"; |
| 25 const WCHAR kWindowName[] = L"clipboardWindow"; | 26 const WCHAR kWindowName[] = L"clipboardWindow"; |
| 26 | 27 |
| 27 // A scoper class that opens and closes the clipboard. | 28 // A scoper class that opens and closes the clipboard. |
| 28 // This class was adapted from the ScopedClipboard class in | 29 // This class was adapted from the ScopedClipboard class in |
| 29 // ui/base/clipboard/clipboard_win.cc. | 30 // ui/base/clipboard/clipboard_win.cc. |
| 30 class ScopedClipboard { | 31 class ScopedClipboard { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 typedef BOOL (WINAPI RemoveClipboardFormatListenerFn)(HWND); | 98 typedef BOOL (WINAPI RemoveClipboardFormatListenerFn)(HWND); |
| 98 | 99 |
| 99 } // namespace | 100 } // namespace |
| 100 | 101 |
| 101 namespace remoting { | 102 namespace remoting { |
| 102 | 103 |
| 103 class ClipboardWin : public Clipboard { | 104 class ClipboardWin : public Clipboard { |
| 104 public: | 105 public: |
| 105 ClipboardWin(); | 106 ClipboardWin(); |
| 106 | 107 |
| 107 virtual void Start() OVERRIDE; | 108 virtual void Start( |
| 109 scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE; |
| 108 virtual void InjectClipboardEvent( | 110 virtual void InjectClipboardEvent( |
| 109 const protocol::ClipboardEvent& event) OVERRIDE; | 111 const protocol::ClipboardEvent& event) OVERRIDE; |
| 110 virtual void Stop() OVERRIDE; | 112 virtual void Stop() OVERRIDE; |
| 111 | 113 |
| 112 private: | 114 private: |
| 113 void OnClipboardUpdate(); | 115 void OnClipboardUpdate(); |
| 114 bool HaveClipboardListenerApi(); | 116 bool HaveClipboardListenerApi(); |
| 115 | 117 |
| 116 static bool RegisterWindowClass(); | 118 static bool RegisterWindowClass(); |
| 117 static LRESULT CALLBACK WndProc(HWND hwmd, UINT msg, WPARAM wParam, | 119 static LRESULT CALLBACK WndProc(HWND hwmd, UINT msg, WPARAM wParam, |
| 118 LPARAM lParam); | 120 LPARAM lParam); |
| 119 | 121 |
| 122 scoped_ptr<protocol::ClipboardStub> client_clipboard_; |
| 120 HWND hwnd_; | 123 HWND hwnd_; |
| 121 AddClipboardFormatListenerFn* add_clipboard_format_listener_; | 124 AddClipboardFormatListenerFn* add_clipboard_format_listener_; |
| 122 RemoveClipboardFormatListenerFn* remove_clipboard_format_listener_; | 125 RemoveClipboardFormatListenerFn* remove_clipboard_format_listener_; |
| 123 bool load_functions_tried_; | 126 bool load_functions_tried_; |
| 124 | 127 |
| 125 DISALLOW_COPY_AND_ASSIGN(ClipboardWin); | 128 DISALLOW_COPY_AND_ASSIGN(ClipboardWin); |
| 126 }; | 129 }; |
| 127 | 130 |
| 128 ClipboardWin::ClipboardWin() | 131 ClipboardWin::ClipboardWin() |
| 129 : hwnd_(NULL), | 132 : hwnd_(NULL), |
| 130 add_clipboard_format_listener_(NULL), | 133 add_clipboard_format_listener_(NULL), |
| 131 remove_clipboard_format_listener_(NULL), | 134 remove_clipboard_format_listener_(NULL), |
| 132 load_functions_tried_(false) { | 135 load_functions_tried_(false) { |
| 133 } | 136 } |
| 134 | 137 |
| 135 void ClipboardWin::Start() { | 138 void ClipboardWin::Start( |
| 139 scoped_ptr<protocol::ClipboardStub> client_clipboard) { |
| 140 client_clipboard_.swap(client_clipboard); |
| 141 |
| 136 if (!load_functions_tried_) { | 142 if (!load_functions_tried_) { |
| 137 load_functions_tried_ = true; | 143 load_functions_tried_ = true; |
| 138 HMODULE user32_module = ::GetModuleHandle(L"user32.dll"); | 144 HMODULE user32_module = ::GetModuleHandle(L"user32.dll"); |
| 139 if (!user32_module) { | 145 if (!user32_module) { |
| 140 LOG(WARNING) << "Couldn't find user32.dll."; | 146 LOG(WARNING) << "Couldn't find user32.dll."; |
| 141 } else { | 147 } else { |
| 142 add_clipboard_format_listener_ = | 148 add_clipboard_format_listener_ = |
| 143 reinterpret_cast<AddClipboardFormatListenerFn*>( | 149 reinterpret_cast<AddClipboardFormatListenerFn*>( |
| 144 ::GetProcAddress(user32_module, "AddClipboardFormatListener")); | 150 ::GetProcAddress(user32_module, "AddClipboardFormatListener")); |
| 145 remove_clipboard_format_listener_ = | 151 remove_clipboard_format_listener_ = |
| (...skipping 23 matching lines...) Expand all Loading... |
| 169 } | 175 } |
| 170 | 176 |
| 171 if (HaveClipboardListenerApi()) { | 177 if (HaveClipboardListenerApi()) { |
| 172 if (!(*add_clipboard_format_listener_)(hwnd_)) { | 178 if (!(*add_clipboard_format_listener_)(hwnd_)) { |
| 173 LOG(WARNING) << "AddClipboardFormatListener() failed: " << GetLastError(); | 179 LOG(WARNING) << "AddClipboardFormatListener() failed: " << GetLastError(); |
| 174 } | 180 } |
| 175 } | 181 } |
| 176 } | 182 } |
| 177 | 183 |
| 178 void ClipboardWin::Stop() { | 184 void ClipboardWin::Stop() { |
| 185 client_clipboard_.reset(); |
| 186 |
| 179 if (hwnd_) { | 187 if (hwnd_) { |
| 180 if (HaveClipboardListenerApi()) { | 188 if (HaveClipboardListenerApi()) { |
| 181 (*remove_clipboard_format_listener_)(hwnd_); | 189 (*remove_clipboard_format_listener_)(hwnd_); |
| 182 } | 190 } |
| 183 ::DestroyWindow(hwnd_); | 191 ::DestroyWindow(hwnd_); |
| 184 hwnd_ = NULL; | 192 hwnd_ = NULL; |
| 185 } | 193 } |
| 186 } | 194 } |
| 187 | 195 |
| 188 void ClipboardWin::InjectClipboardEvent( | 196 void ClipboardWin::InjectClipboardEvent( |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 LOG(WARNING) << "Couldn't lock clipboard data: " << GetLastError(); | 254 LOG(WARNING) << "Couldn't lock clipboard data: " << GetLastError(); |
| 247 return; | 255 return; |
| 248 } | 256 } |
| 249 text.assign(text_lock.get()); | 257 text.assign(text_lock.get()); |
| 250 } | 258 } |
| 251 | 259 |
| 252 protocol::ClipboardEvent event; | 260 protocol::ClipboardEvent event; |
| 253 event.set_mime_type(kMimeTypeTextUtf8); | 261 event.set_mime_type(kMimeTypeTextUtf8); |
| 254 event.set_data(UTF16ToUTF8(text)); | 262 event.set_data(UTF16ToUTF8(text)); |
| 255 | 263 |
| 256 // TODO(simonmorris): Send the event to the client. | 264 if (client_clipboard_.get()) { |
| 265 client_clipboard_->InjectClipboardEvent(event); |
| 266 } |
| 257 } | 267 } |
| 258 } | 268 } |
| 259 | 269 |
| 260 bool ClipboardWin::HaveClipboardListenerApi() { | 270 bool ClipboardWin::HaveClipboardListenerApi() { |
| 261 return add_clipboard_format_listener_ && remove_clipboard_format_listener_; | 271 return add_clipboard_format_listener_ && remove_clipboard_format_listener_; |
| 262 } | 272 } |
| 263 | 273 |
| 264 bool ClipboardWin::RegisterWindowClass() { | 274 bool ClipboardWin::RegisterWindowClass() { |
| 265 // This method is only called on the UI thread, so it doesn't matter | 275 // This method is only called on the UI thread, so it doesn't matter |
| 266 // that the following test is not thread-safe. | 276 // that the following test is not thread-safe. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 return 0; | 310 return 0; |
| 301 } | 311 } |
| 302 return ::DefWindowProc(hwnd, msg, wparam, lparam); | 312 return ::DefWindowProc(hwnd, msg, wparam, lparam); |
| 303 } | 313 } |
| 304 | 314 |
| 305 scoped_ptr<Clipboard> Clipboard::Create() { | 315 scoped_ptr<Clipboard> Clipboard::Create() { |
| 306 return scoped_ptr<Clipboard>(new ClipboardWin()); | 316 return scoped_ptr<Clipboard>(new ClipboardWin()); |
| 307 } | 317 } |
| 308 | 318 |
| 309 } // namespace remoting | 319 } // namespace remoting |
| OLD | NEW |