Home | History | Annotate | Download | only in device_sensors
      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 "device_orientation_event_pump.h"
      6 
      7 #include <cmath>
      8 
      9 #include "content/common/device_sensors/device_orientation_messages.h"
     10 #include "content/public/renderer/render_thread.h"
     11 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h"
     12 
     13 namespace content {
     14 
     15 const double DeviceOrientationEventPump::kOrientationThreshold = 0.1;
     16 
     17 DeviceOrientationEventPump::DeviceOrientationEventPump(RenderThread* thread)
     18     : DeviceSensorEventPump<blink::WebDeviceOrientationListener>(thread) {
     19 }
     20 
     21 DeviceOrientationEventPump::~DeviceOrientationEventPump() {
     22 }
     23 
     24 bool DeviceOrientationEventPump::OnControlMessageReceived(
     25     const IPC::Message& message) {
     26   bool handled = true;
     27   IPC_BEGIN_MESSAGE_MAP(DeviceOrientationEventPump, message)
     28     IPC_MESSAGE_HANDLER(DeviceOrientationMsg_DidStartPolling, OnDidStart)
     29     IPC_MESSAGE_UNHANDLED(handled = false)
     30   IPC_END_MESSAGE_MAP()
     31   return handled;
     32 }
     33 
     34 void DeviceOrientationEventPump::FireEvent() {
     35   DCHECK(listener());
     36   blink::WebDeviceOrientationData data;
     37   if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) {
     38     memcpy(&data_, &data, sizeof(data));
     39     listener()->didChangeDeviceOrientation(data);
     40   }
     41 }
     42 
     43 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1,
     44     bool hasAngle2, double angle2) {
     45   if (hasAngle1 != hasAngle2)
     46     return true;
     47   return (hasAngle1 && std::fabs(angle1 - angle2) >=
     48           DeviceOrientationEventPump::kOrientationThreshold);
     49 }
     50 
     51 bool DeviceOrientationEventPump::ShouldFireEvent(
     52     const blink::WebDeviceOrientationData& data) const {
     53   if (!data.allAvailableSensorsAreActive)
     54     return false;
     55 
     56   if (!data.hasAlpha && !data.hasBeta && !data.hasGamma) {
     57     // no data can be provided, this is an all-null event.
     58     return true;
     59   }
     60 
     61   return IsSignificantlyDifferent(
     62              data_.hasAlpha, data_.alpha, data.hasAlpha, data.alpha) ||
     63          IsSignificantlyDifferent(
     64              data_.hasBeta, data_.beta, data.hasBeta, data.beta) ||
     65          IsSignificantlyDifferent(
     66              data_.hasGamma, data_.gamma, data.hasGamma, data.gamma);
     67 }
     68 
     69 bool DeviceOrientationEventPump::InitializeReader(
     70     base::SharedMemoryHandle handle) {
     71   memset(&data_, 0, sizeof(data_));
     72   if (!reader_)
     73     reader_.reset(new DeviceOrientationSharedMemoryReader());
     74   return reader_->Initialize(handle);
     75 }
     76 
     77 void DeviceOrientationEventPump::SendStartMessage() {
     78   RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling());
     79 }
     80 
     81 void DeviceOrientationEventPump::SendStopMessage() {
     82   RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling());
     83 }
     84 
     85 void DeviceOrientationEventPump::SendFakeDataForTesting(void* fake_data) {
     86   blink::WebDeviceOrientationData data =
     87       *static_cast<blink::WebDeviceOrientationData*>(fake_data);
     88 
     89   listener()->didChangeDeviceOrientation(data);
     90 }
     91 
     92 }  // namespace content
     93