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

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: Rebase and add available_capacity into the observer callback 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 #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 21 matching lines...) Expand all
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()
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_(NULL) {
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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 } 115 }
115 116
116 void SystemMonitor::ProcessRemovableStorageDetached(const std::string& id) { 117 void SystemMonitor::ProcessRemovableStorageDetached(const std::string& id) {
117 RemovableStorageMap::iterator it = removable_storage_map_.find(id); 118 RemovableStorageMap::iterator it = removable_storage_map_.find(id);
118 if (it == removable_storage_map_.end()) 119 if (it == removable_storage_map_.end())
119 return; 120 return;
120 removable_storage_map_.erase(it); 121 removable_storage_map_.erase(it);
121 NotifyRemovableStorageDetached(id); 122 NotifyRemovableStorageDetached(id);
122 } 123 }
123 124
125 void SystemMonitor::ProcessStorageFreeSpaceChanged(const FilePath& path,
126 int64 available_capacity) {
127 DVLOG(1) << "Free Space Changing: " << path.value() << " "
128 << available_capacity << " bytes";
129 if (ContainsKey(free_space_changed_observer_map_, path) &&
vandebo (ex-Chrome) 2012/09/12 17:54:47 This needs to be locked.
vandebo (ex-Chrome) 2012/09/12 17:54:47 Use find so you only look up the path once. (else
Hongbo Min 2012/09/13 13:40:53 Done.
Hongbo Min 2012/09/13 13:40:53 Done.
130 free_space_changed_observer_map_[path].get())
131 free_space_changed_observer_map_[path]->Notify(
132 &StorageFreeSpaceChangedObserver::OnStorageFreeSpaceChanged,
133 path, available_capacity);
134 }
135
124 std::vector<SystemMonitor::RemovableStorageInfo> 136 std::vector<SystemMonitor::RemovableStorageInfo>
125 SystemMonitor::GetAttachedRemovableStorage() const { 137 SystemMonitor::GetAttachedRemovableStorage() const {
126 std::vector<RemovableStorageInfo> results; 138 std::vector<RemovableStorageInfo> results;
127 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); 139 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin();
128 it != removable_storage_map_.end(); 140 it != removable_storage_map_.end();
129 ++it) { 141 ++it) {
130 results.push_back(it->second); 142 results.push_back(it->second);
131 } 143 }
132 return results; 144 return results;
133 } 145 }
134 146
135 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { 147 void SystemMonitor::AddPowerObserver(PowerObserver* obs) {
136 power_observer_list_->AddObserver(obs); 148 power_observer_list_->AddObserver(obs);
137 } 149 }
138 150
139 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { 151 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) {
140 power_observer_list_->RemoveObserver(obs); 152 power_observer_list_->RemoveObserver(obs);
141 } 153 }
142 154
143 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { 155 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) {
144 devices_changed_observer_list_->AddObserver(obs); 156 devices_changed_observer_list_->AddObserver(obs);
145 } 157 }
146 158
147 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { 159 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) {
148 devices_changed_observer_list_->RemoveObserver(obs); 160 devices_changed_observer_list_->RemoveObserver(obs);
149 } 161 }
150 162
163 void SystemMonitor::AddStorageFreeSpaceChangedObserver(
164 const FilePath& path,
165 StorageFreeSpaceChangedObserver* obs) {
166 {
167 base::AutoLock lock(free_space_observers_lock_);
168 if (!ContainsKey(free_space_changed_observer_map_, path))
169 free_space_changed_observer_map_[path] =
170 new StorageFreeSpaceChangedObserverList();
vandebo (ex-Chrome) 2012/09/12 17:54:47 nit: Omit () when using new with a constructor tha
Hongbo Min 2012/09/13 13:40:53 Done.
171
172 watching_storage_set_.insert(path);
173 free_space_changed_observer_map_[path]->AddObserver(obs);
174 }
175
176 if (!free_space_delegate_->IsWatchingStorage(path))
177 free_space_delegate_->StartWatchingStorage(path);
vandebo (ex-Chrome) 2012/09/12 17:54:47 Hmm, consumers can call AddStorageFreeSpaceChanged
Hongbo Min 2012/09/13 13:40:53 Done. See the code comment of StorageFreeSpaceDele
178 }
179
180 void SystemMonitor::RemoveStorageFreeSpaceChangedObserver(
181 const FilePath& path,
182 StorageFreeSpaceChangedObserver* obs) {
183 base::AutoLock lock(free_space_observers_lock_);
184
185 std::multiset<FilePath>::iterator it = watching_storage_set_.find(path);
186 // Nobody is interested in the given |path|.
187 if (it == watching_storage_set_.end())
188 return;
189
190 free_space_changed_observer_map_[path]->RemoveObserver(obs);
191 watching_storage_set_.erase(it);
192
193 // Try to stop watching the storage if the last observer is removed.
194 if (!ContainsKey(watching_storage_set_, path) &&
195 free_space_delegate_->IsWatchingStorage(path))
196 free_space_delegate_->StopWatchingStorage(path);
197 }
198
151 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { 199 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) {
152 DVLOG(1) << "DevicesChanged with device type " << device_type; 200 DVLOG(1) << "DevicesChanged with device type " << device_type;
153 devices_changed_observer_list_->Notify( 201 devices_changed_observer_list_->Notify(
154 &DevicesChangedObserver::OnDevicesChanged, device_type); 202 &DevicesChangedObserver::OnDevicesChanged, device_type);
155 } 203 }
156 204
157 void SystemMonitor::NotifyRemovableStorageAttached( 205 void SystemMonitor::NotifyRemovableStorageAttached(
158 const std::string& id, 206 const std::string& id,
159 const string16& name, 207 const string16& name,
160 const FilePath::StringType& location) { 208 const FilePath::StringType& location) {
(...skipping 24 matching lines...) Expand all
185 void SystemMonitor::NotifyResume() { 233 void SystemMonitor::NotifyResume() {
186 DVLOG(1) << "Power Resuming"; 234 DVLOG(1) << "Power Resuming";
187 power_observer_list_->Notify(&PowerObserver::OnResume); 235 power_observer_list_->Notify(&PowerObserver::OnResume);
188 } 236 }
189 237
190 void SystemMonitor::BatteryCheck() { 238 void SystemMonitor::BatteryCheck() {
191 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); 239 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
192 } 240 }
193 241
194 } // namespace base 242 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698