OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef BASE_WIN_MESSAGE_WINDOW_H_ | 5 #ifndef BASE_WIN_MESSAGE_WINDOW_H_ |
6 #define BASE_WIN_MESSAGE_WINDOW_H_ | 6 #define BASE_WIN_MESSAGE_WINDOW_H_ |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 namespace win { | 18 namespace win { |
19 | 19 |
20 // Implements a message-only window. | 20 // Implements a message-only window. |
21 class BASE_EXPORT MessageWindow : public base::NonThreadSafe { | 21 class BASE_EXPORT MessageWindow : public base::NonThreadSafe { |
22 public: | 22 public: |
| 23 // Used to register a process-wide message window class. |
| 24 class WindowClass; |
| 25 |
23 // Implement this callback to handle messages received by the message window. | 26 // Implement this callback to handle messages received by the message window. |
24 // If the callback returns |false|, the first four parameters are passed to | 27 // If the callback returns |false|, the first four parameters are passed to |
25 // DefWindowProc(). Otherwise, |*result| is returned by the window procedure. | 28 // DefWindowProc(). Otherwise, |*result| is returned by the window procedure. |
26 typedef base::Callback<bool(UINT message, | 29 typedef base::Callback<bool(UINT message, |
27 WPARAM wparam, | 30 WPARAM wparam, |
28 LPARAM lparam, | 31 LPARAM lparam, |
29 LRESULT* result)> MessageCallback; | 32 LRESULT* result)> MessageCallback; |
30 | 33 |
31 MessageWindow(); | 34 MessageWindow(); |
32 ~MessageWindow(); | 35 ~MessageWindow(); |
33 | 36 |
34 // Creates a message-only window. The incoming messages will be passed by | 37 // Creates a message-only window. The incoming messages will be passed by |
35 // |message_callback|. |message_callback| must outlive |this|. | 38 // |message_callback|. |message_callback| must outlive |this|. |
36 bool Create(const MessageCallback& message_callback); | 39 bool Create(const MessageCallback& message_callback); |
37 | 40 |
38 // Same as Create() but assigns the name to the created window. | 41 // Same as Create() but assigns the name to the created window. |
39 bool CreateNamed(const MessageCallback& message_callback, | 42 bool CreateNamed(const MessageCallback& message_callback, |
40 const string16& window_name); | 43 const string16& window_name); |
41 | 44 |
42 HWND hwnd() const { return window_; } | 45 HWND hwnd() const { return window_; } |
43 | 46 |
| 47 // Retrieves a handle of the first message-only window with matching |
| 48 // |windows_name|. |
| 49 static HWND FindWindow(const string16& window_name); |
| 50 |
44 private: | 51 private: |
| 52 // Give |WindowClass| access to WindowProc(). |
| 53 friend class WindowClass; |
| 54 |
45 // Contains the actual window creation code. | 55 // Contains the actual window creation code. |
46 bool DoCreate(const MessageCallback& message_callback, | 56 bool DoCreate(const MessageCallback& message_callback, |
47 const wchar_t* window_name); | 57 const wchar_t* window_name); |
48 | 58 |
49 // Invoked by the OS to process incoming window messages. | 59 // Invoked by the OS to process incoming window messages. |
50 static LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, | 60 static LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, |
51 LPARAM lparam); | 61 LPARAM lparam); |
52 | 62 |
53 // Atom representing the registered window class. | |
54 ATOM atom_; | |
55 | |
56 // Invoked to handle messages received by the window. | 63 // Invoked to handle messages received by the window. |
57 MessageCallback message_callback_; | 64 MessageCallback message_callback_; |
58 | 65 |
59 // Handle of the input window. | 66 // Handle of the input window. |
60 HWND window_; | 67 HWND window_; |
61 | 68 |
62 DISALLOW_COPY_AND_ASSIGN(MessageWindow); | 69 DISALLOW_COPY_AND_ASSIGN(MessageWindow); |
63 }; | 70 }; |
64 | 71 |
65 } // namespace win | 72 } // namespace win |
66 } // namespace base | 73 } // namespace base |
67 | 74 |
68 #endif // BASE_WIN_MESSAGE_WINDOW_H_ | 75 #endif // BASE_WIN_MESSAGE_WINDOW_H_ |
OLD | NEW |