OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/win/message_window.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/process_util.h" | |
9 #include "base/stringprintf.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "base/win/wrapped_window_proc.h" | |
12 | |
13 const char kClassNameFormat[] = "Chrome_MessageWindow_%p"; | |
14 | |
15 namespace base { | |
16 namespace win { | |
17 | |
18 MessageWindow::MessageWindow() | |
19 : atom_(0), | |
20 instance_(NULL), | |
21 window_(NULL) { | |
22 class_name_ = base::StringPrintf(kClassNameFormat, this); | |
23 instance_ = base::GetModuleFromAddress(static_cast<WNDPROC>( | |
24 &base::win::WrappedWindowProc<WindowProc>)); | |
25 } | |
26 | |
27 MessageWindow::~MessageWindow() { | |
28 DCHECK(CalledOnValidThread()); | |
29 | |
30 if (window_ != NULL) { | |
31 DestroyWindow(window_); | |
32 window_ = NULL; | |
33 } | |
34 | |
35 if (atom_ != 0) { | |
36 UnregisterClass(MAKEINTATOM(atom_), instance_); | |
37 atom_ = 0; | |
38 } | |
39 } | |
40 | |
41 bool MessageWindow::Create(Delegate* delegate) { | |
42 DCHECK(CalledOnValidThread()); | |
43 DCHECK(!atom_); | |
44 DCHECK(!window_); | |
45 | |
46 // Register a separate window class for each instance of |MessageWindow|. | |
47 string16 class_name = UTF8ToUTF16(class_name_); | |
48 WNDCLASSEX window_class; | |
49 window_class.cbSize = sizeof(window_class); | |
50 window_class.style = 0; | |
51 window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>; | |
52 window_class.cbClsExtra = 0; | |
53 window_class.cbWndExtra = 0; | |
54 window_class.hInstance = instance_; | |
55 window_class.hIcon = NULL; | |
56 window_class.hCursor = NULL; | |
57 window_class.hbrBackground = NULL; | |
58 window_class.lpszMenuName = NULL; | |
59 window_class.lpszClassName = class_name.c_str(); | |
60 window_class.hIconSm = NULL; | |
61 atom_ = RegisterClassEx(&window_class); | |
62 if (atom_ == 0) { | |
63 LOG_GETLASTERROR(ERROR) | |
64 << "Failed to register the window class '" << class_name_ << "'"; | |
65 return false; | |
66 } | |
67 | |
68 window_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, | |
69 instance_, delegate); | |
70 if (!window_) { | |
71 LOG_GETLASTERROR(ERROR) << "Failed to create a message-only window"; | |
72 return false; | |
73 } | |
74 | |
75 return true; | |
76 } | |
77 | |
78 // static | |
79 LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd, | |
80 UINT message, | |
81 WPARAM wparam, | |
82 LPARAM lparam) { | |
83 Delegate* delegate = NULL; | |
84 | |
85 // Set up the delegate before handling WM_CREATE. | |
86 if (message == WM_CREATE) { | |
87 CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lparam); | |
88 delegate = reinterpret_cast<Delegate*>(cs->lpCreateParams); | |
89 | |
90 // Store pointer to the delegate to the window's user data. | |
91 SetLastError(ERROR_SUCCESS); | |
92 LONG_PTR result = SetWindowLongPtr( | |
93 hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(delegate)); | |
94 CHECK(result != 0 || GetLastError() == ERROR_SUCCESS); | |
95 } else { | |
96 delegate = reinterpret_cast<Delegate*>(GetWindowLongPtr(hwnd, | |
97 GWLP_USERDATA)); | |
98 } | |
99 | |
100 // Handle the message. | |
101 if (delegate) { | |
102 LRESULT message_result; | |
103 if (delegate->HandleMessage(hwnd, message, wparam, lparam, &message_result)) | |
104 return message_result; | |
105 } | |
106 | |
107 return DefWindowProc(hwnd, message, wparam, lparam); | |
108 } | |
109 | |
110 } // namespace win | |
111 } // namespace base | |
OLD | NEW |