Connect UI related key services in mediator to flutter app. The whole fast pair libraries are in C++ language while flutter app is in dart language. That is, the purpose of adapter is connecting dart (flutter app) and c++ (Fast Pair libraries).

PiperOrigin-RevId: 540013256
This commit is contained in:
hai007
2023-06-13 10:58:48 -07:00
committed by Copybara-Service
parent 1e34f097aa
commit 5efbbe4a40
9 changed files with 311 additions and 202 deletions
+13 -9
View File
@@ -18,10 +18,10 @@ licenses(["notice"])
lexan.cc_windows_dll(
name = "fastpair_adapter",
srcs = [
"fast_pair_wrapper_adapter.cc",
"fast_pair_service_adapter.cc",
],
hdrs = [
"fast_pair_wrapper_adapter.h",
"fast_pair_service_adapter.h",
],
copts = [
"-Ithird_party",
@@ -32,11 +32,12 @@ lexan.cc_windows_dll(
],
tags = ["windows-dll"],
visibility = [
"//fastpair:__subpackages__",
"//location/nearby/apps/better_together/windows/fast_pair:__subpackages__",
],
deps = [
"//fastpair/dart:fastpair_wrapper",
"//fastpair/dart/proto:fastpair_cc_proto",
"//fastpair/keyed_service",
"//fastpair/ui:fast_pair_ui",
"//internal/platform:logging",
"//internal/platform/implementation/windows",
"//internal/platform/implementation/windows/generated:types",
@@ -47,12 +48,12 @@ lexan.cc_windows_dll(
lexan.cc_windows_dll(
name = "fastpair_dart",
srcs = [
"fast_pair_wrapper_adapter.cc",
"fast_pair_wrapper_adapter_dart.cc",
"fast_pair_service_adapter.cc",
"fast_pair_service_adapter_dart.cc",
],
hdrs = [
"fast_pair_wrapper_adapter.h",
"fast_pair_wrapper_adapter_dart.h",
"fast_pair_service_adapter.h",
"fast_pair_service_adapter_dart.h",
],
copts = [
"-Ithird_party",
@@ -67,7 +68,10 @@ lexan.cc_windows_dll(
"//location/nearby/apps/better_together/windows/fast_pair:__subpackages__",
],
deps = [
"//fastpair/dart:fastpair_wrapper",
"//fastpair/common",
"//fastpair/dart/proto:fastpair_cc_proto",
"//fastpair/keyed_service",
"//fastpair/ui:fast_pair_ui",
"//internal/platform:logging",
"//internal/platform/implementation/windows",
"//internal/platform/implementation/windows/generated:types",
@@ -0,0 +1,87 @@
// 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/dart/windows/fast_pair_service_adapter.h"
#include <memory>
#include <string>
#include "fastpair/keyed_service/fast_pair_mediator.h"
#include "fastpair/keyed_service/fast_pair_mediator_factory.h"
#include "fastpair/ui/actions.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
namespace windows {
static Mediator *pMediator_ = nullptr;
void *InitMediator() {
#if defined(NEARBY_LOG_SEVERITY)
// Direct override of logging level.
NEARBY_LOG_SET_SEVERITY(NEARBY_LOG_SEVERITY);
#endif // LOG_SEVERITY_VERBOSE;
Mediator *pMediator = MediatorFactory::GetInstance()->CreateMediator();
pMediator_ = pMediator;
return pMediator_;
}
void CloseMediator(Mediator *pMediator) {
NEARBY_LOGS(INFO) << "[[ Closing Fast Pair Mediator. ]]";
if (pMediator_ != nullptr) delete pMediator_;
NEARBY_LOGS(INFO) << "[[ Successfully closed Fast Pair Mediator. ]]";
}
void __stdcall StartScan(Mediator *pMediator) {
NEARBY_LOGS(INFO) << "StartScan is called";
if (pMediator_ == nullptr) {
NEARBY_LOGS(VERBOSE) << "The pMediator is a null pointer.";
return;
}
pMediator_->StartScanning();
}
void __stdcall AddNotificationControllerObserver(
Mediator *pMediator, FastPairNotificationController::Observer *observer) {
NEARBY_LOGS(INFO) << "AddNotificationControllerObserver is called";
if (pMediator_ == nullptr) {
NEARBY_LOGS(VERBOSE) << "The pMediator is a null pointer.";
return;
}
static_cast<Mediator *>(pMediator)->GetNotificationController()->AddObserver(
observer);
}
void __stdcall RemoveNotificationControllerObserver(
Mediator *pMediator, FastPairNotificationController::Observer *observer) {
if (pMediator_ == nullptr) {
NEARBY_LOGS(VERBOSE) << "The pMediator is a null pointer.";
return;
}
static_cast<Mediator *>(pMediator)
->GetNotificationController()
->RemoveObserver(observer);
}
void __stdcall DiscoveryClicked(Mediator *pMediator, DiscoveryAction action) {
static_cast<Mediator *>(pMediator)
->GetNotificationController()
->OnDiscoveryClicked(action);
}
} // namespace windows
} // namespace fastpair
} // namespace nearby
@@ -0,0 +1,51 @@
// 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_DART_WINDOWS_FAST_PAIR_SERVICE_ADAPTER_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_DART_WINDOWS_FAST_PAIR_SERVICE_ADAPTER_H_
#define DLL_EXPORT extern "C" __declspec(dllexport)
#include <string>
#include "fastpair/keyed_service/fast_pair_mediator.h"
#include "fastpair/ui/actions.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
namespace nearby {
namespace fastpair {
namespace windows {
// Initiates a default Mediator instance. Return the instance handle to client.
DLL_EXPORT void *__stdcall InitMediator();
// Starts scaning service
DLL_EXPORT void __stdcall StartScan(Mediator *pMediator);
// Adds a notification controller observer to the service.
DLL_EXPORT void __stdcall AddNotificationControllerObserver(
Mediator *pMediator, FastPairNotificationController::Observer *observer);
// Removes a notification controller observer to the service.
DLL_EXPORT void __stdcall RemoveNotificationControllerObserver(
Mediator *pMediator, FastPairNotificationController::Observer *observer);
// Triggers discovery click action
DLL_EXPORT void DiscoveryClicked(Mediator *pMediator, DiscoveryAction action);
} // namespace windows
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_DART_WINDOWS_FAST_PAIR_SERVICE_ADAPTER_H_
@@ -0,0 +1,112 @@
// 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/dart/windows/fast_pair_service_adapter_dart.h"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "third_party/dart_lang/v2/runtime/include/dart_api.h"
#include "fastpair/dart/proto/callbacks.proto.h"
#include "fastpair/dart/proto/enum.proto.h"
#include "fastpair/dart/windows/fast_pair_service_adapter.h"
#include "fastpair/keyed_service/fast_pair_mediator.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
namespace windows {
namespace {
class NotificationControllerObserver
: public FastPairNotificationController::Observer {
public:
explicit NotificationControllerObserver(Dart_Port port)
: callback_dart_(port) {}
void OnUpdateDevice(const DeviceMetadata &device) override {
NEARBY_LOGS(INFO) << __func__
<< ": The on update device is triggered for dart. "
<< device.GetDetails().name();
const std::string device_proto =
SerializeDeviceChanged(device).SerializeAsString();
Dart_CObject object_device = {
.type = Dart_CObject_Type::Dart_CObject_kTypedData,
.value = {
.as_typed_data{.type = Dart_TypedData_Type::Dart_TypedData_kUint8,
.length = (intptr_t)device_proto.size(),
.values = (uint8_t *)device_proto.data()}}};
if (!Dart_PostCObject_DL(callback_dart_, &object_device)) {
NEARBY_LOGS(ERROR)
<< "Failed to post OnContactsDownloaded message to dart. id = "
<< device.GetDetails().id();
}
}
private:
::nearby::fastpair::dart::proto::DeviceDownloadedCallbackData
SerializeDeviceChanged(const DeviceMetadata &device) {
::nearby::fastpair::dart::proto::DeviceDownloadedCallbackData callback;
callback.add_devices()->MergeFrom(device.GetDetails());
return callback;
}
Dart_Port callback_dart_;
};
} // namespace
void InitMediatorDart() { InitMediator(); }
void StartScanDart(Mediator *pMediator) { StartScan(pMediator); }
static absl::flat_hash_map<Dart_Port,
std::unique_ptr<NotificationControllerObserver>>
*notification_controller_observer_map_ = new absl::flat_hash_map<
Dart_Port, std::unique_ptr<NotificationControllerObserver>>();
void AddNotificationControllerObserverDart(Mediator *pMediator,
Dart_Port port) {
auto observer = std::make_unique<NotificationControllerObserver>(port);
AddNotificationControllerObserver(pMediator, observer.get());
notification_controller_observer_map_->insert({port, std::move(observer)});
CHECK(notification_controller_observer_map_->contains(port));
}
void RemoveNotificationControllerObserverDart(Mediator *pMediator,
Dart_Port port) {
auto it = notification_controller_observer_map_->find(port);
if (it != notification_controller_observer_map_->end()) {
RemoveNotificationControllerObserver(pMediator, it->second.get());
notification_controller_observer_map_->erase(it);
}
}
void DiscoveryClickedDart(Mediator *pMediator, int action) {
switch (action) {
case ::nearby::fastpair::dart::proto::DISCOVERY_ACTION_PAIR_TO_DEVICE:
DiscoveryClicked(pMediator,
::nearby::fastpair::DiscoveryAction::kPairToDevice);
break;
case ::nearby::fastpair::dart::proto::DISCOVERY_ACTION_LEARN_MORE:
DiscoveryClicked(pMediator,
::nearby::fastpair::DiscoveryAction::kLearnMore);
break;
}
}
} // namespace windows
} // namespace fastpair
} // namespace nearby
@@ -0,0 +1,48 @@
// 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_DART_WINDOWS_FAST_PAIR_SERVICE_ADAPTER_DART_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_DART_WINDOWS_FAST_PAIR_SERVICE_ADAPTER_DART_H_
#include <string>
#include "third_party/dart_lang/v2/runtime/include/dart_api_dl.h"
#include "fastpair/dart/windows/fast_pair_service_adapter.h"
namespace nearby {
namespace fastpair {
namespace windows {
// Initiates a default Mediator instance.
DLL_EXPORT void __stdcall InitMediatorDart();
// Starts scanning service
DLL_EXPORT void __stdcall StartScanDart(Mediator* pMediator);
// Adds a notification controller observer to the service.
DLL_EXPORT void __stdcall AddNotificationControllerObserverDart(
Mediator* pMediator, Dart_Port port);
// Removes a notification controller observer to the service.
DLL_EXPORT void __stdcall RemoveNotificationControllerObserverDart(
Mediator* pMediator, Dart_Port port);
// Triggers discovery click action
DLL_EXPORT void __stdcall DiscoveryClickedDart(Mediator* pMediator, int action);
} // namespace windows
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_DART_WINDOWS_FAST_PAIR_SERVICE_ADAPTER_DART_H_
@@ -1,77 +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/dart/windows/fast_pair_wrapper_adapter.h"
#include "fastpair/dart/fast_pair_wrapper.h"
#include "fastpair/dart/fast_pair_wrapper_impl.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
namespace windows {
static FastPairWrapper *pWrapper_ = nullptr;
void *InitFastPairWrapper() {
#if defined(NEARBY_LOG_SEVERITY)
// Direct override of logging level.
NEARBY_LOG_SET_SEVERITY(NEARBY_LOG_SEVERITY);
#endif // LOG_SEVERITY_VERBOSE;
FastPairWrapperImpl *pWrapper = new FastPairWrapperImpl();
pWrapper_ = pWrapper;
return pWrapper;
}
void CloseFastPairWrapper(FastPairWrapper *pWrapper) {
NEARBY_LOGS(INFO) << "[[ Closing Fast Pair Wrapper. ]]";
if (pWrapper_ != nullptr) delete pWrapper_;
NEARBY_LOGS(INFO) << "[[ Successfully closed Fast Pair Wrapper. ]]";
}
void __stdcall StartScan(FastPairWrapper *pWrapper) {
NEARBY_LOGS(VERBOSE) << "StartScan is called";
if (pWrapper_ == nullptr) {
NEARBY_LOGS(INFO) << "The pWrapper is a null pointer.";
return;
}
return pWrapper_->StartScan();
}
bool __stdcall IsScanning(FastPairWrapper *pWrapper) {
if (pWrapper_ == nullptr) {
return false;
}
return static_cast<FastPairWrapper *>(pWrapper_)->IsScanning();
}
bool __stdcall IsPairing(FastPairWrapper *pWrapper) {
if (pWrapper_ == nullptr) {
return false;
}
// return connecting
return static_cast<FastPairWrapper *>(pWrapper_)->IsPairing();
}
bool __stdcall IsServerAccessing(FastPairWrapper *pWrapper) {
if (pWrapper_ == nullptr) {
return false;
}
return static_cast<FastPairWrapper *>(pWrapper_)->IsServerAccessing();
}
} // namespace windows
} // namespace fastpair
} // namespace nearby
@@ -1,49 +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_DART_WINDOWS_FAST_PAIR_WRAPPER_ADAPTER_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_DART_WINDOWS_FAST_PAIR_WRAPPER_ADAPTER_H_
#define DLL_EXPORT extern "C" __declspec(dllexport)
#include "fastpair/dart/fast_pair_wrapper.h"
namespace nearby {
namespace fastpair {
namespace windows {
// Initiate a default FastPairWrapper instance.
// Return the instance handle to client
DLL_EXPORT void *__stdcall InitFastPairWrapper();
// Closes the Wrapper with stopping all endpoints, then free the memory.
DLL_EXPORT void __stdcall CloseFastPairWrapper(FastPairWrapper *pWrapper);
DLL_EXPORT void __stdcall StartScan(FastPairWrapper *pWrapper);
// Obtain scanning status
DLL_EXPORT bool __stdcall IsScanning(FastPairWrapper *pWrapper);
// Obtain connecting status
DLL_EXPORT bool __stdcall IsPairing(FastPairWrapper *pWrapper);
// Server Access trigger just for adapter implementation testing, will be delete
// after success
DLL_EXPORT bool __stdcall IsServerAccessing(FastPairWrapper *pWrapper);
} // namespace windows
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_DART_WINDOWS_FAST_PAIR_WRAPPER_ADAPTER_H_
@@ -1,34 +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/dart/windows/fast_pair_wrapper_adapter_dart.h"
#include "fastpair/dart/windows/fast_pair_wrapper_adapter.h"
namespace nearby {
namespace fastpair {
namespace windows {
void StartScanDart(FastPairWrapper *pWrapper) {
if (IsScanning(pWrapper)) return;
StartScan(pWrapper);
}
void ServerAccessDart(FastPairWrapper *pWrapper) {
IsServerAccessing(pWrapper);
}
} // namespace windows
} // namespace fastpair
} // namespace nearby
@@ -1,33 +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_DART_WINDOWS_FAST_PAIR_WRAPPER_ADAPTER_DART_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_DART_WINDOWS_FAST_PAIR_WRAPPER_ADAPTER_DART_H_
#include "third_party/dart_lang/v2/runtime/include/dart_api_dl.h"
#include "fastpair/dart/windows/fast_pair_wrapper_adapter.h"
namespace nearby {
namespace fastpair {
namespace windows {
DLL_EXPORT void __stdcall StartScanDart(FastPairWrapper* pWrapper);
DLL_EXPORT void __stdcall ServerAccessDart(FastPairWrapper* pWrapper);
} // namespace windows
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_DART_WINDOWS_FAST_PAIR_WRAPPER_ADAPTER_DART_H_