- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / device_orientation / device_motion_message_filter.cc
1 // Copyright 2013 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_orientation/device_motion_message_filter.h"
6
7 #include "content/browser/device_orientation/device_inertial_sensor_service.h"
8 #include "content/common/device_orientation/device_motion_messages.h"
9
10 namespace content {
11
12 DeviceMotionMessageFilter::DeviceMotionMessageFilter()
13     : is_started_(false) {
14 }
15
16 DeviceMotionMessageFilter::~DeviceMotionMessageFilter() {
17   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
18   if (is_started_)
19     DeviceInertialSensorService::GetInstance()->RemoveConsumer(
20         CONSUMER_TYPE_MOTION);
21 }
22
23 bool DeviceMotionMessageFilter::OnMessageReceived(
24     const IPC::Message& message,
25     bool* message_was_ok) {
26   bool handled = true;
27   IPC_BEGIN_MESSAGE_MAP_EX(DeviceMotionMessageFilter,
28                            message,
29                            *message_was_ok)
30     IPC_MESSAGE_HANDLER(DeviceMotionHostMsg_StartPolling,
31                         OnDeviceMotionStartPolling)
32     IPC_MESSAGE_HANDLER(DeviceMotionHostMsg_StopPolling,
33                         OnDeviceMotionStopPolling)
34     IPC_MESSAGE_UNHANDLED(handled = false)
35   IPC_END_MESSAGE_MAP_EX()
36   return handled;
37 }
38
39 void DeviceMotionMessageFilter::OnDeviceMotionStartPolling() {
40   DCHECK(!is_started_);
41   if (is_started_)
42     return;
43   is_started_ = true;
44   DeviceInertialSensorService::GetInstance()->AddConsumer(
45       CONSUMER_TYPE_MOTION);
46   DidStartDeviceMotionPolling();
47 }
48
49 void DeviceMotionMessageFilter::OnDeviceMotionStopPolling() {
50   DCHECK(is_started_);
51   if (!is_started_)
52     return;
53   is_started_ = false;
54   DeviceInertialSensorService::GetInstance()->RemoveConsumer(
55       CONSUMER_TYPE_MOTION);
56 }
57
58 void DeviceMotionMessageFilter::DidStartDeviceMotionPolling() {
59   Send(new DeviceMotionMsg_DidStartPolling(
60       DeviceInertialSensorService::GetInstance()->
61           GetSharedMemoryHandleForProcess(
62               CONSUMER_TYPE_MOTION, PeerHandle())));
63 }
64
65 }  // namespace content