| 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" |
| 16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 18 | 19 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 RESUME_EVENT // The system is being resumed. | 53 RESUME_EVENT // The system is being resumed. |
| 53 }; | 54 }; |
| 54 | 55 |
| 55 // 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. |
| 56 enum DeviceType { | 57 enum DeviceType { |
| 57 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. | 58 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. |
| 58 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. | 59 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. |
| 59 DEVTYPE_UNKNOWN, // Other devices. | 60 DEVTYPE_UNKNOWN, // Other devices. |
| 60 }; | 61 }; |
| 61 | 62 |
| 63 // The delegate interfaces for storage free space change notification. |
| 64 // Since the client code may call these interfaces from any thread, the |
| 65 // implementation must take care to implement it, e.g. if it is not called |
| 66 // from the thread on which the observation is working, you probably need |
| 67 // to post a task to that thread for starting or stopping the watching |
| 68 // operation. |
| 69 class BASE_EXPORT StorageFreeSpaceDelegate { |
| 70 public: |
| 71 virtual ~StorageFreeSpaceDelegate() {} |
| 72 |
| 73 // Start watching the storage device identified by the |path| parameter. |
| 74 // The |path| can be a drive path on Windows, or the mount point on Posix. |
| 75 // It can be called from any thread. |
| 76 virtual void StartWatchingStorage(const FilePath& path) = 0; |
| 77 |
| 78 // Stop watching the storage device identified by the |path| paramter. It |
| 79 // can be called from any thread. |
| 80 virtual void StopWatchingStorage(const FilePath& path) = 0; |
| 81 }; |
| 82 |
| 62 struct BASE_EXPORT RemovableStorageInfo { | 83 struct BASE_EXPORT RemovableStorageInfo { |
| 63 RemovableStorageInfo(); | 84 RemovableStorageInfo(); |
| 64 RemovableStorageInfo(const std::string& id, | 85 RemovableStorageInfo(const std::string& id, |
| 65 const string16& device_name, | 86 const string16& device_name, |
| 66 const FilePath::StringType& device_location); | 87 const FilePath::StringType& device_location); |
| 67 | 88 |
| 68 // Unique device id - persists between device attachments. | 89 // Unique device id - persists between device attachments. |
| 69 std::string device_id; | 90 std::string device_id; |
| 70 | 91 |
| 71 // Human readable removable storage device name. | 92 // Human readable removable storage device name. |
| 72 string16 name; | 93 string16 name; |
| 73 | 94 |
| 74 // Current attached removable storage device location. | 95 // Current attached removable storage device location. |
| 75 FilePath::StringType location; | 96 FilePath::StringType location; |
| 76 }; | 97 }; |
| 77 | 98 |
| 78 // Create SystemMonitor. Only one SystemMonitor instance per application | 99 // Create SystemMonitor. Only one SystemMonitor instance per application |
| 79 // is allowed. | 100 // is allowed. |
| 80 SystemMonitor(); | 101 SystemMonitor(StorageFreeSpaceDelegate* delegate = NULL); |
| 81 ~SystemMonitor(); | 102 ~SystemMonitor(); |
| 82 | 103 |
| 83 // Get the application-wide SystemMonitor (if not present, returns NULL). | 104 // Get the application-wide SystemMonitor (if not present, returns NULL). |
| 84 static SystemMonitor* Get(); | 105 static SystemMonitor* Get(); |
| 85 | 106 |
| 86 #if defined(OS_MACOSX) | 107 #if defined(OS_MACOSX) |
| 87 // Allocate system resources needed by the SystemMonitor class. | 108 // Allocate system resources needed by the SystemMonitor class. |
| 88 // | 109 // |
| 89 // This function must be called before instantiating an instance of the class | 110 // This function must be called before instantiating an instance of the class |
| 90 // and before the Sandbox is initialized. | 111 // and before the Sandbox is initialized. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 virtual void OnRemovableStorageAttached( | 162 virtual void OnRemovableStorageAttached( |
| 142 const std::string& id, | 163 const std::string& id, |
| 143 const string16& name, | 164 const string16& name, |
| 144 const FilePath::StringType& location) {} | 165 const FilePath::StringType& location) {} |
| 145 virtual void OnRemovableStorageDetached(const std::string& id) {} | 166 virtual void OnRemovableStorageDetached(const std::string& id) {} |
| 146 | 167 |
| 147 protected: | 168 protected: |
| 148 virtual ~DevicesChangedObserver() {} | 169 virtual ~DevicesChangedObserver() {} |
| 149 }; | 170 }; |
| 150 | 171 |
| 172 class BASE_EXPORT StorageFreeSpaceObserver { |
| 173 public: |
| 174 // Triggered when the free space of the storage |path| is changed. The |
| 175 // |available_capacity| indicates the available capacity in bytes. |
| 176 virtual void OnFreeSpaceChanged(const FilePath& path, |
| 177 int64 available_capacity) {} |
| 178 |
| 179 // Triggered when the free space delegate failed to start watching |
| 180 // the given storage |path|. |
| 181 virtual void OnFreeSpaceChangeError(const FilePath& path) {} |
| 182 }; |
| 183 |
| 151 // Add a new observer. | 184 // Add a new observer. |
| 152 // Can be called from any thread. | 185 // Can be called from any thread. |
| 153 // Must not be called from within a notification callback. | 186 // Must not be called from within a notification callback. |
| 154 void AddPowerObserver(PowerObserver* obs); | 187 void AddPowerObserver(PowerObserver* obs); |
| 155 void AddDevicesChangedObserver(DevicesChangedObserver* obs); | 188 void AddDevicesChangedObserver(DevicesChangedObserver* obs); |
| 189 void AddStorageFreeSpaceObserver(const FilePath& path, |
| 190 StorageFreeSpaceObserver* obs); |
| 156 | 191 |
| 157 // Remove an existing observer. | 192 // Remove an existing observer. |
| 158 // Can be called from any thread. | 193 // Can be called from any thread. |
| 159 // Must not be called from within a notification callback. | 194 // Must not be called from within a notification callback. |
| 160 void RemovePowerObserver(PowerObserver* obs); | 195 void RemovePowerObserver(PowerObserver* obs); |
| 161 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); | 196 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); |
| 197 void RemoveStorageFreeSpaceObserver(const FilePath& path, |
| 198 StorageFreeSpaceObserver* obs); |
| 162 | 199 |
| 163 // The ProcessFoo() style methods are a broken pattern and should not | 200 // The ProcessFoo() style methods are a broken pattern and should not |
| 164 // be copied. Any significant addition to this class is blocked on | 201 // be copied. Any significant addition to this class is blocked on |
| 165 // refactoring to improve the state of affairs. See http://crbug.com/149059 | 202 // refactoring to improve the state of affairs. See http://crbug.com/149059 |
| 166 | 203 |
| 167 #if defined(OS_WIN) | 204 #if defined(OS_WIN) |
| 168 // Windows-specific handling of a WM_POWERBROADCAST message. | 205 // Windows-specific handling of a WM_POWERBROADCAST message. |
| 169 // Embedders of this API should hook their top-level window | 206 // Embedders of this API should hook their top-level window |
| 170 // message loop and forward WM_POWERBROADCAST through this call. | 207 // message loop and forward WM_POWERBROADCAST through this call. |
| 171 void ProcessWmPowerBroadcastMessage(int event_id); | 208 void ProcessWmPowerBroadcastMessage(int event_id); |
| 172 #endif | 209 #endif |
| 173 | 210 |
| 174 // Cross-platform handling of a power event. | 211 // Cross-platform handling of a power event. |
| 175 void ProcessPowerMessage(PowerEvent event_id); | 212 void ProcessPowerMessage(PowerEvent event_id); |
| 176 | 213 |
| 177 // Cross-platform handling of a device change event. | 214 // Cross-platform handling of a device change event. |
| 178 void ProcessDevicesChanged(DeviceType device_type); | 215 void ProcessDevicesChanged(DeviceType device_type); |
| 179 void ProcessRemovableStorageAttached(const std::string& id, | 216 void ProcessRemovableStorageAttached(const std::string& id, |
| 180 const string16& name, | 217 const string16& name, |
| 181 const FilePath::StringType& location); | 218 const FilePath::StringType& location); |
| 182 void ProcessRemovableStorageDetached(const std::string& id); | 219 void ProcessRemovableStorageDetached(const std::string& id); |
| 183 | 220 |
| 221 // Cross-platform handling of free space change event. |
| 222 void ProcessStorageFreeSpaceChanged(const FilePath& path, |
| 223 int64 available_capacity); |
| 224 void ProcessStorageFreeSpaceChangeError(const FilePath& path); |
| 225 |
| 184 private: | 226 private: |
| 185 // Mapping of unique device id to device info tuple. | 227 // Mapping of unique device id to device info tuple. |
| 186 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap; | 228 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap; |
| 187 | 229 |
| 230 typedef ObserverListThreadSafe<StorageFreeSpaceObserver> |
| 231 StorageFreeSpaceObserverList; |
| 232 typedef std::set<StorageFreeSpaceObserver*> StorageFreeSpaceObserverSet; |
| 233 struct PathObserverPair { |
| 234 scoped_refptr<StorageFreeSpaceObserverList> observer_list; |
| 235 // Used to help decide when to stop watching a storage path since we can't |
| 236 // know when the thread safe observer list becomes empty. |
| 237 StorageFreeSpaceObserverSet observer_set; |
| 238 }; |
| 239 // Mapping of the storage device path to its observers. |
| 240 typedef std::map<FilePath, PathObserverPair> StorageFreeSpaceObserverListMap; |
| 241 |
| 188 #if defined(OS_MACOSX) | 242 #if defined(OS_MACOSX) |
| 189 void PlatformInit(); | 243 void PlatformInit(); |
| 190 void PlatformDestroy(); | 244 void PlatformDestroy(); |
| 191 #endif | 245 #endif |
| 192 | 246 |
| 193 // Platform-specific method to check whether the system is currently | 247 // Platform-specific method to check whether the system is currently |
| 194 // running on battery power. Returns true if running on batteries, | 248 // running on battery power. Returns true if running on batteries, |
| 195 // false otherwise. | 249 // false otherwise. |
| 196 bool IsBatteryPower(); | 250 bool IsBatteryPower(); |
| 197 | 251 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 222 #if defined(OS_IOS) | 276 #if defined(OS_IOS) |
| 223 // Holds pointers to system event notification observers. | 277 // Holds pointers to system event notification observers. |
| 224 std::vector<id> notification_observers_; | 278 std::vector<id> notification_observers_; |
| 225 #endif | 279 #endif |
| 226 | 280 |
| 227 // For manipulating removable_storage_map_ structure. | 281 // For manipulating removable_storage_map_ structure. |
| 228 mutable base::Lock removable_storage_lock_; | 282 mutable base::Lock removable_storage_lock_; |
| 229 // Map of all the attached removable storage devices. | 283 // Map of all the attached removable storage devices. |
| 230 RemovableStorageMap removable_storage_map_; | 284 RemovableStorageMap removable_storage_map_; |
| 231 | 285 |
| 286 // The delegate that implements free space change notification. |
| 287 StorageFreeSpaceDelegate* free_space_delegate_; |
| 288 |
| 289 // Used to map the storage path being watched to its observer list. |
| 290 StorageFreeSpaceObserverListMap free_space_observer_list_map_; |
| 291 |
| 292 // A lock used to synchronize access to |free_space_observer_list_map_|. |
| 293 base::Lock free_space_observers_lock_; |
| 294 |
| 232 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 295 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
| 233 }; | 296 }; |
| 234 | 297 |
| 235 } // namespace base | 298 } // namespace base |
| 236 | 299 |
| 237 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 300 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
| OLD | NEW |