1/*
2 * Copyright (C) 2013 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitBatteryProvider.h"
22
23#if ENABLE(BATTERY_STATUS)
24
25#include "WebBatteryManagerProxy.h"
26#include "WebBatteryStatus.h"
27#include <limits>
28
29using namespace WebKit;
30
31static inline WebKitBatteryProvider* toBatteryProvider(const void* clientInfo)
32{
33 return static_cast<WebKitBatteryProvider*>(const_cast<void*>(clientInfo));
34}
35
36static void startUpdatingCallback(WKBatteryManagerRef batteryManager, const void* clientInfo)
37{
38 toBatteryProvider(clientInfo)->startUpdating();
39}
40
41static void stopUpdatingCallback(WKBatteryManagerRef batteryManager, const void* clientInfo)
42{
43 toBatteryProvider(clientInfo)->stopUpdating();
44}
45
46PassRefPtr<WebKitBatteryProvider> WebKitBatteryProvider::create(WebBatteryManagerProxy* batteryManager)
47{
48 return adoptRef(new WebKitBatteryProvider(batteryManager));
49}
50
51WebKitBatteryProvider::WebKitBatteryProvider(WebBatteryManagerProxy* batteryManager)
52 : m_batteryManager(batteryManager)
53 , m_provider(this)
54{
55 ASSERT(batteryManager);
56
57 WKBatteryProvider wkBatteryProvider = {
58 kWKBatteryProviderCurrentVersion,
59 this, // clientInfo
60 startUpdatingCallback,
61 stopUpdatingCallback
62 };
63 WKBatteryManagerSetProvider(toAPI(batteryManager), &wkBatteryProvider);
64}
65
66WebKitBatteryProvider::~WebKitBatteryProvider()
67{
68 m_provider.stopUpdating();
69}
70
71void WebKitBatteryProvider::startUpdating()
72{
73 m_provider.startUpdating();
74}
75
76void WebKitBatteryProvider::stopUpdating()
77{
78 m_provider.stopUpdating();
79}
80
81void WebKitBatteryProvider::updateBatteryStatus(WebCore::BatteryProviderUPowerStatus status, double timeRemaining, double level)
82{
83 RefPtr<WebBatteryStatus> batteryStatus;
84
85 switch (status) {
86 case WebCore::NotAvailable:
87 // When an implementation cannot report battery status, the default values should be used.
88 batteryStatus = WebBatteryStatus::create(true, std::numeric_limits<double>::infinity(),
89 std::numeric_limits<double>::infinity(), 1.0);
90 break;
91 case WebCore::Charging:
92 batteryStatus = WebBatteryStatus::create(true, timeRemaining, 0, level);
93 break;
94 case WebCore::Discharging:
95 batteryStatus = WebBatteryStatus::create(false, 0, timeRemaining, level);
96 break;
97 default:
98 ASSERT_NOT_REACHED();
99 }
100
101 m_batteryManager->providerUpdateBatteryStatus(batteryStatus.get());
102}
103
104#endif // ENABLE(BATTERY_STATUS)