[base] Prepare //mojo and //ipc for string16 switch
This change prepares //mojo and //ipc for the upcoming switch of base::string16 to std::u16string by replacing current Windows only usages of base::string16 with std::wstring. Furthermore, it adds a std::wstring specialization of IPC::ParamTraits. Bug: 911896 Change-Id: If09078ba43a381f7210f89bba04a729024812d1f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2572842 Reviewed-by: Daniel Cheng <dcheng@chromium.org> Reviewed-by: Ken Rockot <rockot@google.com> Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org> Cr-Commit-Position: refs/heads/master@{#834355}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
6cc15bfdc6
commit
cf5a6690bc
@@ -409,6 +409,23 @@ void ParamTraits<base::string16>::Log(const param_type& p, std::string* l) {
|
|||||||
l->append(base::UTF16ToUTF8(p));
|
l->append(base::UTF16ToUTF8(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(OS_WIN) && defined(BASE_STRING16_IS_STD_U16STRING)
|
||||||
|
bool ParamTraits<std::wstring>::Read(const base::Pickle* m,
|
||||||
|
base::PickleIterator* iter,
|
||||||
|
param_type* r) {
|
||||||
|
base::StringPiece16 piece16;
|
||||||
|
if (!iter->ReadStringPiece16(&piece16))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
*r = base::AsWString(piece16);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) {
|
||||||
|
l->append(base::WideToUTF8(p));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void ParamTraits<std::vector<char>>::Write(base::Pickle* m,
|
void ParamTraits<std::vector<char>>::Write(base::Pickle* m,
|
||||||
const param_type& p) {
|
const param_type& p) {
|
||||||
if (p.empty()) {
|
if (p.empty()) {
|
||||||
|
@@ -346,6 +346,20 @@ struct ParamTraits<base::string16> {
|
|||||||
COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l);
|
COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if defined(OS_WIN) && defined(BASE_STRING16_IS_STD_U16STRING)
|
||||||
|
template <>
|
||||||
|
struct ParamTraits<std::wstring> {
|
||||||
|
typedef std::wstring param_type;
|
||||||
|
static void Write(base::Pickle* m, const param_type& p) {
|
||||||
|
m->WriteString16(base::AsStringPiece16(p));
|
||||||
|
}
|
||||||
|
static bool Read(const base::Pickle* m,
|
||||||
|
base::PickleIterator* iter,
|
||||||
|
param_type* r);
|
||||||
|
COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l);
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct COMPONENT_EXPORT(IPC) ParamTraits<std::vector<char>> {
|
struct COMPONENT_EXPORT(IPC) ParamTraits<std::vector<char>> {
|
||||||
typedef std::vector<char> param_type;
|
typedef std::vector<char> param_type;
|
||||||
|
@@ -91,9 +91,9 @@ bool BrokerHost::SendChannel(PlatformHandle handle) {
|
|||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
|
|
||||||
void BrokerHost::SendNamedChannel(const base::StringPiece16& pipe_name) {
|
void BrokerHost::SendNamedChannel(base::WStringPiece pipe_name) {
|
||||||
InitData* data;
|
InitData* data;
|
||||||
base::char16* name_data;
|
wchar_t* name_data;
|
||||||
Channel::MessagePtr message = CreateBrokerMessage(
|
Channel::MessagePtr message = CreateBrokerMessage(
|
||||||
BrokerMessageType::INIT, 0, sizeof(*name_data) * pipe_name.length(),
|
BrokerMessageType::INIT, 0, sizeof(*name_data) * pipe_name.length(),
|
||||||
&data, reinterpret_cast<void**>(&name_data));
|
&data, reinterpret_cast<void**>(&name_data));
|
||||||
|
@@ -37,7 +37,7 @@ class BrokerHost : public Channel::Delegate,
|
|||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
// Sends a named channel to the client. Like above, but for named pipes.
|
// Sends a named channel to the client. Like above, but for named pipes.
|
||||||
void SendNamedChannel(const base::StringPiece16& pipe_name);
|
void SendNamedChannel(base::WStringPiece pipe_name);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@@ -111,11 +111,10 @@ Broker::Broker(PlatformHandle handle, bool wait_for_channel_handle)
|
|||||||
CHECK_EQ(message->payload_size(),
|
CHECK_EQ(message->payload_size(),
|
||||||
sizeof(BrokerMessageHeader) + sizeof(InitData) +
|
sizeof(BrokerMessageHeader) + sizeof(InitData) +
|
||||||
data->pipe_name_length * sizeof(base::char16));
|
data->pipe_name_length * sizeof(base::char16));
|
||||||
const base::char16* name_data =
|
auto* name_data = reinterpret_cast<const wchar_t*>(data + 1);
|
||||||
reinterpret_cast<const base::char16*>(data + 1);
|
|
||||||
CHECK(data->pipe_name_length);
|
CHECK(data->pipe_name_length);
|
||||||
inviter_endpoint_ = NamedPlatformChannel::ConnectToServer(
|
inviter_endpoint_ = NamedPlatformChannel::ConnectToServer(
|
||||||
base::string16(name_data, data->pipe_name_length));
|
NamedPlatformChannel::ServerName(name_data, data->pipe_name_length));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -132,7 +132,7 @@ ScopedMessagePipeHandle MultiprocessTestHelper::StartChildWithExtraSwitch(
|
|||||||
temp_dir.AppendASCII(base::NumberToString(base::RandUint64()))
|
temp_dir.AppendASCII(base::NumberToString(base::RandUint64()))
|
||||||
.value();
|
.value();
|
||||||
#elif defined(OS_WIN)
|
#elif defined(OS_WIN)
|
||||||
server_name = base::NumberToString16(base::RandUint64());
|
server_name = base::NumberToWString(base::RandUint64());
|
||||||
#else
|
#else
|
||||||
#error "Platform not yet supported."
|
#error "Platform not yet supported."
|
||||||
#endif
|
#endif
|
||||||
|
@@ -14,8 +14,7 @@ bool StructTraits<mojo_base::mojom::FilePathDataView, base::FilePath>::Read(
|
|||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
ArrayDataView<uint16_t> view;
|
ArrayDataView<uint16_t> view;
|
||||||
data.GetPathDataView(&view);
|
data.GetPathDataView(&view);
|
||||||
path_view = base::StringPiece16(
|
path_view = {reinterpret_cast<const wchar_t*>(view.data()), view.size()};
|
||||||
reinterpret_cast<const base::char16*>(view.data()), view.size());
|
|
||||||
#else
|
#else
|
||||||
if (!data.ReadPath(&path_view)) {
|
if (!data.ReadPath(&path_view)) {
|
||||||
return false;
|
return false;
|
||||||
|
@@ -29,7 +29,7 @@ NamedPlatformChannel& NamedPlatformChannel::operator=(
|
|||||||
NamedPlatformChannel::ServerName NamedPlatformChannel::ServerNameFromUTF8(
|
NamedPlatformChannel::ServerName NamedPlatformChannel::ServerNameFromUTF8(
|
||||||
base::StringPiece name) {
|
base::StringPiece name) {
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
return base::UTF8ToUTF16(name);
|
return base::UTF8ToWide(name);
|
||||||
#else
|
#else
|
||||||
return name.as_string();
|
return name.as_string();
|
||||||
#endif
|
#endif
|
||||||
|
@@ -5,6 +5,8 @@
|
|||||||
#ifndef MOJO_PUBLIC_CPP_PLATFORM_NAMED_PLATFORM_CHANNEL_H_
|
#ifndef MOJO_PUBLIC_CPP_PLATFORM_NAMED_PLATFORM_CHANNEL_H_
|
||||||
#define MOJO_PUBLIC_CPP_PLATFORM_NAMED_PLATFORM_CHANNEL_H_
|
#define MOJO_PUBLIC_CPP_PLATFORM_NAMED_PLATFORM_CHANNEL_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "base/command_line.h"
|
#include "base/command_line.h"
|
||||||
#include "base/component_export.h"
|
#include "base/component_export.h"
|
||||||
#include "base/macros.h"
|
#include "base/macros.h"
|
||||||
@@ -13,9 +15,7 @@
|
|||||||
#include "mojo/public/cpp/platform/platform_channel_endpoint.h"
|
#include "mojo/public/cpp/platform/platform_channel_endpoint.h"
|
||||||
#include "mojo/public/cpp/platform/platform_channel_server_endpoint.h"
|
#include "mojo/public/cpp/platform/platform_channel_server_endpoint.h"
|
||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_POSIX)
|
||||||
#include "base/strings/string16.h"
|
|
||||||
#elif defined(OS_POSIX)
|
|
||||||
#include "base/files/file_path.h"
|
#include "base/files/file_path.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) NamedPlatformChannel {
|
|||||||
static const char kNamedHandleSwitch[];
|
static const char kNamedHandleSwitch[];
|
||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
using ServerName = base::string16;
|
using ServerName = std::wstring;
|
||||||
#else
|
#else
|
||||||
using ServerName = std::string;
|
using ServerName = std::string;
|
||||||
#endif
|
#endif
|
||||||
@@ -49,7 +49,7 @@ class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) NamedPlatformChannel {
|
|||||||
// If non-empty, a security descriptor to use when creating the pipe. If
|
// If non-empty, a security descriptor to use when creating the pipe. If
|
||||||
// empty, a default security descriptor will be used. See
|
// empty, a default security descriptor will be used. See
|
||||||
// |kDefaultSecurityDescriptor|.
|
// |kDefaultSecurityDescriptor|.
|
||||||
base::string16 security_descriptor;
|
std::wstring security_descriptor;
|
||||||
|
|
||||||
// If |true|, only a server endpoint will be allowed with the given name and
|
// If |true|, only a server endpoint will be allowed with the given name and
|
||||||
// only one client will be able to connect. Otherwise many
|
// only one client will be able to connect. Otherwise many
|
||||||
|
@@ -27,16 +27,15 @@ namespace {
|
|||||||
// SY = LOCAL_SYSTEM
|
// SY = LOCAL_SYSTEM
|
||||||
// BA = BUILTIN_ADMINISTRATORS
|
// BA = BUILTIN_ADMINISTRATORS
|
||||||
// OW = OWNER_RIGHTS
|
// OW = OWNER_RIGHTS
|
||||||
constexpr base::char16 kDefaultSecurityDescriptor[] =
|
constexpr wchar_t kDefaultSecurityDescriptor[] =
|
||||||
L"D:(A;;GA;;;SY)(A;;GA;;;BA)(A;;GA;;;OW)";
|
L"D:(A;;GA;;;SY)(A;;GA;;;BA)(A;;GA;;;OW)";
|
||||||
|
|
||||||
NamedPlatformChannel::ServerName GenerateRandomServerName() {
|
NamedPlatformChannel::ServerName GenerateRandomServerName() {
|
||||||
return base::UTF8ToUTF16(
|
return base::StringPrintf(L"%lu.%lu.%I64u", ::GetCurrentProcessId(),
|
||||||
base::StringPrintf("%lu.%lu.%I64u", ::GetCurrentProcessId(),
|
::GetCurrentThreadId(), base::RandUint64());
|
||||||
::GetCurrentThreadId(), base::RandUint64()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
base::string16 GetPipeNameFromServerName(
|
std::wstring GetPipeNameFromServerName(
|
||||||
const NamedPlatformChannel::ServerName& server_name) {
|
const NamedPlatformChannel::ServerName& server_name) {
|
||||||
return L"\\\\.\\pipe\\mojo." + server_name;
|
return L"\\\\.\\pipe\\mojo." + server_name;
|
||||||
}
|
}
|
||||||
@@ -68,7 +67,7 @@ PlatformChannelServerEndpoint NamedPlatformChannel::CreateServerEndpoint(
|
|||||||
const DWORD kPipeMode =
|
const DWORD kPipeMode =
|
||||||
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_REJECT_REMOTE_CLIENTS;
|
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_REJECT_REMOTE_CLIENTS;
|
||||||
|
|
||||||
base::string16 pipe_name = GetPipeNameFromServerName(name);
|
std::wstring pipe_name = GetPipeNameFromServerName(name);
|
||||||
PlatformHandle handle(base::win::ScopedHandle(::CreateNamedPipeW(
|
PlatformHandle handle(base::win::ScopedHandle(::CreateNamedPipeW(
|
||||||
pipe_name.c_str(), kOpenMode, kPipeMode,
|
pipe_name.c_str(), kOpenMode, kPipeMode,
|
||||||
options.enforce_uniqueness ? 1 : 255, // Max instances.
|
options.enforce_uniqueness ? 1 : 255, // Max instances.
|
||||||
@@ -84,7 +83,7 @@ PlatformChannelServerEndpoint NamedPlatformChannel::CreateServerEndpoint(
|
|||||||
// static
|
// static
|
||||||
PlatformChannelEndpoint NamedPlatformChannel::CreateClientEndpoint(
|
PlatformChannelEndpoint NamedPlatformChannel::CreateClientEndpoint(
|
||||||
const ServerName& server_name) {
|
const ServerName& server_name) {
|
||||||
base::string16 pipe_name = GetPipeNameFromServerName(server_name);
|
std::wstring pipe_name = GetPipeNameFromServerName(server_name);
|
||||||
|
|
||||||
// Note: This may block.
|
// Note: This may block.
|
||||||
if (!::WaitNamedPipeW(pipe_name.c_str(), NMPWAIT_USE_DEFAULT_WAIT))
|
if (!::WaitNamedPipeW(pipe_name.c_str(), NMPWAIT_USE_DEFAULT_WAIT))
|
||||||
|
@@ -56,9 +56,9 @@ namespace {
|
|||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
void CreateChannel(PlatformHandle* local_endpoint,
|
void CreateChannel(PlatformHandle* local_endpoint,
|
||||||
PlatformHandle* remote_endpoint) {
|
PlatformHandle* remote_endpoint) {
|
||||||
base::string16 pipe_name = base::UTF8ToUTF16(base::StringPrintf(
|
std::wstring pipe_name = base::StringPrintf(
|
||||||
"\\\\.\\pipe\\mojo.%lu.%lu.%I64u", ::GetCurrentProcessId(),
|
L"\\\\.\\pipe\\mojo.%lu.%lu.%I64u", ::GetCurrentProcessId(),
|
||||||
::GetCurrentThreadId(), base::RandUint64()));
|
::GetCurrentThreadId(), base::RandUint64());
|
||||||
DWORD kOpenMode =
|
DWORD kOpenMode =
|
||||||
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | FILE_FLAG_FIRST_PIPE_INSTANCE;
|
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | FILE_FLAG_FIRST_PIPE_INSTANCE;
|
||||||
const DWORD kPipeMode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE;
|
const DWORD kPipeMode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE;
|
||||||
|
Reference in New Issue
Block a user