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

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: 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_notification_(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(
126 const FilePath::StringType& path) {
127 DVLOG(1) << "Free Space Changing: " << path;
128 if (free_space_changed_observer_map_[path].get())
vandebo (ex-Chrome) 2012/09/11 22:49:27 Use find(path) so that you don't add something to
Hongbo Min 2012/09/12 09:24:31 Done.
129 free_space_changed_observer_map_[path]->Notify(
130 &StorageFreeSpaceChangedObserver::OnStorageFreeSpaceChanged, path);
131 }
132
124 std::vector<SystemMonitor::RemovableStorageInfo> 133 std::vector<SystemMonitor::RemovableStorageInfo>
125 SystemMonitor::GetAttachedRemovableStorage() const { 134 SystemMonitor::GetAttachedRemovableStorage() const {
126 std::vector<RemovableStorageInfo> results; 135 std::vector<RemovableStorageInfo> results;
127 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); 136 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin();
128 it != removable_storage_map_.end(); 137 it != removable_storage_map_.end();
129 ++it) { 138 ++it) {
130 results.push_back(it->second); 139 results.push_back(it->second);
131 } 140 }
132 return results; 141 return results;
133 } 142 }
134 143
135 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { 144 void SystemMonitor::AddPowerObserver(PowerObserver* obs) {
136 power_observer_list_->AddObserver(obs); 145 power_observer_list_->AddObserver(obs);
137 } 146 }
138 147
139 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { 148 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) {
140 power_observer_list_->RemoveObserver(obs); 149 power_observer_list_->RemoveObserver(obs);
141 } 150 }
142 151
143 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { 152 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) {
144 devices_changed_observer_list_->AddObserver(obs); 153 devices_changed_observer_list_->AddObserver(obs);
145 } 154 }
146 155
147 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { 156 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) {
148 devices_changed_observer_list_->RemoveObserver(obs); 157 devices_changed_observer_list_->RemoveObserver(obs);
149 } 158 }
150 159
160 void SystemMonitor::AddStorageFreeSpaceChangedObserver(
161 const FilePath::StringType& path,
162 StorageFreeSpaceChangedObserver* obs) {
163
164 // Try to start watching the storage.
165 if (watching_storage_set_.find(path) == watching_storage_set_.end() &&
166 !free_space_notification_->StartWatchingStorage(path))
vandebo (ex-Chrome) 2012/09/11 22:49:27 Make this two different if's to make it more reada
Hongbo Min 2012/09/12 09:24:31 Done.
167 return;
168
169 watching_storage_set_.insert(path);
170 if (!free_space_changed_observer_map_[path].get())
171 free_space_changed_observer_map_[path] =
172 new StorageFreeSpaceChangedObserverList();
173
174 free_space_changed_observer_map_[path]->AddObserver(obs);
vandebo (ex-Chrome) 2012/09/11 22:49:27 Once you have a lock, I think you'll want to add t
Hongbo Min 2012/09/12 09:24:31 It can avoid the lock occupied too much time in ca
175 }
176
177 void SystemMonitor::RemoveStorageFreeSpaceChangedObserver(
178 const FilePath::StringType& path,
179 StorageFreeSpaceChangedObserver* obs) {
180 size_t count = watching_storage_set_.count(path);
181 if (count == 0)
182 return;
183
184 // Stop watching the storage in case of the last observer is removed.
185 if (count == 1)
186 free_space_notification_->StopWatchingStorage(path);
187
188 free_space_changed_observer_map_[path]->RemoveObserver(obs);
189 watching_storage_set_.erase(path);
190 }
191
151 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { 192 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) {
152 DVLOG(1) << "DevicesChanged with device type " << device_type; 193 DVLOG(1) << "DevicesChanged with device type " << device_type;
153 devices_changed_observer_list_->Notify( 194 devices_changed_observer_list_->Notify(
154 &DevicesChangedObserver::OnDevicesChanged, device_type); 195 &DevicesChangedObserver::OnDevicesChanged, device_type);
155 } 196 }
156 197
157 void SystemMonitor::NotifyRemovableStorageAttached( 198 void SystemMonitor::NotifyRemovableStorageAttached(
158 const std::string& id, 199 const std::string& id,
159 const string16& name, 200 const string16& name,
160 const FilePath::StringType& location) { 201 const FilePath::StringType& location) {
(...skipping 24 matching lines...) Expand all
185 void SystemMonitor::NotifyResume() { 226 void SystemMonitor::NotifyResume() {
186 DVLOG(1) << "Power Resuming"; 227 DVLOG(1) << "Power Resuming";
187 power_observer_list_->Notify(&PowerObserver::OnResume); 228 power_observer_list_->Notify(&PowerObserver::OnResume);
188 } 229 }
189 230
190 void SystemMonitor::BatteryCheck() { 231 void SystemMonitor::BatteryCheck() {
191 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); 232 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
192 } 233 }
193 234
194 } // namespace base 235 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698