Commit 0eb63007 authored by Julien Racle's avatar Julien Racle Committed by Commit Bot

[base/win] HSTRING comparison

Provides a wrapper to WindowsCompareStringOrdinal function.

This CL is needed for base/win/IMap's equality to work properly.

BUG=728870
TBR=gab@chromium.org
BUILD.gn change

Change-Id: Ibf8f4442ff25b7525710182c8dc5969ae68c44ef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1683293
Commit-Queue: Robert Liao <robliao@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Reviewed-by: default avatarJan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#675768}
parent d216f65c
......@@ -1060,6 +1060,8 @@ jumbo_component("base") {
"win/event_trace_controller.h",
"win/event_trace_provider.cc",
"win/event_trace_provider.h",
"win/hstring_compare.cc",
"win/hstring_compare.h",
"win/hstring_reference.cc",
"win/hstring_reference.h",
"win/i18n.cc",
......@@ -2782,6 +2784,7 @@ test("base_unittests") {
"win/event_trace_consumer_unittest.cc",
"win/event_trace_controller_unittest.cc",
"win/event_trace_provider_unittest.cc",
"win/hstring_compare_unittest.cc",
"win/hstring_reference_unittest.cc",
"win/i18n_unittest.cc",
"win/iunknown_impl_unittest.cc",
......
// Copyright 2019 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/hstring_compare.h"
#include <winstring.h>
#include "base/native_library.h"
#include "base/win/windows_version.h"
namespace base {
namespace win {
HRESULT HStringCompare(HSTRING string1, HSTRING string2, INT32* result) {
using CompareStringFunc = decltype(&::WindowsCompareStringOrdinal);
static const auto compare_string_func = []() -> CompareStringFunc {
if (GetVersion() < Version::WIN8)
return nullptr;
NativeLibraryLoadError load_error;
NativeLibrary combase_module =
PinSystemLibrary(FILE_PATH_LITERAL("combase.dll"), &load_error);
if (load_error.code)
return nullptr;
return reinterpret_cast<CompareStringFunc>(
GetFunctionPointerFromNativeLibrary(combase_module,
"WindowsCompareStringOrdinal"));
}();
if (!compare_string_func)
return E_FAIL;
return compare_string_func(string1, string2, result);
}
} // namespace win
} // namespace base
// Copyright 2019 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.
#ifndef BASE_WIN_HSTRING_COMPARE_H_
#define BASE_WIN_HSTRING_COMPARE_H_
#include <hstring.h>
#include "base/base_export.h"
namespace base {
namespace win {
// HStringCompare provides a delayloaded version of WindowsCompareStringOrdinal
// function, which compares HSTRING values.
//
// Note that it requires certain functions that are only available on Windows 8
// and later, and that these functions need to be delayloaded to avoid breaking
// Chrome on Windows 7.
BASE_EXPORT HRESULT HStringCompare(HSTRING string1,
HSTRING string2,
INT32* result);
} // namespace win
} // namespace base
#endif // BASE_WIN_HSTRING_COMPARE_H_
// Copyright 2019 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/hstring_compare.h"
#include "base/win/hstring_reference.h"
#include "base/win/windows_version.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace win {
namespace {
constexpr wchar_t kTestString12[] = L"12";
constexpr wchar_t kTestString123[] = L"123";
constexpr wchar_t kTestString1234[] = L"1234";
} // namespace
TEST(HStringCompareTest, WorksOnWindows8AndAbove) {
INT32 result;
HRESULT hr = HStringCompare(nullptr, nullptr, &result);
// HStringCompare requires WinRT core functions, which are not available in
// older versions.
if (GetVersion() < Version::WIN8)
EXPECT_HRESULT_FAILED(hr);
else
EXPECT_HRESULT_SUCCEEDED(hr);
}
TEST(HStringCompareTest, FirstStringBeforeSecondString) {
if (GetVersion() < Version::WIN8)
return;
ASSERT_TRUE(HStringReference::ResolveCoreWinRTStringDelayload());
const HStringReference string12(kTestString12);
const HStringReference string123(kTestString123);
INT32 result;
HRESULT hr = HStringCompare(string12.Get(), string123.Get(), &result);
EXPECT_HRESULT_SUCCEEDED(hr);
EXPECT_EQ(-1, result);
}
TEST(HStringCompareTest, StringsEqual) {
if (GetVersion() < Version::WIN8)
return;
ASSERT_TRUE(HStringReference::ResolveCoreWinRTStringDelayload());
const HStringReference string123(kTestString123);
INT32 result;
HRESULT hr = HStringCompare(string123.Get(), string123.Get(), &result);
EXPECT_HRESULT_SUCCEEDED(hr);
EXPECT_EQ(0, result);
}
TEST(HStringCompareTest, FirstStringAfterSecondString) {
if (GetVersion() < Version::WIN8)
return;
ASSERT_TRUE(HStringReference::ResolveCoreWinRTStringDelayload());
const HStringReference string123(kTestString123);
const HStringReference string1234(kTestString1234);
INT32 result;
HRESULT hr = HStringCompare(string1234.Get(), string123.Get(), &result);
EXPECT_HRESULT_SUCCEEDED(hr);
EXPECT_EQ(1, result);
}
} // namespace win
} // namespace base
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