0

Cleanup: Get rid of base::ASCIIToWide().

BUG=23581

Review URL: https://codereview.chromium.org/768973003

Cr-Commit-Position: refs/heads/master@{#307333}
This commit is contained in:
thestig
2014-12-08 13:00:22 -08:00
committed by Commit bot
parent daaa100e8c
commit 93570e26b8
7 changed files with 50 additions and 63 deletions

@ -494,10 +494,10 @@ TEST(StringUtilTest, ConvertASCII) {
const char chars_with_nul[] = "test\0string";
const int length_with_nul = arraysize(chars_with_nul) - 1;
std::string string_with_nul(chars_with_nul, length_with_nul);
std::wstring wide_with_nul = ASCIIToWide(string_with_nul);
EXPECT_EQ(static_cast<std::wstring::size_type>(length_with_nul),
wide_with_nul.length());
std::string narrow_with_nul = UTF16ToASCII(WideToUTF16(wide_with_nul));
base::string16 string16_with_nul = ASCIIToUTF16(string_with_nul);
EXPECT_EQ(static_cast<base::string16::size_type>(length_with_nul),
string16_with_nul.length());
std::string narrow_with_nul = UTF16ToASCII(string16_with_nul);
EXPECT_EQ(static_cast<std::string::size_type>(length_with_nul),
narrow_with_nul.length());
EXPECT_EQ(0, string_with_nul.compare(narrow_with_nul));

@ -209,11 +209,6 @@ std::string UTF16ToUTF8(const string16& utf16) {
#endif
std::wstring ASCIIToWide(const StringPiece& ascii) {
DCHECK(IsStringASCII(ascii)) << ascii;
return std::wstring(ascii.begin(), ascii.end());
}
string16 ASCIIToUTF16(const StringPiece& ascii) {
DCHECK(IsStringASCII(ascii)) << ascii;
return string16(ascii.begin(), ascii.end());

@ -39,9 +39,8 @@ BASE_EXPORT bool UTF16ToUTF8(const char16* src, size_t src_len,
std::string* output);
BASE_EXPORT std::string UTF16ToUTF8(const string16& utf16);
// These convert an ASCII string, typically a hardcoded constant, to a
// UTF16/Wide string.
BASE_EXPORT std::wstring ASCIIToWide(const StringPiece& ascii);
// This converts an ASCII string, typically a hardcoded constant, to a UTF16
// string.
BASE_EXPORT string16 ASCIIToUTF16(const StringPiece& ascii);
// Converts to 7-bit ASCII by truncating. The result must be known to be ASCII

@ -21,10 +21,10 @@
#include "rlz/win/lib/registry_util.h"
#include "rlz/win/lib/rlz_value_store_registry.h"
const wchar_t kDccValueName[] = L"DCC";
namespace {
const wchar_t kDccValueName[] = L"DCC";
// Current DCC can only uses [a-zA-Z0-9_-!@$*();.<>,:]
// We will be more liberal and allow some additional chars, but not url meta
// chars.
@ -117,7 +117,7 @@ bool GetResponseValue(const std::string& response_line,
return true;
}
} // namespace anonymous
} // namespace
namespace rlz_lib {
@ -150,7 +150,7 @@ bool MachineDealCode::Set(const char* dcc) {
// Write the DCC to HKLM. Note that we need to include the null character
// when writing the string.
if (!RegKeyWriteValue(hklm_key, kDccValueName, normalized_dcc)) {
if (!RegKeyWriteValue(&hklm_key, kDccValueName, normalized_dcc)) {
ASSERT_STRING("MachineDealCode::Set: Could not write the DCC value");
return false;
}

@ -16,7 +16,7 @@
namespace rlz_lib {
bool RegKeyReadValue(base::win::RegKey& key, const wchar_t* name,
bool RegKeyReadValue(const base::win::RegKey& key, const wchar_t* name,
char* value, size_t* value_size) {
value[0] = 0;
@ -36,10 +36,10 @@ bool RegKeyReadValue(base::win::RegKey& key, const wchar_t* name,
return true;
}
bool RegKeyWriteValue(base::win::RegKey& key, const wchar_t* name,
bool RegKeyWriteValue(base::win::RegKey* key, const wchar_t* name,
const char* value) {
std::wstring value_string(base::ASCIIToWide(value));
return key.WriteValue(name, value_string.c_str()) == ERROR_SUCCESS;
base::string16 value_string(base::ASCIIToUTF16(value));
return key->WriteValue(name, value_string.c_str()) == ERROR_SUCCESS;
}
bool HasUserKeyAccess(bool write_access) {

@ -13,12 +13,12 @@ class RegKey;
namespace rlz_lib {
bool RegKeyReadValue(base::win::RegKey& key,
bool RegKeyReadValue(const base::win::RegKey& key,
const wchar_t* name,
char* value,
size_t* value_size);
bool RegKeyWriteValue(base::win::RegKey& key,
bool RegKeyWriteValue(base::win::RegKey* key,
const wchar_t* name,
const char* value);

@ -13,7 +13,7 @@
#include "rlz/lib/string_utils.h"
#include "rlz/win/lib/registry_util.h"
using base::ASCIIToWide;
using base::ASCIIToUTF16;
namespace rlz_lib {
@ -47,7 +47,7 @@ const char kStatefulEventsSubkeyName[] = "StatefulEvents";
const char kPingTimesSubkeyName[] = "PTimes";
std::wstring GetWideProductName(Product product) {
return ASCIIToWide(GetProductName(product));
return ASCIIToUTF16(GetProductName(product));
}
void AppendBrandToString(std::string* str) {
@ -61,16 +61,13 @@ bool GetRegKey(const char* name, REGSAM access, base::win::RegKey* key) {
std::string key_location;
base::StringAppendF(&key_location, "%s\\%s", kLibKeyName, name);
AppendBrandToString(&key_location);
base::string16 key_location16 = ASCIIToUTF16(key_location);
LONG ret = ERROR_SUCCESS;
if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK)) {
ret = key->Create(HKEY_CURRENT_USER, ASCIIToWide(key_location).c_str(),
access);
} else {
ret = key->Open(HKEY_CURRENT_USER, ASCIIToWide(key_location).c_str(),
access);
}
LONG ret;
if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK))
ret = key->Create(HKEY_CURRENT_USER, key_location16.c_str(), access);
else
ret = key->Open(HKEY_CURRENT_USER, key_location16.c_str(), access);
return ret == ERROR_SUCCESS;
}
@ -94,16 +91,13 @@ bool GetEventsRegKey(const char* event_type,
base::StringAppendF(&key_location, "\\%s", product_name.c_str());
}
base::string16 key_location16 = ASCIIToUTF16(key_location);
LONG ret = ERROR_SUCCESS;
if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK)) {
ret = key->Create(HKEY_CURRENT_USER, ASCIIToWide(key_location).c_str(),
access);
} else {
ret = key->Open(HKEY_CURRENT_USER, ASCIIToWide(key_location).c_str(),
access);
}
LONG ret;
if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK))
ret = key->Create(HKEY_CURRENT_USER, key_location16.c_str(), access);
else
ret = key->Open(HKEY_CURRENT_USER, key_location16.c_str(), access);
return ret == ERROR_SUCCESS;
}
@ -160,7 +154,7 @@ bool DeleteKeyIfEmpty(HKEY root_key, const wchar_t* key_name) {
// static
std::wstring RlzValueStoreRegistry::GetWideLibKeyName() {
return ASCIIToWide(kLibKeyName);
return ASCIIToUTF16(kLibKeyName);
}
bool RlzValueStoreRegistry::HasAccess(AccessType type) {
@ -207,11 +201,11 @@ bool RlzValueStoreRegistry::WriteAccessPointRlz(AccessPoint access_point,
if (!access_point_name)
return false;
std::wstring access_point_name_wide(ASCIIToWide(access_point_name));
base::string16 access_point_name16(ASCIIToUTF16(access_point_name));
base::win::RegKey key;
GetAccessPointRlzsRegKey(KEY_WRITE, &key);
if (!RegKeyWriteValue(key, access_point_name_wide.c_str(), new_rlz)) {
if (!RegKeyWriteValue(&key, access_point_name16.c_str(), new_rlz)) {
ASSERT_STRING("SetAccessPointRlz: Could not write the new RLZ value");
return false;
}
@ -228,8 +222,8 @@ bool RlzValueStoreRegistry::ReadAccessPointRlz(AccessPoint access_point,
size_t size = rlz_size;
base::win::RegKey key;
GetAccessPointRlzsRegKey(KEY_READ, &key);
if (!RegKeyReadValue(key, ASCIIToWide(access_point_name).c_str(),
rlz, &size)) {
base::string16 access_point_name16 = ASCIIToUTF16(access_point_name);
if (!RegKeyReadValue(key, access_point_name16.c_str(), rlz, &size)) {
rlz[0] = 0;
if (size > rlz_size) {
ASSERT_STRING("GetAccessPointRlz: Insufficient buffer size");
@ -244,16 +238,15 @@ bool RlzValueStoreRegistry::ClearAccessPointRlz(AccessPoint access_point) {
if (!access_point_name)
return false;
std::wstring access_point_name_wide(ASCIIToWide(access_point_name));
base::string16 access_point_name16(ASCIIToUTF16(access_point_name));
base::win::RegKey key;
GetAccessPointRlzsRegKey(KEY_WRITE, &key);
key.DeleteValue(access_point_name_wide.c_str());
key.DeleteValue(access_point_name16.c_str());
// Verify deletion.
DWORD value;
if (key.ReadValueDW(access_point_name_wide.c_str(), &value) ==
ERROR_SUCCESS) {
if (key.ReadValueDW(access_point_name16.c_str(), &value) == ERROR_SUCCESS) {
ASSERT_STRING("SetAccessPointRlz: Could not clear the RLZ value.");
return false;
}
@ -262,10 +255,10 @@ bool RlzValueStoreRegistry::ClearAccessPointRlz(AccessPoint access_point) {
bool RlzValueStoreRegistry::AddProductEvent(Product product,
const char* event_rlz) {
std::wstring event_rlz_wide(ASCIIToWide(event_rlz));
base::string16 event_rlz16(ASCIIToUTF16(event_rlz));
base::win::RegKey reg_key;
GetEventsRegKey(kEventsSubkeyName, &product, KEY_WRITE, &reg_key);
if (reg_key.WriteValue(event_rlz_wide.c_str(), 1) != ERROR_SUCCESS) {
if (reg_key.WriteValue(event_rlz16.c_str(), 1) != ERROR_SUCCESS) {
ASSERT_STRING("AddProductEvent: Could not write the new event value");
return false;
}
@ -301,14 +294,14 @@ bool RlzValueStoreRegistry::ReadProductEvents(Product product,
bool RlzValueStoreRegistry::ClearProductEvent(Product product,
const char* event_rlz) {
std::wstring event_rlz_wide(ASCIIToWide(event_rlz));
base::string16 event_rlz16(ASCIIToUTF16(event_rlz));
base::win::RegKey key;
GetEventsRegKey(kEventsSubkeyName, &product, KEY_WRITE, &key);
key.DeleteValue(event_rlz_wide.c_str());
key.DeleteValue(event_rlz16.c_str());
// Verify deletion.
DWORD value;
if (key.ReadValueDW(event_rlz_wide.c_str(), &value) == ERROR_SUCCESS) {
if (key.ReadValueDW(event_rlz16.c_str(), &value) == ERROR_SUCCESS) {
ASSERT_STRING("ClearProductEvent: Could not delete the event value.");
return false;
}
@ -323,9 +316,9 @@ bool RlzValueStoreRegistry::ClearAllProductEvents(Product product) {
bool RlzValueStoreRegistry::AddStatefulEvent(Product product,
const char* event_rlz) {
base::win::RegKey key;
std::wstring event_rlz_wide(ASCIIToWide(event_rlz));
base::string16 event_rlz16(ASCIIToUTF16(event_rlz));
if (!GetEventsRegKey(kStatefulEventsSubkeyName, &product, KEY_WRITE, &key) ||
key.WriteValue(event_rlz_wide.c_str(), 1) != ERROR_SUCCESS) {
key.WriteValue(event_rlz16.c_str(), 1) != ERROR_SUCCESS) {
ASSERT_STRING(
"AddStatefulEvent: Could not write the new stateful event");
return false;
@ -339,8 +332,8 @@ bool RlzValueStoreRegistry::IsStatefulEvent(Product product,
DWORD value;
base::win::RegKey key;
GetEventsRegKey(kStatefulEventsSubkeyName, &product, KEY_READ, &key);
std::wstring event_rlz_wide(ASCIIToWide(event_rlz));
return key.ReadValueDW(event_rlz_wide.c_str(), &value) == ERROR_SUCCESS;
base::string16 event_rlz16(ASCIIToUTF16(event_rlz));
return key.ReadValueDW(event_rlz16.c_str(), &value) == ERROR_SUCCESS;
}
bool RlzValueStoreRegistry::ClearAllStatefulEvents(Product product) {
@ -349,7 +342,7 @@ bool RlzValueStoreRegistry::ClearAllStatefulEvents(Product product) {
void RlzValueStoreRegistry::CollectGarbage() {
// Delete each of the known subkeys if empty.
const char* subkeys[] = {
const char* const subkeys[] = {
kRlzsSubkeyName,
kEventsSubkeyName,
kStatefulEventsSubkeyName,
@ -360,9 +353,9 @@ void RlzValueStoreRegistry::CollectGarbage() {
std::string subkey_name;
base::StringAppendF(&subkey_name, "%s\\%s", kLibKeyName, subkeys[i]);
AppendBrandToString(&subkey_name);
base::string16 subkey_name16 = ASCIIToUTF16(subkey_name);
VERIFY(DeleteKeyIfEmpty(HKEY_CURRENT_USER,
ASCIIToWide(subkey_name).c_str()));
VERIFY(DeleteKeyIfEmpty(HKEY_CURRENT_USER, subkey_name16.c_str()));
}
// Delete the library key and its parents too now if empty.