Index: base/power_monitor/power_monitor.cc |
diff --git a/base/threading/worker_pool_win.cc b/base/power_monitor/power_monitor.cc |
similarity index 11% |
copy from base/threading/worker_pool_win.cc |
copy to base/power_monitor/power_monitor.cc |
index 1e8be268748e52842d5fe24ce5a22dad9e9606eb..2a8163dfa88151c427320e46fb1f852da595bf08 100644 |
--- a/base/threading/worker_pool_win.cc |
+++ b/base/power_monitor/power_monitor.cc |
@@ -2,72 +2,99 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "base/threading/worker_pool.h" |
vandebo (ex-Chrome)
2012/10/11 20:49:57
It detected this as a copy from the wrong file, yo
Hongbo Min
2012/10/12 14:43:53
Done.
|
- |
-#include "base/bind.h" |
-#include "base/callback.h" |
-#include "base/debug/trace_event.h" |
-#include "base/logging.h" |
-#include "base/pending_task.h" |
-#include "base/threading/thread_local.h" |
-#include "base/tracked_objects.h" |
+#include "base/power_monitor/power_monitor.h" |
namespace base { |
-namespace { |
- |
-base::LazyInstance<ThreadLocalBoolean>::Leaky |
- g_worker_pool_running_on_this_thread = LAZY_INSTANCE_INITIALIZER; |
- |
-DWORD CALLBACK WorkItemCallback(void* param) { |
- PendingTask* pending_task = static_cast<PendingTask*>(param); |
- TRACE_EVENT2("task", "WorkItemCallback::Run", |
- "src_file", pending_task->posted_from.file_name(), |
- "src_func", pending_task->posted_from.function_name()); |
- |
- tracked_objects::TrackedTime start_time = |
- tracked_objects::ThreadData::NowForStartOfRun(pending_task->birth_tally); |
+#if defined(ENABLE_BATTERY_MONITORING) |
+// The amount of time (in ms) to wait before running the initial |
+// battery check. |
+static int kDelayedBatteryCheckMs = 10 * 1000; |
+#endif // defined(ENABLE_BATTERY_MONITORING) |
+ |
+PowerMonitor::PowerMonitor() |
+ : observers_(new ObserverListThreadSafe<PowerObserver>()), |
+ battery_in_use_(false), |
+ suspended_(false) { |
+ DCHECK(MessageLoop::current()); |
+#if defined(ENABLE_BATTERY_MONITORING) |
+ delayed_battery_check_.Start(FROM_HERE, |
+ base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, |
+ &PowerMonitor::BatteryCheck); |
+#endif // defined(ENABLE_BATTERY_MONITORING) |
+ |
+#if defined(OS_MACOSX) |
+ PlatformInit(); |
+#endif |
+} |
- g_worker_pool_running_on_this_thread.Get().Set(true); |
- pending_task->task.Run(); |
- g_worker_pool_running_on_this_thread.Get().Set(false); |
+PowerMonitor::~PowerMonitor() { |
+#if defined(OS_MACOSX) |
+ PlaformDestory(); |
+#endif |
+} |
- tracked_objects::ThreadData::TallyRunOnWorkerThreadIfTracking( |
- pending_task->birth_tally, |
- tracked_objects::TrackedTime(pending_task->time_posted), start_time, |
- tracked_objects::ThreadData::NowForEndOfRun()); |
+// static |
+PowerMonitor* PowerMonitor::GetInstance() { |
+ return Singleton<PowerMonitor>::get(); |
+} |
- delete pending_task; |
- return 0; |
+void PowerMonitor::AddObserver(PowerObserver* obs) { |
+ observers_->AddObserver(obs); |
} |
-// Takes ownership of |pending_task| |
-bool PostTaskInternal(PendingTask* pending_task, bool task_is_slow) { |
- ULONG flags = 0; |
- if (task_is_slow) |
- flags |= WT_EXECUTELONGFUNCTION; |
+void PowerMonitor::RemoveObserver(PowerObserver* obs) { |
+ observers_->RemoveObserver(obs); |
+} |
- if (!QueueUserWorkItem(WorkItemCallback, pending_task, flags)) { |
- DLOG(ERROR) << "QueueUserWorkItem failed: " << GetLastError(); |
- delete pending_task; |
- return false; |
+void PowerMonitor::ProcessPowerMessage(PowerEvent event_id) { |
+ // Suppress duplicate notifications. Some platforms may |
+ // send multiple notifications of the same event. |
+ switch (event_id) { |
+ case POWER_STATE_EVENT: |
+ { |
+ bool on_battery = IsBatteryPower(); |
+ if (on_battery != battery_in_use_) { |
+ battery_in_use_ = on_battery; |
+ NotifyPowerStateChange(); |
+ } |
+ } |
+ break; |
+ case RESUME_EVENT: |
+ if (suspended_) { |
+ suspended_ = false; |
+ NotifyResume(); |
+ } |
+ break; |
+ case SUSPEND_EVENT: |
+ if (!suspended_) { |
+ suspended_ = true; |
+ NotifySuspend(); |
+ } |
+ break; |
} |
+} |
- return true; |
+void PowerMonitor::NotifyPowerStateChange() { |
+ DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
+ << " battery"; |
+ observers_->Notify(&PowerObserver::OnPowerStateChange, |
+ BatteryPower()); |
} |
-} // namespace |
+void PowerMonitor::NotifySuspend() { |
+ DVLOG(1) << "Power Suspending"; |
+ observers_->Notify(&PowerObserver::OnSuspend); |
+} |
-// static |
-bool WorkerPool::PostTask(const tracked_objects::Location& from_here, |
- const base::Closure& task, bool task_is_slow) { |
- PendingTask* pending_task = new PendingTask(from_here, task); |
- return PostTaskInternal(pending_task, task_is_slow); |
+void PowerMonitor::NotifyResume() { |
+ DVLOG(1) << "Power Resuming"; |
+ observers_->Notify(&PowerObserver::OnResume); |
} |
-// static |
-bool WorkerPool::RunsTasksOnCurrentThread() { |
- return g_worker_pool_running_on_this_thread.Get().Get(); |
+void PowerMonitor::BatteryCheck() { |
+ ProcessPowerMessage(PowerMonitor::POWER_STATE_EVENT); |
} |
} // namespace base |
+ |