diff options
-rw-r--r-- | base/file_util_win.cc | 25 | ||||
-rw-r--r-- | base/win_util.cc | 23 | ||||
-rw-r--r-- | base/win_util.h | 7 | ||||
-rw-r--r-- | base/win_util_unittest.cc | 85 | ||||
-rw-r--r-- | chrome/common/win_util.cc | 24 | ||||
-rw-r--r-- | chrome/common/win_util.h | 7 | ||||
-rw-r--r-- | chrome/common/win_util_unittest.cc | 85 |
7 files changed, 130 insertions, 126 deletions
diff --git a/base/file_util_win.cc b/base/file_util_win.cc index f2822d1..168e26e 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -477,18 +477,29 @@ int WriteFile(const std::wstring& filename, const char* data, int size) { CREATE_ALWAYS, 0, NULL)); - if (file == INVALID_HANDLE_VALUE) + if (file == INVALID_HANDLE_VALUE) { + LOG(WARNING) << "CreateFile failed for path " << filename << + " error code=" << GetLastError() << + " error text=" << win_util::FormatLastWin32Error(); return -1; + } - int ret_value; DWORD written; - if (::WriteFile(file, data, size, &written, NULL) && written == size) { - ret_value = static_cast<int>(written); + BOOL result = ::WriteFile(file, data, size, &written, NULL); + if (result && written == size) + return static_cast<int>(written); + + if (!result) { + // WriteFile failed. + LOG(WARNING) << "writing file " << filename << + " failed, error code=" << GetLastError() << + " description=" << win_util::FormatLastWin32Error(); } else { - ret_value = -1; + // Didn't write all the bytes. + LOG(WARNING) << "wrote" << written << " bytes to " << filename << + " expected " << size; } - - return ret_value; + return -1; } bool RenameFileAndResetSecurityDescriptor( diff --git a/base/win_util.cc b/base/win_util.cc index b03e06d..38a4133 100644 --- a/base/win_util.cc +++ b/base/win_util.cc @@ -331,6 +331,28 @@ bool UserAccountControlIsEnabled() { return (uac_enabled == 1); } +std::wstring FormatMessage(unsigned messageid) { + wchar_t* string_buffer = NULL; + unsigned string_length = ::FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, NULL, messageid, 0, + reinterpret_cast<wchar_t *>(&string_buffer), 0, NULL); + + std::wstring formatted_string; + if (string_buffer) { + formatted_string = string_buffer; + LocalFree(reinterpret_cast<HLOCAL>(string_buffer)); + } else { + // The formating failed. simply convert the message value into a string. + SStringPrintf(&formatted_string, L"message number %d", messageid); + } + return formatted_string; +} + +std::wstring FormatLastWin32Error() { + return FormatMessage(GetLastError()); +} + } // namespace win_util #ifdef _MSC_VER @@ -354,4 +376,3 @@ Also make sure you register the SDK with Visual Studio, by selecting \ menu (see Start - All Programs - Microsoft Windows SDK - \ Visual Studio Registration). #endif - diff --git a/base/win_util.h b/base/win_util.h index 4d1891f..2165e62 100644 --- a/base/win_util.h +++ b/base/win_util.h @@ -92,6 +92,13 @@ std::wstring GetClassName(HWND window); // if the OS is Vista. bool UserAccountControlIsEnabled(); +// Use the Win32 API FormatMessage() function to generate a string, using +// Windows's default Message Compiled resources; ignoring the inserts. +std::wstring FormatMessage(unsigned messageid); + +// Uses the last Win32 error to generate a human readable message string. +std::wstring FormatLastWin32Error(); + } // namespace win_util #endif // BASE_WIN_UTIL_H__ diff --git a/base/win_util_unittest.cc b/base/win_util_unittest.cc index c7b4e7b..dddb6f0 100644 --- a/base/win_util_unittest.cc +++ b/base/win_util_unittest.cc @@ -5,11 +5,48 @@ #include <windows.h> #include "testing/gtest/include/gtest/gtest.h" +#include "base/registry.h" +#include "base/string_util.h" #include "base/win_util.h" +class BaseWinUtilTest: public testing::Test { + protected: + // Retrieve the OS primary language + static unsigned GetSystemLanguage() { + std::wstring language; + + typedef BOOL (WINAPI *fnGetThreadPreferredUILanguages)( + DWORD dwFlags, + PULONG pulNumLanguages, + PWSTR pwszLanguagesBuffer, + PULONG pcchLanguagesBuffer); + fnGetThreadPreferredUILanguages pGetThreadPreferredUILanguages = NULL; + pGetThreadPreferredUILanguages = + reinterpret_cast<fnGetThreadPreferredUILanguages>( + GetProcAddress(GetModuleHandle(L"kernel32.dll"), + "GetThreadPreferredUILanguages")); + if (pGetThreadPreferredUILanguages) { + // Vista, MUI-aware. + ULONG number = 0; + wchar_t buffer[256] = {0}; + ULONG buffer_size = sizeof(buffer); + EXPECT_TRUE(pGetThreadPreferredUILanguages(MUI_LANGUAGE_ID, &number, + buffer, &buffer_size)); + language = buffer; + } else { + // XP + RegKey language_key(HKEY_LOCAL_MACHINE, + L"SYSTEM\\CurrentControlSet\\Control\\Nls\\Language"); + language_key.ReadValue(L"InstallLanguage", &language); + } + wchar_t * unused_endptr; + return PRIMARYLANGID(wcstol(language.c_str(), &unused_endptr, 16)); + } +}; + // The test is somewhat silly, because the Vista bots some have UAC enabled // and some have it disabled. At least we check that it does not crash. -TEST(BaseWinUtilTest, TestIsUACEnabled) { +TEST_F(BaseWinUtilTest, TestIsUACEnabled) { if (win_util::GetWinVersion() == win_util::WINVERSION_VISTA) { win_util::UserAccountControlIsEnabled(); } else { @@ -17,13 +54,13 @@ TEST(BaseWinUtilTest, TestIsUACEnabled) { } } -TEST(BaseWinUtilTest, TestGetUserSidString) { +TEST_F(BaseWinUtilTest, TestGetUserSidString) { std::wstring user_sid; EXPECT_TRUE(win_util::GetUserSidString(&user_sid)); EXPECT_TRUE(!user_sid.empty()); } -TEST(BaseWinUtilTest, TestGetNonClientMetrics) { +TEST_F(BaseWinUtilTest, TestGetNonClientMetrics) { NONCLIENTMETRICS metrics = {0}; win_util::GetNonClientMetrics(&metrics); EXPECT_TRUE(metrics.cbSize > 0); @@ -31,3 +68,45 @@ TEST(BaseWinUtilTest, TestGetNonClientMetrics) { EXPECT_TRUE(metrics.iScrollHeight > 0); } +TEST_F(BaseWinUtilTest, FormatMessage) { + unsigned language = GetSystemLanguage(); + ASSERT_TRUE(language); + + const int kAccessDeniedErrorCode = 5; + SetLastError(kAccessDeniedErrorCode); + ASSERT_EQ(GetLastError(), kAccessDeniedErrorCode); + std::wstring value; + + if (language == LANG_ENGLISH) { + // This test would fail on non-English system. + TrimWhitespace(win_util::FormatLastWin32Error(), TRIM_ALL, &value); + EXPECT_EQ(std::wstring(L"Access is denied."), value); + } else if (language == LANG_FRENCH) { + // This test would fail on non-French system. + TrimWhitespace(win_util::FormatLastWin32Error(), TRIM_ALL, &value); + EXPECT_EQ(std::wstring(L"Acc\u00e8s refus\u00e9."), value); + } else { + EXPECT_TRUE(0) << "Please implement the test for your OS language."; + } + + // Manually call the OS function + wchar_t * string_buffer = NULL; + unsigned string_length = + ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, NULL, + kAccessDeniedErrorCode, 0, + reinterpret_cast<wchar_t *>(&string_buffer), 0, NULL); + + // Verify the call succeeded + ASSERT_TRUE(string_length); + ASSERT_TRUE(string_buffer); + + // Verify the string is the same by different calls + EXPECT_EQ(win_util::FormatLastWin32Error(), std::wstring(string_buffer)); + EXPECT_EQ(win_util::FormatMessage(kAccessDeniedErrorCode), + std::wstring(string_buffer)); + + // Done with the buffer allocated by ::FormatMessage() + LocalFree(string_buffer); +} diff --git a/chrome/common/win_util.cc b/chrome/common/win_util.cc index f664cfb..b0ef230 100644 --- a/chrome/common/win_util.cc +++ b/chrome/common/win_util.cc @@ -155,30 +155,6 @@ bool ShouldUseVistaFrame() { return !!f; } -std::wstring FormatMessage(unsigned messageid) { - wchar_t * string_buffer = NULL; - unsigned string_length = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, NULL, - messageid, 0, - reinterpret_cast<wchar_t *>(&string_buffer), - 0, NULL); - - std::wstring formatted_string; - if (string_buffer) { - formatted_string = string_buffer; - LocalFree(reinterpret_cast<HLOCAL>(string_buffer)); - } else { - // The formating failed. simply convert the message value into a string. - SStringPrintf(&formatted_string, L"message number %d", messageid); - } - return formatted_string; -} - -std::wstring FormatLastWin32Error() { - return FormatMessage(GetLastError()); -} - void ShowItemInFolder(const std::wstring& full_path) { std::wstring dir = file_util::GetDirectoryFromPath(full_path); if (dir == L"" || !file_util::PathExists(full_path)) diff --git a/chrome/common/win_util.h b/chrome/common/win_util.h index f6eff24..6c88a12 100644 --- a/chrome/common/win_util.h +++ b/chrome/common/win_util.h @@ -103,13 +103,6 @@ bool IsDrag(const POINT& origin, const POINT& current); // Returns true if we are on Windows Vista and composition is enabled bool ShouldUseVistaFrame(); -// Use the Win32 API FormatMessage() function to generate a string, using -// Windows's default Message Compiled resources; ignoring the inserts. -std::wstring FormatMessage(unsigned messageid); - -// Uses the last Win32 error to generate a human readable message string. -std::wstring FormatLastWin32Error(); - // Open a Windows explorer window with the specified file highlighted. void ShowItemInFolder(const std::wstring& full_path); diff --git a/chrome/common/win_util_unittest.cc b/chrome/common/win_util_unittest.cc index 8df32e1..016e391 100644 --- a/chrome/common/win_util_unittest.cc +++ b/chrome/common/win_util_unittest.cc @@ -2,91 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "base/registry.h" -#include "base/string_util.h" #include "chrome/common/win_util.h" #include "testing/gtest/include/gtest/gtest.h" -class WinUtilTest: public testing::Test { - protected: - // Retrieve the OS primary language - static unsigned GetSystemLanguage() { - std::wstring language; - - typedef BOOL (WINAPI *fnGetThreadPreferredUILanguages)( - DWORD dwFlags, - PULONG pulNumLanguages, - PWSTR pwszLanguagesBuffer, - PULONG pcchLanguagesBuffer); - fnGetThreadPreferredUILanguages pGetThreadPreferredUILanguages = NULL; - pGetThreadPreferredUILanguages = - reinterpret_cast<fnGetThreadPreferredUILanguages>( - GetProcAddress(GetModuleHandle(L"kernel32.dll"), - "GetThreadPreferredUILanguages")); - if (pGetThreadPreferredUILanguages) { - // Vista, MUI-aware. - ULONG number = 0; - wchar_t buffer[256] = {0}; - ULONG buffer_size = sizeof(buffer); - EXPECT_TRUE(pGetThreadPreferredUILanguages(MUI_LANGUAGE_ID, &number, - buffer, &buffer_size)); - language = buffer; - } else { - // XP - RegKey language_key(HKEY_LOCAL_MACHINE, - L"SYSTEM\\CurrentControlSet\\Control\\Nls\\Language"); - language_key.ReadValue(L"InstallLanguage", &language); - } - wchar_t * unused_endptr; - return PRIMARYLANGID(wcstol(language.c_str(), &unused_endptr, 16)); - } -}; - - -TEST_F(WinUtilTest, FormatMessage) { - unsigned language = GetSystemLanguage(); - ASSERT_TRUE(language); - - const int kAccessDeniedErrorCode = 5; - SetLastError(kAccessDeniedErrorCode); - ASSERT_EQ(GetLastError(), kAccessDeniedErrorCode); - std::wstring value; - - if (language == LANG_ENGLISH) { - // This test would fail on non-English system. - TrimWhitespace(win_util::FormatLastWin32Error(), TRIM_ALL, &value); - EXPECT_EQ(std::wstring(L"Access is denied."), value); - } else if (language == LANG_FRENCH) { - // This test would fail on non-French system. - TrimWhitespace(win_util::FormatLastWin32Error(), TRIM_ALL, &value); - EXPECT_EQ(std::wstring(L"Acc\u00e8s refus\u00e9."), value); - } else { - EXPECT_TRUE(0) << "Please implement the test for your OS language."; - } - - // Manually call the OS function - wchar_t * string_buffer = NULL; - unsigned string_length = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, NULL, - kAccessDeniedErrorCode, 0, - reinterpret_cast<wchar_t *>(&string_buffer), - 0, NULL); - - // Verify the call succeeded - ASSERT_TRUE(string_length); - ASSERT_TRUE(string_buffer); - - // Verify the string is the same by different calls - EXPECT_EQ(win_util::FormatLastWin32Error(), std::wstring(string_buffer)); - EXPECT_EQ(win_util::FormatMessage(kAccessDeniedErrorCode), - std::wstring(string_buffer)); - - // Done with the buffer allocated by ::FormatMessage() - LocalFree(string_buffer); -} - -TEST_F(WinUtilTest, EnsureRectIsVisibleInRect) { +TEST(WinUtilTest, EnsureRectIsVisibleInRect) { gfx::Rect parent_rect(0, 0, 500, 400); { @@ -131,5 +50,3 @@ TEST_F(WinUtilTest, EnsureRectIsVisibleInRect) { EXPECT_EQ(gfx::Rect(20, 20, 100, 380), child_rect); } } - - |