Commit fadab808 authored by Greg Thompson's avatar Greg Thompson Committed by Commit Bot

Use StringPiece in base::win::ScopedBstr to reduce strlens.

Also make a few gentle updates to the code.

BUG=none
TBR=isherman@chromium.org,pmonette@chromium.org,csharp@chromium.org,garykac@chromium.org

Change-Id: I328d6859024cbdc88c001920a9fe21cbfbb42cc8
Reviewed-on: https://chromium-review.googlesource.com/1189562
Commit-Queue: Greg Thompson <grt@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#586664}
parent 4336e213
...@@ -11,26 +11,25 @@ ...@@ -11,26 +11,25 @@
namespace base { namespace base {
namespace win { namespace win {
ScopedBstr::ScopedBstr(const char16* non_bstr) ScopedBstr::ScopedBstr(StringPiece16 non_bstr)
: bstr_(SysAllocString(non_bstr)) { : bstr_(::SysAllocStringLen(non_bstr.data(), non_bstr.length())) {}
}
ScopedBstr::~ScopedBstr() { ScopedBstr::~ScopedBstr() {
static_assert(sizeof(ScopedBstr) == sizeof(BSTR), "ScopedBstrSize"); static_assert(sizeof(ScopedBstr) == sizeof(BSTR), "ScopedBstrSize");
SysFreeString(bstr_); ::SysFreeString(bstr_);
} }
void ScopedBstr::Reset(BSTR bstr) { void ScopedBstr::Reset(BSTR bstr) {
if (bstr != bstr_) { if (bstr != bstr_) {
// if |bstr_| is NULL, SysFreeString does nothing. // SysFreeString handles null properly.
SysFreeString(bstr_); ::SysFreeString(bstr_);
bstr_ = bstr; bstr_ = bstr;
} }
} }
BSTR ScopedBstr::Release() { BSTR ScopedBstr::Release() {
BSTR bstr = bstr_; BSTR bstr = bstr_;
bstr_ = NULL; bstr_ = nullptr;
return bstr; return bstr;
} }
...@@ -45,28 +44,28 @@ BSTR* ScopedBstr::Receive() { ...@@ -45,28 +44,28 @@ BSTR* ScopedBstr::Receive() {
return &bstr_; return &bstr_;
} }
BSTR ScopedBstr::Allocate(const char16* str) { BSTR ScopedBstr::Allocate(StringPiece16 str) {
Reset(SysAllocString(str)); Reset(::SysAllocStringLen(str.data(), str.length()));
return bstr_; return bstr_;
} }
BSTR ScopedBstr::AllocateBytes(size_t bytes) { BSTR ScopedBstr::AllocateBytes(size_t bytes) {
Reset(SysAllocStringByteLen(NULL, static_cast<UINT>(bytes))); Reset(::SysAllocStringByteLen(nullptr, static_cast<UINT>(bytes)));
return bstr_; return bstr_;
} }
void ScopedBstr::SetByteLen(size_t bytes) { void ScopedBstr::SetByteLen(size_t bytes) {
DCHECK(bstr_ != NULL) << "attempting to modify a NULL bstr"; DCHECK(bstr_);
uint32_t* data = reinterpret_cast<uint32_t*>(bstr_); uint32_t* data = reinterpret_cast<uint32_t*>(bstr_);
data[-1] = static_cast<uint32_t>(bytes); data[-1] = static_cast<uint32_t>(bytes);
} }
size_t ScopedBstr::Length() const { size_t ScopedBstr::Length() const {
return SysStringLen(bstr_); return ::SysStringLen(bstr_);
} }
size_t ScopedBstr::ByteLength() const { size_t ScopedBstr::ByteLength() const {
return SysStringByteLen(bstr_); return ::SysStringByteLen(bstr_);
} }
} // namespace win } // namespace win
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/strings/string_piece.h"
namespace base { namespace base {
namespace win { namespace win {
...@@ -21,19 +22,18 @@ namespace win { ...@@ -21,19 +22,18 @@ namespace win {
// The class interface is based on unique_ptr. // The class interface is based on unique_ptr.
class BASE_EXPORT ScopedBstr { class BASE_EXPORT ScopedBstr {
public: public:
ScopedBstr() : bstr_(NULL) { ScopedBstr() : bstr_(nullptr) {}
}
// Constructor to create a new BSTR. // Constructor to create a new BSTR.
// //
// NOTE: Do not pass a BSTR to this constructor expecting ownership to // NOTE: Do not pass a BSTR to this constructor expecting ownership to
// be transferred - even though it compiles! ;-) // be transferred - even though it compiles! ;-)
explicit ScopedBstr(const char16* non_bstr); explicit ScopedBstr(StringPiece16 non_bstr);
~ScopedBstr(); ~ScopedBstr();
// 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 = NULL); void Reset(BSTR bstr = nullptr);
// Releases ownership of the BSTR to the caller. // Releases ownership of the BSTR to the caller.
BSTR Release(); BSTR Release();
...@@ -43,11 +43,11 @@ class BASE_EXPORT ScopedBstr { ...@@ -43,11 +43,11 @@ class BASE_EXPORT ScopedBstr {
// If you already have a BSTR and want to transfer ownership to the // If you already have a BSTR and want to transfer ownership to the
// ScopedBstr instance, call |reset| instead. // ScopedBstr instance, call |reset| instead.
// //
// Returns a pointer to the new BSTR, or NULL if allocation failed. // Returns a pointer to the new BSTR, or null if allocation failed.
BSTR Allocate(const char16* str); BSTR Allocate(StringPiece16 str);
// Allocates a new BSTR with the specified number of bytes. // Allocates a new BSTR with the specified number of bytes.
// Returns a pointer to the new BSTR, or NULL if allocation failed. // Returns a pointer to the new BSTR, or null if allocation failed.
BSTR AllocateBytes(size_t bytes); BSTR AllocateBytes(size_t bytes);
// Sets the allocated length field of the already-allocated BSTR to be // Sets the allocated length field of the already-allocated BSTR to be
...@@ -68,7 +68,7 @@ class BASE_EXPORT ScopedBstr { ...@@ -68,7 +68,7 @@ class BASE_EXPORT ScopedBstr {
// Retrieves the pointer address. // Retrieves the pointer address.
// Used to receive BSTRs as out arguments (and take ownership). // Used to receive BSTRs as out arguments (and take ownership).
// The function DCHECKs on the current value being NULL. // The function DCHECKs on the current value being null.
// Usage: GetBstr(bstr.Receive()); // Usage: GetBstr(bstr.Receive());
BSTR* Receive(); BSTR* Receive();
...@@ -82,14 +82,15 @@ class BASE_EXPORT ScopedBstr { ...@@ -82,14 +82,15 @@ class BASE_EXPORT ScopedBstr {
return bstr_; return bstr_;
} }
// Forbid comparison of ScopedBstr types. You should never have the same
// BSTR owned by two different scoped_ptrs.
bool operator==(const ScopedBstr& bstr2) const = delete;
bool operator!=(const ScopedBstr& bstr2) const = delete;
protected: protected:
BSTR bstr_; BSTR bstr_;
private: private:
// Forbid comparison of ScopedBstr types. You should never have the same
// BSTR owned by two different scoped_ptrs.
bool operator==(const ScopedBstr& bstr2) const;
bool operator!=(const ScopedBstr& bstr2) const;
DISALLOW_COPY_AND_ASSIGN(ScopedBstr); DISALLOW_COPY_AND_ASSIGN(ScopedBstr);
}; };
......
...@@ -52,8 +52,8 @@ bool CreateWmiClassMethodObject(IWbemServices* wmi_services, ...@@ -52,8 +52,8 @@ bool CreateWmiClassMethodObject(IWbemServices* wmi_services,
ComPtr<IWbemClassObject>* class_instance) { ComPtr<IWbemClassObject>* class_instance) {
// We attempt to instantiate a COM object that represents a WMI object plus // We attempt to instantiate a COM object that represents a WMI object plus
// a method rolled into one entity. // a method rolled into one entity.
ScopedBstr b_class_name(class_name.data()); ScopedBstr b_class_name(class_name);
ScopedBstr b_method_name(method_name.data()); ScopedBstr b_method_name(method_name);
ComPtr<IWbemClassObject> class_object; ComPtr<IWbemClassObject> class_object;
HRESULT hr; HRESULT hr;
hr = wmi_services->GetObject(b_class_name, 0, nullptr, hr = wmi_services->GetObject(b_class_name, 0, nullptr,
......
...@@ -534,7 +534,7 @@ HRESULT UpdateCheckDriver::BeginUpdateCheckInternal( ...@@ -534,7 +534,7 @@ HRESULT UpdateCheckDriver::BeginUpdateCheckInternal(
// nice to have, a failure to do so does not affect the likelihood that // nice to have, a failure to do so does not affect the likelihood that
// the update check and/or install will succeed. // the update check and/or install will succeed.
app_bundle->put_displayLanguage( app_bundle->put_displayLanguage(
base::win::ScopedBstr(base::UTF8ToUTF16(locale_).c_str())); base::win::ScopedBstr(base::UTF8ToUTF16(locale_)));
} }
hresult = app_bundle->initialize(); hresult = app_bundle->initialize();
......
...@@ -559,7 +559,7 @@ class TaskSchedulerV2 : public TaskScheduler { ...@@ -559,7 +559,7 @@ class TaskSchedulerV2 : public TaskScheduler {
// Start now. // Start now.
base::Time now(base::Time::NowFromSystemTime()); base::Time now(base::Time::NowFromSystemTime());
base::win::ScopedBstr start_boundary(GetTimestampString(now).c_str()); base::win::ScopedBstr start_boundary(GetTimestampString(now));
hr = trigger->put_StartBoundary(start_boundary); hr = trigger->put_StartBoundary(start_boundary);
if (FAILED(hr)) { if (FAILED(hr)) {
PLOG(ERROR) << "Can't put 'StartBoundary' to " << start_boundary << ". " PLOG(ERROR) << "Can't put 'StartBoundary' to " << start_boundary << ". "
...@@ -587,7 +587,7 @@ class TaskSchedulerV2 : public TaskScheduler { ...@@ -587,7 +587,7 @@ class TaskSchedulerV2 : public TaskScheduler {
// None of the triggers should go beyond kNumDaysBeforeExpiry. // None of the triggers should go beyond kNumDaysBeforeExpiry.
base::Time expiry_date(base::Time::NowFromSystemTime() + base::Time expiry_date(base::Time::NowFromSystemTime() +
base::TimeDelta::FromDays(kNumDaysBeforeExpiry)); base::TimeDelta::FromDays(kNumDaysBeforeExpiry));
base::win::ScopedBstr end_boundary(GetTimestampString(expiry_date).c_str()); base::win::ScopedBstr end_boundary(GetTimestampString(expiry_date));
hr = trigger->put_EndBoundary(end_boundary); hr = trigger->put_EndBoundary(end_boundary);
if (FAILED(hr)) { if (FAILED(hr)) {
PLOG(ERROR) << "Can't put 'EndBoundary' to " << end_boundary << ". " PLOG(ERROR) << "Can't put 'EndBoundary' to " << end_boundary << ". "
...@@ -616,14 +616,14 @@ class TaskSchedulerV2 : public TaskScheduler { ...@@ -616,14 +616,14 @@ class TaskSchedulerV2 : public TaskScheduler {
return false; return false;
} }
base::win::ScopedBstr path(run_command.GetProgram().value().c_str()); base::win::ScopedBstr path(run_command.GetProgram().value());
hr = exec_action->put_Path(path); hr = exec_action->put_Path(path);
if (FAILED(hr)) { if (FAILED(hr)) {
PLOG(ERROR) << "Can't set path of exec action. " << std::hex << hr; PLOG(ERROR) << "Can't set path of exec action. " << std::hex << hr;
return false; return false;
} }
base::win::ScopedBstr args(run_command.GetArgumentsString().c_str()); base::win::ScopedBstr args(run_command.GetArgumentsString());
hr = exec_action->put_Arguments(args); hr = exec_action->put_Arguments(args);
if (FAILED(hr)) { if (FAILED(hr)) {
PLOG(ERROR) << "Can't set arguments of exec action. " << std::hex << hr; PLOG(ERROR) << "Can't set arguments of exec action. " << std::hex << hr;
......
...@@ -108,8 +108,7 @@ void AdvancedFirewallManager::DeleteRule( ...@@ -108,8 +108,7 @@ void AdvancedFirewallManager::DeleteRule(
// Rename rule to unique name and delete by unique name. We can't just delete // Rename rule to unique name and delete by unique name. We can't just delete
// rule by name. Multiple rules with the same name and different app are // rule by name. Multiple rules with the same name and different app are
// possible. // possible.
base::win::ScopedBstr unique_name( base::win::ScopedBstr unique_name(base::UTF8ToUTF16(base::GenerateGUID()));
base::UTF8ToUTF16(base::GenerateGUID()).c_str());
rule->put_Name(unique_name); rule->put_Name(unique_name);
firewall_rules_->Remove(unique_name); firewall_rules_->Remove(unique_name);
} }
...@@ -135,16 +134,15 @@ Microsoft::WRL::ComPtr<INetFwRule> AdvancedFirewallManager::CreateUDPRule( ...@@ -135,16 +134,15 @@ Microsoft::WRL::ComPtr<INetFwRule> AdvancedFirewallManager::CreateUDPRule(
return Microsoft::WRL::ComPtr<INetFwRule>(); return Microsoft::WRL::ComPtr<INetFwRule>();
} }
udp_rule->put_Name(base::win::ScopedBstr(rule_name.c_str())); udp_rule->put_Name(base::win::ScopedBstr(rule_name));
udp_rule->put_Description(base::win::ScopedBstr(description.c_str())); udp_rule->put_Description(base::win::ScopedBstr(description));
udp_rule->put_ApplicationName( udp_rule->put_ApplicationName(base::win::ScopedBstr(app_path_.value()));
base::win::ScopedBstr(app_path_.value().c_str()));
udp_rule->put_Protocol(NET_FW_IP_PROTOCOL_UDP); udp_rule->put_Protocol(NET_FW_IP_PROTOCOL_UDP);
udp_rule->put_Direction(NET_FW_RULE_DIR_IN); udp_rule->put_Direction(NET_FW_RULE_DIR_IN);
udp_rule->put_Enabled(VARIANT_TRUE); udp_rule->put_Enabled(VARIANT_TRUE);
udp_rule->put_LocalPorts( udp_rule->put_LocalPorts(
base::win::ScopedBstr(base::StringPrintf(L"%u", port).c_str())); base::win::ScopedBstr(base::StringPrintf(L"%u", port)));
udp_rule->put_Grouping(base::win::ScopedBstr(app_name_.c_str())); udp_rule->put_Grouping(base::win::ScopedBstr(app_name_));
udp_rule->put_Profiles(NET_FW_PROFILE2_ALL); udp_rule->put_Profiles(NET_FW_PROFILE2_ALL);
udp_rule->put_Action(NET_FW_ACTION_ALLOW); udp_rule->put_Action(NET_FW_ACTION_ALLOW);
......
...@@ -108,8 +108,7 @@ bool IsPinnedToTaskbarHelper::ShortcutHasUnpinToTaskbarVerb( ...@@ -108,8 +108,7 @@ bool IsPinnedToTaskbarHelper::ShortcutHasUnpinToTaskbarVerb(
Microsoft::WRL::ComPtr<FolderItem> item; Microsoft::WRL::ComPtr<FolderItem> item;
hresult = folder->ParseName( hresult = folder->ParseName(
base::win::ScopedBstr(shortcut.BaseName().value().c_str()), base::win::ScopedBstr(shortcut.BaseName().value()), item.GetAddressOf());
item.GetAddressOf());
if (FAILED(hresult) || !item) { if (FAILED(hresult) || !item) {
error_occured_ = true; error_occured_ = true;
return false; return false;
......
...@@ -306,7 +306,7 @@ bool RdpSession::Initialize(const ScreenResolution& resolution) { ...@@ -306,7 +306,7 @@ bool RdpSession::Initialize(const ScreenResolution& resolution) {
Microsoft::WRL::ComPtr<IRdpDesktopSessionEventHandler> event_handler( Microsoft::WRL::ComPtr<IRdpDesktopSessionEventHandler> event_handler(
new EventHandler(weak_factory_.GetWeakPtr())); new EventHandler(weak_factory_.GetWeakPtr()));
terminal_id_ = base::GenerateGUID(); terminal_id_ = base::GenerateGUID();
base::win::ScopedBstr terminal_id(base::UTF8ToUTF16(terminal_id_).c_str()); base::win::ScopedBstr terminal_id(base::UTF8ToUTF16(terminal_id_));
result = rdp_desktop_session_->Connect(host_size.width(), host_size.height(), result = rdp_desktop_session_->Connect(host_size.width(), host_size.height(),
kDefaultRdpDpi, kDefaultRdpDpi, kDefaultRdpDpi, kDefaultRdpDpi,
terminal_id, server_port, terminal_id, server_port,
......
...@@ -41,8 +41,8 @@ void AuthCodeGetter::GetAuthCode( ...@@ -41,8 +41,8 @@ void AuthCodeGetter::GetAuthCode(
on_auth_code_.Run(""); on_auth_code_.Run("");
return; return;
} }
base::win::ScopedBstr url(base::UTF8ToWide( base::win::ScopedBstr url(
GetOauthStartUrl(GetDefaultOauthRedirectUrl())).c_str()); base::UTF8ToWide(GetOauthStartUrl(GetDefaultOauthRedirectUrl())));
base::win::ScopedVariant empty_variant; base::win::ScopedVariant empty_variant;
hr = browser_->Navigate(url, empty_variant.AsInput(), empty_variant.AsInput(), hr = browser_->Navigate(url, empty_variant.AsInput(), empty_variant.AsInput(),
empty_variant.AsInput(), empty_variant.AsInput()); empty_variant.AsInput(), empty_variant.AsInput());
......
...@@ -265,8 +265,8 @@ LRESULT RdpClientWindow::OnCreate(CREATESTRUCT* create_struct) { ...@@ -265,8 +265,8 @@ LRESULT RdpClientWindow::OnCreate(CREATESTRUCT* create_struct) {
Microsoft::WRL::ComPtr<mstsc::IMsTscSecuredSettings> secured_settings; Microsoft::WRL::ComPtr<mstsc::IMsTscSecuredSettings> secured_settings;
Microsoft::WRL::ComPtr<mstsc::IMsRdpClientSecuredSettings> secured_settings2; Microsoft::WRL::ComPtr<mstsc::IMsRdpClientSecuredSettings> secured_settings2;
base::win::ScopedBstr server_name( base::win::ScopedBstr server_name(
base::UTF8ToUTF16(server_endpoint_.ToStringWithoutPort()).c_str()); base::UTF8ToUTF16(server_endpoint_.ToStringWithoutPort()));
base::win::ScopedBstr terminal_id(base::UTF8ToUTF16(terminal_id_).c_str()); base::win::ScopedBstr terminal_id(base::UTF8ToUTF16(terminal_id_));
// Create the child window that actually hosts the ActiveX control. // Create the child window that actually hosts the ActiveX control.
RECT rect = {0, 0, screen_resolution_.dimensions().width(), RECT rect = {0, 0, screen_resolution_.dimensions().width(),
......
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