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 "modules/battery/BatteryDispatcher.h" | 9 #include "modules/battery/BatteryDispatcher.h" |
| 10 #include "modules/battery/BatteryStatus.h" | 10 #include "modules/battery/BatteryStatus.h" |
| 11 #include <limits> | 11 #include <limits> |
| 12 | 12 |
| 13 namespace WebCore { | 13 namespace WebCore { |
| 14 | 14 |
| 15 PassRefPtrWillBeRawPtr<BatteryManager> BatteryManager::create(ExecutionContext* context) | 15 PassRefPtrWillBeRawPtr<BatteryManager> BatteryManager::create(ExecutionContext* context) |
| 16 { | 16 { |
| 17 RefPtrWillBeRawPtr<BatteryManager> batteryManager(adoptRefWillBeRefCountedGa rbageCollected(new BatteryManager(context))); | 17 RefPtrWillBeRawPtr<BatteryManager> batteryManager(adoptRefWillBeRefCountedGa rbageCollected(new BatteryManager(context))); |
| 18 batteryManager->suspendIfNeeded(); | 18 batteryManager->suspendIfNeeded(); |
| 19 return batteryManager.release(); | 19 return batteryManager.release(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 BatteryManager::~BatteryManager() | 22 BatteryManager::~BatteryManager() |
| 23 { | 23 { |
| 24 } | 24 } |
| 25 | 25 |
| 26 BatteryManager::BatteryManager(ExecutionContext* context) | 26 BatteryManager::BatteryManager(ExecutionContext* context) |
| 27 : ActiveDOMObject(context) | 27 : ActiveDOMObject(context) |
| 28 , DeviceSensorEventController(toDocument(context)->page()) | 28 , DeviceEventControllerBase(toDocument(context)->page()) |
| 29 { | 29 { |
| 30 m_hasEventListener = true; | 30 m_hasEventListener = true; |
| 31 m_batteryStatus = BatteryStatus::create(); | |
|
Inactive
2014/06/03 13:49:33
This could be part of the initializer list to avoi
timvolodine
2014/06/03 18:45:54
Done.
| |
| 31 startUpdating(); | 32 startUpdating(); |
| 32 } | 33 } |
| 33 | 34 |
| 34 bool BatteryManager::charging() | 35 bool BatteryManager::charging() |
| 35 { | 36 { |
| 36 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) | 37 return m_batteryStatus->charging(); |
| 37 return lastData->charging(); | |
| 38 | |
| 39 return true; | |
| 40 } | 38 } |
| 41 | 39 |
| 42 double BatteryManager::chargingTime() | 40 double BatteryManager::chargingTime() |
| 43 { | 41 { |
| 44 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) | 42 return m_batteryStatus->chargingTime(); |
| 45 return lastData->chargingTime(); | |
| 46 | |
| 47 return 0; | |
| 48 } | 43 } |
| 49 | 44 |
| 50 double BatteryManager::dischargingTime() | 45 double BatteryManager::dischargingTime() |
| 51 { | 46 { |
| 52 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) | 47 return m_batteryStatus->dischargingTime(); |
| 53 return lastData->dischargingTime(); | |
| 54 | |
| 55 return std::numeric_limits<double>::infinity(); | |
| 56 } | 48 } |
| 57 | 49 |
| 58 double BatteryManager::level() | 50 double BatteryManager::level() |
| 59 { | 51 { |
| 60 if (const BatteryStatus* lastData = BatteryDispatcher::instance().getLatestD ata()) | 52 return m_batteryStatus->level(); |
| 61 return lastData->level(); | |
| 62 | |
| 63 return 1; | |
| 64 } | 53 } |
| 65 | 54 |
| 66 void BatteryManager::didChangeBatteryStatus(PassRefPtrWillBeRawPtr<Event> event) | 55 void BatteryManager::didUpdateData() |
| 67 { | 56 { |
| 68 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); | 57 ASSERT(RuntimeEnabledFeatures::batteryStatusEnabled()); |
| 69 | 58 |
| 70 dispatchEvent(event); | 59 RefPtr<BatteryStatus> oldStatus = m_batteryStatus; |
| 60 m_batteryStatus = BatteryDispatcher::instance().getLatestData(); | |
|
Inactive
2014/06/03 13:49:33
It is OK to assign a raw ptr to a RefPtr so this s
timvolodine
2014/06/03 18:45:54
hmm, as I wrote previously the idea is to share th
Inactive
2014/06/03 19:04:23
I believe SVGPathSegList::getItem() is a bad examp
| |
| 61 | |
| 62 Document* document = toDocument(executionContext()); | |
| 63 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped()) | |
| 64 return; | |
| 65 | |
| 66 ASSERT(oldStatus); | |
| 67 | |
| 68 if (m_batteryStatus->charging() != oldStatus->charging()) | |
| 69 dispatchEvent(Event::create(EventTypeNames::chargingchange)); | |
| 70 if (m_batteryStatus->chargingTime() != oldStatus->chargingTime()) | |
| 71 dispatchEvent(Event::create(EventTypeNames::chargingtimechange)); | |
| 72 if (m_batteryStatus->dischargingTime() != oldStatus->dischargingTime()) | |
| 73 dispatchEvent(Event::create(EventTypeNames::dischargingtimechange)); | |
| 74 if (m_batteryStatus->level() != oldStatus->level()) | |
| 75 dispatchEvent(Event::create(EventTypeNames::levelchange)); | |
| 71 } | 76 } |
| 72 | 77 |
| 73 void BatteryManager::registerWithDispatcher() | 78 void BatteryManager::registerWithDispatcher() |
| 74 { | 79 { |
| 75 BatteryDispatcher::instance().addClient(this); | 80 BatteryDispatcher::instance().addController(this); |
| 76 } | 81 } |
| 77 | 82 |
| 78 void BatteryManager::unregisterWithDispatcher() | 83 void BatteryManager::unregisterWithDispatcher() |
| 79 { | 84 { |
| 80 BatteryDispatcher::instance().removeClient(this); | 85 BatteryDispatcher::instance().removeController(this); |
| 81 } | 86 } |
| 82 | 87 |
| 83 bool BatteryManager::hasLastData() | 88 bool BatteryManager::isInvokeInitialUpdate() |
|
Inactive
2014/06/03 13:49:33
This naming seems a bit weird to me but I am not a
timvolodine
2014/06/03 18:45:54
what I meant is: if true this class should perform
Inactive
2014/06/03 19:04:23
What was wrong with the previous hasLastData() nam
| |
| 84 { | 89 { |
| 85 return false; | 90 return BatteryDispatcher::instance().getLatestData(); |
| 86 } | |
| 87 | |
| 88 PassRefPtrWillBeRawPtr<Event> BatteryManager::getLastEvent() | |
| 89 { | |
| 90 // Events are dispached via BatteryManager::didChangeBatteryStatus() | |
| 91 return nullptr; | |
| 92 } | |
| 93 | |
| 94 bool BatteryManager::isNullEvent(Event*) | |
| 95 { | |
| 96 return false; | |
| 97 } | |
| 98 | |
| 99 Document* BatteryManager::document() | |
| 100 { | |
| 101 return toDocument(executionContext()); | |
| 102 } | 91 } |
| 103 | 92 |
| 104 void BatteryManager::suspend() | 93 void BatteryManager::suspend() |
| 105 { | 94 { |
| 106 m_hasEventListener = false; | 95 m_hasEventListener = false; |
| 107 stopUpdating(); | 96 stopUpdating(); |
| 108 } | 97 } |
| 109 | 98 |
| 110 void BatteryManager::resume() | 99 void BatteryManager::resume() |
| 111 { | 100 { |
| 112 m_hasEventListener = true; | 101 m_hasEventListener = true; |
| 113 startUpdating(); | 102 startUpdating(); |
| 114 } | 103 } |
| 115 | 104 |
| 116 void BatteryManager::stop() | 105 void BatteryManager::stop() |
| 117 { | 106 { |
| 118 m_hasEventListener = false; | 107 m_hasEventListener = false; |
| 119 stopUpdating(); | 108 stopUpdating(); |
| 120 } | 109 } |
| 121 | 110 |
| 122 } // namespace WebCore | 111 } // namespace WebCore |
| OLD | NEW |