| 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/base/win/window_impl.h" | 5 #include "ui/base/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 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 // Compares two ClassInfos. Returns true if all members match. | 39 // Compares two ClassInfos. Returns true if all members match. |
| 40 bool Equals(const ClassInfo& other) const { | 40 bool Equals(const ClassInfo& other) const { |
| 41 return (other.style == style && other.icon == icon); | 41 return (other.style == style && other.icon == icon); |
| 42 } | 42 } |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 class ClassRegistrar { | 45 class ClassRegistrar { |
| 46 public: | 46 public: |
| 47 static ClassRegistrar* GetInstance() { | 47 static ClassRegistrar* GetInstance() { |
| 48 return Singleton<ClassRegistrar>::get(); | 48 return Singleton<ClassRegistrar, |
| 49 LeakySingletonTraits<ClassRegistrar> >::get(); |
| 49 } | 50 } |
| 50 | 51 |
| 51 ~ClassRegistrar() { | 52 ~ClassRegistrar() { |
| 52 for (RegisteredClasses::iterator i = registered_classes_.begin(); | |
| 53 i != registered_classes_.end(); ++i) { | |
| 54 if (!UnregisterClass(MAKEINTATOM(i->atom), i->instance)) { | |
| 55 LOG(ERROR) << "Failed to unregister class " << i->name.c_str() | |
| 56 << ". Error = " << GetLastError(); | |
| 57 } | |
| 58 } | |
| 59 } | 53 } |
| 60 | 54 |
| 61 // Puts the name for the class matching |class_info| in |class_name|, creating | 55 // Puts the name for the class matching |class_info| in |class_name|, creating |
| 62 // a new name if the class is not yet known. | 56 // a new name if the class is not yet known. |
| 63 // Returns true if this class was already known, false otherwise. | 57 // Returns true if this class was already known, false otherwise. |
| 64 bool RetrieveClassName(const ClassInfo& class_info, std::wstring* name) { | 58 bool RetrieveClassName(const ClassInfo& class_info, std::wstring* name) { |
| 65 for (RegisteredClasses::const_iterator i = registered_classes_.begin(); | 59 for (RegisteredClasses::const_iterator i = registered_classes_.begin(); |
| 66 i != registered_classes_.end(); ++i) { | 60 i != registered_classes_.end(); ++i) { |
| 67 if (class_info.Equals(i->info)) { | 61 if (class_info.Equals(i->info)) { |
| 68 name->assign(i->name); | 62 name->assign(i->name); |
| 69 return true; | 63 return true; |
| 70 } | 64 } |
| 71 } | 65 } |
| 72 | 66 |
| 73 name->assign(string16(WindowImpl::kBaseClassName) + | 67 name->assign(string16(WindowImpl::kBaseClassName) + |
| 74 base::IntToString16(registered_count_++)); | 68 base::IntToString16(registered_count_++)); |
| 75 return false; | 69 return false; |
| 76 } | 70 } |
| 77 | 71 |
| 78 void RegisterClass(const ClassInfo& class_info, | 72 void RegisterClass(const ClassInfo& class_info, |
| 79 const std::wstring& name, | 73 const std::wstring& name) { |
| 80 ATOM atom, | 74 registered_classes_.push_back(RegisteredClass(class_info, name)); |
| 81 HMODULE instance) { | |
| 82 registered_classes_.push_back( | |
| 83 RegisteredClass(class_info, name, atom, instance)); | |
| 84 } | 75 } |
| 85 | 76 |
| 86 private: | 77 private: |
| 87 // Represents a registered window class. | 78 // Represents a registered window class. |
| 88 struct RegisteredClass { | 79 struct RegisteredClass { |
| 89 RegisteredClass(const ClassInfo& info, | 80 RegisteredClass(const ClassInfo& info, |
| 90 const std::wstring& name, | 81 const std::wstring& name) |
| 91 ATOM atom, | |
| 92 HMODULE instance) | |
| 93 : info(info), | 82 : info(info), |
| 94 name(name), | 83 name(name) { |
| 95 atom(atom), | |
| 96 instance(instance) { | |
| 97 } | 84 } |
| 98 | 85 |
| 99 // Info used to create the class. | 86 // Info used to create the class. |
| 100 ClassInfo info; | 87 ClassInfo info; |
| 101 | 88 |
| 102 // The name given to the window class. | 89 // The name given to the window class. |
| 103 std::wstring name; | 90 std::wstring name; |
| 104 | |
| 105 // The ATOM returned from registering the window class. | |
| 106 ATOM atom; | |
| 107 | |
| 108 // The handle of the module containing the window procedure. | |
| 109 HMODULE instance; | |
| 110 }; | 91 }; |
| 111 | 92 |
| 112 ClassRegistrar() : registered_count_(0) { } | 93 ClassRegistrar() : registered_count_(0) { } |
| 113 friend struct DefaultSingletonTraits<ClassRegistrar>; | 94 friend struct DefaultSingletonTraits<ClassRegistrar>; |
| 114 | 95 |
| 115 typedef std::list<RegisteredClass> RegisteredClasses; | 96 typedef std::list<RegisteredClass> RegisteredClasses; |
| 116 RegisteredClasses registered_classes_; | 97 RegisteredClasses registered_classes_; |
| 117 | 98 |
| 118 // Counter of how many classes have been registered so far. | 99 // Counter of how many classes have been registered so far. |
| 119 int registered_count_; | 100 int registered_count_; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 NULL, | 243 NULL, |
| 263 reinterpret_cast<HBRUSH>(background + 1), | 244 reinterpret_cast<HBRUSH>(background + 1), |
| 264 NULL, | 245 NULL, |
| 265 icon, | 246 icon, |
| 266 icon, | 247 icon, |
| 267 &window_class); | 248 &window_class); |
| 268 HMODULE instance = window_class.hInstance; | 249 HMODULE instance = window_class.hInstance; |
| 269 ATOM atom = RegisterClassEx(&window_class); | 250 ATOM atom = RegisterClassEx(&window_class); |
| 270 CHECK(atom) << GetLastError(); | 251 CHECK(atom) << GetLastError(); |
| 271 | 252 |
| 272 ClassRegistrar::GetInstance()->RegisterClass( | 253 ClassRegistrar::GetInstance()->RegisterClass(class_info, name); |
| 273 class_info, name, atom, instance); | |
| 274 | 254 |
| 275 return name; | 255 return name; |
| 276 } | 256 } |
| 277 | 257 |
| 278 } // namespace ui | 258 } // namespace ui |
| OLD | NEW |