1/*
2 * Copyright 2010, The Android Open Source Project
3 * Copyright (C) 2012 Samsung Electronics. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "DeviceOrientationController.h"
29
30#include "DeviceOrientationClient.h"
31#include "DeviceOrientationData.h"
32#include "DeviceOrientationEvent.h"
33#include "Page.h"
34
35namespace WebCore {
36
37DeviceOrientationController::DeviceOrientationController(DeviceOrientationClient* client)
38    : DeviceController(client)
39{
40#if PLATFORM(IOS)
41    // FIXME: Temporarily avoid asserting while OpenSource is using a different design.
42    // We should reconcile the differences between OpenSource and iOS so that we can
43    // remove this code path.
44    if (m_client)
45        deviceOrientationClient()->setController(this);
46#else
47    ASSERT(m_client);
48    deviceOrientationClient()->setController(this);
49#endif
50}
51
52void DeviceOrientationController::didChangeDeviceOrientation(DeviceOrientationData* orientation)
53{
54    dispatchDeviceEvent(DeviceOrientationEvent::create(eventNames().deviceorientationEvent, orientation));
55}
56
57DeviceOrientationClient* DeviceOrientationController::deviceOrientationClient()
58{
59    return static_cast<DeviceOrientationClient*>(m_client);
60}
61
62#if PLATFORM(IOS)
63// FIXME: We should look to reconcile the iOS and OpenSource differences with this class
64// so that we can either remove these methods or remove the PLATFORM(IOS)-guard.
65void DeviceOrientationController::suspendUpdates()
66{
67    if (m_client)
68        m_client->stopUpdating();
69}
70
71void DeviceOrientationController::resumeUpdates()
72{
73    if (m_client && !m_listeners.isEmpty())
74        m_client->startUpdating();
75}
76#else
77bool DeviceOrientationController::hasLastData()
78{
79    return deviceOrientationClient()->lastOrientation();
80}
81
82PassRefPtr<Event> DeviceOrientationController::getLastEvent()
83{
84    return DeviceOrientationEvent::create(eventNames().deviceorientationEvent, deviceOrientationClient()->lastOrientation());
85}
86#endif // PLATFORM(IOS)
87
88const char* DeviceOrientationController::supplementName()
89{
90    return "DeviceOrientationController";
91}
92
93DeviceOrientationController* DeviceOrientationController::from(Page* page)
94{
95    return static_cast<DeviceOrientationController*>(Supplement<Page>::from(page, supplementName()));
96}
97
98bool DeviceOrientationController::isActiveAt(Page* page)
99{
100    if (DeviceOrientationController* self = DeviceOrientationController::from(page))
101        return self->isActive();
102    return false;
103}
104
105void provideDeviceOrientationTo(Page* page, DeviceOrientationClient* client)
106{
107    DeviceOrientationController::provideTo(page, DeviceOrientationController::supplementName(), std::make_unique<DeviceOrientationController>(client));
108}
109
110} // namespace WebCore
111