OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
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 "device/generic_sensor/sensor_provider_impl.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "device/generic_sensor/platform_sensor_provider.h" | |
12 #include "device/generic_sensor/sensor_impl.h" | |
13 #include "mojo/public/cpp/bindings/strong_binding.h" | |
14 | |
15 namespace device { | |
16 | |
17 namespace { | |
18 | |
19 void RunCallback(mojom::SensorInitParamsPtr init_params, | |
20 mojom::SensorClientRequest client, | |
21 const SensorProviderImpl::GetSensorCallback& callback) { | |
22 callback.Run(std::move(init_params), std::move(client)); | |
23 } | |
24 | |
25 void NotifySensorCreated( | |
26 mojom::SensorInitParamsPtr init_params, | |
27 mojom::SensorClientRequest client, | |
28 const SensorProviderImpl::GetSensorCallback& callback) { | |
29 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
30 FROM_HERE, base::Bind(RunCallback, base::Passed(&init_params), | |
31 base::Passed(&client), callback)); | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 // static | |
37 void SensorProviderImpl::Create( | |
38 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | |
39 mojom::SensorProviderRequest request) { | |
40 PlatformSensorProvider* provider = PlatformSensorProvider::GetInstance(); | |
41 if (provider) { | |
42 provider->SetFileTaskRunner(file_task_runner); | |
43 mojo::MakeStrongBinding(base::WrapUnique(new SensorProviderImpl(provider)), | |
44 std::move(request)); | |
45 } | |
46 } | |
47 | |
48 SensorProviderImpl::SensorProviderImpl(PlatformSensorProvider* provider) | |
49 : provider_(provider), weak_ptr_factory_(this) { | |
50 DCHECK(provider_); | |
51 } | |
52 | |
53 SensorProviderImpl::~SensorProviderImpl() {} | |
54 | |
55 void SensorProviderImpl::GetSensor(mojom::SensorType type, | |
56 mojom::SensorRequest sensor_request, | |
57 const GetSensorCallback& callback) { | |
58 auto cloned_handle = provider_->CloneSharedBufferHandle(); | |
59 if (!cloned_handle.is_valid()) { | |
60 NotifySensorCreated(nullptr, nullptr, callback); | |
61 return; | |
62 } | |
63 | |
64 scoped_refptr<PlatformSensor> sensor = provider_->GetSensor(type); | |
65 if (!sensor) { | |
66 PlatformSensorProviderBase::CreateSensorCallback cb = base::Bind( | |
67 &SensorProviderImpl::SensorCreated, weak_ptr_factory_.GetWeakPtr(), | |
68 type, base::Passed(&cloned_handle), base::Passed(&sensor_request), | |
69 callback); | |
70 provider_->CreateSensor(type, cb); | |
71 return; | |
72 } | |
73 | |
74 SensorCreated(type, std::move(cloned_handle), std::move(sensor_request), | |
75 callback, std::move(sensor)); | |
76 } | |
77 | |
78 void SensorProviderImpl::SensorCreated( | |
79 mojom::SensorType type, | |
80 mojo::ScopedSharedBufferHandle cloned_handle, | |
81 mojom::SensorRequest sensor_request, | |
82 const GetSensorCallback& callback, | |
83 scoped_refptr<PlatformSensor> sensor) { | |
84 if (!sensor) { | |
85 NotifySensorCreated(nullptr, nullptr, callback); | |
86 return; | |
87 } | |
88 | |
89 auto sensor_impl = base::MakeUnique<SensorImpl>(sensor); | |
90 | |
91 auto init_params = mojom::SensorInitParams::New(); | |
92 init_params->memory = std::move(cloned_handle); | |
93 init_params->buffer_offset = SensorReadingSharedBuffer::GetOffset(type); | |
94 init_params->mode = sensor->GetReportingMode(); | |
95 init_params->default_configuration = sensor->GetDefaultConfiguration(); | |
96 | |
97 double maximum_frequency = sensor->GetMaximumSupportedFrequency(); | |
98 DCHECK_GT(maximum_frequency, 0.0); | |
99 if (maximum_frequency > mojom::SensorConfiguration::kMaxAllowedFrequency) | |
100 maximum_frequency = mojom::SensorConfiguration::kMaxAllowedFrequency; | |
101 | |
102 init_params->maximum_frequency = maximum_frequency; | |
103 init_params->minimum_frequency = sensor->GetMinimumSupportedFrequency(); | |
104 DCHECK_GT(init_params->minimum_frequency, 0.0); | |
105 | |
106 NotifySensorCreated(std::move(init_params), sensor_impl->GetClient(), | |
107 callback); | |
108 | |
109 mojo::MakeStrongBinding(std::move(sensor_impl), std::move(sensor_request)); | |
110 } | |
111 | |
112 } // namespace device | |
OLD | NEW |