OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/win_util.h" | 5 #include "base/win_util.h" |
6 | 6 |
7 #include <map> | |
8 #include <sddl.h> | 7 #include <sddl.h> |
9 | 8 |
10 #include "base/logging.h" | 9 #include "base/logging.h" |
11 #include "base/registry.h" | 10 #include "base/registry.h" |
12 #include "base/scoped_handle.h" | 11 #include "base/scoped_handle.h" |
13 #include "base/singleton.h" | |
14 #include "base/string_util.h" | 12 #include "base/string_util.h" |
15 #include "base/tracked.h" | |
16 | 13 |
17 namespace win_util { | 14 namespace win_util { |
18 | 15 |
19 #define SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(struct_name, member) \ | 16 #define SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(struct_name, member) \ |
20 offsetof(struct_name, member) + \ | 17 offsetof(struct_name, member) + \ |
21 (sizeof static_cast<struct_name*>(NULL)->member) | 18 (sizeof static_cast<struct_name*>(NULL)->member) |
22 #define NONCLIENTMETRICS_SIZE_PRE_VISTA \ | 19 #define NONCLIENTMETRICS_SIZE_PRE_VISTA \ |
23 SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(NONCLIENTMETRICS, lfMessageFont) | 20 SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(NONCLIENTMETRICS, lfMessageFont) |
24 | 21 |
25 void GetNonClientMetrics(NONCLIENTMETRICS* metrics) { | 22 void GetNonClientMetrics(NONCLIENTMETRICS* metrics) { |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 // The formating failed. simply convert the message value into a string. | 354 // The formating failed. simply convert the message value into a string. |
358 SStringPrintf(&formatted_string, L"message number %d", messageid); | 355 SStringPrintf(&formatted_string, L"message number %d", messageid); |
359 } | 356 } |
360 return formatted_string; | 357 return formatted_string; |
361 } | 358 } |
362 | 359 |
363 std::wstring FormatLastWin32Error() { | 360 std::wstring FormatLastWin32Error() { |
364 return FormatMessage(GetLastError()); | 361 return FormatMessage(GetLastError()); |
365 } | 362 } |
366 | 363 |
367 typedef std::map<HWND, tracked_objects::Location> HWNDInfoMap; | |
368 struct HWNDBirthMapTrait : public DefaultSingletonTraits<HWNDInfoMap> { | |
369 }; | |
370 struct HWNDDeathMapTrait : public DefaultSingletonTraits<HWNDInfoMap> { | |
371 }; | |
372 | |
373 void NotifyHWNDCreation(const tracked_objects::Location& from_here, HWND hwnd) { | |
374 HWNDInfoMap* birth_map = Singleton<HWNDInfoMap, HWNDBirthMapTrait>::get(); | |
375 HWNDInfoMap::iterator birth_iter = birth_map->find(hwnd); | |
376 if (birth_iter != birth_map->end()) { | |
377 birth_map->erase(birth_iter); | |
378 | |
379 // We have already seen this HWND, was it destroyed? | |
380 HWNDInfoMap* death_map = Singleton<HWNDInfoMap, HWNDDeathMapTrait>::get(); | |
381 HWNDInfoMap::iterator death_iter = death_map->find(hwnd); | |
382 if (death_iter == death_map->end()) { | |
383 // We did not get a destruction notification. The code is probably not | |
384 // calling NotifyHWNDDestruction for that HWND. | |
385 NOTREACHED() << "Creation of HWND reported for already tracked HWND. The " | |
386 "HWND destruction is probably not tracked properly. " | |
387 "Fix it!"; | |
388 } else { | |
389 death_map->erase(death_iter); | |
390 } | |
391 } | |
392 birth_map->insert(std::pair<HWND, tracked_objects::Location>(hwnd, | |
393 from_here)); | |
394 } | |
395 | |
396 void NotifyHWNDDestruction(const tracked_objects::Location& from_here, | |
397 HWND hwnd) { | |
398 HWNDInfoMap* death_map = Singleton<HWNDInfoMap, HWNDDeathMapTrait>::get(); | |
399 HWNDInfoMap::iterator death_iter = death_map->find(hwnd); | |
400 | |
401 HWNDInfoMap* birth_map = Singleton<HWNDInfoMap, HWNDBirthMapTrait>::get(); | |
402 HWNDInfoMap::iterator birth_iter = birth_map->find(hwnd); | |
403 | |
404 if (death_iter != death_map->end()) { | |
405 std::string allocation, first_delete, second_delete; | |
406 if (birth_iter != birth_map->end()) | |
407 birth_iter->second.Write(true, true, &allocation); | |
408 death_iter->second.Write(true, true, &first_delete); | |
409 from_here.Write(true, true, &second_delete); | |
410 NOTREACHED() << "Double delete of an HWND. Please file a bug with info on " | |
411 "how you got that assertion and the following information:\n" | |
412 "Double delete of HWND 0x" << hwnd << "\n" << | |
413 "Allocated at " << allocation << "\n" << | |
414 "Deleted first at " << first_delete << "\n" << | |
415 "Deleted again at " << second_delete; | |
416 death_map->erase(death_iter); | |
417 } | |
418 | |
419 if (birth_iter == birth_map->end()) { | |
420 NOTREACHED() << "Destruction of HWND reported for unknown HWND. The HWND " | |
421 "construction is probably not tracked properly. Fix it!"; | |
422 } | |
423 death_map->insert(std::pair<HWND, tracked_objects::Location>(hwnd, | |
424 from_here)); | |
425 } | |
426 | |
427 } // namespace win_util | 364 } // namespace win_util |
428 | 365 |
429 #ifdef _MSC_VER | 366 #ifdef _MSC_VER |
430 // | 367 // |
431 // If the ASSERT below fails, please install Visual Studio 2005 Service Pack 1. | 368 // If the ASSERT below fails, please install Visual Studio 2005 Service Pack 1. |
432 // | 369 // |
433 extern char VisualStudio2005ServicePack1Detection[10]; | 370 extern char VisualStudio2005ServicePack1Detection[10]; |
434 COMPILE_ASSERT(sizeof(&VisualStudio2005ServicePack1Detection) == sizeof(void*), | 371 COMPILE_ASSERT(sizeof(&VisualStudio2005ServicePack1Detection) == sizeof(void*), |
435 VS2005SP1Detect); | 372 VS2005SP1Detect); |
436 // | 373 // |
437 // Chrome requires at least Service Pack 1 for Visual Studio 2005. | 374 // Chrome requires at least Service Pack 1 for Visual Studio 2005. |
438 // | 375 // |
439 #endif // _MSC_VER | 376 #endif // _MSC_VER |
440 | 377 |
441 #ifndef COPY_FILE_COPY_SYMLINK | 378 #ifndef COPY_FILE_COPY_SYMLINK |
442 #error You must install the Windows 2008 or Vista Software Development Kit and \ | 379 #error You must install the Windows 2008 or Vista Software Development Kit and \ |
443 set it as your default include path to build this library. You can grab it by \ | 380 set it as your default include path to build this library. You can grab it by \ |
444 searching for "download windows sdk 2008" in your favorite web search engine. \ | 381 searching for "download windows sdk 2008" in your favorite web search engine. \ |
445 Also make sure you register the SDK with Visual Studio, by selecting \ | 382 Also make sure you register the SDK with Visual Studio, by selecting \ |
446 "Integrate Windows SDK with Visual Studio 2005" from the Windows SDK \ | 383 "Integrate Windows SDK with Visual Studio 2005" from the Windows SDK \ |
447 menu (see Start - All Programs - Microsoft Windows SDK - \ | 384 menu (see Start - All Programs - Microsoft Windows SDK - \ |
448 Visual Studio Registration). | 385 Visual Studio Registration). |
449 #endif | 386 #endif |
OLD | NEW |