Files
nearby/fastpair/server_access/fast_pair_metadata_downloader_impl.cc
T

119 lines
4.4 KiB
C++

// 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 <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/strings/string_view.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(
std::optional<std::string> 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(
std::optional<std::string> 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(
const std::optional<std::string>& model_id) {
NEARBY_LOGS(VERBOSE) << __func__
<< ": Making server accessing RPC call to fetch device "
"information with model ID: "
<< model_id.value_or("[null]");
proto::GetObservedDeviceRequest request;
request.set_device_id(std::stoi(model_id.value_or("[null]"), nullptr, 16));
request.set_mode(proto::GetObservedDeviceRequest::MODE_RELEASE);
repository_ = repository_factory_->CreateInstance();
repository_->GetObservedDevice(
request,
[&](const proto::GetObservedDeviceResponse& response) {
NEARBY_LOGS(INFO) << "Maggie: " << __func__
<< "Name: " << response.device().name();
NEARBY_LOGS(INFO) << "Maggie: " << __func__
<< "Image URL: " << response.device().image_url();
NEARBY_LOGS(INFO)
<< "Maggie: " << __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) {
device_ = response.device();
NEARBY_LOGS(VERBOSE) << __func__ << ": Download " << device_.name()
<< " succeeded.";
Succeed(std::move(device_));
}
} // namespace fastpair
} // namespace nearby