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