Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 5 #ifndef BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
| 6 #define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 6 #define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/base_export.h" | 13 #include "base/base_export.h" |
| 13 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 14 #include "base/file_path.h" | 15 #include "base/file_path.h" |
| 15 #include "base/string16.h" | 16 #include "base/string16.h" |
| 17 #include "base/synchronization/lock.h" | |
| 16 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 17 | 19 |
| 18 // Windows HiRes timers drain the battery faster so we need to know the battery | 20 // Windows HiRes timers drain the battery faster so we need to know the battery |
| 19 // status. This isn't true for other platforms. | 21 // status. This isn't true for other platforms. |
| 20 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
| 21 #define ENABLE_BATTERY_MONITORING 1 | 23 #define ENABLE_BATTERY_MONITORING 1 |
| 22 #else | 24 #else |
| 23 #undef ENABLE_BATTERY_MONITORING | 25 #undef ENABLE_BATTERY_MONITORING |
| 24 #endif // !OS_WIN | 26 #endif // !OS_WIN |
| 25 | 27 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 51 RESUME_EVENT // The system is being resumed. | 53 RESUME_EVENT // The system is being resumed. |
| 52 }; | 54 }; |
| 53 | 55 |
| 54 // Type of devices whose change need to be monitored, such as add/remove. | 56 // Type of devices whose change need to be monitored, such as add/remove. |
| 55 enum DeviceType { | 57 enum DeviceType { |
| 56 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. | 58 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. |
| 57 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. | 59 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. |
| 58 DEVTYPE_UNKNOWN, // Other devices. | 60 DEVTYPE_UNKNOWN, // Other devices. |
| 59 }; | 61 }; |
| 60 | 62 |
| 63 // The delegate interfaces for storage free space. | |
| 64 class BASE_EXPORT StorageFreeSpaceDelegate { | |
| 65 public: | |
| 66 virtual ~StorageFreeSpaceDelegate() {} | |
| 67 | |
| 68 // Return true if the given storage |path| is already being watched. | |
| 69 virtual bool IsWatchingStorage(const FilePath& path) = 0; | |
|
vandebo (ex-Chrome)
2012/09/12 17:54:47
Not sure you need this.
Hongbo Min
2012/09/13 13:40:53
Removed it already.
| |
| 70 | |
| 71 // Start watching the storage device identified by the |path| parameter. | |
| 72 // The |path| can be a drive path on Windows, or the mount point on Linux. | |
|
vandebo (ex-Chrome)
2012/09/12 17:54:47
nit: Linux -> Posix
Hongbo Min
2012/09/13 13:40:53
Done.
| |
| 73 virtual void StartWatchingStorage(const FilePath& path) = 0; | |
| 74 | |
| 75 // Stop watching the storage device identified by the |path| paramter. | |
| 76 virtual void StopWatchingStorage(const FilePath& path) = 0; | |
| 77 }; | |
| 78 | |
| 61 struct BASE_EXPORT RemovableStorageInfo { | 79 struct BASE_EXPORT RemovableStorageInfo { |
| 62 RemovableStorageInfo(); | 80 RemovableStorageInfo(); |
| 63 RemovableStorageInfo(const std::string& id, | 81 RemovableStorageInfo(const std::string& id, |
| 64 const string16& device_name, | 82 const string16& device_name, |
| 65 const FilePath::StringType& device_location); | 83 const FilePath::StringType& device_location); |
| 66 | 84 |
| 67 // Unique device id - persists between device attachments. | 85 // Unique device id - persists between device attachments. |
| 68 std::string device_id; | 86 std::string device_id; |
| 69 | 87 |
| 70 // Human readable removable storage device name. | 88 // Human readable removable storage device name. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 virtual void OnRemovableStorageAttached( | 158 virtual void OnRemovableStorageAttached( |
| 141 const std::string& id, | 159 const std::string& id, |
| 142 const string16& name, | 160 const string16& name, |
| 143 const FilePath::StringType& location) {} | 161 const FilePath::StringType& location) {} |
| 144 virtual void OnRemovableStorageDetached(const std::string& id) {} | 162 virtual void OnRemovableStorageDetached(const std::string& id) {} |
| 145 | 163 |
| 146 protected: | 164 protected: |
| 147 virtual ~DevicesChangedObserver() {} | 165 virtual ~DevicesChangedObserver() {} |
| 148 }; | 166 }; |
| 149 | 167 |
| 168 class BASE_EXPORT StorageFreeSpaceChangedObserver { | |
| 169 public: | |
| 170 // Triggered when the free space of the storage |path| is changed. The | |
| 171 // |available_capacity| indicates the free space in bytes. | |
| 172 virtual void OnStorageFreeSpaceChanged(const FilePath& path, | |
| 173 int64 available_capacity) {} | |
| 174 }; | |
| 175 | |
| 150 // Add a new observer. | 176 // Add a new observer. |
| 151 // Can be called from any thread. | 177 // Can be called from any thread. |
| 152 // Must not be called from within a notification callback. | 178 // Must not be called from within a notification callback. |
| 153 void AddPowerObserver(PowerObserver* obs); | 179 void AddPowerObserver(PowerObserver* obs); |
| 154 void AddDevicesChangedObserver(DevicesChangedObserver* obs); | 180 void AddDevicesChangedObserver(DevicesChangedObserver* obs); |
| 181 void AddStorageFreeSpaceChangedObserver( | |
| 182 const FilePath& path, | |
| 183 StorageFreeSpaceChangedObserver* obs); | |
| 155 | 184 |
| 156 // Remove an existing observer. | 185 // Remove an existing observer. |
| 157 // Can be called from any thread. | 186 // Can be called from any thread. |
| 158 // Must not be called from within a notification callback. | 187 // Must not be called from within a notification callback. |
| 159 void RemovePowerObserver(PowerObserver* obs); | 188 void RemovePowerObserver(PowerObserver* obs); |
| 160 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); | 189 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); |
| 190 void RemoveStorageFreeSpaceChangedObserver( | |
| 191 const FilePath& path, | |
| 192 StorageFreeSpaceChangedObserver* obs); | |
| 161 | 193 |
| 162 #if defined(OS_WIN) | 194 #if defined(OS_WIN) |
| 163 // Windows-specific handling of a WM_POWERBROADCAST message. | 195 // Windows-specific handling of a WM_POWERBROADCAST message. |
| 164 // Embedders of this API should hook their top-level window | 196 // Embedders of this API should hook their top-level window |
| 165 // message loop and forward WM_POWERBROADCAST through this call. | 197 // message loop and forward WM_POWERBROADCAST through this call. |
| 166 void ProcessWmPowerBroadcastMessage(int event_id); | 198 void ProcessWmPowerBroadcastMessage(int event_id); |
| 167 #endif | 199 #endif |
| 168 | 200 |
| 169 // Cross-platform handling of a power event. | 201 // Cross-platform handling of a power event. |
| 170 void ProcessPowerMessage(PowerEvent event_id); | 202 void ProcessPowerMessage(PowerEvent event_id); |
| 171 | 203 |
| 172 // Cross-platform handling of a device change event. | 204 // Cross-platform handling of a device change event. |
| 173 void ProcessDevicesChanged(DeviceType device_type); | 205 void ProcessDevicesChanged(DeviceType device_type); |
| 174 void ProcessRemovableStorageAttached(const std::string& id, | 206 void ProcessRemovableStorageAttached(const std::string& id, |
| 175 const string16& name, | 207 const string16& name, |
| 176 const FilePath::StringType& location); | 208 const FilePath::StringType& location); |
| 177 void ProcessRemovableStorageDetached(const std::string& id); | 209 void ProcessRemovableStorageDetached(const std::string& id); |
| 178 | 210 |
| 211 void ProcessStorageFreeSpaceChanged(const FilePath& path, | |
| 212 int64 available_capacity); | |
| 213 | |
| 214 void set_storage_free_space_delegate(StorageFreeSpaceDelegate* delegate) { | |
| 215 free_space_delegate_ = delegate; | |
|
vandebo (ex-Chrome)
2012/09/12 17:54:47
Add a DCHECK that free_sapce_delegate_ is NULL. W
Hongbo Min
2012/09/13 13:40:53
Done, but allow the free_space_delegate_ to be set
| |
| 216 } | |
| 217 | |
| 179 private: | 218 private: |
| 180 // Mapping of unique device id to device info tuple. | 219 // Mapping of unique device id to device info tuple. |
| 181 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap; | 220 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap; |
| 182 | 221 |
| 222 typedef ObserverListThreadSafe<StorageFreeSpaceChangedObserver> | |
| 223 StorageFreeSpaceChangedObserverList; | |
| 224 | |
| 225 // Mapping of the storage device path to the observer list. | |
| 226 typedef std::map<FilePath, scoped_refptr< | |
| 227 StorageFreeSpaceChangedObserverList> > StorageFreeSpaceChangedObserverMap; | |
| 228 | |
| 183 #if defined(OS_MACOSX) | 229 #if defined(OS_MACOSX) |
| 184 void PlatformInit(); | 230 void PlatformInit(); |
| 185 void PlatformDestroy(); | 231 void PlatformDestroy(); |
| 186 #endif | 232 #endif |
| 187 | 233 |
| 188 // Platform-specific method to check whether the system is currently | 234 // Platform-specific method to check whether the system is currently |
| 189 // running on battery power. Returns true if running on batteries, | 235 // running on battery power. Returns true if running on batteries, |
| 190 // false otherwise. | 236 // false otherwise. |
| 191 bool IsBatteryPower(); | 237 bool IsBatteryPower(); |
| 192 | 238 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 215 #endif | 261 #endif |
| 216 | 262 |
| 217 #if defined(OS_IOS) | 263 #if defined(OS_IOS) |
| 218 // Holds pointers to system event notification observers. | 264 // Holds pointers to system event notification observers. |
| 219 std::vector<id> notification_observers_; | 265 std::vector<id> notification_observers_; |
| 220 #endif | 266 #endif |
| 221 | 267 |
| 222 // Map of all the attached removable storage devices. | 268 // Map of all the attached removable storage devices. |
| 223 RemovableStorageMap removable_storage_map_; | 269 RemovableStorageMap removable_storage_map_; |
| 224 | 270 |
| 271 // The delegate instance for storage free space change. | |
|
vandebo (ex-Chrome)
2012/09/12 17:54:47
nit: The delegate that implements free space-chang
Hongbo Min
2012/09/13 13:40:53
Done.
| |
| 272 StorageFreeSpaceDelegate* free_space_delegate_; | |
| 273 | |
| 274 // Map of the storage being watched to the observer list. | |
| 275 StorageFreeSpaceChangedObserverMap free_space_changed_observer_map_; | |
| 276 | |
| 277 // The set of the storage path being watched. Since we can not know when | |
|
vandebo (ex-Chrome)
2012/09/12 17:54:47
nit: path -> paths, can not -> can't
Hongbo Min
2012/09/13 13:40:53
Done.
| |
| 278 // the thread-safe observer list becomse empty, we have to use this set | |
| 279 // to decide to stop watching a storage path when its last observer has | |
| 280 // been removed. | |
| 281 std::multiset<FilePath> watching_storage_set_; | |
| 282 | |
| 283 // The lock for manipulating free_space_changed_observer_map_ | |
|
vandebo (ex-Chrome)
2012/09/12 17:54:47
nit: remove The: "Lock for..."
Hongbo Min
2012/09/13 13:40:53
Done.
| |
| 284 // and watching_storage_set_. | |
| 285 base::Lock free_space_observers_lock_; | |
| 286 | |
| 225 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 287 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
| 226 }; | 288 }; |
| 227 | 289 |
| 228 } // namespace base | 290 } // namespace base |
| 229 | 291 |
| 230 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 292 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
| OLD | NEW |