#include "services/device/device_service.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "services/device/binder_overrides.h"
#include "services/device/fingerprint/fingerprint.h"
#include "services/device/generic_sensor/platform_sensor_provider.h"
#include "services/device/generic_sensor/sensor_provider_impl.h"
#include "services/device/geolocation/geolocation_config.h"
#include "services/device/geolocation/geolocation_context.h"
#include "services/device/geolocation/public_ip_address_geolocator.h"
#include "services/device/geolocation/public_ip_address_location_notifier.h"
#include "services/device/power_monitor/power_monitor_message_broadcaster.h"
#include "services/device/public/mojom/battery_monitor.mojom.h"
#include "services/device/serial/serial_port_manager_impl.h"
#include "services/device/time_zone_monitor/time_zone_monitor.h"
#include "services/device/vibration/vibration_manager_impl.h"
#include "services/device/wake_lock/wake_lock_provider.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/gfx/native_widget_types.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#include "services/device/device_service_jni_headers/InterfaceRegistrar_jni.h"
#include "services/device/screen_orientation/screen_orientation_listener_android.h"
#include "services/device/vibration/vibration_manager_android.h"
#else
#include "services/device/battery/battery_monitor_impl.h"
#include "services/device/battery/battery_status_service.h"
#include "services/device/hid/hid_manager_impl.h"
#endif
#if BUILDFLAG(ENABLE_COMPUTE_PRESSURE)
#include "services/device/compute_pressure/pressure_manager_impl.h"
#endif
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && defined(USE_UDEV)
#include "services/device/hid/input_service_linux.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chromeos/lacros/lacros_service.h"
#endif
namespace {
#if !BUILDFLAG(IS_ANDROID)
constexpr bool IsLaCrOS() { … }
#endif
#if !BUILDFLAG(IS_ANDROID)
void BindLaCrOSHidManager(
mojo::PendingReceiver<device::mojom::HidManager> receiver) { … }
#endif
}
namespace device {
DeviceServiceParams::DeviceServiceParams() = default;
DeviceServiceParams::~DeviceServiceParams() = default;
std::unique_ptr<DeviceService> CreateDeviceService(
std::unique_ptr<DeviceServiceParams> params,
mojo::PendingReceiver<mojom::DeviceService> receiver) { … }
DeviceService::DeviceService(
std::unique_ptr<DeviceServiceParams> params,
mojo::PendingReceiver<mojom::DeviceService> receiver)
: … { … }
DeviceService::~DeviceService() { … }
void DeviceService::AddReceiver(
mojo::PendingReceiver<mojom::DeviceService> receiver) { … }
void DeviceService::SetSensorProviderImplForTesting(
std::unique_ptr<SensorProviderImpl> sensor_provider) { … }
void DeviceService::OverrideGeolocationContextBinderForTesting(
GeolocationContextBinder binder) { … }
#if BUILDFLAG(ENABLE_COMPUTE_PRESSURE)
void DeviceService::OverridePressureManagerBinderForTesting(
PressureManagerBinder binder) { … }
#endif
void DeviceService::OverrideTimeZoneMonitorBinderForTesting(
TimeZoneMonitorBinder binder) { … }
void DeviceService::BindBatteryMonitor(
mojo::PendingReceiver<mojom::BatteryMonitor> receiver) { … }
#if BUILDFLAG(ENABLE_COMPUTE_PRESSURE)
void DeviceService::BindPressureManager(
mojo::PendingReceiver<mojom::PressureManager> receiver) { … }
#endif
#if BUILDFLAG(IS_ANDROID)
void DeviceService::OverrideNFCProviderBinderForTesting(
NFCProviderBinder binder) {
internal::GetNFCProviderBinderOverride() = std::move(binder);
}
void DeviceService::BindNFCProvider(
mojo::PendingReceiver<mojom::NFCProvider> receiver) {
const auto& binder_override = internal::GetNFCProviderBinderOverride();
if (binder_override)
binder_override.Run(std::move(receiver));
else
GetJavaInterfaceProvider()->GetInterface(std::move(receiver));
}
#endif
void DeviceService::BindVibrationManager(
mojo::PendingReceiver<mojom::VibrationManager> receiver,
mojo::PendingRemote<mojom::VibrationManagerListener> listener) { … }
#if !BUILDFLAG(IS_ANDROID)
void DeviceService::BindHidManager(
mojo::PendingReceiver<mojom::HidManager> receiver) { … }
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
void DeviceService::BindMtpManager(
mojo::PendingReceiver<mojom::MtpManager> receiver) {
if (!mtp_device_manager_)
mtp_device_manager_ = MtpDeviceManager::Initialize();
mtp_device_manager_->AddReceiver(std::move(receiver));
}
#endif
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && defined(USE_UDEV)
void DeviceService::BindInputDeviceManager(
mojo::PendingReceiver<mojom::InputDeviceManager> receiver) { … }
#endif
void DeviceService::BindFingerprint(
mojo::PendingReceiver<mojom::Fingerprint> receiver) { … }
void DeviceService::BindGeolocationConfig(
mojo::PendingReceiver<mojom::GeolocationConfig> receiver) { … }
void DeviceService::BindGeolocationContext(
mojo::PendingReceiver<mojom::GeolocationContext> receiver) { … }
void DeviceService::BindGeolocationControl(
mojo::PendingReceiver<mojom::GeolocationControl> receiver) { … }
void DeviceService::BindGeolocationInternals(
mojo::PendingReceiver<mojom::GeolocationInternals> receiver) { … }
void DeviceService::BindPowerMonitor(
mojo::PendingReceiver<mojom::PowerMonitor> receiver) { … }
void DeviceService::BindPublicIpAddressGeolocationProvider(
mojo::PendingReceiver<mojom::PublicIpAddressGeolocationProvider> receiver) { … }
void DeviceService::BindScreenOrientationListener(
mojo::PendingReceiver<mojom::ScreenOrientationListener> receiver) { … }
void DeviceService::BindSensorProvider(
mojo::PendingReceiver<mojom::SensorProvider> receiver) { … }
void DeviceService::BindSerialPortManager(
mojo::PendingReceiver<mojom::SerialPortManager> receiver) { … }
void DeviceService::BindTimeZoneMonitor(
mojo::PendingReceiver<mojom::TimeZoneMonitor> receiver) { … }
void DeviceService::BindWakeLockProvider(
mojo::PendingReceiver<mojom::WakeLockProvider> receiver) { … }
void DeviceService::BindUsbDeviceManager(
mojo::PendingReceiver<mojom::UsbDeviceManager> receiver) { … }
void DeviceService::BindUsbDeviceManagerTest(
mojo::PendingReceiver<mojom::UsbDeviceManagerTest> receiver) { … }
#if BUILDFLAG(IS_ANDROID)
service_manager::InterfaceProvider* DeviceService::GetJavaInterfaceProvider() {
if (!java_interface_provider_initialized_) {
mojo::PendingRemote<service_manager::mojom::InterfaceProvider> provider;
JNIEnv* env = base::android::AttachCurrentThread();
Java_InterfaceRegistrar_createInterfaceRegistryForContext(
env,
provider.InitWithNewPipeAndPassReceiver().PassPipe().release().value(),
java_nfc_delegate_);
java_interface_provider_.Bind(std::move(provider));
java_interface_provider_initialized_ = true;
}
return &java_interface_provider_;
}
#endif
}