summaryrefslogtreecommitdiffstats
path: root/base/win
diff options
context:
space:
mode:
authoralexeypa@chromium.org <alexeypa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-05-24 01:34:51 +0000
committeralexeypa@chromium.org <alexeypa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-05-24 01:34:51 +0000
commitd5eb9ccc118f56565325cff1cbb24571511d1925 (patch)
treee5b9c05d2954b4c1f416970c8647463587aa73a1 /base/win
parent3214fd26bb2ccd17e2611f69c895f4b43cf87bf1 (diff)
downloadchromium_src-d5eb9ccc118f56565325cff1cbb24571511d1925.zip
chromium_src-d5eb9ccc118f56565325cff1cbb24571511d1925.tar.gz
chromium_src-d5eb9ccc118f56565325cff1cbb24571511d1925.tar.bz2
Allow multiple base::MessagePumpForUI instances to be created simultanenously on Windows.
The current implementation of base::MessagePumpForUI on Windows registers a window class with a predefined name in order to create a message-only window. The window class is unregistered when base::MessagePumpForUI is deleted. This causes issues when two or more instances of base::MessagePumpForUI are created/destroyed simultanenously on different threads. For instance once thread can unregister the window class right before the other thread is trying to create a window using this class. The CL addresses this problem by switching MessageWindow to implement a message-only window. It also moves MessageWindow from remoting/host/win to base/win along with the corresponding unit test. MessageWindow registers a uniquely named window class per MessageWindow instance making sure that different MessageWindow objects do not share any resources. In the future this can be optimized further by registering a common window class shared by all MessageWindow objects in a thread-safe manner (by using LazyInstance for example). BUG=241939 Review URL: https://chromiumcodereview.appspot.com/15261005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@201955 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/win')
-rw-r--r--base/win/message_window.cc111
-rw-r--r--base/win/message_window.h67
-rw-r--r--base/win/message_window_unittest.cc61
3 files changed, 239 insertions, 0 deletions
diff --git a/base/win/message_window.cc b/base/win/message_window.cc
new file mode 100644
index 0000000..10d20ec
--- /dev/null
+++ b/base/win/message_window.cc
@@ -0,0 +1,111 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/win/message_window.h"
+
+#include "base/logging.h"
+#include "base/process_util.h"
+#include "base/stringprintf.h"
+#include "base/utf_string_conversions.h"
+#include "base/win/wrapped_window_proc.h"
+
+const char kClassNameFormat[] = "Chrome_MessageWindow_%p";
+
+namespace base {
+namespace win {
+
+MessageWindow::MessageWindow()
+ : atom_(0),
+ instance_(NULL),
+ window_(NULL) {
+ class_name_ = base::StringPrintf(kClassNameFormat, this);
+ instance_ = base::GetModuleFromAddress(static_cast<WNDPROC>(
+ &base::win::WrappedWindowProc<WindowProc>));
+}
+
+MessageWindow::~MessageWindow() {
+ DCHECK(CalledOnValidThread());
+
+ if (window_ != NULL) {
+ DestroyWindow(window_);
+ window_ = NULL;
+ }
+
+ if (atom_ != 0) {
+ UnregisterClass(MAKEINTATOM(atom_), instance_);
+ atom_ = 0;
+ }
+}
+
+bool MessageWindow::Create(Delegate* delegate) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(!atom_);
+ DCHECK(!window_);
+
+ // Register a separate window class for each instance of |MessageWindow|.
+ string16 class_name = UTF8ToUTF16(class_name_);
+ WNDCLASSEX window_class;
+ window_class.cbSize = sizeof(window_class);
+ window_class.style = 0;
+ window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>;
+ window_class.cbClsExtra = 0;
+ window_class.cbWndExtra = 0;
+ window_class.hInstance = instance_;
+ window_class.hIcon = NULL;
+ window_class.hCursor = NULL;
+ window_class.hbrBackground = NULL;
+ window_class.lpszMenuName = NULL;
+ window_class.lpszClassName = class_name.c_str();
+ window_class.hIconSm = NULL;
+ atom_ = RegisterClassEx(&window_class);
+ if (atom_ == 0) {
+ LOG_GETLASTERROR(ERROR)
+ << "Failed to register the window class '" << class_name_ << "'";
+ return false;
+ }
+
+ window_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
+ instance_, delegate);
+ if (!window_) {
+ LOG_GETLASTERROR(ERROR) << "Failed to create a message-only window";
+ return false;
+ }
+
+ return true;
+}
+
+// static
+LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd,
+ UINT message,
+ WPARAM wparam,
+ LPARAM lparam) {
+ Delegate* delegate = NULL;
+
+ // Set up the delegate before handling WM_CREATE.
+ if (message == WM_CREATE) {
+ CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lparam);
+ delegate = reinterpret_cast<Delegate*>(cs->lpCreateParams);
+
+ // Store pointer to the delegate to the window's user data.
+ SetLastError(ERROR_SUCCESS);
+ LONG_PTR result = SetWindowLongPtr(
+ hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(delegate));
+ CHECK(result != 0 || GetLastError() == ERROR_SUCCESS);
+ } else {
+ delegate = reinterpret_cast<Delegate*>(GetWindowLongPtr(hwnd,
+ GWLP_USERDATA));
+ }
+
+ // Handle the message.
+ if (delegate) {
+ LRESULT message_result;
+ if (delegate->HandleMessage(hwnd, message, wparam, lparam, &message_result))
+ return message_result;
+ }
+
+ return DefWindowProc(hwnd, message, wparam, lparam);
+}
+
+} // namespace win
+} // namespace base
diff --git a/base/win/message_window.h b/base/win/message_window.h
new file mode 100644
index 0000000..9cc4573
--- /dev/null
+++ b/base/win/message_window.h
@@ -0,0 +1,67 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_WIN_MESSAGE_WINDOW_H_
+#define BASE_WIN_MESSAGE_WINDOW_H_
+
+#include <windows.h>
+
+#include <string>
+
+#include "base/base_export.h"
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "base/threading/non_thread_safe.h"
+
+namespace base {
+namespace win {
+
+// Implements a message-only window.
+class BASE_EXPORT MessageWindow : base::NonThreadSafe {
+ public:
+ // Handles incoming window messages.
+ class BASE_EXPORT Delegate {
+ public:
+ virtual ~Delegate() {}
+
+ virtual bool HandleMessage(HWND hwnd,
+ UINT message,
+ WPARAM wparam,
+ LPARAM lparam,
+ LRESULT* result) = 0;
+ };
+
+ MessageWindow();
+ ~MessageWindow();
+
+ // Registers the window class and creates the window. The incoming messages
+ // will be handled by |delegate|. |delegate| must outlive |this|.
+ bool Create(Delegate* delegate);
+
+ HWND hwnd() const { return window_; }
+
+ private:
+ // Invoked by the OS to process incoming window messages.
+ static LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam,
+ LPARAM lparam);
+
+ // Atom representing the registered window class.
+ ATOM atom_;
+
+ // MessageWindow class name.
+ std::string class_name_;
+
+ // Instance of the module containing the window procedure.
+ HINSTANCE instance_;
+
+ // Handle of the input window.
+ HWND window_;
+
+ DISALLOW_COPY_AND_ASSIGN(MessageWindow);
+};
+
+} // namespace win
+} // namespace base
+
+#endif // BASE_WIN_MESSAGE_WINDOW_H_
diff --git a/base/win/message_window_unittest.cc b/base/win/message_window_unittest.cc
new file mode 100644
index 0000000..5910a7b
--- /dev/null
+++ b/base/win/message_window_unittest.cc
@@ -0,0 +1,61 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/win/message_window.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+namespace {
+
+class MessageWindowDelegate : public win::MessageWindow::Delegate {
+ public:
+ MessageWindowDelegate();
+ virtual ~MessageWindowDelegate();
+
+ // MessageWindow::Delegate interface.
+ virtual bool HandleMessage(HWND hwnd,
+ UINT message,
+ WPARAM wparam,
+ LPARAM lparam,
+ LRESULT* result) OVERRIDE;
+};
+
+MessageWindowDelegate::MessageWindowDelegate() {
+}
+
+MessageWindowDelegate::~MessageWindowDelegate() {
+}
+
+bool MessageWindowDelegate::HandleMessage(
+ HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, LRESULT* result) {
+ // Return |wparam| as the result of WM_USER message.
+ if (message == WM_USER) {
+ *result = wparam;
+ return true;
+ }
+
+ return false;
+}
+
+} // namespace
+
+// Checks that a window can be created.
+TEST(MessageWindowTest, Create) {
+ MessageWindowDelegate delegate;
+ win::MessageWindow window;
+ EXPECT_TRUE(window.Create(&delegate));
+}
+
+// Verifies that the created window can receive messages.
+TEST(MessageWindowTest, SendMessage) {
+ MessageWindowDelegate delegate;
+ win::MessageWindow window;
+ EXPECT_TRUE(window.Create(&delegate));
+
+ EXPECT_EQ(SendMessage(window.hwnd(), WM_USER, 100, 0), 100);
+}
+
+} // namespace base