Move fast_pair_repository to third_party/nearby/fastpair/repository and remove all deprecated files

PiperOrigin-RevId: 546057062
This commit is contained in:
Qin Wang
2023-07-06 12:10:57 -07:00
committed by Copybara-Service
parent 4a185d0317
commit 5bce94d8ee
53 changed files with 142 additions and 1592 deletions
+3 -1
View File
@@ -28,7 +28,7 @@ cc_library(
"//fastpair/common",
"//fastpair/handshake",
"//fastpair/message_stream",
"//fastpair/server_access",
"//fastpair/repository",
"//internal/base",
"//internal/platform:comm",
"//internal/platform:types",
@@ -99,7 +99,9 @@ cc_library(
":fast_pair_seeker",
"//fastpair/common",
"//fastpair/internal",
"//fastpair/repository",
"//fastpair/repository:device_repository",
"//fastpair/repository:repository_impl",
"//fastpair/server_access",
"//internal/account",
"//internal/auth:oauth_lib",
+1 -1
View File
@@ -25,7 +25,7 @@
#include "fastpair/common/protocol.h"
#include "fastpair/handshake/fast_pair_data_encryptor_impl.h"
#include "fastpair/message_stream/message_stream.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/bluetooth_classic.h"
#include "internal/platform/single_thread_executor.h"
+1 -1
View File
@@ -25,7 +25,7 @@
#include "fastpair/internal/fast_pair_seeker_impl.h"
#include "fastpair/server_access/fast_pair_client_impl.h"
#include "fastpair/server_access/fast_pair_http_notifier.h"
#include "fastpair/server_access/fast_pair_repository_impl.h"
#include "fastpair/repository/fast_pair_repository_impl.h"
#include "internal/account/account_manager_impl.h"
#include "internal/auth/authentication_manager_impl.h"
#include "internal/flags/nearby_flags.h"
+1 -1
View File
@@ -26,7 +26,7 @@
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/server_access/fast_pair_client.h"
#include "fastpair/server_access/fast_pair_http_notifier.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "internal/account/account_manager.h"
#include "internal/auth/authentication_manager.h"
#include "internal/network/http_client.h"
+2 -3
View File
@@ -41,7 +41,6 @@ cc_library(
"//fastpair/dataparser",
"//fastpair/internal/mediums",
"//fastpair/repository",
"//fastpair/server_access",
"//internal/base:bluetooth_address",
"//internal/platform:comm",
"//internal/platform:logging",
@@ -88,7 +87,7 @@ cc_test(
"//fastpair/crypto",
"//fastpair/dataparser",
"//fastpair/internal/mediums",
"//fastpair/server_access:test_support",
"//fastpair/repository:test_support",
"//fastpair/testing",
"//internal/platform:logging",
"//internal/platform:types",
@@ -140,7 +139,7 @@ cc_test(
":handshake",
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/server_access:test_support",
"//fastpair/repository:test_support",
"//internal/platform:base",
"//internal/platform:test_util",
"//internal/platform:types",
@@ -37,7 +37,7 @@
#include "fastpair/crypto/fast_pair_key_pair.h"
#include "fastpair/dataparser/fast_pair_data_parser.h"
#include "fastpair/handshake/fast_pair_data_encryptor.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "internal/platform/logging.h"
namespace nearby {
@@ -34,7 +34,7 @@
#include "fastpair/common/protocol.h"
#include "fastpair/handshake/fast_pair_gatt_service_client_impl.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/medium_environment.h"
+1 -1
View File
@@ -36,7 +36,7 @@ cc_test(
"//fastpair:fast_pair_events",
"//fastpair/message_stream:fake_gatt_callbacks",
"//fastpair/message_stream:fake_provider",
"//fastpair/server_access:test_support",
"//fastpair/repository:test_support",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
@@ -27,7 +27,7 @@
#include "fastpair/fast_pair_events.h"
#include "fastpair/message_stream/fake_gatt_callbacks.h"
#include "fastpair/message_stream/fake_provider.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/medium_environment.h"
+2 -1
View File
@@ -31,7 +31,9 @@ cc_library(
"//fastpair/common",
"//fastpair/internal/mediums",
"//fastpair/pairing",
"//fastpair/repository",
"//fastpair/repository:device_repository",
"//fastpair/repository:repository_impl",
"//fastpair/scanning:scanner",
"//fastpair/server_access",
"//fastpair/ui:fast_pair_ui",
@@ -60,7 +62,6 @@ cc_test(
shard_count = 16,
deps = [
":keyed_service",
"//fastpair/server_access:test_support",
"//fastpair/testing",
"//fastpair/ui:fast_pair_ui",
"//fastpair/ui:mock_fast_pair_ui",
+1 -1
View File
@@ -28,7 +28,7 @@
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/scanner_broker_impl.h"
#include "fastpair/server_access/fast_pair_client_impl.h"
#include "fastpair/server_access/fast_pair_repository_impl.h"
#include "fastpair/repository/fast_pair_repository_impl.h"
#include "fastpair/ui/actions.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "fastpair/ui/ui_broker_impl.h"
+1 -1
View File
@@ -26,7 +26,7 @@
#include "fastpair/scanning/scanner_broker.h"
#include "fastpair/server_access/fast_pair_client.h"
#include "fastpair/server_access/fast_pair_http_notifier.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "fastpair/ui/ui_broker.h"
#include "internal/account/account_manager.h"
@@ -24,7 +24,6 @@
#include "gtest/gtest.h"
#include "absl/strings/escaping.h"
#include "absl/strings/string_view.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/testing/fast_pair_service_data_creator.h"
#include "fastpair/ui/actions.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
@@ -47,9 +46,6 @@ constexpr absl::string_view kModelId = "718c17";
constexpr absl::string_view kServiceID = "Fast Pair";
constexpr absl::string_view kFastPairServiceUuid =
"0000FE2C-0000-1000-8000-00805F9B34FB";
constexpr absl::string_view kPublicAntiSpoof =
"Wuyr48lD3txnUhGiMF1IfzlTwRxxe+wMB1HLzP+"
"0wVcljfT3XPoiy1fntlneziyLD5knDVAJSE+RM/zlPRP/Jg==";
constexpr int kNotDiscoverableAdvHeader = 0b00000110;
constexpr int kAccountKeyFilterHeader = 0b01100000;
constexpr int kSaltHeader = 0b00010001;
@@ -57,9 +53,6 @@ constexpr absl::string_view kAccountKeyFilter("112233445566");
constexpr absl::string_view kSalt("01");
constexpr absl::string_view kModelId2 = "9adb11";
constexpr absl::string_view kPublicAntiSpoof2 =
"z+grhW8lWVA34JUQhXOxMrk1WqVy+VpEDd2K+01ZJvS6KdV0OUg7FRMzq+"
"ITuOqKO/2TIRKEAEfMKdyk2Ob1Vw==";
constexpr absl::string_view kAddress = "74:74:46:01:6C:21";
class MediatorTest : public testing::Test {
@@ -88,7 +81,6 @@ class MediatorTest : public testing::Test {
void TearDown() override {
executor_.reset();
mediums_.reset();
repository_.reset();
ui_broker_.reset();
mock_ui_broker_ = nullptr;
notification_controller_.reset();
@@ -120,7 +112,6 @@ class MediatorTest : public testing::Test {
protected:
MediumEnvironment& env_{MediumEnvironment::Instance()};
std::unique_ptr<Mediums> mediums_;
std::unique_ptr<FakeFastPairRepository> repository_;
std::unique_ptr<UIBroker> ui_broker_;
std::unique_ptr<FastPairNotificationController> notification_controller_;
std::unique_ptr<SingleThreadExecutor> executor_;
+1 -1
View File
@@ -54,7 +54,7 @@ cc_test(
"//fastpair/handshake",
"//fastpair/internal/mediums",
"//fastpair/pairing/fastpair:pairing",
"//fastpair/server_access:test_support",
"//fastpair/repository:test_support",
"//internal/base:bluetooth_address",
"//internal/platform:comm",
"//internal/platform:test_util",
+1 -1
View File
@@ -51,7 +51,7 @@ cc_test(
":pairing",
"//fastpair/common",
"//fastpair/handshake",
"//fastpair/server_access:test_support",
"//fastpair/repository:test_support",
"//internal/base:bluetooth_address",
"//internal/platform:comm",
"//internal/platform:test_util",
@@ -42,7 +42,7 @@
#include "fastpair/handshake/fast_pair_data_encryptor_impl.h"
#include "fastpair/handshake/fast_pair_handshake_impl.h"
#include "fastpair/pairing/fastpair/fast_pair_pairer.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "internal/base/bluetooth_address.h"
#include "internal/platform/ble_v2.h"
#include "internal/platform/bluetooth_adapter.h"
+1 -1
View File
@@ -33,7 +33,7 @@
#include "fastpair/handshake/fast_pair_handshake_lookup.h"
#include "fastpair/internal/mediums/mediums.h"
#include "fastpair/pairing/fastpair/fast_pair_pairer.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "internal/base/bluetooth_address.h"
#include "internal/platform/ble_v2.h"
#include "internal/platform/count_down_latch.h"
+1 -1
View File
@@ -56,7 +56,7 @@ cc_library(
deps = [
":fastpair_cc_proto",
"//fastpair/common",
"//fastpair/server_access",
"//fastpair/repository",
"//internal/platform:logging",
"@com_google_absl//absl/time",
],
+1 -1
View File
@@ -25,7 +25,7 @@
#include "fastpair/proto/enum.proto.h"
#include "fastpair/proto/fast_pair_string.proto.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "internal/platform/logging.h"
namespace nearby {
+92 -33
View File
@@ -3,14 +3,10 @@ licenses(["notice"])
cc_library(
name = "repository",
srcs = [
"fast_pair_metadata_fetcher_impl.cc",
"fast_pair_metadata_repository_impl.cc",
"fast_pair_repository.cc",
],
hdrs = [
"fast_pair_metadata_fetcher.h",
"fast_pair_metadata_fetcher_impl.h",
"fast_pair_metadata_repository.h",
"fast_pair_metadata_repository_impl.h",
"fast_pair_repository.h",
],
copts = [
"-Ithird_party",
@@ -18,16 +14,42 @@ cc_library(
visibility = ["//fastpair:__subpackages__"],
deps = [
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"//internal/network:types",
"//internal/platform:logging",
"//internal/platform/implementation:types",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status:statusor",
"//internal/base:bluetooth_address",
"//internal/crypto",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/strings",
],
)
cc_library(
name = "repository_impl",
srcs = [
"fast_pair_repository_impl.cc",
],
hdrs = [
"fast_pair_repository_impl.h",
],
compatible_with = ["//buildenv/target:non_prod"],
copts = [
"-Ithird_party",
],
visibility = ["//fastpair:__subpackages__"],
deps = [
":repository",
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/proto:proto_builder",
"//fastpair/server_access",
"//internal/base:bluetooth_address",
"//internal/crypto",
"//internal/platform:logging",
"//internal/platform:types",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
],
)
cc_library(
name = "device_repository",
srcs = [
@@ -47,6 +69,41 @@ cc_library(
],
)
cc_library(
name = "test_support",
srcs = ["fake_fast_pair_repository.cc"],
hdrs = [
"fake_fast_pair_repository.h",
],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
":repository",
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"//internal/platform:types",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
],
)
cc_library(
name = "mocks",
testonly = 1,
hdrs = [
"mock_fast_pair_repository.h",
],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
":repository",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_for_library_testonly",
],
)
cc_test(
name = "device_repository_test",
srcs = [
@@ -62,24 +119,10 @@ cc_test(
],
)
cc_library(
name = "fake_fast_pair_metadata_repository",
hdrs = ["fake_fast_pair_metadata_repository.h"],
copts = [
"-Ithird_party",
],
visibility = ["//visibility:public"],
deps = [
":repository",
"//fastpair/proto:fastpair_cc_proto",
],
)
cc_test(
name = "repository_test",
name = "fast_pair_repository_test",
srcs = [
"fast_pair_metadata_fetcher_impl_test.cc",
"fast_pair_metadata_repository_impl_test.cc",
"fast_pair_repository_test.cc",
],
copts = [
"-Ithird_party",
@@ -87,14 +130,30 @@ cc_test(
deps = [
":repository",
"//fastpair/common",
"//fastpair/internal/test:nearby_fastpair_test",
"//fastpair/proto:fastpair_cc_proto",
"//internal/network:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "fast_pair_repository_impl_test",
srcs = [
"fast_pair_repository_impl_test.cc",
],
copts = [
"-Ithird_party",
],
deps = [
":repository",
":repository_impl",
":test_support",
"//fastpair/common",
"//fastpair/server_access:test_support",
"//internal/platform:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
@@ -1,82 +0,0 @@
// Copyright 2022 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_FASTPAIR_REPOSITORY_FAKE_FAST_PAIR_METADATA_REPOSITORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAKE_FAST_PAIR_METADATA_REPOSITORY_H_
#include <memory>
#include <utility>
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
namespace nearby {
namespace fastpair {
class FakeFastPairMetadataRepository : public FastPairMetadataRepository {
public:
struct GetObservedDeviceRequest {
GetObservedDeviceRequest(const proto::GetObservedDeviceRequest& request,
ObservedDeviceCallback callback,
ErrorCallback error_callback)
: request(request),
callback(std::move(callback)),
error_callback(std::move(error_callback)) {}
GetObservedDeviceRequest(GetObservedDeviceRequest&& request) = default;
~GetObservedDeviceRequest() = default;
proto::GetObservedDeviceRequest request;
ObservedDeviceCallback callback;
ErrorCallback error_callback;
};
FakeFastPairMetadataRepository() = default;
~FakeFastPairMetadataRepository() override = default;
std::unique_ptr<GetObservedDeviceRequest>& get_observed_device_request() {
return get_observed_device_request_;
}
private:
void GetObservedDevice(const proto::GetObservedDeviceRequest& request,
ObservedDeviceCallback response_callback,
ErrorCallback error_callback) override {
get_observed_device_request_ = std::make_unique<GetObservedDeviceRequest>(
request, std::move(response_callback), std::move(error_callback));
}
std::unique_ptr<GetObservedDeviceRequest> get_observed_device_request_;
};
class FakeFastPairMetadataRepositoryFactory
: public FastPairMetadataRepositoryFactory {
public:
FakeFastPairMetadataRepositoryFactory() = default;
~FakeFastPairMetadataRepositoryFactory() override = default;
FakeFastPairMetadataRepository* fake_repository() { return fake_repository_; }
private:
std::unique_ptr<FastPairMetadataRepository> CreateInstance() override {
auto fake_repository = std::make_unique<FakeFastPairMetadataRepository>();
fake_repository_ = fake_repository.get();
return fake_repository;
}
FakeFastPairMetadataRepository* fake_repository_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAKE_FAST_PAIR_METADATA_REPOSITORY_H_
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include <memory>
#include <string>
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAKE_FAST_PAIR_REPOSITORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAKE_FAST_PAIR_REPOSITORY_H_
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAKE_FAST_PAIR_REPOSITORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAKE_FAST_PAIR_REPOSITORY_H_
#include <memory>
#include <string>
@@ -21,7 +21,7 @@
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "internal/platform/single_thread_executor.h"
namespace nearby {
@@ -49,4 +49,4 @@ class FakeFastPairRepository : public FastPairRepository {
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAKE_FAST_PAIR_REPOSITORY_H_
#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAKE_FAST_PAIR_REPOSITORY_H_
@@ -1,60 +0,0 @@
// Copyright 2022 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_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_FETCHER_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_FETCHER_H_
#include <functional>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/fast_pair_http_result.h"
#include "internal/network/http_client.h"
#include "internal/network/url.h"
namespace nearby {
namespace fastpair {
class FastPairMetadataFetcher {
public:
using ResultCallback = std::function<void(absl::string_view result_response)>;
using ErrorCallback = std::function<void(FastPairHttpError error)>;
using QueryParameters = std::vector<std::pair<std::string, std::string>>;
FastPairMetadataFetcher() = default;
virtual ~FastPairMetadataFetcher() = default;
// Starts the API GET request call.
// |request_url|: The URL endpoint of the API request.
// |request_as_query_parameters|: The request proto represented as key-value
// pairs to be sent as query parameters.
// Note: A key can have multiple values.
// |http_client|: The HTTP client is used to access backend APIs.
// |result_callback|: Called when the flow completes successfully
// with a serialized response proto.
// |error_callback|: Called when the flow completes with an error.
virtual void StartGetUnauthRequest(
const network::Url& request_url,
const QueryParameters& request_as_query_parameters,
network::HttpClient* http_client, ResultCallback result_callback,
ErrorCallback error_callback) = 0;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_FETCHER_H_
@@ -1,113 +0,0 @@
// Copyright 2022 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 "fastpair/repository/fast_pair_metadata_fetcher_impl.h"
#include <optional>
#include <string>
#include "absl/strings/string_view.h"
#include "fastpair/common/fast_pair_http_result.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
namespace {
const char kQueryParameterAlternateOutputKey[] = "alt";
const char kQueryParameterAlternateOutputProto[] = "proto";
const char kPlatformTypeHeaderName[] = "X-Sharing-Platform-Type";
absl::string_view GetPlatformTypeString(api::DeviceInfo::OsType os_type) {
switch (os_type) {
case api::DeviceInfo::OsType::kAndroid:
return "OSType.ANDROID";
case api::DeviceInfo::OsType::kChromeOs:
return "OSType.CHROME_OS";
case api::DeviceInfo::OsType::kIos:
return "OSType.IOS";
case api::DeviceInfo::OsType::kWindows:
return "OSType.WINDOWS";
default:
return "OSType.UNKNOWN";
}
}
} // namespace
void FastPairMetadataFetcherImpl::StartGetUnauthRequest(
const network::Url& request_url,
const QueryParameters& request_as_query_parameters,
network::HttpClient* http_client, ResultCallback result_callback,
ErrorCallback error_callback) {
request_url_ = request_url;
request_as_query_parameters_ = request_as_query_parameters;
http_client_ = http_client;
result_callback_ = result_callback;
error_callback_ = error_callback;
Execute();
}
void FastPairMetadataFetcherImpl::ProcessApiCallSuccess(
const network::HttpResponse* response) {
network::HttpResponseBody response_body = response->GetBody();
result_callback_(response_body.GetRawData());
}
void FastPairMetadataFetcherImpl::ProcessApiCallFailure(absl::Status status) {
std::optional<FastPairHttpError> error;
std::string error_message;
error = FastPairHttpErrorForHttpResponseCode(status);
NEARBY_LOGS(ERROR) << "Fetcher failed: "
<< FastPairHttpStatus(status).ToString();
if (status.code() == absl::StatusCode::kFailedPrecondition) {
error = FastPairHttpError::kHttpErrorOffline;
}
error_callback_(*error);
}
void FastPairMetadataFetcherImpl::Execute() {
network::HttpRequest request{request_url_};
request.AddQueryParameter(kQueryParameterAlternateOutputKey,
kQueryParameterAlternateOutputProto);
if (request_as_query_parameters_) {
for (const auto& key_value_pair : *request_as_query_parameters_) {
request.AddQueryParameter(key_value_pair.first, key_value_pair.second);
}
}
request.AddHeader(kPlatformTypeHeaderName, GetPlatformTypeString(os_type_));
request.AddHeader("Content-Type", std::string());
// Handle request body
request.SetBody(std::string());
http_client_->StartRequest(
request, [&](const absl::StatusOr<network::HttpResponse>& response) {
response_ = response;
if (response_.ok()) {
body_string_ = std::string(response_->GetBody().GetRawData());
ProcessApiCallSuccess(&response_.value());
} else {
ProcessApiCallFailure(response.status());
}
});
}
} // namespace fastpair
} // namespace nearby
@@ -1,84 +0,0 @@
// Copyright 2022 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_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_FETCHER_IMPL_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_FETCHER_IMPL_H_
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "fastpair/repository/fast_pair_metadata_fetcher.h"
#include "internal/network/http_client.h"
#include "internal/network/url.h"
#include "internal/platform/implementation/device_info.h"
namespace nearby {
namespace fastpair {
class FastPairMetadataFetcherImpl : public FastPairMetadataFetcher {
public:
explicit FastPairMetadataFetcherImpl(api::DeviceInfo::OsType os_type)
: os_type_(os_type) {}
~FastPairMetadataFetcherImpl() override = default;
FastPairMetadataFetcherImpl(const FastPairMetadataFetcherImpl&) = delete;
FastPairMetadataFetcherImpl& operator=(const FastPairMetadataFetcherImpl&) =
delete;
// FastPairApiCallFlow
void StartGetUnauthRequest(const network::Url& request_url,
const QueryParameters& request_as_query_parameters,
network::HttpClient* http_client,
ResultCallback result_callback,
ErrorCallback error_callback) override;
protected:
network::HttpRequestMethod GetRequestMethod() const;
void ProcessApiCallSuccess(const network::HttpResponse* response);
void ProcessApiCallFailure(absl::Status status);
void Execute();
private:
// The URL of the endpoint serving the request.
network::Url request_url_;
// The request message proto represented as key-value pairs that will be sent
// as query parameters in the API GET request. Note: A key can have multiple
// values.
std::optional<QueryParameters> request_as_query_parameters_;
// Callback invoked with the serialized response message proto when the flow
// completes successfully.
ResultCallback result_callback_;
// Callback invoked with an error message when the flow fails.
ErrorCallback error_callback_;
// Http client to execute request
network::HttpClient* http_client_ = nullptr;
// Store a copy of HTTP response
absl::StatusOr<network::HttpResponse> response_;
std::string body_string_;
// Used to indicate the OS type.
const api::DeviceInfo::OsType os_type_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_FETCHER_IMPL_H_
@@ -1,184 +0,0 @@
// Copyright 2022 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 "fastpair/repository/fast_pair_metadata_fetcher_impl.h"
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "gtest/gtest.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "fastpair/internal/test/fast_pair_fake_http_client.h"
namespace nearby {
namespace fastpair {
namespace {
using ::nearby::network::FastPairFakeHttpClient;
using ::nearby::network::Url;
using OsType = ::nearby::api::DeviceInfo::OsType;
constexpr char kResponseProto[] = "result_proto";
constexpr char kRequestUrl[] = "https://googleapis.com/nearbysharing/test";
constexpr char kQueryParameterAlternateOutputKey[] = "alt";
constexpr char kQueryParameterAlternateOutputProto[] = "proto";
const FastPairMetadataFetcher::QueryParameters&
GetTestRequestProtoAsQueryParameters() {
static const FastPairMetadataFetcher::QueryParameters*
request_as_query_parameters =
new FastPairMetadataFetcher::QueryParameters(
{{"key1", "value1_1"}, {"key1", "value1_2"}, {"key2", "value2"}});
return *request_as_query_parameters;
}
// Adds the key-value pairs of |request_as_query_parameters| as query
// parameters.|request_as_query_parameters| is only non-null.
Url UrlWithQueryParameters(
absl::string_view url,
const std::optional<FastPairMetadataFetcher::QueryParameters>&
request_as_query_parameters) {
auto url_with_qp = Url::Create(url);
EXPECT_TRUE(url_with_qp.ok());
url_with_qp->AddQueryParameter(kQueryParameterAlternateOutputKey,
kQueryParameterAlternateOutputProto);
if (request_as_query_parameters) {
for (const auto& key_value : *request_as_query_parameters) {
url_with_qp->AddQueryParameter(key_value.first, key_value.second);
}
}
return url_with_qp.value();
}
class FastPairMetadataFetcherImplTest : public ::testing::Test {
protected:
FastPairMetadataFetcherImplTest() {
http_client_ = std::make_unique<FastPairFakeHttpClient>();
}
void SetUp() override {
result_ = nullptr;
network_error_ = nullptr;
}
void StartGetUnauthRequest() {
StartGetUnauthRequestWithRequestAsQueryParameters(
GetTestRequestProtoAsQueryParameters());
}
void OnResult(absl::string_view result) {
EXPECT_FALSE(result_ || network_error_);
result_ = std::make_unique<std::string>(result);
}
void OnError(FastPairHttpError network_error) {
EXPECT_FALSE(result_ || network_error_);
network_error_ = std::make_unique<FastPairHttpError>(network_error);
}
void StartGetUnauthRequestWithRequestAsQueryParameters(
const FastPairMetadataFetcher::QueryParameters&
request_as_query_parameters) {
auto url = Url::Create(kRequestUrl);
ASSERT_TRUE(url.ok());
flow_.StartGetUnauthRequest(
url.value(), request_as_query_parameters, http_client_.get(),
[&](absl::string_view response) { OnResult(response); },
[&](FastPairHttpError error) { OnError(error); });
CheckFastPairRepositoryGetUnauthRequest(request_as_query_parameters);
}
void CheckPlatformTypeHeader(
const absl::flat_hash_map<std::string, std::vector<std::string>>&
headers) {
auto platform_type =
GetHeaderFirstValue("X-Sharing-Platform-Type", headers);
EXPECT_EQ("OSType.CHROME_OS", platform_type);
}
std::string GetHeaderFirstValue(
absl::string_view key,
const absl::flat_hash_map<std::string, std::vector<std::string>>&
headers) {
auto it = headers.find(key);
while (it != headers.end()) {
return it->second[0];
}
return "";
}
void CheckFastPairRepositoryGetUnauthRequest(
const FastPairMetadataFetcher::QueryParameters&
request_as_query_parameters) {
EXPECT_EQ(http_client_->GetPendingRequest().size(), 1);
const network::HttpRequest& request =
http_client_->GetPendingRequest()[0].request;
CheckPlatformTypeHeader(request.GetAllHeaders());
EXPECT_EQ(UrlWithQueryParameters(kRequestUrl, request_as_query_parameters),
request.GetUrl());
EXPECT_EQ("GET", request.GetMethodString());
// Expect no body.
auto body = request.GetBody();
EXPECT_TRUE(body.empty());
auto content_type =
GetHeaderFirstValue("Content-Type", request.GetAllHeaders());
EXPECT_EQ("", content_type);
}
void CompleteGetUnauthRequest(
int error,
std::optional<network::HttpStatusCode> response_code = std::nullopt,
const std::optional<std::string>& response_string = std::nullopt) {
http_client_->CompleteRequest(error, response_code, response_string);
EXPECT_TRUE(result_ || network_error_);
}
std::unique_ptr<std::string> result_;
std::unique_ptr<FastPairHttpError> network_error_;
private:
std::unique_ptr<FastPairFakeHttpClient> http_client_;
FastPairMetadataFetcherImpl flow_{api::DeviceInfo::OsType::kChromeOs};
};
TEST_F(FastPairMetadataFetcherImplTest, GetUnauthRequestSuccess) {
StartGetUnauthRequest();
CompleteGetUnauthRequest(0, network::HttpStatusCode::kHttpOk, kResponseProto);
EXPECT_EQ(*result_, kResponseProto);
EXPECT_FALSE(network_error_);
}
TEST_F(FastPairMetadataFetcherImplTest, GetUnauthRequestFailure) {
StartGetUnauthRequest();
CompleteGetUnauthRequest(0xff00);
EXPECT_FALSE(result_);
EXPECT_EQ(*network_error_, FastPairHttpError::kHttpErrorOffline);
}
TEST_F(FastPairMetadataFetcherImplTest, GetUnauthRequestStatus500) {
StartGetUnauthRequest();
CompleteGetUnauthRequest(0, network::HttpStatusCode::kHttpInternalServerError,
"Fast Pair Meltdown.");
EXPECT_FALSE(result_);
EXPECT_EQ(*network_error_, FastPairHttpError::kHttpErrorInternal);
}
} // namespace
} // namespace fastpair
} // namespace nearby
@@ -1,60 +0,0 @@
// Copyright 2022 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_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_REPOSITORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_REPOSITORY_H_
#include <functional>
#include <memory>
#include "absl/strings/string_view.h"
#include "fastpair/common/fast_pair_http_result.h"
namespace nearby {
namespace fastpair {
namespace proto {
class GetObservedDeviceRequest;
class GetObservedDeviceResponse;
} // namespace proto
class FastPairMetadataRepository {
public:
using ErrorCallback = std::function<void(FastPairHttpError)>;
using ObservedDeviceCallback =
std::function<void(const proto::GetObservedDeviceResponse&)>;
FastPairMetadataRepository() = default;
virtual ~FastPairMetadataRepository() = default;
virtual void GetObservedDevice(const proto::GetObservedDeviceRequest& request,
ObservedDeviceCallback callback,
ErrorCallback error_callback) = 0;
};
// Interface for creating FastPairMetadataRepository instances to increase the
// flexibility
class FastPairMetadataRepositoryFactory {
public:
FastPairMetadataRepositoryFactory() = default;
virtual ~FastPairMetadataRepositoryFactory() = default;
virtual std::unique_ptr<FastPairMetadataRepository> CreateInstance() = 0;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_REPOSITORY_H_
@@ -1,140 +0,0 @@
// Copyright 2022 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 "fastpair/repository/fast_pair_metadata_repository_impl.h"
#include <algorithm>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/fast_pair_http_result.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/repository/fast_pair_metadata_fetcher.h"
#include "fastpair/repository/fast_pair_metadata_fetcher_impl.h"
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "internal/network/http_client.h"
#include "internal/network/http_client_factory.h"
#include "internal/network/url.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
const char kDefaultNearbyDeviceV1HTTPHost[] =
"https://nearbydevices-pa.googleapis.com";
const char kNearbyShareV1Path[] = "v1";
const char kListDevicePath[] = "device";
const char kClientId[] = "AIzaSyBv7ZrOlX5oIJLVQrZh-WkZFKm5L6FlStQ";
const char kMode[] = "mode";
const char* GetObservedDeviceMode[3] = {"MODE_UNKNOWN", "MODE_RELEASE",
"MODE_DEBUG"};
FastPairMetadataRepositoryImpl::FastPairMetadataRepositoryImpl(
std::unique_ptr<FastPairMetadataFetcher> fetcher,
std::unique_ptr<network::HttpClient> http_client)
: fetcher_(std::move(fetcher)), http_client_(std::move(http_client)) {}
FastPairMetadataRepositoryImpl::~FastPairMetadataRepositoryImpl() = default;
// Create full fastpair v1 URL
nearby::network::Url CreateV1DeviceRequestUrlPath(
const proto::GetObservedDeviceRequest& request) {
std::string path = absl::StrFormat(
"%s/%s/%s/%s", kDefaultNearbyDeviceV1HTTPHost, kNearbyShareV1Path,
kListDevicePath, std::to_string(request.device_id()));
return nearby::network::Url::Create(path).value();
}
FastPairMetadataFetcher::QueryParameters
GetObservedDeviceRequestToQueryParameters(
const proto::GetObservedDeviceRequest& request) {
FastPairMetadataFetcher::QueryParameters params;
params.push_back({"key", kClientId});
params.push_back({kMode, GetObservedDeviceMode[request.mode()]});
return params;
}
void FastPairMetadataRepositoryImpl::GetObservedDevice(
const proto::GetObservedDeviceRequest& request,
ObservedDeviceCallback callback, ErrorCallback error_callback) {
ObservedDeviceCallback new_callback =
[=](const proto::GetObservedDeviceResponse& response) {
callback(response);
};
ErrorCallback new_error_callback = [=](FastPairHttpError error) {
error_callback(error);
};
MakeUnauthFetcher(CreateV1DeviceRequestUrlPath(request),
GetObservedDeviceRequestToQueryParameters(request),
new_callback, new_error_callback);
}
void FastPairMetadataRepositoryImpl::MakeUnauthFetcher(
const nearby::network::Url& request_url,
const std::optional<FastPairMetadataFetcher::QueryParameters>&
request_as_query_parameters,
ObservedDeviceCallback response_callback, ErrorCallback error_callback) {
has_call_started_ = true;
request_url_ = request_url;
error_callback_ = std::move(error_callback);
fetcher_->StartGetUnauthRequest(
request_url_, *request_as_query_parameters, http_client_.get(),
[&, response_callback =
std::move(response_callback)](absl::string_view result_response) {
OnFetcherSuccess(response_callback, result_response);
},
[&](FastPairHttpError error) { OnFetcherFailed(error); });
}
void FastPairMetadataRepositoryImpl::OnFetcherSuccess(
ObservedDeviceCallback callback, absl::string_view result_response) {
proto::GetObservedDeviceResponse response;
if (!response.ParseFromString(result_response)) {
OnFetcherFailed(FastPairHttpError::kHttpErrorOtherFailure);
return;
}
callback(response);
}
void FastPairMetadataRepositoryImpl::OnFetcherFailed(FastPairHttpError error) {
NEARBY_LOGS(ERROR)
<< "Fast pair server accessing RPC call failed with error. " << error;
error_callback_(error);
}
FastPairMetadataRepositoryFactoryImpl::FastPairMetadataRepositoryFactoryImpl(
network::HttpClientFactory* http_client_factory)
: http_client_factory_(http_client_factory) {}
FastPairMetadataRepositoryFactoryImpl::
~FastPairMetadataRepositoryFactoryImpl() = default;
std::unique_ptr<FastPairMetadataRepository>
FastPairMetadataRepositoryFactoryImpl::CreateInstance() {
NEARBY_LOGS(VERBOSE) << "Device Type is hardcode to kWindows.";
return std::make_unique<FastPairMetadataRepositoryImpl>(
std::make_unique<FastPairMetadataFetcherImpl>(
api::DeviceInfo::OsType::kWindows),
http_client_factory_->CreateInstance());
}
} // namespace fastpair
} // namespace nearby
@@ -1,89 +0,0 @@
// Copyright 2022 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_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_REPOSITORY_IMPL_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_REPOSITORY_IMPL_H_
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "fastpair/repository/fast_pair_metadata_fetcher.h"
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "internal/network/http_client.h"
#include "internal/network/http_client_factory.h"
namespace nearby {
namespace fastpair {
class FastPairMetadataRepositoryImpl : public FastPairMetadataRepository {
public:
FastPairMetadataRepositoryImpl(
std::unique_ptr<FastPairMetadataFetcher> fetcher,
std::unique_ptr<network::HttpClient> http_client);
~FastPairMetadataRepositoryImpl() override;
FastPairMetadataRepositoryImpl(FastPairMetadataRepositoryImpl&) = delete;
FastPairMetadataRepositoryImpl& operator=(FastPairMetadataRepositoryImpl&) =
delete;
void GetObservedDevice(const proto::GetObservedDeviceRequest& request,
ObservedDeviceCallback response_callback,
ErrorCallback error_callback) override;
private:
void MakeUnauthFetcher(
const nearby::network::Url& request_url,
const std::optional<FastPairMetadataFetcher::QueryParameters>&
request_as_query_parameters,
ObservedDeviceCallback response_callback, ErrorCallback error_callback);
// Called when the fetcher success fails at any step.
void OnFetcherSuccess(ObservedDeviceCallback callback,
absl::string_view result_response);
// Called when the fetcher fails at any step.
void OnFetcherFailed(FastPairHttpError error);
bool has_call_started_;
std::unique_ptr<FastPairMetadataFetcher> fetcher_;
std::unique_ptr<nearby::network::HttpClient> http_client_;
nearby::network::Url request_url_;
ErrorCallback error_callback_;
};
class FastPairMetadataRepositoryFactoryImpl
: public FastPairMetadataRepositoryFactory {
public:
explicit FastPairMetadataRepositoryFactoryImpl(
network::HttpClientFactory* http_client_factory);
~FastPairMetadataRepositoryFactoryImpl() override;
FastPairMetadataRepositoryFactoryImpl(
FastPairMetadataRepositoryFactoryImpl&) = delete;
FastPairMetadataRepositoryFactoryImpl& operator=(
FastPairMetadataRepositoryFactoryImpl&) = delete;
std::unique_ptr<FastPairMetadataRepository> CreateInstance() override;
private:
network::HttpClientFactory* http_client_factory_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_METADATA_REPOSITORY_IMPL_H_
@@ -1,156 +0,0 @@
// Copyright 2022 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 "fastpair/repository/fast_pair_metadata_repository_impl.h"
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "fastpair/common/fast_pair_http_result.h"
#include "fastpair/internal/test/fast_pair_fake_http_client.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/repository/fast_pair_metadata_fetcher.h"
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "internal/network/url.h"
namespace nearby {
namespace fastpair {
using ::nearby::network::HttpClient;
using ::nearby::network::Url;
const char kImageUrl[] = "https://example.com/image.jpg";
const char kListDevicePath[] = "device/";
const char kClientId[] = "AIzaSyBv7ZrOlX5oIJLVQrZh-WkZFKm5L6FlStQ";
const char kTestGoogleApisUrl[] = "https://nearbydevices-pa.googleapis.com";
constexpr int64_t kDeviceId = 10148625;
const char kDeviceName[] = "devicename";
class FakeFastPairMetadataFetcher : public FastPairMetadataFetcher {
public:
FakeFastPairMetadataFetcher() = default;
~FakeFastPairMetadataFetcher() override = default;
FakeFastPairMetadataFetcher(const FakeFastPairMetadataFetcher&) = delete;
FakeFastPairMetadataFetcher& operator=(const FakeFastPairMetadataFetcher&) =
delete;
void StartGetUnauthRequest(const Url& request_url,
const QueryParameters& request_as_query_parameters,
HttpClient* http_client,
ResultCallback result_callback,
ErrorCallback error_callback) override {
request_url_ = request_url;
request_as_query_parameters_ = request_as_query_parameters;
http_client_ = http_client;
result_callback_ = result_callback;
error_callback_ = error_callback;
}
Url request_url_;
QueryParameters request_as_query_parameters_;
HttpClient* http_client_;
ResultCallback result_callback_;
ErrorCallback error_callback_;
};
std::vector<std::string> ExpectQueryStringValues(
const FastPairMetadataFetcher::QueryParameters& query_parameters,
absl::string_view key) {
std::vector<std::string> values;
for (const auto& pair : query_parameters) {
const auto& query_key = pair.first;
const auto& query_value = pair.second;
if (query_key == key) {
values.push_back(query_value);
}
}
EXPECT_GT(values.size(), 0);
return values;
}
class FastPairMetadataRepositoryImplTest : public ::testing::Test {
protected:
FastPairMetadataRepositoryImplTest() = default;
void SetUp() override {
http_client_ = std::make_unique<network::FastPairFakeHttpClient>();
auto fetcher = std::make_unique<FakeFastPairMetadataFetcher>();
fetcher_ = fetcher.get();
repository_ = std::make_unique<FastPairMetadataRepositoryImpl>(
std::move(fetcher), std::move(http_client_));
}
const FastPairMetadataFetcher::QueryParameters&
request_as_query_parameters() {
return fetcher_->request_as_query_parameters_;
}
const Url& request_url() { return fetcher_->request_url_; }
Url GetUrl(absl::string_view path) {
return Url::Create(
absl::StrCat(kTestGoogleApisUrl, "/v1/", kListDevicePath, path))
.value();
}
FakeFastPairMetadataFetcher* fetcher_;
std::unique_ptr<HttpClient> http_client_;
std::unique_ptr<FastPairMetadataRepository> repository_;
Url request_url_;
FastPairMetadataFetcher::QueryParameters request_as_query_parameters_;
FastPairMetadataFetcher::ResultCallback result_callback_;
FastPairMetadataFetcher::ErrorCallback error_callback_;
};
TEST_F(FastPairMetadataRepositoryImplTest, GetObservedDeviceSuccess) {
proto::GetObservedDeviceRequest request;
proto::GetObservedDeviceResponse result;
request.set_device_id(kDeviceId);
request.set_mode(proto::GetObservedDeviceRequest::MODE_RELEASE);
repository_->GetObservedDevice(
request,
[&result](const proto::GetObservedDeviceResponse& response) {
result = response;
},
[&](FastPairHttpError error) {});
EXPECT_EQ(request_url(), GetUrl(std::to_string(request.device_id())));
EXPECT_EQ(std::vector<std::string>{"MODE_RELEASE"},
ExpectQueryStringValues(request_as_query_parameters(), "mode"));
EXPECT_EQ(std::vector<std::string>{kClientId},
ExpectQueryStringValues(request_as_query_parameters(), "key"));
proto::GetObservedDeviceResponse test_response;
test_response.mutable_device()->set_id(kDeviceId);
test_response.mutable_device()->set_name(kDeviceName);
test_response.mutable_device()->set_image_url(kImageUrl);
EXPECT_EQ(test_response.device().id(), kDeviceId);
EXPECT_EQ(test_response.device().name(), kDeviceName);
EXPECT_EQ(test_response.device().image_url(), kImageUrl);
}
} // namespace fastpair
} // namespace nearby
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include <array>
#include <string>
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAST_PAIR_REPOSITORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAST_PAIR_REPOSITORY_H_
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_REPOSITORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_REPOSITORY_H_
#include <functional>
#include <optional>
@@ -49,4 +49,4 @@ class FastPairRepository {
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAST_PAIR_REPOSITORY_H_
#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_REPOSITORY_H_
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/server_access/fast_pair_repository_impl.h"
#include "fastpair/repository/fast_pair_repository_impl.h"
#include <memory>
#include <optional>
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAST_PAIR_REPOSITORY_IMPL_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAST_PAIR_REPOSITORY_IMPL_H_
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_REPOSITORY_IMPL_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_REPOSITORY_IMPL_H_
#include <memory>
#include <string>
@@ -21,7 +21,7 @@
#include "absl/strings/string_view.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/server_access/fast_pair_client.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "internal/platform/single_thread_executor.h"
namespace nearby {
@@ -48,4 +48,4 @@ class FastPairRepositoryImpl : public FastPairRepository {
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAST_PAIR_REPOSITORY_IMPL_H_
#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_FAST_PAIR_REPOSITORY_IMPL_H_
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/server_access/fast_pair_repository_impl.h"
#include "fastpair/repository/fast_pair_repository_impl.h"
#include <memory>
#include <optional>
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "gtest/gtest.h"
#include "absl/strings/escaping.h"
@@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_MOCK_FAST_PAIR_REPOSITORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_MOCK_FAST_PAIR_REPOSITORY_H_
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_MOCK_FAST_PAIR_REPOSITORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_MOCK_FAST_PAIR_REPOSITORY_H_
#include "gmock/gmock.h"
#include "absl/strings/string_view.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
namespace nearby {
namespace fastpair {
@@ -33,4 +33,4 @@ class MockFastPairRepository : public FastPairRepository {
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_MOCK_FAST_PAIR_REPOSITORY_H_
#endif // THIRD_PARTY_NEARBY_FASTPAIR_REPOSITORY_MOCK_FAST_PAIR_REPOSITORY_H_
+1 -1
View File
@@ -59,7 +59,7 @@ cc_test(
"//fastpair/message_stream:fake_gatt_callbacks",
"//fastpair/message_stream:fake_provider",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/server_access:test_support",
"//fastpair/repository:test_support",
"//internal/base:bluetooth_address",
"//internal/platform:base",
"//internal/platform:comm",
+1 -1
View File
@@ -26,7 +26,7 @@
#include "fastpair/message_stream/fake_gatt_callbacks.h"
#include "fastpair/message_stream/fake_provider.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "internal/base/bluetooth_address.h"
#include "internal/platform/bluetooth_classic.h"
#include "internal/platform/byte_array.h"
+1 -1
View File
@@ -68,7 +68,7 @@ cc_test(
"//fastpair/common",
"//fastpair/internal/mediums",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/server_access:test_support",
"//fastpair/repository:test_support",
"//internal/platform:base",
"//internal/platform:test_util",
"//internal/platform:types",
+1 -2
View File
@@ -37,7 +37,6 @@ cc_library(
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/repository",
"//fastpair/repository:device_repository",
"//fastpair/server_access",
"//internal/base",
"//internal/platform:base",
"//internal/platform:comm",
@@ -104,7 +103,7 @@ cc_test(
":scanning",
":test_support",
"//fastpair/repository:device_repository",
"//fastpair/server_access:test_support",
"//fastpair/repository:test_support",
"//fastpair/testing",
"//internal/platform:comm",
"//internal/platform/implementation/g3", # build_cleaner: keep
@@ -30,7 +30,7 @@
#include "fastpair/common/protocol.h"
#include "fastpair/dataparser/fast_pair_data_parser.h"
#include "fastpair/proto/fastpair_rpcs.pb.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
@@ -22,7 +22,7 @@
#include "absl/synchronization/notification.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/scanning/fastpair/fake_fast_pair_scanner.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "fastpair/testing/fast_pair_service_data_creator.h"
#include "internal/platform/bluetooth_adapter.h"
@@ -25,7 +25,7 @@
#include "fastpair/internal/mediums/mediums.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/scanning/scanner_broker.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/medium_environment.h"
-92
View File
@@ -19,19 +19,11 @@ cc_library(
srcs = [
"fast_pair_client_impl.cc",
"fast_pair_http_notifier.cc",
"fast_pair_metadata_downloader.cc",
"fast_pair_metadata_downloader_impl.cc",
"fast_pair_repository.cc",
"fast_pair_repository_impl.cc",
],
hdrs = [
"fast_pair_client.h",
"fast_pair_client_impl.h",
"fast_pair_http_notifier.h",
"fast_pair_metadata_downloader.h",
"fast_pair_metadata_downloader_impl.h",
"fast_pair_repository.h",
"fast_pair_repository_impl.h",
],
visibility = [
"//fastpair:__subpackages__",
@@ -40,17 +32,12 @@ cc_library(
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/proto:proto_to_json",
"//fastpair/repository",
"//internal/account",
"//internal/auth:types",
"//internal/base",
"//internal/base:bluetooth_address",
"//internal/crypto",
"//internal/network:nearby_http_client",
"//internal/network:types",
"//internal/platform:logging",
"//internal/platform:types",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
@@ -61,76 +48,15 @@ cc_library(
cc_library(
name = "test_support",
srcs = ["fake_fast_pair_repository.cc"],
hdrs = [
"fake_fast_pair_client.h",
"fake_fast_pair_repository.h",
],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
":server_access",
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"//internal/platform:types",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
],
)
cc_library(
name = "mocks",
testonly = 1,
hdrs = [
"mock_fast_pair_repository.h",
],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
":server_access",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_for_library_testonly",
],
)
cc_test(
name = "fast_pair_metadata_downloader_impl_test",
srcs = [
"fast_pair_metadata_downloader_impl_test.cc",
],
copts = [
"-Ithird_party",
],
deps = [
":server_access",
"//fastpair/common",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/repository:fake_fast_pair_metadata_repository",
"//internal/platform/implementation/g3",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "fast_pair_repository_impl_test",
srcs = [
"fast_pair_repository_impl_test.cc",
],
copts = [
"-Ithird_party",
],
deps = [
":server_access",
":test_support",
"//fastpair/common",
"//internal/platform:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
@@ -151,24 +77,6 @@ cc_test(
],
)
cc_test(
name = "fast_pair_repository_test",
srcs = [
"fast_pair_repository_test.cc",
],
copts = [
"-Ithird_party",
],
deps = [
":server_access",
"//fastpair/common",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "fast_pair_client_impl_test",
srcs = [
@@ -1,59 +0,0 @@
// Copyright 2022 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 "fastpair/server_access/fast_pair_metadata_downloader.h"
#include <optional>
#include <string>
#include <utility>
#include "absl/strings/string_view.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/proto/fastpair_rpcs.pb.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
FastPairMetadataDownloader::FastPairMetadataDownloader(
absl::string_view model_id, SuccessCallback success_callback,
FailureCallback failure_callback)
: model_id_(model_id),
success_callback_(std::move(success_callback)),
failure_callback_(std::move(failure_callback)) {}
FastPairMetadataDownloader::~FastPairMetadataDownloader() = default;
void FastPairMetadataDownloader::Run() {
DCHECK(!was_run_);
was_run_ = true;
OnRun();
}
void FastPairMetadataDownloader::Succeed(DeviceMetadata& device_metadata) {
DCHECK(was_run_);
DCHECK(success_callback_);
NEARBY_LOGS(INFO) << __func__ << ": Fast Pair download of "
<< device_metadata.GetDetails().name() << " succeeded.";
std::move(success_callback_)(device_metadata);
}
void FastPairMetadataDownloader::Fail() {
DCHECK(was_run_);
DCHECK(failure_callback_);
std::move(failure_callback_)();
}
} // namespace fastpair
} // namespace nearby
@@ -1,58 +0,0 @@
// Copyright 2022 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_FASTPAIR_SERVER_ACCESS_FAST_PAIR_METADATA_DOWNLOADER_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAST_PAIR_METADATA_DOWNLOADER_H_
#include <functional>
#include <optional>
#include <string>
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/device_metadata.h"
namespace nearby {
namespace fastpair {
class FastPairMetadataDownloader {
public:
using SuccessCallback = absl::AnyInvocable<void(DeviceMetadata&)>;
using FailureCallback = absl::AnyInvocable<void()>;
FastPairMetadataDownloader(absl::string_view model_id,
SuccessCallback success_callback,
FailureCallback failure_callback);
virtual ~FastPairMetadataDownloader();
// Starts downloading the device information
void Run();
protected:
absl::string_view model_id() const { return model_id_; }
virtual void OnRun() = 0;
void Succeed(DeviceMetadata& device_metadata);
void Fail();
private:
absl::string_view model_id_;
SuccessCallback success_callback_;
FailureCallback failure_callback_;
bool was_run_ = false;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAST_PAIR_METADATA_DOWNLOADER_H_
@@ -1,122 +0,0 @@
// Copyright 2022 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 "fastpair/server_access/fast_pair_metadata_downloader_impl.h"
#include <algorithm>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/strings/numbers.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "fastpair/server_access/fast_pair_metadata_downloader.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
FastPairMetadataDownloaderImpl::Factory*
FastPairMetadataDownloaderImpl::Factory::test_factory_ = nullptr;
std::unique_ptr<FastPairMetadataDownloader>
FastPairMetadataDownloaderImpl::Factory::Create(
absl::string_view model_id,
FastPairMetadataRepositoryFactory* repository_factory,
SuccessCallback success_callback, FailureCallback failure_callback) {
if (test_factory_) {
return test_factory_->CreateInstance(model_id, repository_factory,
std::move(success_callback),
std::move(failure_callback));
}
return absl::WrapUnique(new FastPairMetadataDownloaderImpl(
model_id, repository_factory, std::move(success_callback),
std::move(failure_callback)));
}
// static
void FastPairMetadataDownloaderImpl::Factory::SetFactoryForTesting(
Factory* test_factory) {
test_factory_ = test_factory;
}
FastPairMetadataDownloaderImpl::Factory::~Factory() = default;
FastPairMetadataDownloaderImpl::~FastPairMetadataDownloaderImpl() = default;
FastPairMetadataDownloaderImpl::FastPairMetadataDownloaderImpl(
absl::string_view model_id,
FastPairMetadataRepositoryFactory* repository_factory,
SuccessCallback success_callback, FailureCallback failure_callback)
: FastPairMetadataDownloader(model_id, std::move(success_callback),
std::move(failure_callback)),
repository_factory_(repository_factory) {}
void FastPairMetadataDownloaderImpl::OnRun() {
NEARBY_LOGS(VERBOSE) << __func__ << " : Starting metadata downloading.";
CallAccessServer(model_id());
}
void FastPairMetadataDownloaderImpl::CallAccessServer(
absl::string_view model_id) {
NEARBY_LOGS(VERBOSE) << __func__
<< ": Making server accessing RPC call to fetch device "
"information with model ID: "
<< model_id;
proto::GetObservedDeviceRequest request;
int64_t device_id;
CHECK(absl::SimpleHexAtoi(model_id, &device_id));
request.set_device_id(device_id);
request.set_mode(proto::GetObservedDeviceRequest::MODE_RELEASE);
repository_ = repository_factory_->CreateInstance();
repository_->GetObservedDevice(
request,
[&](const proto::GetObservedDeviceResponse& response) {
NEARBY_LOGS(INFO) << __func__ << "Name: " << response.device().name();
NEARBY_LOGS(INFO) << __func__
<< "Image URL: " << response.device().image_url();
NEARBY_LOGS(INFO)
<< __func__ << "StringNotification: "
<< response.strings().initial_notification_description();
OnAccessServerSuccess(response);
},
[&](FastPairHttpError error) { OnAccessServerFailure(error); });
}
void FastPairMetadataDownloaderImpl::OnAccessServerFailure(
FastPairHttpError error) {
NEARBY_LOGS(ERROR) << __func__
<< ": Server accessing RPC call failed with error "
<< error;
Fail();
}
void FastPairMetadataDownloaderImpl::OnAccessServerSuccess(
const proto::GetObservedDeviceResponse& response) {
DeviceMetadata device_metadata(response);
NEARBY_LOGS(VERBOSE) << __func__ << ": Download "
<< device_metadata.GetDetails().name() << " succeeded.";
Succeed(device_metadata);
}
} // namespace fastpair
} // namespace nearby
@@ -1,75 +0,0 @@
// Copyright 2022 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_FASTPAIR_SERVER_ACCESS_FAST_PAIR_METADATA_DOWNLOADER_IMPL_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAST_PAIR_METADATA_DOWNLOADER_IMPL_H_
#include <memory>
#include <optional>
#include <string>
#include "absl/strings/string_view.h"
#include "fastpair/common/fast_pair_http_result.h"
#include "fastpair/proto/fastpair_rpcs.pb.h"
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "fastpair/server_access/fast_pair_metadata_downloader.h"
namespace nearby {
namespace fastpair {
class FastPairMetadataRepository;
class FastPairMetadataDownloaderImpl : public FastPairMetadataDownloader {
public:
class Factory {
public:
static std::unique_ptr<FastPairMetadataDownloader> Create(
absl::string_view model_id,
FastPairMetadataRepositoryFactory* repository_factory,
SuccessCallback success_callback, FailureCallback failure_callback);
static void SetFactoryForTesting(Factory* test_factory);
protected:
virtual ~Factory();
virtual std::unique_ptr<FastPairMetadataDownloader> CreateInstance(
absl::string_view model_id,
FastPairMetadataRepositoryFactory* repository_factory,
SuccessCallback success_callback, FailureCallback failure_callback) = 0;
private:
static Factory* test_factory_;
};
~FastPairMetadataDownloaderImpl() override;
private:
FastPairMetadataDownloaderImpl(
absl::string_view model_id,
FastPairMetadataRepositoryFactory* repository_factory,
SuccessCallback success_callback, FailureCallback failure_callback);
void OnRun() override;
void CallAccessServer(absl::string_view model_id);
void OnAccessServerSuccess(const proto::GetObservedDeviceResponse& response);
void OnAccessServerFailure(FastPairHttpError error);
std::unique_ptr<FastPairMetadataRepository> repository_;
FastPairMetadataRepositoryFactory* repository_factory_ = nullptr;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_SERVER_ACCESS_FAST_PAIR_METADATA_DOWNLOADER_IMPL_H_
@@ -1,126 +0,0 @@
// Copyright 2022 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 "fastpair/server_access/fast_pair_metadata_downloader_impl.h"
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_http_result.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/repository/fake_fast_pair_metadata_repository.h"
#include "fastpair/server_access/fast_pair_metadata_downloader.h"
namespace nearby {
namespace fastpair {
namespace {
const int64_t kDeviceId = 10148625;
const char kModelId[] = "9adb11";
const char kDeviceName[] = "Pixel Buds Pro";
class FastPairMetadataDownloaderImplTest : public ::testing::Test {
protected:
struct Result {
bool success;
std::optional<proto::Device> device;
};
FastPairMetadataDownloaderImplTest() = default;
~FastPairMetadataDownloaderImplTest() override = default;
void VerifyRequest() {
ASSERT_TRUE(fake_repository_factory_.fake_repository() != nullptr);
FakeFastPairMetadataRepository* repository =
fake_repository_factory_.fake_repository();
const proto::GetObservedDeviceRequest& request =
repository->get_observed_device_request()->request;
EXPECT_EQ(request.device_id(), kDeviceId);
}
void GetObservedDataRequestSuccess(
const proto::GetObservedDeviceResponse& response) {
VerifyRequest();
FakeFastPairMetadataRepository* repository =
fake_repository_factory_.fake_repository();
std::move(repository->get_observed_device_request()->callback)(response);
}
void GetObservedDataRequestFailure(
const proto::GetObservedDeviceResponse& response) {
VerifyRequest();
FakeFastPairMetadataRepository* repository =
fake_repository_factory_.fake_repository();
std::move(repository->get_observed_device_request()->error_callback)(
FastPairHttpError::kHttpErrorInvalidArgument);
}
// The callbacks passed into NearbyShareContactDownloader ctor.
void OnSuccess(DeviceMetadata& device_metadata) {
const proto::Device device = device_metadata.GetDetails();
result_ = Result();
result_->success = true;
result_->device = std::move(device);
}
void OnFailure() {
result_ = Result();
result_->success = false;
result_->device.reset();
}
std::optional<Result> result_;
FakeFastPairMetadataRepositoryFactory fake_repository_factory_;
std::unique_ptr<FastPairMetadataDownloader> downloader_;
};
TEST_F(FastPairMetadataDownloaderImplTest, GetObservedDeviceDownloadSuccess) {
downloader_ = FastPairMetadataDownloaderImpl::Factory::Create(
kModelId, &fake_repository_factory_,
[&](DeviceMetadata& device_metadata) { OnSuccess(device_metadata); },
[&]() { OnFailure(); });
downloader_->Run();
proto::GetObservedDeviceResponse response;
response.mutable_device()->set_id(kDeviceId);
response.mutable_device()->set_name(kDeviceName);
GetObservedDataRequestSuccess(response);
ASSERT_TRUE(result_);
EXPECT_TRUE(result_->success);
ASSERT_TRUE(result_->device);
EXPECT_EQ(result_->device->name(), kDeviceName);
EXPECT_EQ(result_->device->id(), kDeviceId);
}
TEST_F(FastPairMetadataDownloaderImplTest, GetObservedDeviceDownloadFailure) {
downloader_ = FastPairMetadataDownloaderImpl::Factory::Create(
kModelId, &fake_repository_factory_,
[&](DeviceMetadata& device_metadata) { OnSuccess(device_metadata); },
[&]() { OnFailure(); });
downloader_->Run();
proto::GetObservedDeviceResponse response;
response.mutable_device()->set_id(kDeviceId);
response.mutable_device()->set_name(kDeviceName);
GetObservedDataRequestFailure(response);
ASSERT_TRUE(result_);
EXPECT_FALSE(result_->success);
EXPECT_FALSE(result_->device);
}
} // namespace
} // namespace fastpair
} // namespace nearby
-1
View File
@@ -36,7 +36,6 @@ cc_library(
deps = [
"//fastpair/common",
"//fastpair/repository",
"//fastpair/server_access",
"//internal/base",
"//internal/platform:logging",
"@com_google_absl//absl/functional:any_invocable",
@@ -20,7 +20,7 @@
#include "fastpair/common/device_metadata.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/server_access/fast_pair_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "internal/platform/logging.h"