Fix emulator build error
[platform/framework/web/chromium-efl.git] / services / device / device_service.cc
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "services/device/device_service.h"
6
7 #include <utility>
8
9 #include "base/functional/bind.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/task/single_thread_task_runner.h"
12 #include "base/task/thread_pool.h"
13 #include "build/build_config.h"
14 #include "build/chromeos_buildflags.h"
15 #include "mojo/public/cpp/bindings/pending_remote.h"
16 #include "mojo/public/cpp/system/message_pipe.h"
17 #include "services/device/binder_overrides.h"
18 #include "services/device/compute_pressure/pressure_manager_impl.h"
19 #include "services/device/device_posture/device_posture_platform_provider.h"
20 #include "services/device/device_posture/device_posture_provider_impl.h"
21 #include "services/device/fingerprint/fingerprint.h"
22 #include "services/device/generic_sensor/platform_sensor_provider.h"
23 #include "services/device/generic_sensor/sensor_provider_impl.h"
24 #include "services/device/geolocation/geolocation_config.h"
25 #include "services/device/geolocation/geolocation_context.h"
26 #include "services/device/geolocation/public_ip_address_geolocator.h"
27 #include "services/device/geolocation/public_ip_address_location_notifier.h"
28 #include "services/device/power_monitor/power_monitor_message_broadcaster.h"
29 #include "services/device/public/mojom/battery_monitor.mojom.h"
30 #include "services/device/serial/serial_port_manager_impl.h"
31 #include "services/device/time_zone_monitor/time_zone_monitor.h"
32 #include "services/device/wake_lock/wake_lock_provider.h"
33 #include "services/network/public/cpp/shared_url_loader_factory.h"
34 #include "ui/gfx/native_widget_types.h"
35
36 #if BUILDFLAG(IS_ANDROID)
37 #include "base/android/jni_android.h"
38 #include "services/device/device_service_jni_headers/InterfaceRegistrar_jni.h"
39 #include "services/device/screen_orientation/screen_orientation_listener_android.h"
40 #else
41 #include "services/device/battery/battery_monitor_impl.h"
42 #include "services/device/battery/battery_status_service.h"
43 #include "services/device/hid/hid_manager_impl.h"
44 #include "services/device/vibration/vibration_manager_impl.h"
45 #endif
46
47 #if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && defined(USE_UDEV)
48 #include "services/device/hid/input_service_linux.h"
49 #endif
50
51 #if BUILDFLAG(IS_CHROMEOS_LACROS)
52 #include "chromeos/lacros/lacros_service.h"
53 #endif
54
55 namespace {
56
57 #if !BUILDFLAG(IS_ANDROID)
58 constexpr bool IsLaCrOS() {
59 #if BUILDFLAG(IS_CHROMEOS_LACROS)
60   return true;
61 #else
62   return false;
63 #endif
64 }
65 #endif
66
67 #if !BUILDFLAG(IS_ANDROID)
68 void BindLaCrOSHidManager(
69     mojo::PendingReceiver<device::mojom::HidManager> receiver) {
70 #if BUILDFLAG(IS_CHROMEOS_LACROS)
71   // LaCrOS does not have direct access to the permission_broker service over
72   // D-Bus. Use the HidManager interface from ash-chrome instead.
73   auto* lacros_service = chromeos::LacrosService::Get();
74   DCHECK(lacros_service);
75   // If the Hid manager is not available, then the pending receiver is deleted.
76   if (lacros_service->IsAvailable<device::mojom::HidManager>()) {
77     lacros_service->GetRemote<device::mojom::HidManager>()->AddReceiver(
78         std::move(receiver));
79   }
80 #endif
81 }
82 #endif
83
84 }  // namespace
85
86 namespace device {
87
88 DeviceServiceParams::DeviceServiceParams() = default;
89
90 DeviceServiceParams::~DeviceServiceParams() = default;
91
92 std::unique_ptr<DeviceService> CreateDeviceService(
93     std::unique_ptr<DeviceServiceParams> params,
94     mojo::PendingReceiver<mojom::DeviceService> receiver) {
95   GeolocationProviderImpl::SetGeolocationConfiguration(
96       params->url_loader_factory, params->geolocation_api_key,
97       params->custom_location_provider_callback, params->geolocation_manager,
98       params->use_gms_core_location_provider);
99   return std::make_unique<DeviceService>(std::move(params),
100                                          std::move(receiver));
101 }
102
103 DeviceService::DeviceService(
104     std::unique_ptr<DeviceServiceParams> params,
105     mojo::PendingReceiver<mojom::DeviceService> receiver)
106     : file_task_runner_(std::move(params->file_task_runner)),
107       io_task_runner_(std::move(params->io_task_runner)),
108       url_loader_factory_(std::move(params->url_loader_factory)),
109       network_connection_tracker_(params->network_connection_tracker),
110       geolocation_api_key_(params->geolocation_api_key),
111       wake_lock_context_callback_(params->wake_lock_context_callback),
112       wake_lock_provider_(file_task_runner_,
113                           params->wake_lock_context_callback) {
114   receivers_.Add(this, std::move(receiver));
115
116 #if BUILDFLAG(IS_ANDROID)
117   java_nfc_delegate_.Reset(params->java_nfc_delegate);
118 #endif
119
120 #if defined(IS_SERIAL_ENABLED_PLATFORM)
121 #if BUILDFLAG(IS_MAC)
122   // On macOS the SerialDeviceEnumerator needs to run on the UI thread so that
123   // it has access to a CFRunLoop where it can register a notification source.
124   auto serial_port_manager_task_runner =
125       base::SingleThreadTaskRunner::GetCurrentDefault();
126 #else
127   // On other platforms it must be allowed to do blocking IO.
128   auto serial_port_manager_task_runner =
129       base::ThreadPool::CreateSequencedTaskRunner(
130           {base::MayBlock(), base::TaskPriority::BEST_EFFORT});
131 #endif
132   serial_port_manager_.emplace(
133       std::move(serial_port_manager_task_runner), io_task_runner_,
134       base::SingleThreadTaskRunner::GetCurrentDefault());
135 #endif  // defined(IS_SERIAL_ENABLED_PLATFORM)
136
137 #if !BUILDFLAG(IS_ANDROID)
138   // Ensure that the battery backend is initialized now; otherwise it may end up
139   // getting initialized on access during destruction, when it's no longer safe
140   // to initialize.
141   device::BatteryStatusService::GetInstance();
142 #endif  // !BUILDFLAG(IS_ANDROID)
143 }
144
145 DeviceService::~DeviceService() {
146 #if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_CHROMEOS_ASH)
147   // NOTE: We don't call this on Chrome OS due to https://crbug.com/856771, as
148   // Shutdown() implicitly depends on DBusThreadManager, which may already be
149   // destroyed by the time DeviceService is destroyed. Fortunately on Chrome OS
150   // it's not really important that this runs anyway.
151   device::BatteryStatusService::GetInstance()->Shutdown();
152 #endif
153 }
154
155 void DeviceService::AddReceiver(
156     mojo::PendingReceiver<mojom::DeviceService> receiver) {
157   receivers_.Add(this, std::move(receiver));
158 }
159
160 void DeviceService::SetSensorProviderImplForTesting(
161     std::unique_ptr<SensorProviderImpl> sensor_provider) {
162   CHECK(!sensor_provider_);
163   sensor_provider_ = std::move(sensor_provider);
164 }
165
166 // static
167 void DeviceService::OverrideGeolocationContextBinderForTesting(
168     GeolocationContextBinder binder) {
169   internal::GetGeolocationContextBinderOverride() = std::move(binder);
170 }
171
172 // static
173 void DeviceService::OverridePressureManagerBinderForTesting(
174     PressureManagerBinder binder) {
175   internal::GetPressureManagerBinderOverride() = std::move(binder);
176 }
177
178 void DeviceService::BindBatteryMonitor(
179     mojo::PendingReceiver<mojom::BatteryMonitor> receiver) {
180 #if BUILDFLAG(IS_ANDROID)
181   GetJavaInterfaceProvider()->GetInterface(std::move(receiver));
182 #else
183   BatteryMonitorImpl::Create(std::move(receiver));
184 #endif
185 }
186
187 void DeviceService::BindPressureManager(
188     mojo::PendingReceiver<mojom::PressureManager> receiver) {
189   const auto& binder_override = internal::GetPressureManagerBinderOverride();
190   if (binder_override) {
191     binder_override.Run(std::move(receiver));
192     return;
193   }
194
195   if (!pressure_manager_)
196     pressure_manager_ = PressureManagerImpl::Create();
197   pressure_manager_->Bind(std::move(receiver));
198 }
199
200 #if BUILDFLAG(IS_ANDROID)
201 // static
202 void DeviceService::OverrideNFCProviderBinderForTesting(
203     NFCProviderBinder binder) {
204   internal::GetNFCProviderBinderOverride() = std::move(binder);
205 }
206
207 void DeviceService::BindNFCProvider(
208     mojo::PendingReceiver<mojom::NFCProvider> receiver) {
209   const auto& binder_override = internal::GetNFCProviderBinderOverride();
210   if (binder_override)
211     binder_override.Run(std::move(receiver));
212   else
213     GetJavaInterfaceProvider()->GetInterface(std::move(receiver));
214 }
215 #endif
216
217 void DeviceService::BindVibrationManager(
218     mojo::PendingReceiver<mojom::VibrationManager> receiver) {
219 #if BUILDFLAG(IS_ANDROID)
220   GetJavaInterfaceProvider()->GetInterface(std::move(receiver));
221 #else
222   VibrationManagerImpl::Create(std::move(receiver));
223 #endif
224 }
225
226 #if !BUILDFLAG(IS_ANDROID)
227 void DeviceService::BindHidManager(
228     mojo::PendingReceiver<mojom::HidManager> receiver) {
229   if (IsLaCrOS() && !HidManagerImpl::IsHidServiceTesting()) {
230     BindLaCrOSHidManager(std::move(receiver));
231   } else {
232     if (!hid_manager_)
233       hid_manager_ = std::make_unique<HidManagerImpl>();
234     hid_manager_->AddReceiver(std::move(receiver));
235   }
236 }
237 #endif
238
239 #if BUILDFLAG(IS_CHROMEOS_ASH)
240 void DeviceService::BindMtpManager(
241     mojo::PendingReceiver<mojom::MtpManager> receiver) {
242   if (!mtp_device_manager_)
243     mtp_device_manager_ = MtpDeviceManager::Initialize();
244   mtp_device_manager_->AddReceiver(std::move(receiver));
245 }
246 #endif
247
248 #if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && defined(USE_UDEV)
249 void DeviceService::BindInputDeviceManager(
250     mojo::PendingReceiver<mojom::InputDeviceManager> receiver) {
251   file_task_runner_->PostTask(
252       FROM_HERE,
253       base::BindOnce(&InputServiceLinux::BindReceiver, std::move(receiver)));
254 }
255 #endif
256
257 void DeviceService::BindFingerprint(
258     mojo::PendingReceiver<mojom::Fingerprint> receiver) {
259   Fingerprint::Create(std::move(receiver));
260 }
261
262 void DeviceService::BindGeolocationConfig(
263     mojo::PendingReceiver<mojom::GeolocationConfig> receiver) {
264   GeolocationConfig::Create(std::move(receiver));
265 }
266
267 void DeviceService::BindGeolocationContext(
268     mojo::PendingReceiver<mojom::GeolocationContext> receiver) {
269   const auto& binder_override = internal::GetGeolocationContextBinderOverride();
270   if (binder_override) {
271     binder_override.Run(std::move(receiver));
272     return;
273   }
274
275   GeolocationContext::Create(std::move(receiver));
276 }
277
278 void DeviceService::BindGeolocationControl(
279     mojo::PendingReceiver<mojom::GeolocationControl> receiver) {
280   GeolocationProviderImpl::GetInstance()->BindGeolocationControlReceiver(
281       std::move(receiver));
282 }
283
284 void DeviceService::BindGeolocationInternals(
285     mojo::PendingReceiver<mojom::GeolocationInternals> receiver) {
286   GeolocationProviderImpl::GetInstance()->BindGeolocationInternalsReceiver(
287       std::move(receiver));
288 }
289
290 void DeviceService::BindPowerMonitor(
291     mojo::PendingReceiver<mojom::PowerMonitor> receiver) {
292   if (!power_monitor_message_broadcaster_) {
293     power_monitor_message_broadcaster_ =
294         std::make_unique<PowerMonitorMessageBroadcaster>();
295   }
296   power_monitor_message_broadcaster_->Bind(std::move(receiver));
297 }
298
299 void DeviceService::BindPublicIpAddressGeolocationProvider(
300     mojo::PendingReceiver<mojom::PublicIpAddressGeolocationProvider> receiver) {
301   if (!public_ip_address_geolocation_provider_) {
302     public_ip_address_geolocation_provider_ =
303         std::make_unique<PublicIpAddressGeolocationProvider>(
304             url_loader_factory_, network_connection_tracker_,
305             geolocation_api_key_);
306   }
307   public_ip_address_geolocation_provider_->Bind(std::move(receiver));
308 }
309
310 void DeviceService::BindScreenOrientationListener(
311     mojo::PendingReceiver<mojom::ScreenOrientationListener> receiver) {
312 #if BUILDFLAG(IS_ANDROID)
313   if (io_task_runner_) {
314     io_task_runner_->PostTask(
315         FROM_HERE, base::BindOnce(&ScreenOrientationListenerAndroid::Create,
316                                   std::move(receiver)));
317   }
318 #endif
319 }
320
321 void DeviceService::BindSensorProvider(
322     mojo::PendingReceiver<mojom::SensorProvider> receiver) {
323   if (!sensor_provider_) {
324     auto platform_provider = PlatformSensorProvider::Create();
325     if (!platform_provider)
326       return;
327     sensor_provider_ =
328         std::make_unique<SensorProviderImpl>(std::move(platform_provider));
329   }
330   sensor_provider_->Bind(std::move(receiver));
331 }
332
333 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
334 void DeviceService::BindDevicePostureProvider(
335     mojo::PendingReceiver<mojom::DevicePostureProvider> receiver) {
336   if (!device_posture_provider_) {
337     auto posture_platform_provider_ = DevicePosturePlatformProvider::Create();
338     if (!posture_platform_provider_)
339       return;
340     device_posture_provider_ = std::make_unique<DevicePostureProviderImpl>(
341         std::move(posture_platform_provider_));
342   }
343   device_posture_provider_->Bind(std::move(receiver));
344 }
345 #endif
346
347 void DeviceService::BindSerialPortManager(
348     mojo::PendingReceiver<mojom::SerialPortManager> receiver) {
349 #if defined(IS_SERIAL_ENABLED_PLATFORM)
350   serial_port_manager_.AsyncCall(&SerialPortManagerImpl::Bind, FROM_HERE)
351       .WithArgs(std::move(receiver));
352 #else   // defined(IS_SERIAL_ENABLED_PLATFORM)
353   NOTREACHED() << "Serial devices not supported on this platform.";
354 #endif  // defined(IS_SERIAL_ENABLED_PLATFORM)
355 }
356
357 void DeviceService::BindTimeZoneMonitor(
358     mojo::PendingReceiver<mojom::TimeZoneMonitor> receiver) {
359   if (!time_zone_monitor_)
360     time_zone_monitor_ = TimeZoneMonitor::Create(file_task_runner_);
361   time_zone_monitor_->Bind(std::move(receiver));
362 }
363
364 void DeviceService::BindWakeLockProvider(
365     mojo::PendingReceiver<mojom::WakeLockProvider> receiver) {
366   wake_lock_provider_.AddBinding(std::move(receiver));
367 }
368
369 void DeviceService::BindUsbDeviceManager(
370     mojo::PendingReceiver<mojom::UsbDeviceManager> receiver) {
371   // TODO(crbug.com/1109621): usb::DeviceManagerImpl depends on the
372   // permission_broker service on Chromium OS. We will need to redirect
373   // connections for LaCrOS here.
374   if (!usb_device_manager_)
375     usb_device_manager_ = std::make_unique<usb::DeviceManagerImpl>();
376
377   usb_device_manager_->AddReceiver(std::move(receiver));
378 }
379
380 void DeviceService::BindUsbDeviceManagerTest(
381     mojo::PendingReceiver<mojom::UsbDeviceManagerTest> receiver) {
382   // TODO(crbug.com/1109621): usb::DeviceManagerImpl depends on the
383   // permission_broker service on Chromium OS. We will need to redirect
384   // connections for LaCrOS here.
385   if (!usb_device_manager_)
386     usb_device_manager_ = std::make_unique<usb::DeviceManagerImpl>();
387
388   if (!usb_device_manager_test_) {
389     usb_device_manager_test_ = std::make_unique<usb::DeviceManagerTest>(
390         usb_device_manager_->GetUsbService());
391   }
392
393   usb_device_manager_test_->BindReceiver(std::move(receiver));
394 }
395
396 #if BUILDFLAG(IS_ANDROID)
397 service_manager::InterfaceProvider* DeviceService::GetJavaInterfaceProvider() {
398   if (!java_interface_provider_initialized_) {
399     mojo::PendingRemote<service_manager::mojom::InterfaceProvider> provider;
400     JNIEnv* env = base::android::AttachCurrentThread();
401     Java_InterfaceRegistrar_createInterfaceRegistryForContext(
402         env,
403         provider.InitWithNewPipeAndPassReceiver().PassPipe().release().value(),
404         java_nfc_delegate_);
405     java_interface_provider_.Bind(std::move(provider));
406
407     java_interface_provider_initialized_ = true;
408   }
409
410   return &java_interface_provider_;
411 }
412 #endif
413
414 #if BUILDFLAG(IS_TIZEN_TV)
415 void DeviceService::UpdateTimeZone(const std::string& timezone) {
416   if (time_zone_monitor_)
417     time_zone_monitor_->UpdateTimeZone(timezone);
418 }
419 #endif
420 }  // namespace device