Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: base/system_monitor/system_monitor.h

Issue 10917166: Extend the capability of SystemMonitor to support watching storage free space change (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: updated patch Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
vandebo (ex-Chrome) 2012/09/13 19:57:34 Your other CL was committed, so you need to rebase
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
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 change notification.
64 // Since the client code may call these interfaces from any thread, the
vandebo (ex-Chrome) 2012/09/13 19:57:34 I intended for you to save the thread id in delega
Hongbo Min 2012/09/14 06:04:36 Actually, the base code is not allowed to access B
vandebo (ex-Chrome) 2012/09/14 08:07:18 No, but it is allowed to look at the thread ID. L
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
61 struct BASE_EXPORT RemovableStorageInfo { 83 struct BASE_EXPORT RemovableStorageInfo {
62 RemovableStorageInfo(); 84 RemovableStorageInfo();
63 RemovableStorageInfo(const std::string& id, 85 RemovableStorageInfo(const std::string& id,
64 const string16& device_name, 86 const string16& device_name,
65 const FilePath::StringType& device_location); 87 const FilePath::StringType& device_location);
66 88
67 // Unique device id - persists between device attachments. 89 // Unique device id - persists between device attachments.
68 std::string device_id; 90 std::string device_id;
69 91
70 // Human readable removable storage device name. 92 // Human readable removable storage device name.
(...skipping 20 matching lines...) Expand all
91 static void AllocateSystemIOPorts(); 113 static void AllocateSystemIOPorts();
92 #else 114 #else
93 static void AllocateSystemIOPorts() {} 115 static void AllocateSystemIOPorts() {}
94 #endif // OS_IOS 116 #endif // OS_IOS
95 #endif // OS_MACOSX 117 #endif // OS_MACOSX
96 118
97 // Returns information for attached removable storage. 119 // Returns information for attached removable storage.
98 std::vector<RemovableStorageInfo> GetAttachedRemovableStorage() const; 120 std::vector<RemovableStorageInfo> GetAttachedRemovableStorage() const;
99 121
100 // 122 //
101 // Power-related APIs 123 // Power-related APIs
willchan no longer on Chromium 2012/09/13 18:41:14 It looks like a mix of trying to group by API type
vandebo (ex-Chrome) 2012/09/13 20:08:41 Added to my post M23 branch todo list.
102 // 124 //
103 125
104 // Is the computer currently on battery power. 126 // Is the computer currently on battery power.
105 // Can be called on any thread. 127 // Can be called on any thread.
106 bool BatteryPower() const { 128 bool BatteryPower() const {
107 // Using a lock here is not necessary for just a bool. 129 // Using a lock here is not necessary for just a bool.
108 return battery_in_use_; 130 return battery_in_use_;
109 } 131 }
110 132
111 // Callbacks will be called on the thread which creates the SystemMonitor. 133 // Callbacks will be called on the thread which creates the SystemMonitor.
(...skipping 28 matching lines...) Expand all
140 virtual void OnRemovableStorageAttached( 162 virtual void OnRemovableStorageAttached(
141 const std::string& id, 163 const std::string& id,
142 const string16& name, 164 const string16& name,
143 const FilePath::StringType& location) {} 165 const FilePath::StringType& location) {}
144 virtual void OnRemovableStorageDetached(const std::string& id) {} 166 virtual void OnRemovableStorageDetached(const std::string& id) {}
145 167
146 protected: 168 protected:
147 virtual ~DevicesChangedObserver() {} 169 virtual ~DevicesChangedObserver() {}
148 }; 170 };
149 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) {}
vandebo (ex-Chrome) 2012/09/13 19:57:34 Shouldn't this be a uint64?
178
179 // Triggered when the free space delegate failed to start watching
180 // the given storage |path|.
181 virtual void OnFreeSpaceWatchingError(const FilePath& path) {}
vandebo (ex-Chrome) 2012/09/13 19:57:34 Maybe OnFreeSpaceChangeError()? What do you think
182 };
183
150 // Add a new observer. 184 // Add a new observer.
151 // Can be called from any thread. 185 // Can be called from any thread.
152 // Must not be called from within a notification callback. 186 // Must not be called from within a notification callback.
153 void AddPowerObserver(PowerObserver* obs); 187 void AddPowerObserver(PowerObserver* obs);
154 void AddDevicesChangedObserver(DevicesChangedObserver* obs); 188 void AddDevicesChangedObserver(DevicesChangedObserver* obs);
189 void AddStorageFreeSpaceObserver(const FilePath& path,
190 StorageFreeSpaceObserver* obs);
155 191
156 // Remove an existing observer. 192 // Remove an existing observer.
157 // Can be called from any thread. 193 // Can be called from any thread.
158 // Must not be called from within a notification callback. 194 // Must not be called from within a notification callback.
159 void RemovePowerObserver(PowerObserver* obs); 195 void RemovePowerObserver(PowerObserver* obs);
160 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); 196 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs);
197 void RemoveStorageFreeSpaceObserver(const FilePath& path,
198 StorageFreeSpaceObserver* obs);
161 199
162 #if defined(OS_WIN) 200 #if defined(OS_WIN)
163 // Windows-specific handling of a WM_POWERBROADCAST message. 201 // Windows-specific handling of a WM_POWERBROADCAST message.
164 // Embedders of this API should hook their top-level window 202 // Embedders of this API should hook their top-level window
165 // message loop and forward WM_POWERBROADCAST through this call. 203 // message loop and forward WM_POWERBROADCAST through this call.
166 void ProcessWmPowerBroadcastMessage(int event_id); 204 void ProcessWmPowerBroadcastMessage(int event_id);
167 #endif 205 #endif
168 206
169 // Cross-platform handling of a power event. 207 // Cross-platform handling of a power event.
170 void ProcessPowerMessage(PowerEvent event_id); 208 void ProcessPowerMessage(PowerEvent event_id);
171 209
172 // Cross-platform handling of a device change event. 210 // Cross-platform handling of a device change event.
173 void ProcessDevicesChanged(DeviceType device_type); 211 void ProcessDevicesChanged(DeviceType device_type);
174 void ProcessRemovableStorageAttached(const std::string& id, 212 void ProcessRemovableStorageAttached(const std::string& id,
175 const string16& name, 213 const string16& name,
176 const FilePath::StringType& location); 214 const FilePath::StringType& location);
177 void ProcessRemovableStorageDetached(const std::string& id); 215 void ProcessRemovableStorageDetached(const std::string& id);
178 216
217 // Cross-platform handling of free space change event. The parameter
218 // |available_capacity| is valid only if the |error| is false.
219 void ProcessStorageFreeSpaceMessage(const FilePath& path,
vandebo (ex-Chrome) 2012/09/13 19:57:34 Because of the pattern for other things in the fil
220 int64 available_capacity,
221 bool error);
222
223 StorageFreeSpaceDelegate* storage_free_space_delegate() const {
vandebo (ex-Chrome) 2012/09/13 19:57:34 I don't think you really need this method.
224 return free_space_delegate_;
225 }
226
227 // Called on the thread that starts and stops watching storage paths.
228 void set_storage_free_space_delegate(StorageFreeSpaceDelegate* delegate);
willchan no longer on Chromium 2012/09/13 18:41:14 Ah, this is interesting. Please correct me if I'm
vandebo (ex-Chrome) 2012/09/13 20:08:41 Yes, the various parts of system monitor are imple
willchan no longer on Chromium 2012/09/13 20:24:33 Not needed. The setter is the constructor of Syste
229
179 private: 230 private:
180 // Mapping of unique device id to device info tuple. 231 // Mapping of unique device id to device info tuple.
181 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap; 232 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap;
182 233
234 typedef ObserverListThreadSafe<StorageFreeSpaceObserver>
235 StorageFreeSpaceObserverList;
236 // Mapping of the storage device path to the observer list.
237 typedef std::map<FilePath, scoped_refptr<StorageFreeSpaceObserverList> >
238 StorageFreeSpaceObserverListMap;
239 // Multi-mapping of the storage path to its observer.
240 typedef std::multimap<FilePath, StorageFreeSpaceObserver*>
vandebo (ex-Chrome) 2012/09/13 19:57:34 Using a multimap, you've almost accomplished what
Hongbo Min 2012/09/14 06:04:36 I don't think std::set can reduce the code complex
vandebo (ex-Chrome) 2012/09/14 08:07:18 Indeed, but using std::map<FilePath, std::set<Stor
241 StorageFreeSpaceObserverMap;
242
183 #if defined(OS_MACOSX) 243 #if defined(OS_MACOSX)
184 void PlatformInit(); 244 void PlatformInit();
185 void PlatformDestroy(); 245 void PlatformDestroy();
186 #endif 246 #endif
187 247
188 // Platform-specific method to check whether the system is currently 248 // Platform-specific method to check whether the system is currently
189 // running on battery power. Returns true if running on batteries, 249 // running on battery power. Returns true if running on batteries,
190 // false otherwise. 250 // false otherwise.
191 bool IsBatteryPower(); 251 bool IsBatteryPower();
192 252
193 // Checks the battery status and notifies observers if the battery 253 // Checks the battery status and notifies observers if the battery
194 // status has changed. 254 // status has changed.
195 void BatteryCheck(); 255 void BatteryCheck();
196 256
197 // Functions to trigger notifications. 257 // Functions to trigger notifications.
198 void NotifyDevicesChanged(DeviceType device_type); 258 void NotifyDevicesChanged(DeviceType device_type);
199 void NotifyRemovableStorageAttached(const std::string& id, 259 void NotifyRemovableStorageAttached(const std::string& id,
200 const string16& name, 260 const string16& name,
201 const FilePath::StringType& location); 261 const FilePath::StringType& location);
202 void NotifyRemovableStorageDetached(const std::string& id); 262 void NotifyRemovableStorageDetached(const std::string& id);
203 void NotifyPowerStateChange(); 263 void NotifyPowerStateChange();
204 void NotifySuspend(); 264 void NotifySuspend();
205 void NotifyResume(); 265 void NotifyResume();
206 266
267 // Notify the |observers| observing the given |path| for free space change
268 // event.
269 void NotifyStorageFreeSpaceChanged(
270 scoped_refptr<StorageFreeSpaceObserverList> observers,
271 const FilePath& path,
272 int64 available_capacity);
273 void NotifyStorageFreeSpaceWatchingError(
274 scoped_refptr<StorageFreeSpaceObserverList> observers,
275 const FilePath& path);
276
207 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; 277 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_;
208 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > 278 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> >
209 devices_changed_observer_list_; 279 devices_changed_observer_list_;
210 bool battery_in_use_; 280 bool battery_in_use_;
211 bool suspended_; 281 bool suspended_;
212 282
213 #if defined(ENABLE_BATTERY_MONITORING) 283 #if defined(ENABLE_BATTERY_MONITORING)
214 base::OneShotTimer<SystemMonitor> delayed_battery_check_; 284 base::OneShotTimer<SystemMonitor> delayed_battery_check_;
215 #endif 285 #endif
216 286
217 #if defined(OS_IOS) 287 #if defined(OS_IOS)
218 // Holds pointers to system event notification observers. 288 // Holds pointers to system event notification observers.
219 std::vector<id> notification_observers_; 289 std::vector<id> notification_observers_;
220 #endif 290 #endif
221 291
222 // Map of all the attached removable storage devices. 292 // Map of all the attached removable storage devices.
223 RemovableStorageMap removable_storage_map_; 293 RemovableStorageMap removable_storage_map_;
224 294
295 // The delegate that implements free space change notification.
296 StorageFreeSpaceDelegate* free_space_delegate_;
297
298 // Used to map the storage path being watched to its observer list.
299 StorageFreeSpaceObserverListMap free_space_observer_list_map_;
300
301 // Used to map the storage path and its associated observer. We use multimap
302 // because a path may be observed by more than one observer. It helps decide
303 // when to stop watching a storage path since we can't know when the thread
304 // safe observer list becomes empty.
305 StorageFreeSpaceObserverMap free_space_observer_map_;
306
307 // A lock used to synchronize access to |free_space_observer_list_map_| and
308 // |free_space_observer_map_| data.
309 base::Lock free_space_observers_lock_;
310
225 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); 311 DISALLOW_COPY_AND_ASSIGN(SystemMonitor);
226 }; 312 };
227 313
228 } // namespace base 314 } // namespace base
229 315
230 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ 316 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_
OLDNEW
« no previous file with comments | « no previous file | base/system_monitor/system_monitor.cc » ('j') | base/system_monitor/system_monitor.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698