Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: device/generic_sensor/polling_platform_sensor.cc

Issue 2370343002: [sensors] Ambient light sensor implementation for ChromeOS and Linux. (Closed)
Patch Set: ctor and dtor for deleter Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/polling_platform_sensor.h"
6
7 #include "base/message_loop/message_loop.h"
8
9 namespace device {
10
11 PollingPlatformSensor::Deleter::Deleter() = default;
12 PollingPlatformSensor::Deleter::~Deleter() = default;
13
14 void PollingPlatformSensor::Deleter::operator()(
15 base::RepeatingTimer* timer) const {
16 DCHECK(task_runner_);
17 task_runner_->PostTask(FROM_HERE, base::Bind(&Deleter::destroy_timer,
18 base::Unretained(this), timer));
19 }
20
21 PollingPlatformSensor::PollingPlatformSensor(
22 mojom::SensorType type,
23 mojo::ScopedSharedBufferMapping mapping,
24 PlatformSensorProvider* provider,
25 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner)
26 : PlatformSensor(type, std::move(mapping), provider),
27 polling_thread_task_runner_(base::MessageLoop::current()->task_runner()),
28 ipc_task_runner_(ipc_task_runner),
29 weak_factory_(this) {}
30
31 PollingPlatformSensor::~PollingPlatformSensor() = default;
32
33 bool PollingPlatformSensor::StartSensor(
34 const PlatformSensorConfiguration& configuration) {
35 return polling_thread_task_runner_->PostTask(
36 FROM_HERE, base::Bind(&PollingPlatformSensor::BeginPoll,
37 weak_factory_.GetWeakPtr(), configuration));
38 }
39
40 void PollingPlatformSensor::BeginPoll(
41 const PlatformSensorConfiguration& configuration) {
42 timer_.reset(new base::RepeatingTimer());
43 timer_.get_deleter().set_helper(polling_thread_task_runner_);
44 timer_->Start(FROM_HERE, base::TimeDelta::FromMicroseconds(
45 base::Time::kMicrosecondsPerSecond /
46 configuration.frequency()),
47 this, &PollingPlatformSensor::UpdateReading);
48 }
49
50 void PollingPlatformSensor::StopSensor() {
51 polling_thread_task_runner_->PostTask(
52 FROM_HERE, base::Bind(&PollingPlatformSensor::StopPoll, this));
53 }
54
55 void PollingPlatformSensor::StopPoll() {
56 timer_->Stop();
57 }
58
59 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698