ipc/chromium/src/base/system_monitor.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
     2 // Use of this source code is governed by a BSD-style license that can be
     3 // found in the LICENSE file.
     5 #ifndef BASE_SYSTEM_MONITOR_H_
     6 #define BASE_SYSTEM_MONITOR_H_
     8 #include "base/observer_list_threadsafe.h"
     9 #include "base/singleton.h"
    11 // Windows HiRes timers drain the battery faster so we need to know the battery
    12 // status.  This isn't true for other platforms.
    13 #if defined(OS_WIN)
    14 #define ENABLE_BATTERY_MONITORING 1
    15 #else
    16 #undef ENABLE_BATTERY_MONITORING
    17 #endif  // !OS_WIN
    19 namespace base {
    21 // Class for monitoring various system-related subsystems
    22 // such as power management, network status, etc.
    23 // TODO(mbelshe):  Add support beyond just power management.
    24 class SystemMonitor {
    25  public:
    26   // Access to the Singleton
    27   static SystemMonitor* Get() {
    28     // Uses the LeakySingletonTrait because cleanup is optional.
    29     return
    30         Singleton<SystemMonitor, LeakySingletonTraits<SystemMonitor> >::get();
    31   }
    33   // Start the System Monitor within a process.  This method
    34   // is provided so that the battery check can be deferred.
    35   // The MessageLoop must be started before calling this
    36   // method.
    37   // This is a no-op on platforms for which ENABLE_BATTERY_MONITORING is
    38   // disabled.
    39   static void Start();
    41   //
    42   // Power-related APIs
    43   //
    45   // Is the computer currently on battery power.
    46   // Can be called on any thread.
    47   bool BatteryPower() {
    48     // Using a lock here is not necessary for just a bool.
    49     return battery_in_use_;
    50   }
    52   // Normalized list of power events.
    53   enum PowerEvent {
    54     POWER_STATE_EVENT,  // The Power status of the system has changed.
    55     SUSPEND_EVENT,      // The system is being suspended.
    56     RESUME_EVENT        // The system is being resumed.
    57   };
    59   // Callbacks will be called on the thread which creates the SystemMonitor.
    60   // During the callback, Add/RemoveObserver will block until the callbacks
    61   // are finished. Observers should implement quick callback functions; if
    62   // lengthy operations are needed, the observer should take care to invoke
    63   // the operation on an appropriate thread.
    64   class PowerObserver {
    65   public:
    66     // Notification of a change in power status of the computer, such
    67     // as from switching between battery and A/C power.
    68     virtual void OnPowerStateChange(SystemMonitor*) = 0;
    70     // Notification that the system is suspending.
    71     virtual void OnSuspend(SystemMonitor*) = 0;
    73     // Notification that the system is resuming.
    74     virtual void OnResume(SystemMonitor*) = 0;
    75   };
    77   // Add a new observer.
    78   // Can be called from any thread.
    79   // Must not be called from within a notification callback.
    80   void AddObserver(PowerObserver* obs);
    82   // Remove an existing observer.
    83   // Can be called from any thread.
    84   // Must not be called from within a notification callback.
    85   void RemoveObserver(PowerObserver* obs);
    87 #if defined(OS_WIN)
    88   // Windows-specific handling of a WM_POWERBROADCAST message.
    89   // Embedders of this API should hook their top-level window
    90   // message loop and forward WM_POWERBROADCAST through this call.
    91   void ProcessWmPowerBroadcastMessage(int event_id);
    92 #endif
    94   // Cross-platform handling of a power event.
    95   void ProcessPowerMessage(PowerEvent event_id);
    97   // Constructor.
    98   // Don't use this; access SystemMonitor via the Singleton.
    99   SystemMonitor();
   101  private:
   102   // Platform-specific method to check whether the system is currently
   103   // running on battery power.  Returns true if running on batteries,
   104   // false otherwise.
   105   bool IsBatteryPower();
   107   // Checks the battery status and notifies observers if the battery
   108   // status has changed.
   109   void BatteryCheck();
   111   // Functions to trigger notifications.
   112   void NotifyPowerStateChange();
   113   void NotifySuspend();
   114   void NotifyResume();
   116   scoped_refptr<ObserverListThreadSafe<PowerObserver> > observer_list_;
   117   bool battery_in_use_;
   118   bool suspended_;
   120 #if defined(ENABLE_BATTERY_MONITORING)
   121   base::OneShotTimer<SystemMonitor> delayed_battery_check_;
   122 #endif
   124   DISALLOW_COPY_AND_ASSIGN(SystemMonitor);
   125 };
   127 }
   129 #endif  // BASE_SYSTEM_MONITOR_H_

mercurial