Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "modules/battery/BatteryManager.h" | 6 #include "modules/battery/BatteryManager.h" |
| 7 | 7 |
| 8 #include "RuntimeEnabledFeatures.h" | 8 #include "RuntimeEnabledFeatures.h" |
| 9 #include "core/events/Event.h" | 9 #include "core/events/Event.h" |
| 10 #include "modules/battery/BatteryDispatcher.h" | |
| 10 #include "modules/battery/BatteryStatus.h" | 11 #include "modules/battery/BatteryStatus.h" |
| 11 #include <limits> | 12 #include <limits> |
| 12 | 13 |
| 13 namespace WebCore { | 14 namespace WebCore { |
| 14 | 15 |
| 15 PassRefPtrWillBeRawPtr<BatteryManager> BatteryManager::create(ExecutionContext* context) | 16 PassRefPtrWillBeRawPtr<BatteryManager> BatteryManager::create(ExecutionContext* context) |
| 16 { | 17 { |
| 17 return adoptRefWillBeRefCountedGarbageCollected(new BatteryManager(context)) ; | 18 RefPtrWillBeRawPtr<BatteryManager> batteryManager(adoptRefWillBeRefCountedGa rbageCollected(new BatteryManager(context))); |
| 19 batteryManager->suspendIfNeeded(); | |
| 20 return batteryManager.release(); | |
| 18 } | 21 } |
| 19 | 22 |
| 20 BatteryManager::~BatteryManager() | 23 BatteryManager::~BatteryManager() |
| 21 { | 24 { |
| 25 stopUpdating(); | |
| 22 } | 26 } |
| 23 | 27 |
| 24 BatteryManager::BatteryManager(ExecutionContext* context) | 28 BatteryManager::BatteryManager(ExecutionContext* context) |
| 25 : ContextLifecycleObserver(context) | 29 : ActiveDOMObject(context) |
| 26 , m_batteryStatus(nullptr) | 30 , DeviceSensorEventController(*toDocument(context)) |
| 27 { | 31 { |
| 32 // Need to figure out a way to startUpdating only afters EventListeners are added: didAddEventListener? | |
|
timvolodine
2014/04/09 18:59:02
also, we need to think about this as it might be a
| |
| 33 m_hasEventListener = true; | |
| 34 startUpdating(); | |
| 28 } | 35 } |
| 29 | 36 |
| 30 bool BatteryManager::charging() | 37 bool BatteryManager::charging() |
| 31 { | 38 { |
| 32 return m_batteryStatus ? m_batteryStatus->charging() : true; | 39 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
| 40 return lastData->charging(); | |
| 41 | |
| 42 return true; | |
| 33 } | 43 } |
| 34 | 44 |
| 35 double BatteryManager::chargingTime() | 45 double BatteryManager::chargingTime() |
| 36 { | 46 { |
| 37 if (!m_batteryStatus) | 47 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) { |
| 38 return 0; | 48 // The spec requires that if level == 1.0, chargingTime == 0 but this ha s to |
| 49 // be implement by the backend. Adding this assert will help enforcing i t. | |
| 50 ASSERT(lastData->level() != 1.0 && lastData->chargingTime() == 0.0); | |
|
timvolodine
2014/04/09 15:19:18
please remove this assert, as you mention in the c
Srini
2014/04/09 16:09:57
Ok.
| |
| 39 | 51 |
| 40 if (!m_batteryStatus->charging()) | 52 return lastData->chargingTime(); |
| 41 return std::numeric_limits<double>::infinity(); | 53 } |
| 42 | 54 |
| 43 // The spec requires that if level == 1.0, chargingTime == 0 but this has to | 55 return 0; |
|
timvolodine
2014/04/09 15:19:18
should this be infinity as below?
Srini
2014/04/09 16:09:57
I think that default is 0 when the charging defaul
timvolodine
2014/04/09 18:59:02
I thought the default was +inf, but looking at the
| |
| 44 // be implement by the backend. Adding this assert will help enforcing it. | |
| 45 ASSERT(level() != 1.0 && m_batteryStatus->chargingTime() == 0.0); | |
| 46 | |
| 47 return m_batteryStatus->chargingTime(); | |
| 48 } | 56 } |
| 49 | 57 |
| 50 double BatteryManager::dischargingTime() | 58 double BatteryManager::dischargingTime() |
| 51 { | 59 { |
| 52 if (!m_batteryStatus || m_batteryStatus->charging()) | 60 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
| 53 return std::numeric_limits<double>::infinity(); | 61 return lastData->dischargingTime(); |
| 54 | 62 |
| 55 return m_batteryStatus->dischargingTime(); | 63 return std::numeric_limits<double>::infinity(); |
| 56 } | 64 } |
| 57 | 65 |
| 58 double BatteryManager::level() | 66 double BatteryManager::level() |
| 59 { | 67 { |
| 60 return m_batteryStatus ? m_batteryStatus->level() : 1; | 68 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) |
| 69 return lastData->level(); | |
| 70 | |
| 71 return 1; | |
| 61 } | 72 } |
| 62 | 73 |
| 63 void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event, PassOwnPtr<BatteryStatus> batteryStatus) | 74 void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event) |
| 64 { | 75 { |
| 65 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); | 76 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); |
| 66 | 77 |
| 67 m_batteryStatus = batteryStatus; | |
| 68 dispatchEvent(event); | 78 dispatchEvent(event); |
| 69 } | 79 } |
| 70 | 80 |
| 81 void BatteryManager::registerWithDispatcher() | |
| 82 { | |
| 83 BatteryDispatcher::instance().addClient(this); | |
| 84 } | |
| 85 | |
| 86 void BatteryManager::unregisterWithDispatcher() | |
| 87 { | |
| 88 BatteryDispatcher::instance().removeClient(this); | |
| 89 } | |
| 90 | |
| 91 bool BatteryManager::hasLastData() | |
| 92 { | |
| 93 return false; | |
|
timvolodine
2014/04/09 15:19:18
call to dispatcher.
I've uploaded https://coderevi
Srini
2014/04/09 16:09:57
Ill add this once your patch lands. Thanks for thi
| |
| 94 } | |
| 95 | |
| 96 PassRefPtrWillBeRawPtr<Event> BatteryManager::getLastEvent() | |
| 97 { | |
| 98 // We would fire events directly. | |
| 99 return nullptr; | |
|
timvolodine
2014/04/09 15:19:18
call to dispatcher + return create event
Srini
2014/04/09 16:09:57
I think I need some help here. Now that I was tryi
timvolodine
2014/04/09 18:59:02
yeah, I see the problem. The DeviceSensorEventCont
| |
| 100 } | |
| 101 | |
| 102 bool BatteryManager::isNullEvent(Event*) | |
| 103 { | |
| 104 return true; | |
|
timvolodine
2014/04/09 15:19:18
return false;
I don't think we have the concept of
Srini
2014/04/09 16:09:57
Ok.
| |
| 105 } | |
| 106 | |
| 107 void BatteryManager::suspend() | |
| 108 { | |
| 109 stopUpdating(); | |
| 110 } | |
| 111 | |
| 112 void BatteryManager::resume() | |
| 113 { | |
| 114 startUpdating(); | |
| 115 } | |
| 116 | |
| 117 void BatteryManager::stop() | |
| 118 { | |
| 119 stopUpdating(); | |
| 120 } | |
| 121 | |
| 71 } // namespace WebCore | 122 } // namespace WebCore |
| OLD | NEW |