OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 Samsung Electronics | |
3 * Copyright (C) 2012 Google Inc. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Library General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Library General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Library General Public License | |
16 * along with this library; see the file COPYING.LIB. If not, write to | |
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
18 * Boston, MA 02110-1301, USA. | |
19 */ | |
20 | |
21 #include "config.h" | |
22 #include "modules/battery/BatteryController.h" | |
23 | |
24 #if ENABLE(BATTERY_STATUS) | |
25 | |
26 #include "core/dom/Event.h" | |
27 #include "modules/battery/BatteryClient.h" | |
28 #include "modules/battery/BatteryStatus.h" | |
29 | |
30 namespace WebCore { | |
31 | |
32 BatteryController::BatteryController(BatteryClient* client) | |
33 : m_client(client) | |
34 { | |
35 ASSERT(m_client); | |
36 } | |
37 | |
38 BatteryController::~BatteryController() | |
39 { | |
40 for (ListenerVector::iterator it = m_listeners.begin(); it != m_listeners.en
d(); ++it) | |
41 (*it)->batteryControllerDestroyed(); | |
42 m_client->batteryControllerDestroyed(); | |
43 } | |
44 | |
45 PassOwnPtr<BatteryController> BatteryController::create(BatteryClient* client) | |
46 { | |
47 return adoptPtr(new BatteryController(client)); | |
48 } | |
49 | |
50 void BatteryController::addListener(BatteryManager* batteryManager) | |
51 { | |
52 m_listeners.append(batteryManager); | |
53 m_client->startUpdating(); | |
54 } | |
55 | |
56 void BatteryController::removeListener(BatteryManager* batteryManager) | |
57 { | |
58 size_t pos = m_listeners.find(batteryManager); | |
59 if (pos == WTF::notFound) | |
60 return; | |
61 m_listeners.remove(pos); | |
62 if (m_listeners.isEmpty()) | |
63 m_client->stopUpdating(); | |
64 } | |
65 | |
66 void BatteryController::updateBatteryStatus(PassRefPtr<BatteryStatus> batterySta
tus) | |
67 { | |
68 RefPtr<BatteryStatus> status = batteryStatus; | |
69 if (m_batteryStatus) { | |
70 if (m_batteryStatus->charging() != status->charging()) | |
71 didChangeBatteryStatus(WebCore::eventNames().chargingchangeEvent, st
atus); | |
72 else if (status->charging() && m_batteryStatus->chargingTime() != status
->chargingTime()) | |
73 didChangeBatteryStatus(WebCore::eventNames().chargingtimechangeEvent
, status); | |
74 else if (!status->charging() && m_batteryStatus->dischargingTime() != st
atus->dischargingTime()) | |
75 didChangeBatteryStatus(WebCore::eventNames().dischargingtimechangeEv
ent, status); | |
76 | |
77 if (m_batteryStatus->level() != status->level()) | |
78 didChangeBatteryStatus(WebCore::eventNames().levelchangeEvent, statu
s); | |
79 } | |
80 | |
81 m_batteryStatus = status.release(); | |
82 } | |
83 | |
84 void BatteryController::didChangeBatteryStatus(const AtomicString& eventType, Pa
ssRefPtr<BatteryStatus> batteryStatus) | |
85 { | |
86 RefPtr<Event> event = Event::create(eventType, false, false); | |
87 RefPtr<BatteryStatus> battery = batteryStatus; | |
88 for (ListenerVector::iterator it = m_listeners.begin(); it != m_listeners.en
d(); ++it) | |
89 (*it)->didChangeBatteryStatus(event, battery); | |
90 } | |
91 | |
92 const char* BatteryController::supplementName() | |
93 { | |
94 return "BatteryController"; | |
95 } | |
96 | |
97 bool BatteryController::isActive(Page* page) | |
98 { | |
99 return static_cast<bool>(BatteryController::from(page)); | |
100 } | |
101 | |
102 void provideBatteryTo(Page* page, BatteryClient* client) | |
103 { | |
104 Supplement<Page>::provideTo(page, BatteryController::supplementName(), Batte
ryController::create(client)); | |
105 } | |
106 | |
107 } | |
108 | |
109 #endif // BATTERY_STATUS | |
110 | |
OLD | NEW |