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 RefPtr<BatteryManager> batteryManager(adoptRefWillBeRefCountedGarbageCollect ed(new BatteryManager(context))); |
|
sof
2014/04/07 12:52:44
Use RefPtrWillBeRawPtr<> instead of RefPtr<>.
| |
| 19 batteryManager->suspendIfNeeded(); | |
| 20 return batteryManager.release(); | |
| 18 } | 21 } |
| 19 | 22 |
| 20 BatteryManager::~BatteryManager() | 23 BatteryManager::~BatteryManager() |
| 21 { | 24 { |
|
timvolodine
2014/04/02 14:24:51
we probably want to unregister here by calling sto
| |
| 22 } | 25 } |
| 23 | 26 |
| 24 BatteryManager::BatteryManager(ExecutionContext* context) | 27 BatteryManager::BatteryManager(ExecutionContext* context) |
| 25 : ContextLifecycleObserver(context) | 28 : ActiveDOMObject(context) |
| 29 , DeviceSensorEventController(*toDocument(context)) | |
| 26 , m_batteryStatus(nullptr) | 30 , m_batteryStatus(nullptr) |
| 27 { | 31 { |
| 32 // Need to figure out a way to startUpdating only afters EventListeners are added: didAddEventListener? | |
| 33 m_hasEventListener = true; | |
| 34 startUpdating(); | |
|
timvolodine
2014/04/02 14:24:51
yeah this is a bit tricky. Think it's generally ok
Srini
2014/04/02 17:25:36
The spec mentions a default value when the battery
| |
| 28 } | 35 } |
| 29 | 36 |
| 30 bool BatteryManager::charging() | 37 bool BatteryManager::charging() |
| 31 { | 38 { |
| 32 return m_batteryStatus ? m_batteryStatus->charging() : true; | 39 return m_batteryStatus ? m_batteryStatus->charging() : true; |
| 33 } | 40 } |
| 34 | 41 |
| 35 double BatteryManager::chargingTime() | 42 double BatteryManager::chargingTime() |
| 36 { | 43 { |
| 37 if (!m_batteryStatus || !m_batteryStatus->charging()) | 44 if (!m_batteryStatus || !m_batteryStatus->charging()) |
| 38 return std::numeric_limits<double>::infinity(); | 45 return std::numeric_limits<double>::infinity(); |
| 39 | 46 |
| 40 return m_batteryStatus->chargingTime(); | 47 return m_batteryStatus->chargingTime(); |
| 41 } | 48 } |
| 42 | 49 |
| 43 double BatteryManager::dischargingTime() | 50 double BatteryManager::dischargingTime() |
| 44 { | 51 { |
| 45 if (!m_batteryStatus || m_batteryStatus->charging()) | 52 if (!m_batteryStatus || m_batteryStatus->charging()) |
| 46 return std::numeric_limits<double>::infinity(); | 53 return std::numeric_limits<double>::infinity(); |
| 47 | 54 |
| 48 return m_batteryStatus->dischargingTime(); | 55 return m_batteryStatus->dischargingTime(); |
| 49 } | 56 } |
| 50 | 57 |
| 51 double BatteryManager::level() | 58 double BatteryManager::level() |
| 52 { | 59 { |
| 53 return m_batteryStatus ? m_batteryStatus->level() : 1; | 60 return m_batteryStatus ? m_batteryStatus->level() : 1; |
|
timvolodine
2014/04/03 13:48:16
Also batteryStatus appears to be stored twice in b
| |
| 54 } | 61 } |
| 55 | 62 |
| 56 void BatteryManager::didChangeBatteryStatus(PassRefPtr<Event> event, PassOwnPtr< BatteryStatus> batteryStatus) | 63 void BatteryManager::didChangeBatteryStatus(PassRefPtr<Event> event, PassRefPtr< BatteryStatus> batteryStatus) |
|
sof
2014/04/07 12:52:44
Change PassRefPtr<Event> to PassRefPtrWillBeRawPtr
| |
| 57 { | 64 { |
| 58 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); | 65 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); |
| 59 | 66 |
| 60 m_batteryStatus = batteryStatus; | 67 m_batteryStatus = batteryStatus; |
| 61 dispatchEvent(event); | 68 dispatchEvent(event); |
| 62 } | 69 } |
| 63 | 70 |
| 71 void BatteryManager::registerWithDispatcher() | |
| 72 { | |
| 73 BatteryDispatcher::instance().addClient(this); | |
| 74 } | |
| 75 | |
| 76 void BatteryManager::unregisterWithDispatcher() | |
| 77 { | |
| 78 BatteryDispatcher::instance().removeClient(this); | |
| 79 } | |
| 80 | |
| 81 bool BatteryManager::hasLastData() | |
| 82 { | |
| 83 /* Battery Manager sends the event itself */ | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 PassRefPtr<Event> BatteryManager::getLastEvent() | |
|
sof
2014/04/07 12:52:44
Change this to PassRefPtrWillBeRawPtr<Event>
| |
| 88 { | |
| 89 // This is called only when hasLastData() is true. | |
| 90 ASSERT_NOT_REACHED(); | |
| 91 return nullptr; | |
| 92 } | |
| 93 | |
| 94 bool BatteryManager::isNullEvent(Event*) | |
| 95 { | |
| 96 // This is called only when hasLastData() is true. | |
| 97 ASSERT_NOT_REACHED(); | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 void BatteryManager::suspend() | |
| 102 { | |
| 103 stopUpdating(); | |
| 104 } | |
| 105 | |
| 106 void BatteryManager::resume() | |
| 107 { | |
| 108 startUpdating(); | |
| 109 } | |
| 110 | |
| 111 void BatteryManager::stop() | |
| 112 { | |
| 113 stopUpdating(); | |
| 114 } | |
| 115 | |
| 64 } // namespace WebCore | 116 } // namespace WebCore |
| OLD | NEW |