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 "BatteryController.h" 23 24#if ENABLE(BATTERY_STATUS) 25 26#include "BatteryClient.h" 27#include "BatteryStatus.h" 28#include "Event.h" 29 30namespace WebCore { 31 32BatteryController::BatteryController(BatteryClient* client) 33 : m_client(client) 34{ 35 ASSERT(m_client); 36} 37 38BatteryController::~BatteryController() 39{ 40 for (ListenerVector::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it) 41 (*it)->batteryControllerDestroyed(); 42 m_client->batteryControllerDestroyed(); 43} 44 45PassOwnPtr<BatteryController> BatteryController::create(BatteryClient* client) 46{ 47 return adoptPtr(new BatteryController(client)); 48} 49 50void BatteryController::addListener(BatteryManager* batteryManager) 51{ 52 m_listeners.append(batteryManager); 53 m_client->startUpdating(); 54 55 if (m_batteryStatus) 56 batteryManager->updateBatteryStatus(m_batteryStatus); 57} 58 59void BatteryController::removeListener(BatteryManager* batteryManager) 60{ 61 size_t pos = m_listeners.find(batteryManager); 62 if (pos == WTF::notFound) 63 return; 64 m_listeners.remove(pos); 65 if (m_listeners.isEmpty()) 66 m_client->stopUpdating(); 67} 68 69void BatteryController::updateBatteryStatus(PassRefPtr<BatteryStatus> batteryStatus) 70{ 71 RefPtr<BatteryStatus> status = batteryStatus; 72 if (m_batteryStatus) { 73 if (m_batteryStatus->charging() != status->charging()) 74 didChangeBatteryStatus(WebCore::eventNames().chargingchangeEvent, status); 75 else if (status->charging() && m_batteryStatus->chargingTime() != status->chargingTime()) 76 didChangeBatteryStatus(WebCore::eventNames().chargingtimechangeEvent, status); 77 else if (!status->charging() && m_batteryStatus->dischargingTime() != status->dischargingTime()) 78 didChangeBatteryStatus(WebCore::eventNames().dischargingtimechangeEvent, status); 79 80 if (m_batteryStatus->level() != status->level()) 81 didChangeBatteryStatus(WebCore::eventNames().levelchangeEvent, status); 82 } else { 83 for (ListenerVector::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it) 84 (*it)->updateBatteryStatus(status); 85 } 86 87 m_batteryStatus = status.release(); 88} 89 90void BatteryController::didChangeBatteryStatus(const AtomicString& eventType, PassRefPtr<BatteryStatus> batteryStatus) 91{ 92 RefPtr<Event> event = Event::create(eventType, false, false); 93 RefPtr<BatteryStatus> battery = batteryStatus; 94 for (ListenerVector::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it) 95 (*it)->didChangeBatteryStatus(event, battery); 96} 97 98const char* BatteryController::supplementName() 99{ 100 return "BatteryController"; 101} 102 103bool BatteryController::isActive(Page* page) 104{ 105 return static_cast<bool>(BatteryController::from(page)); 106} 107 108void provideBatteryTo(Page* page, BatteryClient* client) 109{ 110 Supplement<Page>::provideTo(page, BatteryController::supplementName(), BatteryController::create(client)); 111} 112 113} 114 115#endif // BATTERY_STATUS 116 117