Commit 63099e25 authored by Robert Liao's avatar Robert Liao Committed by Commit Bot

Introduce ScopedBstr::Get()

ScopedBstr::Get provides a migration pathway away from
operator BSTR() const, a violation of the implicit conversion rule in
the C++ style guide.

https://google.github.io/styleguide/cppguide.html#Implicit_Conversions

BUG=1034666

Change-Id: Idacb984b3c97b6deafbd91b3dd0d8928a12f932b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2036522
Auto-Submit: Robert Liao <robliao@chromium.org>
Reviewed-by: default avatarWill Harris <wfh@chromium.org>
Commit-Queue: Robert Liao <robliao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#738227}
parent 3f786663
...@@ -31,6 +31,8 @@ class BASE_EXPORT ScopedBstr { ...@@ -31,6 +31,8 @@ class BASE_EXPORT ScopedBstr {
explicit ScopedBstr(WStringPiece non_bstr); explicit ScopedBstr(WStringPiece non_bstr);
~ScopedBstr(); ~ScopedBstr();
BSTR Get() const { return bstr_; }
// Give ScopedBstr ownership over an already allocated BSTR or null. // Give ScopedBstr ownership over an already allocated BSTR or null.
// If you need to allocate a new BSTR instance, use |allocate| instead. // If you need to allocate a new BSTR instance, use |allocate| instead.
void Reset(BSTR bstr = nullptr); void Reset(BSTR bstr = nullptr);
...@@ -78,6 +80,7 @@ class BASE_EXPORT ScopedBstr { ...@@ -78,6 +80,7 @@ class BASE_EXPORT ScopedBstr {
// Returns the number of bytes allocated for the BSTR. // Returns the number of bytes allocated for the BSTR.
size_t ByteLength() const; size_t ByteLength() const;
// DEPRECATED: Use ScopedBstr::Get() instead.
operator BSTR() const { return bstr_; } operator BSTR() const { return bstr_; }
// Forbid comparison of ScopedBstr types. You should never have the same // Forbid comparison of ScopedBstr types. You should never have the same
......
...@@ -21,15 +21,15 @@ size_t test2_len = size(kTestString2) - 1; ...@@ -21,15 +21,15 @@ size_t test2_len = size(kTestString2) - 1;
void DumbBstrTests() { void DumbBstrTests() {
ScopedBstr b; ScopedBstr b;
EXPECT_TRUE(b == nullptr); EXPECT_TRUE(b.Get() == nullptr);
EXPECT_EQ(0u, b.Length()); EXPECT_EQ(0u, b.Length());
EXPECT_EQ(0u, b.ByteLength()); EXPECT_EQ(0u, b.ByteLength());
b.Reset(nullptr); b.Reset(nullptr);
EXPECT_TRUE(b == nullptr); EXPECT_TRUE(b.Get() == nullptr);
EXPECT_TRUE(b.Release() == nullptr); EXPECT_TRUE(b.Release() == nullptr);
ScopedBstr b2; ScopedBstr b2;
b.Swap(b2); b.Swap(b2);
EXPECT_TRUE(b2 == nullptr); EXPECT_TRUE(b.Get() == nullptr);
} }
void GiveMeABstr(BSTR* ret) { void GiveMeABstr(BSTR* ret) {
...@@ -45,24 +45,24 @@ void BasicBstrTests() { ...@@ -45,24 +45,24 @@ void BasicBstrTests() {
b1.Swap(b2); b1.Swap(b2);
EXPECT_EQ(test1_len, b2.Length()); EXPECT_EQ(test1_len, b2.Length());
EXPECT_EQ(0u, b1.Length()); EXPECT_EQ(0u, b1.Length());
EXPECT_STREQ(b2, kTestString1); EXPECT_STREQ(b2.Get(), kTestString1);
BSTR tmp = b2.Release(); BSTR tmp = b2.Release();
EXPECT_TRUE(tmp != nullptr); EXPECT_TRUE(tmp != nullptr);
EXPECT_STREQ(tmp, kTestString1); EXPECT_STREQ(tmp, kTestString1);
EXPECT_TRUE(b2 == nullptr); EXPECT_TRUE(b2.Get() == nullptr);
SysFreeString(tmp); SysFreeString(tmp);
GiveMeABstr(b2.Receive()); GiveMeABstr(b2.Receive());
EXPECT_TRUE(b2 != nullptr); EXPECT_TRUE(b2.Get() != nullptr);
b2.Reset(); b2.Reset();
EXPECT_TRUE(b2.AllocateBytes(100) != nullptr); EXPECT_TRUE(b2.AllocateBytes(100) != nullptr);
EXPECT_EQ(100u, b2.ByteLength()); EXPECT_EQ(100u, b2.ByteLength());
EXPECT_EQ(100 / sizeof(kTestString1[0]), b2.Length()); EXPECT_EQ(100 / sizeof(kTestString1[0]), b2.Length());
lstrcpy(static_cast<BSTR>(b2), kTestString1); lstrcpy(b2.Get(), kTestString1);
EXPECT_EQ(test1_len, static_cast<size_t>(lstrlen(b2))); EXPECT_EQ(test1_len, static_cast<size_t>(lstrlen(b2.Get())));
EXPECT_EQ(100 / sizeof(kTestString1[0]), b2.Length()); EXPECT_EQ(100 / sizeof(kTestString1[0]), b2.Length());
b2.SetByteLen(lstrlen(b2) * sizeof(kTestString2[0])); b2.SetByteLen(lstrlen(b2.Get()) * sizeof(kTestString2[0]));
EXPECT_EQ(b2.Length(), static_cast<size_t>(lstrlen(b2))); EXPECT_EQ(b2.Length(), static_cast<size_t>(lstrlen(b2.Get())));
EXPECT_TRUE(b1.Allocate(kTestString2) != nullptr); EXPECT_TRUE(b1.Allocate(kTestString2) != nullptr);
EXPECT_EQ(test2_len, b1.Length()); EXPECT_EQ(test2_len, b1.Length());
......
...@@ -36,9 +36,9 @@ bool CreateLocalWmiConnection(bool set_blanket, ...@@ -36,9 +36,9 @@ bool CreateLocalWmiConnection(bool set_blanket,
return false; return false;
ComPtr<IWbemServices> wmi_services_r; ComPtr<IWbemServices> wmi_services_r;
hr = hr = wmi_locator->ConnectServer(ScopedBstr(L"ROOT\\CIMV2").Get(), nullptr,
wmi_locator->ConnectServer(ScopedBstr(L"ROOT\\CIMV2"), nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr,
nullptr, 0, nullptr, nullptr, &wmi_services_r); &wmi_services_r);
if (FAILED(hr)) if (FAILED(hr))
return false; return false;
...@@ -64,13 +64,13 @@ bool CreateWmiClassMethodObject(IWbemServices* wmi_services, ...@@ -64,13 +64,13 @@ bool CreateWmiClassMethodObject(IWbemServices* wmi_services,
ScopedBstr b_method_name(method_name); ScopedBstr b_method_name(method_name);
ComPtr<IWbemClassObject> class_object; ComPtr<IWbemClassObject> class_object;
HRESULT hr; HRESULT hr;
hr = hr = wmi_services->GetObject(b_class_name.Get(), 0, nullptr, &class_object,
wmi_services->GetObject(b_class_name, 0, nullptr, &class_object, nullptr); nullptr);
if (FAILED(hr)) if (FAILED(hr))
return false; return false;
ComPtr<IWbemClassObject> params_def; ComPtr<IWbemClassObject> params_def;
hr = class_object->GetMethod(b_method_name, 0, &params_def, nullptr); hr = class_object->GetMethod(b_method_name.Get(), 0, &params_def, nullptr);
if (FAILED(hr)) if (FAILED(hr))
return false; return false;
...@@ -112,7 +112,7 @@ bool WmiLaunchProcess(const std::wstring& command_line, int* process_id) { ...@@ -112,7 +112,7 @@ bool WmiLaunchProcess(const std::wstring& command_line, int* process_id) {
ComPtr<IWbemClassObject> out_params; ComPtr<IWbemClassObject> out_params;
HRESULT hr = wmi_local->ExecMethod( HRESULT hr = wmi_local->ExecMethod(
ScopedBstr(class_name), ScopedBstr(method_name), 0, nullptr, ScopedBstr(class_name).Get(), ScopedBstr(method_name).Get(), 0, nullptr,
process_create.Get(), &out_params, nullptr); process_create.Get(), &out_params, nullptr);
if (FAILED(hr)) if (FAILED(hr))
return false; return false;
...@@ -155,10 +155,10 @@ void WmiComputerSystemInfo::PopulateModelAndManufacturer( ...@@ -155,10 +155,10 @@ void WmiComputerSystemInfo::PopulateModelAndManufacturer(
L"SELECT Manufacturer,Model FROM Win32_ComputerSystem"; L"SELECT Manufacturer,Model FROM Win32_ComputerSystem";
ComPtr<IEnumWbemClassObject> enumerator_computer_system; ComPtr<IEnumWbemClassObject> enumerator_computer_system;
HRESULT hr = HRESULT hr = services->ExecQuery(
services->ExecQuery(ScopedBstr(L"WQL"), ScopedBstr(query_computer_system), ScopedBstr(L"WQL").Get(), ScopedBstr(query_computer_system).Get(),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, nullptr,
nullptr, &enumerator_computer_system); &enumerator_computer_system);
if (FAILED(hr) || !enumerator_computer_system.Get()) if (FAILED(hr) || !enumerator_computer_system.Get())
return; return;
...@@ -189,10 +189,10 @@ void WmiComputerSystemInfo::PopulateSerialNumber( ...@@ -189,10 +189,10 @@ void WmiComputerSystemInfo::PopulateSerialNumber(
L"SELECT SerialNumber FROM Win32_Bios"; L"SELECT SerialNumber FROM Win32_Bios";
ComPtr<IEnumWbemClassObject> enumerator_bios; ComPtr<IEnumWbemClassObject> enumerator_bios;
HRESULT hr = HRESULT hr = services->ExecQuery(
services->ExecQuery(ScopedBstr(L"WQL"), ScopedBstr(query_bios), ScopedBstr(L"WQL").Get(), ScopedBstr(query_bios).Get(),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, nullptr,
nullptr, &enumerator_bios); &enumerator_bios);
if (FAILED(hr) || !enumerator_bios.Get()) if (FAILED(hr) || !enumerator_bios.Get())
return; return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment