Commit 07e67cc7 authored by Jan Wilken Doerrie's avatar Jan Wilken Doerrie Committed by Commit Bot

[base] Provide ITypedEventHandler Implementation

This change provides an implementation of Windows::Foundation's
ITypedEventHandlder. It serves as a thin wrapper around a
base::RepeatingCallback of the appropriate signature.

Bug: 821766
Change-Id: Ib940ebc5a4d90e5304be2c08839efbc020011180
Reviewed-on: https://chromium-review.googlesource.com/1060054
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#559212}
parent ca207e09
...@@ -1088,6 +1088,7 @@ jumbo_component("base") { ...@@ -1088,6 +1088,7 @@ jumbo_component("base") {
"win/shortcut.h", "win/shortcut.h",
"win/startup_information.cc", "win/startup_information.cc",
"win/startup_information.h", "win/startup_information.h",
"win/typed_event_handler.h",
"win/wait_chain.cc", "win/wait_chain.cc",
"win/wait_chain.h", "win/wait_chain.h",
"win/win_util.cc", "win/win_util.cc",
...@@ -2456,6 +2457,7 @@ test("base_unittests") { ...@@ -2456,6 +2457,7 @@ test("base_unittests") {
"win/scoped_winrt_initializer_unittest.cc", "win/scoped_winrt_initializer_unittest.cc",
"win/shortcut_unittest.cc", "win/shortcut_unittest.cc",
"win/startup_information_unittest.cc", "win/startup_information_unittest.cc",
"win/typed_event_handler_unittest.cc",
"win/wait_chain_unittest.cc", "win/wait_chain_unittest.cc",
"win/win_includes_unittest.cc", "win/win_includes_unittest.cc",
"win/win_util_unittest.cc", "win/win_util_unittest.cc",
......
// Copyright 2018 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_TYPED_EVENT_HANDLER_H_
#define BASE_WIN_TYPED_EVENT_HANDLER_H_
#include <windows.foundation.collections.h>
#include <wrl/implements.h>
#include <utility>
#include "base/callback.h"
namespace base {
namespace win {
// This file provides an implementation of Windows::Foundation's
// ITypedEventHandler. It serves as a thin wrapper around a RepeatingCallback,
// that forwards the arguments to its |Invoke| method to the callback's |Run|
// method.
template <typename SenderT, typename ArgsT>
class TypedEventHandler
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
ABI::Windows::Foundation::ITypedEventHandler<SenderT, ArgsT>> {
public:
using SenderAbiT =
typename ABI::Windows::Foundation::Internal::GetAbiType<SenderT>::type;
using ArgsAbiT =
typename ABI::Windows::Foundation::Internal::GetAbiType<ArgsT>::type;
using Handler = base::RepeatingCallback<HRESULT(SenderAbiT, ArgsAbiT)>;
explicit TypedEventHandler(Handler handler) : handler_(std::move(handler)) {}
// ABI::Windows::Foundation::ITypedEventHandler:
IFACEMETHODIMP Invoke(SenderAbiT sender, ArgsAbiT args) override {
return handler_.Run(std::move(sender), std::move(args));
}
private:
Handler handler_;
};
} // namespace win
} // namespace base
#endif // BASE_WIN_TYPED_EVENT_HANDLER_H_
// Copyright 2018 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/typed_event_handler.h"
#include <windows.foundation.h>
#include "base/test/bind_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace win {
TEST(TypedEventHandlerTest, InvokeSuccess) {
bool called_callback = false;
TypedEventHandler<IInspectable*, IInspectable*> handler(
base::BindLambdaForTesting([&](IInspectable* sender, IInspectable* args) {
EXPECT_EQ(reinterpret_cast<IInspectable*>(0x01), sender);
EXPECT_EQ(reinterpret_cast<IInspectable*>(0x02), args);
called_callback = true;
return S_OK;
}));
EXPECT_FALSE(called_callback);
HRESULT hr = handler.Invoke(reinterpret_cast<IInspectable*>(0x01),
reinterpret_cast<IInspectable*>(0x02));
EXPECT_TRUE(called_callback);
EXPECT_EQ(S_OK, hr);
}
TEST(TypedEventHandlerTest, InvokeFail) {
bool called_callback = false;
TypedEventHandler<IInspectable*, IInspectable*> handler(
base::BindLambdaForTesting([&](IInspectable* sender, IInspectable* args) {
EXPECT_EQ(nullptr, sender);
EXPECT_EQ(nullptr, args);
called_callback = true;
return E_FAIL;
}));
EXPECT_FALSE(called_callback);
HRESULT hr = handler.Invoke(nullptr, nullptr);
EXPECT_TRUE(called_callback);
EXPECT_EQ(E_FAIL, hr);
}
} // 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