From b2efc04bb2b2267c8efb1ed48377aa622d1558ff Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Wed, 29 May 2024 14:54:41 -0700 Subject: [PATCH] Introduce WrappedShareTargetDiscoveredCallback. PiperOrigin-RevId: 638421531 --- sharing/BUILD | 14 +++ ...rapped_share_target_discovered_callback.cc | 71 ++++++++++++++ ...wrapped_share_target_discovered_callback.h | 46 +++++++++ ...d_share_target_discovered_callback_test.cc | 97 +++++++++++++++++++ 4 files changed, 228 insertions(+) create mode 100644 sharing/wrapped_share_target_discovered_callback.cc create mode 100644 sharing/wrapped_share_target_discovered_callback.h create mode 100644 sharing/wrapped_share_target_discovered_callback_test.cc diff --git a/sharing/BUILD b/sharing/BUILD index b4ed7d17..fc4c64ff 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -122,6 +122,7 @@ cc_library( "payload_tracker.cc", "share_target_info.cc", "transfer_manager.cc", + "wrapped_share_target_discovered_callback.cc", ], hdrs = [ "connection_lifecycle_listener.h", @@ -152,6 +153,7 @@ cc_library( "share_target_info.h", "transfer_manager.h", "transfer_update_callback.h", + "wrapped_share_target_discovered_callback.h", ], copts = [ "-DNEARBY_SHARING_DLL", @@ -611,3 +613,15 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "wrapped_share_target_discovered_callback_test", + srcs = ["wrapped_share_target_discovered_callback_test.cc"], + deps = [ + ":nearby_sharing_service", + ":types", + "//internal/platform/implementation/g3", # fixdeps: keep + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/sharing/wrapped_share_target_discovered_callback.cc b/sharing/wrapped_share_target_discovered_callback.cc new file mode 100644 index 00000000..5823e80a --- /dev/null +++ b/sharing/wrapped_share_target_discovered_callback.cc @@ -0,0 +1,71 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "sharing/wrapped_share_target_discovered_callback.h" + +#include + +#include "sharing/internal/public/logging.h" +#include "sharing/share_target.h" + +namespace nearby { +namespace sharing { +namespace { +constexpr uint8_t kNoVendorId = 0; +} + +bool WrappedShareTargetDiscoveredCallback::ShouldBlockShareTarget( + const ShareTarget& share_target) const { + return blocked_vendor_id_ != kNoVendorId && + share_target.vendor_id == blocked_vendor_id_; +} + +void WrappedShareTargetDiscoveredCallback::OnShareTargetDiscovered( + const ShareTarget& share_target) { + if (ShouldBlockShareTarget(share_target)) { + NL_LOG(INFO) << "Skipping share target discovered for vendor id " + << static_cast(blocked_vendor_id_); + return; + } + if (callback_ != nullptr) { + callback_->OnShareTargetDiscovered(share_target); + } +} + +void WrappedShareTargetDiscoveredCallback::OnShareTargetUpdated( + const ShareTarget& share_target) { + if (ShouldBlockShareTarget(share_target)) { + NL_LOG(INFO) << "Skipping share target updated for vendor id " + << static_cast(blocked_vendor_id_); + return; + } + if (callback_ != nullptr) { + callback_->OnShareTargetUpdated(share_target); + } +} + +void WrappedShareTargetDiscoveredCallback::OnShareTargetLost( + const ShareTarget& share_target) { + if (ShouldBlockShareTarget(share_target)) { + NL_LOG(INFO) << "Skipping share target lost for vendor id " + << static_cast(blocked_vendor_id_); + return; + } + if (callback_ != nullptr) { + callback_->OnShareTargetLost(share_target); + } +} + +} // namespace sharing +} // namespace nearby diff --git a/sharing/wrapped_share_target_discovered_callback.h b/sharing/wrapped_share_target_discovered_callback.h new file mode 100644 index 00000000..6929321f --- /dev/null +++ b/sharing/wrapped_share_target_discovered_callback.h @@ -0,0 +1,46 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_NEARBY_SHARING_WRAPPED_SHARE_TARGET_DISCOVERED_CALLBACK_H_ +#define THIRD_PARTY_NEARBY_SHARING_WRAPPED_SHARE_TARGET_DISCOVERED_CALLBACK_H_ + +#include + +#include "sharing/share_target.h" +#include "sharing/share_target_discovered_callback.h" + +namespace nearby { +namespace sharing { +// Wraps a |ShareTargetDiscoveredCallback| for vendor ID blocking. +// If the vendor ID is not default (0), it will automatically be blocked. +class WrappedShareTargetDiscoveredCallback + : public ShareTargetDiscoveredCallback { + public: + explicit WrappedShareTargetDiscoveredCallback( + ShareTargetDiscoveredCallback* callback, uint8_t blocked_vendor_id) + : callback_(callback), blocked_vendor_id_(blocked_vendor_id) {} + void OnShareTargetDiscovered(const ShareTarget& target) override; + void OnShareTargetUpdated(const ShareTarget& target) override; + void OnShareTargetLost(const ShareTarget& target) override; + + private: + bool ShouldBlockShareTarget(const ShareTarget& target) const; + + ShareTargetDiscoveredCallback* callback_; + const uint8_t blocked_vendor_id_; +}; +} // namespace sharing +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_SHARING_WRAPPED_SHARE_TARGET_DISCOVERED_CALLBACK_H_ diff --git a/sharing/wrapped_share_target_discovered_callback_test.cc b/sharing/wrapped_share_target_discovered_callback_test.cc new file mode 100644 index 00000000..5efe3ccd --- /dev/null +++ b/sharing/wrapped_share_target_discovered_callback_test.cc @@ -0,0 +1,97 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "sharing/wrapped_share_target_discovered_callback.h" + +#include + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" +#include "sharing/share_target.h" +#include "sharing/share_target_discovered_callback.h" + +namespace nearby { +namespace sharing { +namespace { + +using ::testing::_; + +class MockShareTargetDiscoveredCallback : public ShareTargetDiscoveredCallback { + public: + MOCK_METHOD(void, OnShareTargetDiscovered, (const ShareTarget&), (override)); + MOCK_METHOD(void, OnShareTargetUpdated, (const ShareTarget&), (override)); + MOCK_METHOD(void, OnShareTargetLost, (const ShareTarget&), (override)); +}; + +ShareTarget GetShareTarget(uint8_t vendor_id) { + ShareTarget share_target; + share_target.vendor_id = vendor_id; + return share_target; +} + +TEST(WrappedShareTargetDiscoveredCallbackTest, BlocksDiscoveryForSameVendorId) { + MockShareTargetDiscoveredCallback callback; + ShareTarget share_target = GetShareTarget(/*vendor_id=*/1); + WrappedShareTargetDiscoveredCallback wrapped(&callback, /*vendor_id=*/1); + EXPECT_CALL(callback, OnShareTargetDiscovered(_)).Times(0); + wrapped.OnShareTargetDiscovered(share_target); +} + +TEST(WrappedShareTargetDiscoveredCallbackTest, BlocksUpdatedForSameVendorId) { + MockShareTargetDiscoveredCallback callback; + ShareTarget share_target = GetShareTarget(/*vendor_id=*/1); + WrappedShareTargetDiscoveredCallback wrapped(&callback, /*vendor_id=*/1); + EXPECT_CALL(callback, OnShareTargetUpdated(_)).Times(0); + wrapped.OnShareTargetUpdated(share_target); +} + +TEST(WrappedShareTargetDiscoveredCallbackTest, BlocksLostForSameVendorId) { + MockShareTargetDiscoveredCallback callback; + ShareTarget share_target = GetShareTarget(/*vendor_id=*/1); + WrappedShareTargetDiscoveredCallback wrapped(&callback, /*vendor_id=*/1); + EXPECT_CALL(callback, OnShareTargetLost(_)).Times(0); + wrapped.OnShareTargetLost(share_target); +} + +TEST(WrappedShareTargetDiscoveredCallbackTest, + DoesNotBlockDiscoveryForDifferentVendorId) { + MockShareTargetDiscoveredCallback callback; + ShareTarget share_target = GetShareTarget(/*vendor_id=*/0); + WrappedShareTargetDiscoveredCallback wrapped(&callback, /*vendor_id=*/1); + EXPECT_CALL(callback, OnShareTargetLost(_)); + wrapped.OnShareTargetLost(share_target); +} + +TEST(WrappedShareTargetDiscoveredCallbackTest, + DoesNotBlockUpdatedForDifferentVendorId) { + MockShareTargetDiscoveredCallback callback; + ShareTarget share_target = GetShareTarget(/*vendor_id=*/0); + WrappedShareTargetDiscoveredCallback wrapped(&callback, /*vendor_id=*/1); + EXPECT_CALL(callback, OnShareTargetLost(_)); + wrapped.OnShareTargetLost(share_target); +} + +TEST(WrappedShareTargetDiscoveredCallbackTest, + DoesNotBlockLostForDifferentVendorId) { + MockShareTargetDiscoveredCallback callback; + ShareTarget share_target = GetShareTarget(/*vendor_id=*/0); + WrappedShareTargetDiscoveredCallback wrapped(&callback, /*vendor_id=*/1); + EXPECT_CALL(callback, OnShareTargetLost(_)); + wrapped.OnShareTargetLost(share_target); +} + +} // namespace +} // namespace sharing +} // namespace nearby