1// Copyright 2014 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 "content/browser/device_sensors/device_light_message_filter.h"
6
7#include "content/browser/device_sensors/device_inertial_sensor_service.h"
8#include "content/common/device_sensors/device_light_messages.h"
9
10namespace content {
11
12DeviceLightMessageFilter::DeviceLightMessageFilter()
13    : BrowserMessageFilter(DeviceLightMsgStart), is_started_(false) {
14}
15
16DeviceLightMessageFilter::~DeviceLightMessageFilter() {
17  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
18  if (is_started_) {
19    DeviceInertialSensorService::GetInstance()->RemoveConsumer(
20        CONSUMER_TYPE_LIGHT);
21  }
22}
23
24bool DeviceLightMessageFilter::OnMessageReceived(const IPC::Message& message) {
25  bool handled = true;
26  IPC_BEGIN_MESSAGE_MAP(DeviceLightMessageFilter, message)
27  IPC_MESSAGE_HANDLER(DeviceLightHostMsg_StartPolling,
28                      OnDeviceLightStartPolling)
29  IPC_MESSAGE_HANDLER(DeviceLightHostMsg_StopPolling, OnDeviceLightStopPolling)
30  IPC_MESSAGE_UNHANDLED(handled = false)
31  IPC_END_MESSAGE_MAP()
32  return handled;
33}
34
35void DeviceLightMessageFilter::OnDeviceLightStartPolling() {
36  DCHECK(!is_started_);
37  if (is_started_)
38    return;
39  is_started_ = true;
40  DeviceInertialSensorService::GetInstance()->AddConsumer(CONSUMER_TYPE_LIGHT);
41  DidStartDeviceLightPolling();
42}
43
44void DeviceLightMessageFilter::OnDeviceLightStopPolling() {
45  DCHECK(is_started_);
46  if (!is_started_)
47    return;
48  is_started_ = false;
49  DeviceInertialSensorService::GetInstance()->RemoveConsumer(
50      CONSUMER_TYPE_LIGHT);
51}
52
53void DeviceLightMessageFilter::DidStartDeviceLightPolling() {
54  Send(new DeviceLightMsg_DidStartPolling(
55      DeviceInertialSensorService::GetInstance()
56          ->GetSharedMemoryHandleForProcess(CONSUMER_TYPE_LIGHT,
57                                            PeerHandle())));
58}
59
60}  // namespace content
61