summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authoralexeypa@chromium.org <alexeypa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-21 08:49:23 +0000
committeralexeypa@chromium.org <alexeypa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-21 08:49:23 +0000
commit5f6ed8ecdc9065f60d1aa2db2b401699f20665be (patch)
treefe1b36e9a8834927e48b9e3c7e25417f4860414a /base
parent59c4e6bffc83ddae0959afe5c355d0f437be3e02 (diff)
downloadchromium_src-5f6ed8ecdc9065f60d1aa2db2b401699f20665be.zip
chromium_src-5f6ed8ecdc9065f60d1aa2db2b401699f20665be.tar.gz
chromium_src-5f6ed8ecdc9065f60d1aa2db2b401699f20665be.tar.bz2
Moved remoting::win::MessageWindow to base::win::MessageWindow so that it could be re-used outside of src/remoting.
BUG=241939 Review URL: https://chromiumcodereview.appspot.com/16780006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@207756 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.gyp3
-rw-r--r--base/base.gypi10
-rw-r--r--base/win/message_window.cc121
-rw-r--r--base/win/message_window.h60
-rw-r--r--base/win/message_window_unittest.cc61
5 files changed, 250 insertions, 5 deletions
diff --git a/base/base.gyp b/base/base.gyp
index d27b2b8..70d5ee7 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -631,6 +631,7 @@
'win/event_trace_provider_unittest.cc',
'win/i18n_unittest.cc',
'win/iunknown_impl_unittest.cc',
+ 'win/message_window_unittest.cc',
'win/object_watcher_unittest.cc',
'win/pe_image_unittest.cc',
'win/registry_unittest.cc',
@@ -639,9 +640,9 @@
'win/scoped_comptr_unittest.cc',
'win/scoped_handle_unittest.cc',
'win/scoped_process_information_unittest.cc',
+ 'win/scoped_variant_unittest.cc',
'win/shortcut_unittest.cc',
'win/startup_information_unittest.cc',
- 'win/scoped_variant_unittest.cc',
'win/win_util_unittest.cc',
'win/wrapped_window_proc_unittest.cc',
],
diff --git a/base/base.gypi b/base/base.gypi
index b28efd2..81d4b71 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -581,8 +581,8 @@
'nix/mime_util_xdg.h',
'nix/xdg_util.cc',
'nix/xdg_util.h',
- 'win/enum_variant.h',
'win/enum_variant.cc',
+ 'win/enum_variant.h',
'win/event_trace_consumer.h',
'win/event_trace_controller.cc',
'win/event_trace_controller.h',
@@ -592,8 +592,10 @@
'win/i18n.h',
'win/iat_patch_function.cc',
'win/iat_patch_function.h',
- 'win/iunknown_impl.h',
'win/iunknown_impl.cc',
+ 'win/iunknown_impl.h',
+ 'win/message_window.cc',
+ 'win/message_window.h',
'win/metro.cc',
'win/metro.h',
'win/object_watcher.cc',
@@ -626,10 +628,10 @@
'win/startup_information.h',
'win/text_services_message_filter.cc',
'win/text_services_message_filter.h',
- 'win/windows_version.cc',
- 'win/windows_version.h',
'win/win_util.cc',
'win/win_util.h',
+ 'win/windows_version.cc',
+ 'win/windows_version.h',
'win/wrapped_window_proc.cc',
'win/wrapped_window_proc.h',
],
diff --git a/base/win/message_window.cc b/base/win/message_window.cc
new file mode 100644
index 0000000..6b2594e
--- /dev/null
+++ b/base/win/message_window.cc
@@ -0,0 +1,121 @@
+// 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/strings/string16.h"
+#include "base/strings/stringprintf.h"
+#include "base/win/wrapped_window_proc.h"
+
+const wchar_t kClassNameFormat[] = L"Chrome_MessageWindow_%p";
+
+namespace base {
+namespace win {
+
+MessageWindow::MessageWindow()
+ : atom_(0),
+ window_(NULL) {
+}
+
+MessageWindow::~MessageWindow() {
+ DCHECK(CalledOnValidThread());
+
+ if (window_ != NULL) {
+ BOOL result = DestroyWindow(window_);
+ DCHECK(result);
+ }
+
+ if (atom_ != 0) {
+ BOOL result = UnregisterClass(
+ MAKEINTATOM(atom_),
+ base::GetModuleFromAddress(&MessageWindow::WindowProc));
+ DCHECK(result);
+ }
+}
+
+bool MessageWindow::Create(Delegate* delegate, const wchar_t* window_name) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(!atom_);
+ DCHECK(!window_);
+
+ // Register a separate window class for each instance of |MessageWindow|.
+ string16 class_name = base::StringPrintf(kClassNameFormat, this);
+ HINSTANCE instance = base::GetModuleFromAddress(&MessageWindow::WindowProc);
+
+ 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 for a message-only window";
+ return false;
+ }
+
+ window_ = CreateWindow(MAKEINTATOM(atom_), window_name, 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 = reinterpret_cast<Delegate*>(
+ GetWindowLongPtr(hwnd, GWLP_USERDATA));
+
+ switch (message) {
+ // Set up the delegate before handling WM_CREATE.
+ case 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);
+ break;
+ }
+
+ // Clear the pointer to stop calling the delegate once WM_DESTROY is
+ // received.
+ case WM_DESTROY: {
+ SetLastError(ERROR_SUCCESS);
+ LONG_PTR result = SetWindowLongPtr(hwnd, GWLP_USERDATA, NULL);
+ CHECK(result != 0 || GetLastError() == ERROR_SUCCESS);
+ break;
+ }
+ }
+
+ // 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..ba4b0b3
--- /dev/null
+++ b/base/win/message_window.h
@@ -0,0 +1,60 @@
+// 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 "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 : public 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();
+
+ // Creates a message-only window. The incoming messages will be handled by
+ // |delegate|. |delegate| must outlive |this|. |window_name| is optional and
+ // can be NULL.
+ bool Create(Delegate* delegate, const wchar_t* window_name);
+
+ 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_;
+
+ // 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..5658af6
--- /dev/null
+++ b/base/win/message_window_unittest.cc
@@ -0,0 +1,61 @@
+// 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 "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, NULL));
+}
+
+// Verifies that the created window can receive messages.
+TEST(MessageWindowTest, SendMessage) {
+ MessageWindowDelegate delegate;
+ win::MessageWindow window;
+ EXPECT_TRUE(window.Create(&delegate, NULL));
+
+ EXPECT_EQ(SendMessage(window.hwnd(), WM_USER, 100, 0), 100);
+}
+
+} // namespace base