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

Side by Side Diff: remoting/host/win/message_window.cc

Issue 13142002: Moved code implementing message-only windows to a dedicated class (Windows only). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "remoting/host/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[] = "Chromoting_MessageWindow_%p";
14
15 namespace remoting {
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(const std::string& class_name, HINSTANCE instance)
28 : atom_(0),
29 class_name_(class_name),
30 instance_(instance),
31 window_(NULL) {
32 }
33
34 MessageWindow::~MessageWindow() {
35 DCHECK(CalledOnValidThread());
36
37 if (window_ != NULL) {
38 DestroyWindow(window_);
39 window_ = NULL;
40 }
41
42 if (atom_ != 0) {
43 UnregisterClass(MAKEINTATOM(atom_), instance_);
44 atom_ = 0;
45 }
46 }
47
48 bool MessageWindow::Create(Delegate* delegate) {
49 DCHECK(CalledOnValidThread());
50 DCHECK(!atom_);
51 DCHECK(!window_);
52
53 // Register a separate window class for each instance of |MessageWindow|.
54 string16 class_name = UTF8ToUTF16(class_name_);
55 WNDCLASSEX window_class;
56 window_class.cbSize = sizeof(window_class);
57 window_class.style = 0;
58 window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>;
59 window_class.cbClsExtra = 0;
60 window_class.cbWndExtra = 0;
61 window_class.hInstance = instance_;
62 window_class.hIcon = NULL;
63 window_class.hCursor = NULL;
64 window_class.hbrBackground = NULL;
65 window_class.lpszMenuName = NULL;
66 window_class.lpszClassName = class_name.c_str();
67 window_class.hIconSm = NULL;
68 atom_ = RegisterClassEx(&window_class);
69 if (atom_ == 0) {
70 LOG_GETLASTERROR(ERROR)
71 << "Failed to register the window class '" << class_name_ << "'";
72 return false;
73 }
74
75 window_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
76 instance_, delegate);
77 if (window_ == NULL) {
78 LOG_GETLASTERROR(ERROR) << "Failed to create a message-only window";
79 return false;
80 }
81
82 return true;
83 }
84
85 // static
86 LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd,
87 UINT message,
88 WPARAM wparam,
89 LPARAM lparam) {
90 // Store |this| to the window's user data.
dcaiafa 2013/03/28 23:25:35 You're actually storing delegate here, not |this|.
alexeypa (please no reviews) 2013/03/29 16:57:01 Done.
91 if (message == WM_CREATE) {
92 CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lparam);
93 SetLastError(ERROR_SUCCESS);
94 LONG_PTR result = SetWindowLongPtr(
95 hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(cs->lpCreateParams));
96 CHECK(result != 0 || GetLastError() == ERROR_SUCCESS);
97 }
98
99 // Handle the message.
100 Delegate* delegate = reinterpret_cast<Delegate*>(
101 GetWindowLongPtr(hwnd, GWLP_USERDATA));
102 if (delegate) {
103 LRESULT result;
104 if (delegate->HandleMessage(message, wparam, lparam, &result))
105 return result;
106 }
107
108 return DefWindowProc(hwnd, message, wparam, lparam);
109 }
110
111 } // namespace win
112 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698