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

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

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: Update and add unit test 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
« no previous file with comments | « base/system_monitor/system_monitor.h ('k') | base/system_monitor/system_monitor_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "base/system_monitor/system_monitor.h" 5 #include "base/system_monitor/system_monitor.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 16 matching lines...) Expand all
27 27
28 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo( 28 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo(
29 const std::string& id, 29 const std::string& id,
30 const string16& device_name, 30 const string16& device_name,
31 const FilePath::StringType& device_location) 31 const FilePath::StringType& device_location)
32 : device_id(id), 32 : device_id(id),
33 name(device_name), 33 name(device_name),
34 location(device_location) { 34 location(device_location) {
35 } 35 }
36 36
37 SystemMonitor::SystemMonitor() 37 SystemMonitor::SystemMonitor(StorageFreeSpaceDelegate* delegate)
38 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), 38 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()),
39 devices_changed_observer_list_( 39 devices_changed_observer_list_(
40 new ObserverListThreadSafe<DevicesChangedObserver>()), 40 new ObserverListThreadSafe<DevicesChangedObserver>()),
41 battery_in_use_(false), 41 battery_in_use_(false),
42 suspended_(false) { 42 suspended_(false),
43 free_space_delegate_(delegate) {
43 DCHECK(!g_system_monitor); 44 DCHECK(!g_system_monitor);
44 g_system_monitor = this; 45 g_system_monitor = this;
45 46
46 DCHECK(MessageLoop::current()); 47 DCHECK(MessageLoop::current());
47 #if defined(ENABLE_BATTERY_MONITORING) 48 #if defined(ENABLE_BATTERY_MONITORING)
48 delayed_battery_check_.Start(FROM_HERE, 49 delayed_battery_check_.Start(FROM_HERE,
49 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, 50 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this,
50 &SystemMonitor::BatteryCheck); 51 &SystemMonitor::BatteryCheck);
51 #endif // defined(ENABLE_BATTERY_MONITORING) 52 #endif // defined(ENABLE_BATTERY_MONITORING)
52 #if defined(OS_MACOSX) 53 #if defined(OS_MACOSX)
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 { 121 {
121 base::AutoLock lock(removable_storage_lock_); 122 base::AutoLock lock(removable_storage_lock_);
122 RemovableStorageMap::iterator it = removable_storage_map_.find(id); 123 RemovableStorageMap::iterator it = removable_storage_map_.find(id);
123 if (it == removable_storage_map_.end()) 124 if (it == removable_storage_map_.end())
124 return; 125 return;
125 removable_storage_map_.erase(it); 126 removable_storage_map_.erase(it);
126 } 127 }
127 NotifyRemovableStorageDetached(id); 128 NotifyRemovableStorageDetached(id);
128 } 129 }
129 130
131 void SystemMonitor::ProcessStorageFreeSpaceChanged(const FilePath& path,
132 int64 available_capacity) {
133 DVLOG(1) << "Free Space Change: " << path.value() << " "
134 << available_capacity << " bytes";
135 scoped_refptr<StorageFreeSpaceObserverList> observers;
136 {
137 base::AutoLock lock(free_space_observers_lock_);
138 StorageFreeSpaceObserverListMap::iterator it =
139 free_space_observer_list_map_.find(path);
140 if (it == free_space_observer_list_map_.end())
141 return;
142 observers = it->second.observer_list;
143 }
144 if (observers.get())
145 observers->Notify(&StorageFreeSpaceObserver::OnFreeSpaceChanged,
146 path, available_capacity);
147 }
148
149 void SystemMonitor::ProcessStorageFreeSpaceChangeError(const FilePath& path) {
150 DVLOG(1) << "Failed to watch free space change for " << path.value();
151 scoped_refptr<StorageFreeSpaceObserverList> observers;
152 {
153 base::AutoLock lock(free_space_observers_lock_);
154 StorageFreeSpaceObserverListMap::iterator it =
155 free_space_observer_list_map_.find(path);
156 if (it == free_space_observer_list_map_.end())
157 return;
158 observers = it->second.observer_list;
159 }
160 if (observers.get())
161 observers->Notify(&StorageFreeSpaceObserver::OnFreeSpaceChangeError,
162 path);
163 }
164
130 std::vector<SystemMonitor::RemovableStorageInfo> 165 std::vector<SystemMonitor::RemovableStorageInfo>
131 SystemMonitor::GetAttachedRemovableStorage() const { 166 SystemMonitor::GetAttachedRemovableStorage() const {
132 std::vector<RemovableStorageInfo> results; 167 std::vector<RemovableStorageInfo> results;
133 168
134 base::AutoLock lock(removable_storage_lock_); 169 base::AutoLock lock(removable_storage_lock_);
135 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); 170 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin();
136 it != removable_storage_map_.end(); 171 it != removable_storage_map_.end();
137 ++it) { 172 ++it) {
138 results.push_back(it->second); 173 results.push_back(it->second);
139 } 174 }
140 return results; 175 return results;
141 } 176 }
142 177
143 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { 178 void SystemMonitor::AddPowerObserver(PowerObserver* obs) {
144 power_observer_list_->AddObserver(obs); 179 power_observer_list_->AddObserver(obs);
145 } 180 }
146 181
147 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { 182 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) {
148 power_observer_list_->RemoveObserver(obs); 183 power_observer_list_->RemoveObserver(obs);
149 } 184 }
150 185
151 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { 186 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) {
152 devices_changed_observer_list_->AddObserver(obs); 187 devices_changed_observer_list_->AddObserver(obs);
153 } 188 }
154 189
155 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { 190 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) {
156 devices_changed_observer_list_->RemoveObserver(obs); 191 devices_changed_observer_list_->RemoveObserver(obs);
157 } 192 }
158 193
194 void SystemMonitor::AddStorageFreeSpaceObserver(const FilePath& path,
195 StorageFreeSpaceObserver* obs) {
196 DCHECK(free_space_delegate_ != NULL);
197 if (!free_space_delegate_)
198 return;
199 scoped_refptr<StorageFreeSpaceObserverList> observers;
200 {
201 base::AutoLock lock(free_space_observers_lock_);
202 // Try to start watching the given storage |path| if the first observer
203 // will be added.
204 StorageFreeSpaceObserverListMap::iterator iter =
205 free_space_observer_list_map_.find(path);
206 if (iter == free_space_observer_list_map_.end()) {
207 PathObserverPair pair;
208 pair.observer_list = observers = new StorageFreeSpaceObserverList;
209 pair.observer_set.insert(obs);
210 free_space_observer_list_map_.insert(std::make_pair(path, pair));
211 free_space_delegate_->StartWatchingStorage(path);
212 } else {
213 observers = iter->second.observer_list;
214 iter->second.observer_set.insert(obs);
215 }
216 }
217 observers->AddObserver(obs);
218 }
219
220 void SystemMonitor::RemoveStorageFreeSpaceObserver(
221 const FilePath& path,
222 StorageFreeSpaceObserver* obs) {
223 DCHECK(free_space_delegate_ != NULL);
224 if (!free_space_delegate_)
225 return;
226 scoped_refptr<StorageFreeSpaceObserverList> observers;
227 {
228 base::AutoLock lock(free_space_observers_lock_);
229 StorageFreeSpaceObserverListMap::iterator iter =
230 free_space_observer_list_map_.find(path);
231 // In case of no observer is interested in the given storage |path|.
232 if (iter == free_space_observer_list_map_.end()) {
233 return;
234 }
235
236 StorageFreeSpaceObserverSet::iterator sit
237 = iter->second.observer_set.find(obs);
238 // No the given observer is found.
239 if (sit == iter->second.observer_set.end())
240 return;
241 iter->second.observer_set.erase(sit);
242 // Try to stop watching in case of the last observer is removed.
243 if (iter->second.observer_set.size() == 0) {
244 free_space_delegate_->StopWatchingStorage(path);
245 free_space_observer_list_map_.erase(path);
246 return;
247 }
248 observers = iter->second.observer_list;
249 }
250 observers->RemoveObserver(obs);
251 }
252
159 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { 253 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) {
160 DVLOG(1) << "DevicesChanged with device type " << device_type; 254 DVLOG(1) << "DevicesChanged with device type " << device_type;
161 devices_changed_observer_list_->Notify( 255 devices_changed_observer_list_->Notify(
162 &DevicesChangedObserver::OnDevicesChanged, device_type); 256 &DevicesChangedObserver::OnDevicesChanged, device_type);
163 } 257 }
164 258
165 void SystemMonitor::NotifyRemovableStorageAttached( 259 void SystemMonitor::NotifyRemovableStorageAttached(
166 const std::string& id, 260 const std::string& id,
167 const string16& name, 261 const string16& name,
168 const FilePath::StringType& location) { 262 const FilePath::StringType& location) {
(...skipping 24 matching lines...) Expand all
193 void SystemMonitor::NotifyResume() { 287 void SystemMonitor::NotifyResume() {
194 DVLOG(1) << "Power Resuming"; 288 DVLOG(1) << "Power Resuming";
195 power_observer_list_->Notify(&PowerObserver::OnResume); 289 power_observer_list_->Notify(&PowerObserver::OnResume);
196 } 290 }
197 291
198 void SystemMonitor::BatteryCheck() { 292 void SystemMonitor::BatteryCheck() {
199 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); 293 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
200 } 294 }
201 295
202 } // namespace base 296 } // namespace base
OLDNEW
« no previous file with comments | « base/system_monitor/system_monitor.h ('k') | base/system_monitor/system_monitor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698