Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Unified Diff: ui/base/clipboard/clipboard_win.cc

Issue 17285002: ui::Clipboard now uses base::win::MessageWindow to create a message-only window on Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/base/clipboard/clipboard.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/base/clipboard/clipboard_win.cc
diff --git a/ui/base/clipboard/clipboard_win.cc b/ui/base/clipboard/clipboard_win.cc
index 47a97d90bbcd5efbadfafba5eaa75e313cfa851e..0c636c43c51ae8aadd23f4dbf0ae063d063162a8 100644
--- a/ui/base/clipboard/clipboard_win.cc
+++ b/ui/base/clipboard/clipboard_win.cc
@@ -21,6 +21,7 @@
#include "base/strings/string_util.h"
#include "base/strings/utf_offset_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
+#include "base/win/message_window.h"
#include "base/win/scoped_gdi_object.h"
#include "base/win/scoped_hdc.h"
#include "base/win/wrapped_window_proc.h"
@@ -94,37 +95,6 @@ class ScopedClipboard {
bool opened_;
};
-LRESULT CALLBACK ClipboardOwnerWndProc(HWND hwnd,
- UINT message,
- WPARAM wparam,
- LPARAM lparam) {
- LRESULT lresult = 0;
-
- switch (message) {
- case WM_RENDERFORMAT:
- // This message comes when SetClipboardData was sent a null data handle
- // and now it's come time to put the data on the clipboard.
- // We always set data, so there isn't a need to actually do anything here.
- break;
- case WM_RENDERALLFORMATS:
- // This message comes when SetClipboardData was sent a null data handle
- // and now this application is about to quit, so it must put data on
- // the clipboard before it exits.
- // We always set data, so there isn't a need to actually do anything here.
- break;
- case WM_DRAWCLIPBOARD:
- break;
- case WM_DESTROY:
- break;
- case WM_CHANGECBCHAIN:
- break;
- default:
- lresult = DefWindowProc(hwnd, message, wparam, lparam);
- break;
- }
- return lresult;
-}
-
template <typename charT>
HGLOBAL CreateGlobalData(const std::basic_string<charT>& str) {
HGLOBAL data =
@@ -161,6 +131,84 @@ void MakeBitmapOpaque(const SkBitmap& bitmap) {
} // namespace
+// Used to create a message-only window handling clipboard-related window
+// messages. It is intended to be passed to OpenClipboard() API.
+class Clipboard::ClipboardWindow
+ : public base::win::MessageWindow::Delegate {
+ public:
+ ClipboardWindow();
+ virtual ~ClipboardWindow();
+
+ // Creates the window if it is not created yat and returns its handle. Returns
+ // NULL if an error occurs.
+ HWND GetHandle();
+
+ // Returns |true| if the window is created.
+ bool IsValid() const;
+
+ private:
+ // base::win::MessageWindow::Delegate interface.
+ virtual bool HandleMessage(HWND hwnd,
+ UINT message,
+ WPARAM wparam,
+ LPARAM lparam,
+ LRESULT* result) OVERRIDE;
+
+ scoped_ptr<base::win::MessageWindow> window_;
cpu_(ooo_6.6-7.5) 2013/06/19 00:11:36 why scoped_ptr?
alexeypa (please no reviews) 2013/06/19 17:57:18 Yes, indeed. It does not need to be.
+
+ DISALLOW_COPY_AND_ASSIGN(ClipboardWindow);
+};
+
+
+Clipboard::ClipboardWindow::ClipboardWindow()
+ : window_(new base::win::MessageWindow()) {
+}
+
+Clipboard::ClipboardWindow::~ClipboardWindow() {
+}
+
+HWND Clipboard::ClipboardWindow::GetHandle() {
+ if (!IsValid())
+ window_->Create(this);
+
+ return window_->hwnd();
+}
+
+bool Clipboard::ClipboardWindow::IsValid() const {
+ return window_->hwnd() != NULL;
+}
+
+bool Clipboard::ClipboardWindow::HandleMessage(HWND hwnd,
+ UINT message,
+ WPARAM wparam,
+ LPARAM lparam,
+ LRESULT* result) {
+ switch (message) {
+ case WM_RENDERFORMAT:
+ // This message comes when SetClipboardData was sent a null data handle
+ // and now it's come time to put the data on the clipboard.
+ // We always set data, so there isn't a need to actually do anything here.
+ break;
+ case WM_RENDERALLFORMATS:
+ // This message comes when SetClipboardData was sent a null data handle
+ // and now this application is about to quit, so it must put data on
+ // the clipboard before it exits.
+ // We always set data, so there isn't a need to actually do anything here.
+ break;
+ case WM_DRAWCLIPBOARD:
+ break;
+ case WM_DESTROY:
+ break;
+ case WM_CHANGECBCHAIN:
+ break;
+ default:
+ return false;
+ }
+
+ *result = 0;
+ return true;
+}
+
Clipboard::FormatType::FormatType() : data_() {}
Clipboard::FormatType::FormatType(UINT native_format) : data_() {
@@ -203,26 +251,12 @@ bool Clipboard::FormatType::operator<(const FormatType& other) const {
return ToUINT() < other.ToUINT();
}
-Clipboard::Clipboard() : create_window_(false) {
- if (base::MessageLoop::current()->type() == base::MessageLoop::TYPE_UI) {
- // Make a dummy HWND to be the clipboard's owner.
- WNDCLASSEX window_class;
- base::win::InitializeWindowClass(
- L"ClipboardOwnerWindowClass",
- &base::win::WrappedWindowProc<ClipboardOwnerWndProc>,
- 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
- &window_class);
- ::RegisterClassEx(&window_class);
- create_window_ = true;
- }
-
- clipboard_owner_ = NULL;
+Clipboard::Clipboard() {
+ if (base::MessageLoop::current()->type() == base::MessageLoop::TYPE_UI)
+ clipboard_owner_.reset(new ClipboardWindow());
}
Clipboard::~Clipboard() {
- if (clipboard_owner_)
- ::DestroyWindow(clipboard_owner_);
- clipboard_owner_ = NULL;
}
void Clipboard::WriteObjects(Buffer buffer, const ObjectMap& objects) {
@@ -283,7 +317,7 @@ void Clipboard::WriteBookmark(const char* title_data,
}
void Clipboard::WriteWebSmartPaste() {
- DCHECK(clipboard_owner_);
+ DCHECK(clipboard_owner_->IsValid());
::SetClipboardData(GetWebKitSmartPasteFormatType().ToUINT(), NULL);
}
@@ -378,7 +412,7 @@ void Clipboard::WriteData(const FormatType& format,
}
void Clipboard::WriteToClipboard(unsigned int format, HANDLE handle) {
- DCHECK(clipboard_owner_);
+ DCHECK(clipboard_owner_->IsValid());
if (handle && !::SetClipboardData(format, handle)) {
DCHECK(ERROR_CLIPBOARD_NOT_OPEN != GetLastError());
FreeData(format, handle);
@@ -825,14 +859,7 @@ void Clipboard::FreeData(unsigned int format, HANDLE data) {
}
HWND Clipboard::GetClipboardWindow() const {
- if (!clipboard_owner_ && create_window_) {
- clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass",
- L"ClipboardOwnerWindow",
- 0, 0, 0, 0, 0,
- HWND_MESSAGE,
- 0, 0, 0);
- }
- return clipboard_owner_;
+ return clipboard_owner_ ? clipboard_owner_->GetHandle() : NULL;
}
} // namespace ui
« no previous file with comments | « ui/base/clipboard/clipboard.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698