OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "ui/gfx/win/window_impl.h" | 5 #include "ui/gfx/win/window_impl.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/debug/alias.h" | 9 #include "base/debug/alias.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 } | 43 } |
44 }; | 44 }; |
45 | 45 |
46 // WARNING: this class may be used on multiple threads. | 46 // WARNING: this class may be used on multiple threads. |
47 class ClassRegistrar { | 47 class ClassRegistrar { |
48 public: | 48 public: |
49 ~ClassRegistrar(); | 49 ~ClassRegistrar(); |
50 | 50 |
51 static ClassRegistrar* GetInstance(); | 51 static ClassRegistrar* GetInstance(); |
52 | 52 |
| 53 void set_unregister_at_exit(bool unregister_at_exit) { |
| 54 unregister_at_exit_ = unregister_at_exit; |
| 55 } |
| 56 |
53 // Returns the atom identifying the class matching |class_info|, | 57 // Returns the atom identifying the class matching |class_info|, |
54 // creating and registering a new class if the class is not yet known. | 58 // creating and registering a new class if the class is not yet known. |
55 ATOM RetrieveClassAtom(const ClassInfo& class_info); | 59 ATOM RetrieveClassAtom(const ClassInfo& class_info); |
56 | 60 |
57 private: | 61 private: |
58 // Represents a registered window class. | 62 // Represents a registered window class. |
59 struct RegisteredClass { | 63 struct RegisteredClass { |
60 RegisteredClass(const ClassInfo& info, ATOM atom); | 64 RegisteredClass(const ClassInfo& info, |
| 65 base::string16 name, |
| 66 ATOM atom, |
| 67 HINSTANCE instance); |
61 | 68 |
62 // Info used to create the class. | 69 // Info used to create the class. |
63 ClassInfo info; | 70 ClassInfo info; |
64 | 71 |
| 72 // The name given to the window class |
| 73 base::string16 name; |
| 74 |
65 // The atom identifying the window class. | 75 // The atom identifying the window class. |
66 ATOM atom; | 76 ATOM atom; |
| 77 |
| 78 // The handle of the module containing the window proceedure. |
| 79 HMODULE instance; |
67 }; | 80 }; |
68 | 81 |
69 ClassRegistrar(); | 82 ClassRegistrar(); |
70 friend struct DefaultSingletonTraits<ClassRegistrar>; | 83 friend struct DefaultSingletonTraits<ClassRegistrar>; |
71 | 84 |
72 typedef std::list<RegisteredClass> RegisteredClasses; | 85 typedef std::list<RegisteredClass> RegisteredClasses; |
73 RegisteredClasses registered_classes_; | 86 RegisteredClasses registered_classes_; |
74 | 87 |
75 // Counter of how many classes have been registered so far. | 88 // Counter of how many classes have been registered so far. |
76 int registered_count_; | 89 int registered_count_; |
77 | 90 |
| 91 // If true all registered classes will be unregistered upon exit. |
| 92 bool unregister_at_exit_; |
| 93 |
78 base::Lock lock_; | 94 base::Lock lock_; |
79 | 95 |
80 DISALLOW_COPY_AND_ASSIGN(ClassRegistrar); | 96 DISALLOW_COPY_AND_ASSIGN(ClassRegistrar); |
81 }; | 97 }; |
82 | 98 |
83 ClassRegistrar::~ClassRegistrar() {} | 99 ClassRegistrar::~ClassRegistrar() { |
| 100 if (!unregister_at_exit_) |
| 101 return; |
| 102 |
| 103 for (RegisteredClasses::iterator i = registered_classes_.begin(); |
| 104 i != registered_classes_.end(); ++i) { |
| 105 if (!UnregisterClass(MAKEINTATOM(i->atom), i->instance)) { |
| 106 LOG(ERROR) << "Failed to unregister class " << i->name |
| 107 << ". Error = " << GetLastError(); |
| 108 } |
| 109 } |
| 110 } |
84 | 111 |
85 // static | 112 // static |
86 ClassRegistrar* ClassRegistrar::GetInstance() { | 113 ClassRegistrar* ClassRegistrar::GetInstance() { |
87 return Singleton<ClassRegistrar, | 114 return Singleton<ClassRegistrar>::get(); |
88 LeakySingletonTraits<ClassRegistrar> >::get(); | |
89 } | 115 } |
90 | 116 |
91 ATOM ClassRegistrar::RetrieveClassAtom(const ClassInfo& class_info) { | 117 ATOM ClassRegistrar::RetrieveClassAtom(const ClassInfo& class_info) { |
92 base::AutoLock auto_lock(lock_); | 118 base::AutoLock auto_lock(lock_); |
93 for (RegisteredClasses::const_iterator i = registered_classes_.begin(); | 119 for (RegisteredClasses::const_iterator i = registered_classes_.begin(); |
94 i != registered_classes_.end(); ++i) { | 120 i != registered_classes_.end(); ++i) { |
95 if (class_info.Equals(i->info)) | 121 if (class_info.Equals(i->info)) |
96 return i->atom; | 122 return i->atom; |
97 } | 123 } |
98 | 124 |
(...skipping 11 matching lines...) Expand all Loading... |
110 NULL, | 136 NULL, |
111 reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)), | 137 reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)), |
112 NULL, | 138 NULL, |
113 class_info.icon, | 139 class_info.icon, |
114 class_info.icon, | 140 class_info.icon, |
115 &window_class); | 141 &window_class); |
116 HMODULE instance = window_class.hInstance; | 142 HMODULE instance = window_class.hInstance; |
117 ATOM atom = RegisterClassEx(&window_class); | 143 ATOM atom = RegisterClassEx(&window_class); |
118 CHECK(atom) << GetLastError(); | 144 CHECK(atom) << GetLastError(); |
119 | 145 |
120 registered_classes_.push_back(RegisteredClass(class_info, atom)); | 146 registered_classes_.push_back(RegisteredClass( |
| 147 class_info, name, atom, instance)); |
121 | 148 |
122 return atom; | 149 return atom; |
123 } | 150 } |
124 | 151 |
125 ClassRegistrar::RegisteredClass::RegisteredClass(const ClassInfo& info, | 152 ClassRegistrar::RegisteredClass::RegisteredClass(const ClassInfo& info, |
126 ATOM atom) | 153 base::string16 name, |
| 154 ATOM atom, |
| 155 HMODULE instance) |
127 : info(info), | 156 : info(info), |
128 atom(atom) {} | 157 name(name), |
| 158 atom(atom), |
| 159 instance(instance) {} |
129 | 160 |
130 ClassRegistrar::ClassRegistrar() : registered_count_(0) {} | 161 ClassRegistrar::ClassRegistrar() |
| 162 : registered_count_(0), |
| 163 unregister_at_exit_(false) {} |
131 | 164 |
132 | 165 |
133 /////////////////////////////////////////////////////////////////////////////// | 166 /////////////////////////////////////////////////////////////////////////////// |
134 // WindowImpl, public | 167 // WindowImpl, public |
135 | 168 |
136 WindowImpl::WindowImpl() | 169 WindowImpl::WindowImpl() |
137 : window_style_(0), | 170 : window_style_(0), |
138 window_ex_style_(kWindowDefaultExStyle), | 171 window_ex_style_(kWindowDefaultExStyle), |
139 class_style_(CS_DBLCLKS), | 172 class_style_(CS_DBLCLKS), |
140 hwnd_(NULL), | 173 hwnd_(NULL), |
141 got_create_(false), | 174 got_create_(false), |
142 got_valid_hwnd_(false), | 175 got_valid_hwnd_(false), |
143 destroyed_(NULL) { | 176 destroyed_(NULL) { |
144 } | 177 } |
145 | 178 |
146 WindowImpl::~WindowImpl() { | 179 WindowImpl::~WindowImpl() { |
147 if (destroyed_) | 180 if (destroyed_) |
148 *destroyed_ = true; | 181 *destroyed_ = true; |
149 ClearUserData(); | 182 ClearUserData(); |
150 } | 183 } |
151 | 184 |
| 185 // static |
| 186 void WindowImpl::SetUnregisterClassesAtExit(bool unregister_classes_at_exit) { |
| 187 ClassRegistrar::GetInstance()->set_unregister_at_exit( |
| 188 unregister_classes_at_exit); |
| 189 } |
| 190 |
152 void WindowImpl::Init(HWND parent, const Rect& bounds) { | 191 void WindowImpl::Init(HWND parent, const Rect& bounds) { |
153 if (window_style_ == 0) | 192 if (window_style_ == 0) |
154 window_style_ = parent ? kWindowDefaultChildStyle : kWindowDefaultStyle; | 193 window_style_ = parent ? kWindowDefaultChildStyle : kWindowDefaultStyle; |
155 | 194 |
156 if (parent == HWND_DESKTOP) { | 195 if (parent == HWND_DESKTOP) { |
157 // Only non-child windows can have HWND_DESKTOP (0) as their parent. | 196 // Only non-child windows can have HWND_DESKTOP (0) as their parent. |
158 CHECK((window_style_ & WS_CHILD) == 0); | 197 CHECK((window_style_ & WS_CHILD) == 0); |
159 parent = GetWindowToParentTo(false); | 198 parent = GetWindowToParentTo(false); |
160 } else if (parent == ::GetDesktopWindow()) { | 199 } else if (parent == ::GetDesktopWindow()) { |
161 // Any type of window can have the "Desktop Window" as their parent. | 200 // Any type of window can have the "Desktop Window" as their parent. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 return window->OnWndProc(message, w_param, l_param); | 306 return window->OnWndProc(message, w_param, l_param); |
268 } | 307 } |
269 | 308 |
270 ATOM WindowImpl::GetWindowClassAtom() { | 309 ATOM WindowImpl::GetWindowClassAtom() { |
271 HICON icon = GetDefaultWindowIcon(); | 310 HICON icon = GetDefaultWindowIcon(); |
272 ClassInfo class_info(initial_class_style(), icon); | 311 ClassInfo class_info(initial_class_style(), icon); |
273 return ClassRegistrar::GetInstance()->RetrieveClassAtom(class_info); | 312 return ClassRegistrar::GetInstance()->RetrieveClassAtom(class_info); |
274 } | 313 } |
275 | 314 |
276 } // namespace gfx | 315 } // namespace gfx |
OLD | NEW |