Introduce WrappedShareTargetDiscoveredCallback.

PiperOrigin-RevId: 638421531
This commit is contained in:
Anay Wadhera
2024-05-29 14:56:55 -07:00
committed by Copybara-Service
parent 4d21499342
commit b2efc04bb2
4 changed files with 228 additions and 0 deletions
+14
View File
@@ -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",
],
)
@@ -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 <cstdint>
#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<uint32_t>(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<uint32_t>(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<uint32_t>(blocked_vendor_id_);
return;
}
if (callback_ != nullptr) {
callback_->OnShareTargetLost(share_target);
}
}
} // namespace sharing
} // namespace nearby
@@ -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 <cstdint>
#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_
@@ -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 <cstdint>
#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