From 47de9bdfaacb8b1782968635418443dd0ca71a01 Mon Sep 17 00:00:00 2001 From: Qin Wang Date: Thu, 13 Jul 2023 16:06:24 -0700 Subject: [PATCH] Implementation account management for fast pair windows PiperOrigin-RevId: 547955144 --- fastpair/dart/binding/BUILD | 34 ---- fastpair/dart/binding/lib/bindings.dart | 55 ------ .../dart/binding/lib/device_metadata.dart | 36 ---- .../dart/binding/lib/fast_pair_mapping.dart | 104 ----------- fastpair/dart/binding/lib/ffi_types.dart | 62 ------- fastpair/dart/binding/lib/listener.dart | 36 ---- fastpair/dart/proto/BUILD | 63 ------- fastpair/dart/proto/callbacks.proto | 29 --- fastpair/dart/proto/device.proto | 26 --- fastpair/dart/proto/enum.proto | 34 ---- fastpair/dart/windows/BUILD | 86 --------- .../dart/windows/fast_pair_service_adapter.cc | 167 ------------------ .../dart/windows/fast_pair_service_adapter.h | 54 ------ .../windows/fast_pair_service_adapter_dart.cc | 110 ------------ .../windows/fast_pair_service_adapter_dart.h | 48 ----- fastpair/fast_pair_service.h | 4 + fastpair/keyed_service/fast_pair_mediator.h | 2 + 17 files changed, 6 insertions(+), 944 deletions(-) delete mode 100644 fastpair/dart/binding/BUILD delete mode 100644 fastpair/dart/binding/lib/bindings.dart delete mode 100644 fastpair/dart/binding/lib/device_metadata.dart delete mode 100644 fastpair/dart/binding/lib/fast_pair_mapping.dart delete mode 100644 fastpair/dart/binding/lib/ffi_types.dart delete mode 100644 fastpair/dart/binding/lib/listener.dart delete mode 100644 fastpair/dart/proto/BUILD delete mode 100644 fastpair/dart/proto/callbacks.proto delete mode 100644 fastpair/dart/proto/device.proto delete mode 100644 fastpair/dart/proto/enum.proto delete mode 100644 fastpair/dart/windows/BUILD delete mode 100644 fastpair/dart/windows/fast_pair_service_adapter.cc delete mode 100644 fastpair/dart/windows/fast_pair_service_adapter.h delete mode 100644 fastpair/dart/windows/fast_pair_service_adapter_dart.cc delete mode 100644 fastpair/dart/windows/fast_pair_service_adapter_dart.h diff --git a/fastpair/dart/binding/BUILD b/fastpair/dart/binding/BUILD deleted file mode 100644 index 6f9ceb85..00000000 --- a/fastpair/dart/binding/BUILD +++ /dev/null @@ -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", - ], -) diff --git a/fastpair/dart/binding/lib/bindings.dart b/fastpair/dart/binding/lib/bindings.dart deleted file mode 100644 index 2171d00f..00000000 --- a/fastpair/dart/binding/lib/bindings.dart +++ /dev/null @@ -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>( - 'InitMediatorDart', - ) - .asFunction(); - - late final CloseMediatorDartType closeMediator = _lib - .lookup>('CloseMediator') - .asFunction(); - - late final StartFastPairScanningDartType startFastPairScanning = _lib - .lookup>('StartScanDart') - .asFunction(); - - late final InitializeApiDartType initializeApi = _lib - .lookup>('Dart_InitializeApiDL') - .asFunction(); - - late final AddNotificationControllerObserverDartType - addNotificationControllerObserver = _lib - .lookup>( - 'AddNotificationControllerObserverDart') - .asFunction(); - - late final RemoveNotificationControllerObserverDartType - removeNotificationControllerObserver = _lib - .lookup>( - 'RemoveNotificationControllerObserverDart') - .asFunction(); - - late final DiscoveryClickedDartType discoveryClicked = _lib - .lookup>('DiscoveryClickedDart') - .asFunction(); -} diff --git a/fastpair/dart/binding/lib/device_metadata.dart b/fastpair/dart/binding/lib/device_metadata.dart deleted file mode 100644 index d1c2810f..00000000 --- a/fastpair/dart/binding/lib/device_metadata.dart +++ /dev/null @@ -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'], - ); - } -} diff --git a/fastpair/dart/binding/lib/fast_pair_mapping.dart b/fastpair/dart/binding/lib/fast_pair_mapping.dart deleted file mode 100644 index d33dd410..00000000 --- a/fastpair/dart/binding/lib/fast_pair_mapping.dart +++ /dev/null @@ -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 _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.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); - } -} diff --git a/fastpair/dart/binding/lib/ffi_types.dart b/fastpair/dart/binding/lib/ffi_types.dart deleted file mode 100644 index f92b402a..00000000 --- a/fastpair/dart/binding/lib/ffi_types.dart +++ /dev/null @@ -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 Function(); -typedef InitMediatorDartType = Pointer Function(); - -typedef CloseMediatorType = Void Function(Pointer /*service*/); -typedef CloseMediatorDartType = void Function(Pointer /*service*/); - -typedef StartFastPairScanningType = Void Function(Pointer /*service*/); -typedef StartFastPairScanningDartType = void Function( - Pointer /*service*/); - -typedef AddNotificationControllerObserverType = Void Function( - Pointer /*service*/, - Int64 /* callback*/, -); -typedef AddNotificationControllerObserverDartType = void Function( - Pointer /*service*/, - int /* callback*/, -); - -typedef RemoveNotificationControllerObserverType = Void Function( - Pointer /*service*/, - Int64 /* callback*/, -); -typedef RemoveNotificationControllerObserverDartType = void Function( - Pointer /*service*/, - int /* callback*/, -); - -typedef DiscoveryClickedType = Void Function( - Pointer /*service*/, - Int64 /* callback*/, -); -typedef DiscoveryClickedDartType = void Function( - Pointer /*service*/, - int /* callback*/, -); - -typedef InitializeApiType = IntPtr Function(Pointer); -typedef InitializeApiDartType = int Function(Pointer); - -//callback type -typedef OnMetadataChanged = void Function({ - required List deviceMetadata, -}); diff --git a/fastpair/dart/binding/lib/listener.dart b/fastpair/dart/binding/lib/listener.dart deleted file mode 100644 index 8d6662a0..00000000 --- a/fastpair/dart/binding/lib/listener.dart +++ /dev/null @@ -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); -} diff --git a/fastpair/dart/proto/BUILD b/fastpair/dart/proto/BUILD deleted file mode 100644 index 2890174d..00000000 --- a/fastpair/dart/proto/BUILD +++ /dev/null @@ -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"], -) diff --git a/fastpair/dart/proto/callbacks.proto b/fastpair/dart/proto/callbacks.proto deleted file mode 100644 index 0ba3d9a1..00000000 --- a/fastpair/dart/proto/callbacks.proto +++ /dev/null @@ -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; -} diff --git a/fastpair/dart/proto/device.proto b/fastpair/dart/proto/device.proto deleted file mode 100644 index b724ee79..00000000 --- a/fastpair/dart/proto/device.proto +++ /dev/null @@ -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; -} diff --git a/fastpair/dart/proto/enum.proto b/fastpair/dart/proto/enum.proto deleted file mode 100644 index c2932b05..00000000 --- a/fastpair/dart/proto/enum.proto +++ /dev/null @@ -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; -} diff --git a/fastpair/dart/windows/BUILD b/fastpair/dart/windows/BUILD deleted file mode 100644 index fcdebdd8..00000000 --- a/fastpair/dart/windows/BUILD +++ /dev/null @@ -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", - ], -) diff --git a/fastpair/dart/windows/fast_pair_service_adapter.cc b/fastpair/dart/windows/fast_pair_service_adapter.cc deleted file mode 100644 index 9122e1e9..00000000 --- a/fastpair/dart/windows/fast_pair_service_adapter.cc +++ /dev/null @@ -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 -#include - -#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(); - plugin_state->fast_pair_service->RegisterPluginProvider( - "admin", std::make_unique(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( - 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(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(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(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(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 diff --git a/fastpair/dart/windows/fast_pair_service_adapter.h b/fastpair/dart/windows/fast_pair_service_adapter.h deleted file mode 100644 index cdc2a8f0..00000000 --- a/fastpair/dart/windows/fast_pair_service_adapter.h +++ /dev/null @@ -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 - -#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_ diff --git a/fastpair/dart/windows/fast_pair_service_adapter_dart.cc b/fastpair/dart/windows/fast_pair_service_adapter_dart.cc deleted file mode 100644 index 43c22cd8..00000000 --- a/fastpair/dart/windows/fast_pair_service_adapter_dart.cc +++ /dev/null @@ -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 -#include -#include -#include - -#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> - *notification_controller_observer_map_ = new absl::flat_hash_map< - Dart_Port, std::unique_ptr>(); - -void AddNotificationControllerObserverDart(void *instance, Dart_Port port) { - auto observer = std::make_unique(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 diff --git a/fastpair/dart/windows/fast_pair_service_adapter_dart.h b/fastpair/dart/windows/fast_pair_service_adapter_dart.h deleted file mode 100644 index b59168ac..00000000 --- a/fastpair/dart/windows/fast_pair_service_adapter_dart.h +++ /dev/null @@ -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 - -#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_ diff --git a/fastpair/fast_pair_service.h b/fastpair/fast_pair_service.h index 7443d625..19d7d938 100644 --- a/fastpair/fast_pair_service.h +++ b/fastpair/fast_pair_service.h @@ -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. diff --git a/fastpair/keyed_service/fast_pair_mediator.h b/fastpair/keyed_service/fast_pair_mediator.h index 0265efcb..40569cfd 100644 --- a/fastpair/keyed_service/fast_pair_mediator.h +++ b/fastpair/keyed_service/fast_pair_mediator.h @@ -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(); }