Implementation account management for fast pair windows

PiperOrigin-RevId: 547955144
This commit is contained in:
Qin Wang
2023-07-13 16:08:39 -07:00
committed by Copybara-Service
parent e0b2e176c8
commit 47de9bdfaa
17 changed files with 6 additions and 944 deletions
-34
View File
@@ -1,34 +0,0 @@
# Copyright 2023 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.
load("//dart:build_defs.bzl", "dart_library", "dart_package")
dart_package(sound_null_safety = True)
dart_library(
name = "fast_pair_mapping",
srcs = glob(["lib/**/*.dart"]),
required_libs = [
"isolate",
"ffi",
],
visibility = [
"//fastpair/dart:__subpackages__",
"//location/nearby/apps/better_together/app/fast_pair:__subpackages__",
],
deps = [
"//fastpair/dart/proto:fastpair_dart_proto",
"//third_party/dart/auto_disposable",
],
)
-55
View File
@@ -1,55 +0,0 @@
// Copyright 2023 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.
import 'dart:ffi';
import 'ffi_types.dart';
class FastpairBinding {
static final _lib = DynamicLibrary.open('fastpair_dart.dll');
late final InitMediatorDartType initMediator = _lib
.lookup<NativeFunction<InitMediatorType>>(
'InitMediatorDart',
)
.asFunction();
late final CloseMediatorDartType closeMediator = _lib
.lookup<NativeFunction<CloseMediatorType>>('CloseMediator')
.asFunction();
late final StartFastPairScanningDartType startFastPairScanning = _lib
.lookup<NativeFunction<StartFastPairScanningType>>('StartScanDart')
.asFunction();
late final InitializeApiDartType initializeApi = _lib
.lookup<NativeFunction<InitializeApiType>>('Dart_InitializeApiDL')
.asFunction();
late final AddNotificationControllerObserverDartType
addNotificationControllerObserver = _lib
.lookup<NativeFunction<AddNotificationControllerObserverType>>(
'AddNotificationControllerObserverDart')
.asFunction();
late final RemoveNotificationControllerObserverDartType
removeNotificationControllerObserver = _lib
.lookup<NativeFunction<RemoveNotificationControllerObserverType>>(
'RemoveNotificationControllerObserverDart')
.asFunction();
late final DiscoveryClickedDartType discoveryClicked = _lib
.lookup<NativeFunction<DiscoveryClickedType>>('DiscoveryClickedDart')
.asFunction();
}
@@ -1,36 +0,0 @@
// Copyright 2023 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.
// Detail information of a observed device. Compared to the device information
// get from GetObservedDeviceResponse, this class only store the device
// information that is related to UI showing.
class DeviceMetadata {
String id;
String name;
String imageUrl;
DeviceMetadata({
required this.id,
required this.name,
required this.imageUrl,
});
factory DeviceMetadata.fromJson(dynamic data) {
return DeviceMetadata(
id: data['id'],
name: data['name'] == '' ? null : data['name'],
imageUrl: data['imageUrl'] == '' ? null : data['imageUrl'],
);
}
}
@@ -1,104 +0,0 @@
// Copyright 2023 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.
import 'dart:ffi';
import 'device_metadata.dart';
import 'dart:typed_data';
import 'package:auto_disposable/auto_disposable.dart';
import 'listener.dart';
import 'bindings.dart';
import 'ffi_types.dart';
import 'package:third_party.nearby.fastpair.dart.proto/callbacks.pb.dart'
as proto;
import 'package:third_party.nearby.fastpair.dart.proto/enum.pb.dart'
as enumproto;
class FastPairMapping with AutoDisposerMixin {
final binding = FastpairBinding();
late final Pointer<Uint64> _fastpairPointer;
FastPairMapping._internal() {
final result = binding.initializeApi(NativeApi.initializeApiDLData);
if (result != 0) {
throw 'failed to init API.';
}
_fastpairPointer = binding.initMediator();
if (_fastpairPointer == nullptr) {
throw 'initMediator failed: _fastpairPointer is null.';
}
autoDisposeCustom(() => binding.closeMediator(_fastpairPointer));
}
static final _instance = FastPairMapping._internal();
factory FastPairMapping() => _instance;
void startScan() {
binding.startFastPairScanning(_fastpairPointer);
}
Listener addNotificationControllerObserver(
OnMetadataChanged onMetadataChanged) {
final listener = Listener();
listener.onData((data) {
if (data is Uint8List) {
final params = proto.DeviceDownloadedCallbackData.fromBuffer(data);
onMetadataChanged(
deviceMetadata: List<DeviceMetadata>.from(
params.devices.map(
(device) => DeviceMetadata(
id: device.id.toString(),
name: device.name,
imageUrl: device.imageUrl),
),
),
);
}
});
binding.addNotificationControllerObserver(_fastpairPointer, listener.port);
return listener;
}
void removeNotificationControllerObserver(dynamic listener) {
binding.removeNotificationControllerObserver(
_fastpairPointer,
listener.port,
);
}
void connectClicked() {
binding.discoveryClicked(_fastpairPointer,
enumproto.DiscoveryAction.DISCOVERY_ACTION_PAIR_TO_DEVICE.value);
}
void learnMoreClicked() {
binding.discoveryClicked(_fastpairPointer,
enumproto.DiscoveryAction.DISCOVERY_ACTION_LEARN_MORE.value);
}
void dismissedByUserClicked() {
binding.discoveryClicked(_fastpairPointer,
enumproto.DiscoveryAction.DISCOVERY_ACTION_DISMISSED_BY_USER.value);
}
void unknownAction() {
binding.discoveryClicked(_fastpairPointer,
enumproto.DiscoveryAction.DISCOVERY_ACTION_UNKNOWN.value);
}
}
-62
View File
@@ -1,62 +0,0 @@
// Copyright 2023 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.
import 'dart:ffi';
import 'device_metadata.dart';
typedef InitMediatorType = Pointer<Uint64> Function();
typedef InitMediatorDartType = Pointer<Uint64> Function();
typedef CloseMediatorType = Void Function(Pointer<Uint64> /*service*/);
typedef CloseMediatorDartType = void Function(Pointer<Uint64> /*service*/);
typedef StartFastPairScanningType = Void Function(Pointer<Uint64> /*service*/);
typedef StartFastPairScanningDartType = void Function(
Pointer<Uint64> /*service*/);
typedef AddNotificationControllerObserverType = Void Function(
Pointer<Uint64> /*service*/,
Int64 /* callback*/,
);
typedef AddNotificationControllerObserverDartType = void Function(
Pointer<Uint64> /*service*/,
int /* callback*/,
);
typedef RemoveNotificationControllerObserverType = Void Function(
Pointer<Uint64> /*service*/,
Int64 /* callback*/,
);
typedef RemoveNotificationControllerObserverDartType = void Function(
Pointer<Uint64> /*service*/,
int /* callback*/,
);
typedef DiscoveryClickedType = Void Function(
Pointer<Uint64> /*service*/,
Int64 /* callback*/,
);
typedef DiscoveryClickedDartType = void Function(
Pointer<Uint64> /*service*/,
int /* callback*/,
);
typedef InitializeApiType = IntPtr Function(Pointer<Void>);
typedef InitializeApiDartType = int Function(Pointer<Void>);
//callback type
typedef OnMetadataChanged = void Function({
required List<DeviceMetadata> deviceMetadata,
});
-36
View File
@@ -1,36 +0,0 @@
// Copyright 2023 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.
import 'dart:ffi';
import 'dart:isolate';
import 'package:auto_disposable/auto_disposable.dart';
/// A convenience class for listening to a [ReceivePort].
///
/// This can be used to listen to ongoing updates from the c++ layer.
class Listener with AutoDisposerMixin {
Listener() {
autoDisposeCustom(_port.close);
autoDisposeCustom(_subscription.cancel);
}
final _port = ReceivePort();
late final _subscription = _port.listen(null);
int get port => _port.sendPort.nativePort;
void onData(void Function(dynamic data)? handleData) =>
_subscription.onData(handleData);
}
-63
View File
@@ -1,63 +0,0 @@
# Copyright 2023 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.
load("//dart:build_defs.bzl", "dart_proto_library")
load("@rules_cc//cc:defs.bzl", "cc_proto_library")
licenses(["notice"])
proto_library(
name = "fastpair_callback_proto",
srcs = [
"callbacks.proto",
],
deps = [
"//fastpair/proto:fastpair_proto",
],
)
proto_library(
name = "fastpair_device_proto",
srcs = [
"device.proto",
],
)
proto_library(
name = "fastpair_enum_proto",
srcs = [
"enum.proto",
],
)
proto_library(
name = "fastpair_proto",
deps = [
":fastpair_callback_proto",
":fastpair_device_proto",
":fastpair_enum_proto",
],
)
cc_proto_library(
name = "fastpair_cc_proto",
visibility = ["//visibility:public"],
deps = [":fastpair_proto"],
)
dart_proto_library(
name = "fastpair_dart_proto",
visibility = ["//visibility:public"],
deps = [":fastpair_proto"],
)
-29
View File
@@ -1,29 +0,0 @@
// Copyright 2023 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.
syntax = "proto3";
package nearby.fastpair.dart.proto;
import "third_party/nearby/fastpair/proto/fastpair_rpcs.proto";
// NextId: 2
message CallbackData {
string message = 1;
}
// NextId: 2
message DeviceDownloadedCallbackData {
repeated nearby.fastpair.proto.Device devices = 1;
}
-26
View File
@@ -1,26 +0,0 @@
// Copyright 2023 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.
syntax = "proto3";
package nearby.fastpair.dart.proto;
// Describes a FastPair scanned device. The device class will have more
// properties and methods in the future based on the new feature added.
// NextId: 4
message Device {
string id = 1; // The unique identifier of the device.
string name = 2;
string image_url = 3;
}
-34
View File
@@ -1,34 +0,0 @@
// Copyright 2023 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.
syntax = "proto3";
package nearby.fastpair.dart.proto;
// Represents the action after discovery
enum DiscoveryAction {
// Uknown
DISCOVERY_ACTION_UNKNOWN = 0;
// User is willing to pair the discovered device and clicks the connect button
DISCOVERY_ACTION_PAIR_TO_DEVICE = 1;
// User is not willing to pair the discovered device and manually dismiss the
// notification
DISCOVERY_ACTION_DISMISSED_BY_USER = 2;
// OS dismissed
DISCOVERY_ACTION_DISSMISSED_BY_OS = 3;
// User clicks the learn more button
DISCOVERY_ACTION_LEARN_MORE = 4;
// Time out and dismissed automatically
DISCOVERY_ACTION_DISMISSED_BY_TIMEOUT = 5;
}
-86
View File
@@ -1,86 +0,0 @@
# Copyright 2023 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.
load("//third_party/lexan/build_defs:lexan.bzl", "lexan")
licenses(["notice"])
lexan.cc_windows_dll(
name = "fastpair_adapter",
srcs = [
"fast_pair_service_adapter.cc",
],
hdrs = [
"fast_pair_service_adapter.h",
],
copts = [
"-Ithird_party",
],
defines = [
"LOG_SEVERITY_VERBOSE",
"_WIN32_WINNT=_WIN32_WINNT_WIN10",
],
tags = ["windows-dll"],
visibility = [
"//location/nearby/apps/better_together/windows/fast_pair:__subpackages__",
],
deps = [
"//fastpair:fast_pair_service",
"//fastpair/dart/proto:fastpair_cc_proto",
"//fastpair/keyed_service",
"//fastpair/plugins:windows_admin_plugin",
"//fastpair/ui:fast_pair_ui",
"//internal/platform:logging",
"//internal/platform/implementation/windows",
"//internal/platform/implementation/windows/generated:types",
"@com_google_absl//absl/strings",
],
)
lexan.cc_windows_dll(
name = "fastpair_dart",
srcs = [
"fast_pair_service_adapter.cc",
"fast_pair_service_adapter_dart.cc",
],
hdrs = [
"fast_pair_service_adapter.h",
"fast_pair_service_adapter_dart.h",
],
copts = [
"-Wc++17-compat",
"-Ithird_party",
],
defines = [
"LOG_SEVERITY_VERBOSE",
"DART_SHARED_LIB=1",
"_WIN32_WINNT=_WIN32_WINNT_WIN10",
],
tags = ["windows-dll"],
visibility = [
"//location/nearby/apps/better_together/windows/fast_pair:__subpackages__",
],
deps = [
"//fastpair:fast_pair_service",
"//fastpair/common",
"//fastpair/dart/proto:fastpair_cc_proto",
"//fastpair/keyed_service",
"//fastpair/plugins:windows_admin_plugin",
"//fastpair/ui:fast_pair_ui",
"//internal/platform:logging",
"//internal/platform/implementation/windows",
"//internal/platform/implementation/windows/generated:types",
"//third_party/dart_lang/v2:dart_api_dl",
"@com_google_absl//absl/strings",
],
)
@@ -1,167 +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_service_adapter.h"
#include <memory>
#include <string>
#include "fastpair/fast_pair_service.h"
#include "fastpair/internal/fast_pair_seeker_impl.h"
#include "fastpair/keyed_service/fast_pair_mediator.h"
#include "fastpair/keyed_service/fast_pair_mediator_factory.h"
#include "fastpair/plugins/windows_admin_plugin.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 {
// Set to 1 to use Mediator. Set to 0 to use scalable seeker.
#define USE_MEDIATOR 0
namespace {
#if USE_MEDIATOR
Mediator *pMediator_ = nullptr;
#else
WindowsAdminPlugin::PluginState *plugin_state = nullptr;
#endif /* USE_MEDIATOR */
WindowsAdminPlugin::PluginState *InitFastPairService() {
NEARBY_LOGS(INFO) << "[[ Init Fast Pair Service. ]]";
plugin_state = new WindowsAdminPlugin::PluginState();
plugin_state->fast_pair_service = std::make_unique<FastPairService>();
plugin_state->fast_pair_service->RegisterPluginProvider(
"admin", std::make_unique<WindowsAdminPlugin::Provider>(plugin_state));
return plugin_state;
}
void CloseFastPairService(void *instance) {
NEARBY_LOGS(INFO) << "[[ Closing Fast Pair Service. ]]";
CHECK_EQ(plugin_state, instance);
delete plugin_state;
plugin_state = nullptr;
NEARBY_LOGS(INFO) << "[[ Successfully closed Fast Pair Service. ]]";
}
void StartFastPairServiceScan(void *instance) {
CHECK_EQ(plugin_state, instance);
FastPairSeekerExt *seeker = static_cast<FastPairSeekerExt *>(
plugin_state->fast_pair_service->GetSeeker());
absl::Status status = seeker->StartFastPairScan();
NEARBY_LOGS(INFO) << "Start FP scan result: " << status;
}
} // namespace
void *InitMediator() {
#if defined(NEARBY_LOG_SEVERITY)
// Direct override of logging level.
NEARBY_LOG_SET_SEVERITY(NEARBY_LOG_SEVERITY);
#endif // LOG_SEVERITY_VERBOSE;
#if USE_MEDIATOR
pMediator_ = MediatorFactory::GetInstance()->CreateMediator();
return pMediator_;
#else
return InitFastPairService();
#endif /* USE_MEDIATOR */
}
void CloseMediator(void *instance) {
#if USE_MEDIATOR
NEARBY_LOGS(INFO) << "[[ Closing Fast Pair Mediator. ]]";
if (pMediator_ != nullptr) delete pMediator_;
NEARBY_LOGS(INFO) << "[[ Successfully closed Fast Pair Mediator. ]]";
#else
CloseFastPairService(instance);
#endif /* USE_MEDIATOR */
}
void __stdcall StartScan(void *instance) {
NEARBY_LOGS(INFO) << "StartScan is called";
#if USE_MEDIATOR
if (pMediator_ == nullptr) {
NEARBY_LOGS(VERBOSE) << "The pMediator is a null pointer.";
return;
}
Mediator *mediator = static_cast<Mediator *>(instance);
mediator->StartScanning();
#else
StartFastPairServiceScan(instance);
#endif /* USE_MEDIATOR */
}
void __stdcall AddNotificationControllerObserver(
void *instance, FastPairNotificationController::Observer *observer) {
NEARBY_LOGS(INFO) << "AddNotificationControllerObserver is called";
#if USE_MEDIATOR
if (pMediator_ == nullptr) {
NEARBY_LOGS(VERBOSE) << "The pMediator is a null pointer.";
return;
}
Mediator *mediator = static_cast<Mediator *>(instance);
mediator->GetNotificationController()->AddObserver(observer);
#else
CHECK_EQ(plugin_state, instance);
plugin_state->observers.AddObserver(observer);
#endif /* USE_MEDIATOR */
}
void __stdcall RemoveNotificationControllerObserver(
void *instance, FastPairNotificationController::Observer *observer) {
#if USE_MEDIATOR
if (pMediator_ == nullptr) {
NEARBY_LOGS(VERBOSE) << "The pMediator is a null pointer.";
return;
}
Mediator *mediator = static_cast<Mediator *>(instance);
mediator->GetNotificationController()->RemoveObserver(observer);
#else
CHECK_EQ(plugin_state, instance);
plugin_state->observers.RemoveObserver(observer);
#endif /* USE_MEDIATOR */
}
void __stdcall DiscoveryClicked(void *instance, DiscoveryAction action) {
#if USE_MEDIATOR
Mediator *mediator = static_cast<Mediator *>(instance);
mediator->GetNotificationController()->OnDiscoveryClicked(action);
#else
CHECK_EQ(plugin_state, instance);
plugin_state->DiscoveryClicked(action);
#endif /* USE_MEDIATOR */
}
void __stdcall SetIsScreenLocked(bool is_locked) {
#if USE_MEDIATOR
if (pMediator_ == nullptr) {
NEARBY_LOGS(INFO) << "The pMediator is a null pointer.";
return;
}
NEARBY_LOGS(INFO) << "SetIsScreenLocked :" << is_locked;
pMediator_->SetIsScreenLocked(is_locked);
#else
if (plugin_state == nullptr) {
NEARBY_LOGS(INFO) << "Plugin not initialized";
return;
}
plugin_state->SetIsScreenLocked(is_locked);
#endif /* USE_MEDIATOR */
}
} // namespace windows
} // namespace fastpair
} // namespace nearby
@@ -1,54 +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_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 scanning service
DLL_EXPORT void __stdcall StartScan(void *instance);
// Adds a notification controller observer to the service.
DLL_EXPORT void __stdcall AddNotificationControllerObserver(
void *instance, FastPairNotificationController::Observer *observer);
// Removes a notification controller observer to the service.
DLL_EXPORT void __stdcall RemoveNotificationControllerObserver(
void *instance, FastPairNotificationController::Observer *observer);
// Triggers discovery click action
DLL_EXPORT void DiscoveryClicked(void *instance, DiscoveryAction action);
// Sends screen locked event.
DLL_EXPORT void __stdcall SetIsScreenLocked(bool is_locked);
} // namespace windows
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_DART_WINDOWS_FAST_PAIR_SERVICE_ADAPTER_H_
@@ -1,110 +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_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() { return InitMediator(); }
void StartScanDart(void *instance) { StartScan(instance); }
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(void *instance, Dart_Port port) {
auto observer = std::make_unique<NotificationControllerObserver>(port);
AddNotificationControllerObserver(instance, observer.get());
notification_controller_observer_map_->insert({port, std::move(observer)});
CHECK(notification_controller_observer_map_->contains(port));
}
void RemoveNotificationControllerObserverDart(void *instance, Dart_Port port) {
auto it = notification_controller_observer_map_->find(port);
if (it != notification_controller_observer_map_->end()) {
RemoveNotificationControllerObserver(instance, it->second.get());
notification_controller_observer_map_->erase(it);
}
}
void DiscoveryClickedDart(void *instance, int action) {
switch (action) {
case ::nearby::fastpair::dart::proto::DISCOVERY_ACTION_PAIR_TO_DEVICE:
DiscoveryClicked(instance,
::nearby::fastpair::DiscoveryAction::kPairToDevice);
break;
case ::nearby::fastpair::dart::proto::DISCOVERY_ACTION_LEARN_MORE:
DiscoveryClicked(instance,
::nearby::fastpair::DiscoveryAction::kLearnMore);
break;
}
}
} // namespace windows
} // namespace fastpair
} // namespace nearby
@@ -1,48 +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_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(void* instance);
// Adds a notification controller observer to the service.
DLL_EXPORT void __stdcall AddNotificationControllerObserverDart(void* instance,
Dart_Port port);
// Removes a notification controller observer to the service.
DLL_EXPORT void __stdcall RemoveNotificationControllerObserverDart(
void* instance, Dart_Port port);
// Triggers discovery click action
DLL_EXPORT void __stdcall DiscoveryClickedDart(void* instance, int action);
} // namespace windows
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_DART_WINDOWS_FAST_PAIR_SERVICE_ADAPTER_DART_H_
+4
View File
@@ -65,6 +65,10 @@ class FastPairService {
// same lifetime as this `FastPairService` instance.
FastPairSeeker* GetSeeker() const { return seeker_.get(); }
// Returns a `AccountManager` implementation. The returned object has the
// same lifetime as this `FastPairService` instance.
AccountManager* GetAccountManager() { return account_manager_.get(); }
private:
struct PluginState {
// Gets the plugin for `device`. Creates the plugin if it does not exist.
@@ -66,6 +66,8 @@ class Mediator final : public ScannerBroker::Observer,
ui_broker_.reset();
};
AccountManager* GetAccountManager() { return account_manager_.get(); }
FastPairNotificationController* GetNotificationController() {
return notification_controller_.get();
}