diff options
author | brettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-31 17:18:50 +0000 |
---|---|---|
committer | brettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-31 17:18:50 +0000 |
commit | a8e2058011129cbef38bf89834ee01715556b392 (patch) | |
tree | 00cb892894c52145a66d23048acf2a81f0eed61d /base/win | |
parent | a1fa87c8042ed2a3f9edd74ad2f313c84b1e407a (diff) | |
download | chromium_src-a8e2058011129cbef38bf89834ee01715556b392.zip chromium_src-a8e2058011129cbef38bf89834ee01715556b392.tar.gz chromium_src-a8e2058011129cbef38bf89834ee01715556b392.tar.bz2 |
Move base/win_util to the base/win directory and use the base::win namespace.
Fix up includes, many files including base/win_util don't actually need it.
TEST=it compiles
BUG=none
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70341 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/win')
-rw-r--r-- | base/win/win_util.cc | 162 | ||||
-rw-r--r-- | base/win/win_util.h | 78 | ||||
-rw-r--r-- | base/win/win_util_unittest.cc | 58 |
3 files changed, 298 insertions, 0 deletions
diff --git a/base/win/win_util.cc b/base/win/win_util.cc new file mode 100644 index 0000000..cce3d9c --- /dev/null +++ b/base/win/win_util.cc @@ -0,0 +1,162 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/win/win_util.h" + +#include <aclapi.h> +#include <shobjidl.h> // Must be before propkey. +#include <propkey.h> +#include <propvarutil.h> +#include <sddl.h> +#include <shlobj.h> + +#include "base/logging.h" +#include "base/win/registry.h" +#include "base/scoped_handle.h" +#include "base/scoped_ptr.h" +#include "base/string_util.h" +#include "base/stringprintf.h" +#include "base/thread_restrictions.h" +#include "base/win/windows_version.h" + +namespace base { +namespace win { + +#define SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(struct_name, member) \ + offsetof(struct_name, member) + \ + (sizeof static_cast<struct_name*>(NULL)->member) +#define NONCLIENTMETRICS_SIZE_PRE_VISTA \ + SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(NONCLIENTMETRICS, lfMessageFont) + +void GetNonClientMetrics(NONCLIENTMETRICS* metrics) { + DCHECK(metrics); + + static const UINT SIZEOF_NONCLIENTMETRICS = + (base::win::GetVersion() >= base::win::VERSION_VISTA) ? + sizeof(NONCLIENTMETRICS) : NONCLIENTMETRICS_SIZE_PRE_VISTA; + metrics->cbSize = SIZEOF_NONCLIENTMETRICS; + const bool success = !!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, + SIZEOF_NONCLIENTMETRICS, metrics, + 0); + DCHECK(success); +} + +bool GetUserSidString(std::wstring* user_sid) { + // Get the current token. + HANDLE token = NULL; + if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token)) + return false; + ScopedHandle token_scoped(token); + + DWORD size = sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE; + scoped_array<BYTE> user_bytes(new BYTE[size]); + TOKEN_USER* user = reinterpret_cast<TOKEN_USER*>(user_bytes.get()); + + if (!::GetTokenInformation(token, TokenUser, user, size, &size)) + return false; + + if (!user->User.Sid) + return false; + + // Convert the data to a string. + wchar_t* sid_string; + if (!::ConvertSidToStringSid(user->User.Sid, &sid_string)) + return false; + + *user_sid = sid_string; + + ::LocalFree(sid_string); + + return true; +} + +bool IsShiftPressed() { + return (::GetKeyState(VK_SHIFT) & 0x8000) == 0x8000; +} + +bool IsCtrlPressed() { + return (::GetKeyState(VK_CONTROL) & 0x8000) == 0x8000; +} + +bool IsAltPressed() { + return (::GetKeyState(VK_MENU) & 0x8000) == 0x8000; +} + +bool UserAccountControlIsEnabled() { + // This can be slow if Windows ends up going to disk. Should watch this key + // for changes and only read it once, preferably on the file thread. + // http://code.google.com/p/chromium/issues/detail?id=61644 + base::ThreadRestrictions::ScopedAllowIO allow_io; + + base::win::RegKey key(HKEY_LOCAL_MACHINE, + L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", + KEY_READ); + DWORD uac_enabled; + if (!key.ReadValueDW(L"EnableLUA", &uac_enabled)) + return true; + // Users can set the EnableLUA value to something arbitrary, like 2, which + // Vista will treat as UAC enabled, so we make sure it is not set to 0. + return (uac_enabled != 0); +} + +bool SetAppIdForPropertyStore(IPropertyStore* property_store, + const wchar_t* app_id) { + DCHECK(property_store); + + // App id should be less than 128 chars and contain no space. And recommended + // format is CompanyName.ProductName[.SubProduct.ProductNumber]. + // See http://msdn.microsoft.com/en-us/library/dd378459%28VS.85%29.aspx + DCHECK(lstrlen(app_id) < 128 && wcschr(app_id, L' ') == NULL); + + PROPVARIANT property_value; + if (FAILED(InitPropVariantFromString(app_id, &property_value))) + return false; + + HRESULT result = property_store->SetValue(PKEY_AppUserModel_ID, + property_value); + if (S_OK == result) + result = property_store->Commit(); + + PropVariantClear(&property_value); + return SUCCEEDED(result); +} + +static const char16 kAutoRunKeyPath[] = + L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"; + +bool AddCommandToAutoRun(HKEY root_key, const string16& name, + const string16& command) { + base::win::RegKey autorun_key(root_key, kAutoRunKeyPath, KEY_SET_VALUE); + return autorun_key.WriteValue(name.c_str(), command.c_str()); +} + +bool RemoveCommandFromAutoRun(HKEY root_key, const string16& name) { + base::win::RegKey autorun_key(root_key, kAutoRunKeyPath, KEY_SET_VALUE); + return autorun_key.DeleteValue(name.c_str()); +} + +} // namespace win +} // namespace base + +#ifdef _MSC_VER +// +// If the ASSERT below fails, please install Visual Studio 2005 Service Pack 1. +// +extern char VisualStudio2005ServicePack1Detection[10]; +COMPILE_ASSERT(sizeof(&VisualStudio2005ServicePack1Detection) == sizeof(void*), + VS2005SP1Detect); +// +// Chrome requires at least Service Pack 1 for Visual Studio 2005. +// +#endif // _MSC_VER + +#ifndef COPY_FILE_COPY_SYMLINK +#error You must install the Windows 2008 or Vista Software Development Kit and \ +set it as your default include path to build this library. You can grab it by \ +searching for "download windows sdk 2008" in your favorite web search engine. \ +Also make sure you register the SDK with Visual Studio, by selecting \ +"Integrate Windows SDK with Visual Studio 2005" from the Windows SDK \ +menu (see Start - All Programs - Microsoft Windows SDK - \ +Visual Studio Registration). +#endif diff --git a/base/win/win_util.h b/base/win/win_util.h new file mode 100644 index 0000000..187e42d --- /dev/null +++ b/base/win/win_util.h @@ -0,0 +1,78 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// ============================================================================= +// PLEASE READ +// +// In general, you should not be adding stuff to this file. +// +// - If your thing is only used in one place, just put it in a reasonable +// location in or near that one place. It's nice you want people to be able +// to re-use your function, but realistically, if it hasn't been necessary +// before after so many years of development, it's probably not going to be +// used in other places in the future unless you know of them now. +// +// - If your thing is used by multiple callers and is UI-related, it should +// probably be in app/win/ instead. Try to put it in the most specific file +// possible (avoiding the *_util files when practical). +// +// ============================================================================= + +#ifndef BASE_WIN_WIN_UTIL_H_ +#define BASE_WIN_WIN_UTIL_H_ +#pragma once + +#include <windows.h> + +#include <string> + +#include "base/string16.h" + +struct IPropertyStore; +struct _tagpropertykey; +typedef _tagpropertykey PROPERTYKEY; + +namespace base { +namespace win { + +void GetNonClientMetrics(NONCLIENTMETRICS* metrics); + +// Returns the string representing the current user sid. +bool GetUserSidString(std::wstring* user_sid); + +// Returns true if the shift key is currently pressed. +bool IsShiftPressed(); + +// Returns true if the ctrl key is currently pressed. +bool IsCtrlPressed(); + +// Returns true if the alt key is currently pressed. +bool IsAltPressed(); + +// Returns false if user account control (UAC) has been disabled with the +// EnableLUA registry flag. Returns true if user account control is enabled. +// NOTE: The EnableLUA registry flag, which is ignored on Windows XP +// machines, might still exist and be set to 0 (UAC disabled), in which case +// this function will return false. You should therefore check this flag only +// if the OS is Vista. +bool UserAccountControlIsEnabled(); + +// Sets the application id in given IPropertyStore. The function is intended +// for tagging application/chromium shortcut, browser window and jump list for +// Win7. +bool SetAppIdForPropertyStore(IPropertyStore* property_store, + const wchar_t* app_id); + +// Adds the specified |command| using the specified |name| to the AutoRun key. +// |root_key| could be HKCU or HKLM or the root of any user hive. +bool AddCommandToAutoRun(HKEY root_key, const string16& name, + const string16& command); +// Removes the command specified by |name| from the AutoRun key. |root_key| +// could be HKCU or HKLM or the root of any user hive. +bool RemoveCommandFromAutoRun(HKEY root_key, const string16& name); + +} // namespace win +} // namespace base + +#endif // BASE_WIN_WIN_UTIL_H_ diff --git a/base/win/win_util_unittest.cc b/base/win/win_util_unittest.cc new file mode 100644 index 0000000..b79ed56 --- /dev/null +++ b/base/win/win_util_unittest.cc @@ -0,0 +1,58 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <windows.h> + +#include "base/basictypes.h" +#include "base/string_util.h" +#include "base/win/win_util.h" +#include "base/win/windows_version.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace base { +namespace win { + +namespace { + +// Saves the current thread's locale ID when initialized, and restores it when +// the instance is going out of scope. +class ThreadLocaleSaver { + public: + ThreadLocaleSaver() : original_locale_id_(GetThreadLocale()) {} + ~ThreadLocaleSaver() { SetThreadLocale(original_locale_id_); } + + private: + LCID original_locale_id_; + + DISALLOW_COPY_AND_ASSIGN(ThreadLocaleSaver); +}; + +} // namespace + +// 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) { + if (GetVersion() >= base::win::VERSION_VISTA) { + UserAccountControlIsEnabled(); + } else { + EXPECT_TRUE(UserAccountControlIsEnabled()); + } +} + +TEST(BaseWinUtilTest, TestGetUserSidString) { + std::wstring user_sid; + EXPECT_TRUE(GetUserSidString(&user_sid)); + EXPECT_TRUE(!user_sid.empty()); +} + +TEST(BaseWinUtilTest, TestGetNonClientMetrics) { + NONCLIENTMETRICS metrics = {0}; + GetNonClientMetrics(&metrics); + EXPECT_TRUE(metrics.cbSize > 0); + EXPECT_TRUE(metrics.iScrollWidth > 0); + EXPECT_TRUE(metrics.iScrollHeight > 0); +} + +} // namespace win +} // namespace base |