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

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

Issue 10905237: Fix a potential multi-thread issue when manipulating removable storage map (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: use mutable for lock 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"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/time.h" 12 #include "base/time.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 14
15 namespace base { 15 namespace base {
16 16
17 static SystemMonitor* g_system_monitor = NULL; 17 static SystemMonitor* g_system_monitor = NULL;
Philippe 2012/12/14 13:53:03 Could we use a leaky lazy instance here? I'm conce
vandebo (ex-Chrome) 2013/01/04 19:46:54 g_system_monitor will go away during the refactor.
18 18
19 #if defined(ENABLE_BATTERY_MONITORING) 19 #if defined(ENABLE_BATTERY_MONITORING)
20 // The amount of time (in ms) to wait before running the initial 20 // The amount of time (in ms) to wait before running the initial
21 // battery check. 21 // battery check.
22 static int kDelayedBatteryCheckMs = 10 * 1000; 22 static int kDelayedBatteryCheckMs = 10 * 1000;
23 #endif // defined(ENABLE_BATTERY_MONITORING) 23 #endif // defined(ENABLE_BATTERY_MONITORING)
24 24
25 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo() { 25 SystemMonitor::RemovableStorageInfo::RemovableStorageInfo() {
26 } 26 }
27 27
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 #endif 60 #endif
61 DCHECK_EQ(this, g_system_monitor); 61 DCHECK_EQ(this, g_system_monitor);
62 g_system_monitor = NULL; 62 g_system_monitor = NULL;
63 } 63 }
64 64
65 // static 65 // static
66 SystemMonitor* SystemMonitor::Get() { 66 SystemMonitor* SystemMonitor::Get() {
67 return g_system_monitor; 67 return g_system_monitor;
68 } 68 }
69 69
70 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { 70 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
Philippe 2012/12/14 13:53:03 I believe this function is supposed to be callable
Hongbo Min 2012/12/15 14:03:28 There is a bug for refactoring SystemMonitor to re
vandebo (ex-Chrome) 2013/01/04 19:46:54 This function will go away in the refactor, but it
71 // Suppress duplicate notifications. Some platforms may 71 // Suppress duplicate notifications. Some platforms may
72 // send multiple notifications of the same event. 72 // send multiple notifications of the same event.
73 switch (event_id) { 73 switch (event_id) {
74 case POWER_STATE_EVENT: 74 case POWER_STATE_EVENT:
75 { 75 {
76 bool on_battery = IsBatteryPower(); 76 bool on_battery = IsBatteryPower();
77 if (on_battery != battery_in_use_) { 77 if (on_battery != battery_in_use_) {
78 battery_in_use_ = on_battery; 78 battery_in_use_ = on_battery;
79 NotifyPowerStateChange(); 79 NotifyPowerStateChange();
80 } 80 }
(...skipping 15 matching lines...) Expand all
96 } 96 }
97 97
98 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { 98 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) {
99 NotifyDevicesChanged(device_type); 99 NotifyDevicesChanged(device_type);
100 } 100 }
101 101
102 void SystemMonitor::ProcessRemovableStorageAttached( 102 void SystemMonitor::ProcessRemovableStorageAttached(
103 const std::string& id, 103 const std::string& id,
104 const string16& name, 104 const string16& name,
105 const FilePath::StringType& location) { 105 const FilePath::StringType& location) {
106 if (ContainsKey(removable_storage_map_, id)) { 106 {
107 // This can happen if our unique id scheme fails. Ignore the incoming 107 base::AutoLock lock(removable_storage_lock_);
108 // non-unique attachment. 108 if (ContainsKey(removable_storage_map_, id)) {
109 return; 109 // This can happen if our unique id scheme fails. Ignore the incoming
110 // non-unique attachment.
111 return;
112 }
113 RemovableStorageInfo info(id, name, location);
114 removable_storage_map_.insert(std::make_pair(id, info));
110 } 115 }
111 RemovableStorageInfo info(id, name, location);
112 removable_storage_map_.insert(std::make_pair(id, info));
113 NotifyRemovableStorageAttached(id, name, location); 116 NotifyRemovableStorageAttached(id, name, location);
114 } 117 }
115 118
116 void SystemMonitor::ProcessRemovableStorageDetached(const std::string& id) { 119 void SystemMonitor::ProcessRemovableStorageDetached(const std::string& id) {
117 RemovableStorageMap::iterator it = removable_storage_map_.find(id); 120 {
118 if (it == removable_storage_map_.end()) 121 base::AutoLock lock(removable_storage_lock_);
119 return; 122 RemovableStorageMap::iterator it = removable_storage_map_.find(id);
120 removable_storage_map_.erase(it); 123 if (it == removable_storage_map_.end())
124 return;
125 removable_storage_map_.erase(it);
126 }
121 NotifyRemovableStorageDetached(id); 127 NotifyRemovableStorageDetached(id);
122 } 128 }
123 129
124 std::vector<SystemMonitor::RemovableStorageInfo> 130 std::vector<SystemMonitor::RemovableStorageInfo>
125 SystemMonitor::GetAttachedRemovableStorage() const { 131 SystemMonitor::GetAttachedRemovableStorage() const {
126 std::vector<RemovableStorageInfo> results; 132 std::vector<RemovableStorageInfo> results;
133
134 base::AutoLock lock(removable_storage_lock_);
127 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); 135 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin();
128 it != removable_storage_map_.end(); 136 it != removable_storage_map_.end();
129 ++it) { 137 ++it) {
130 results.push_back(it->second); 138 results.push_back(it->second);
131 } 139 }
132 return results; 140 return results;
133 } 141 }
134 142
135 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { 143 void SystemMonitor::AddPowerObserver(PowerObserver* obs) {
136 power_observer_list_->AddObserver(obs); 144 power_observer_list_->AddObserver(obs);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 void SystemMonitor::NotifyResume() { 193 void SystemMonitor::NotifyResume() {
186 DVLOG(1) << "Power Resuming"; 194 DVLOG(1) << "Power Resuming";
187 power_observer_list_->Notify(&PowerObserver::OnResume); 195 power_observer_list_->Notify(&PowerObserver::OnResume);
188 } 196 }
189 197
190 void SystemMonitor::BatteryCheck() { 198 void SystemMonitor::BatteryCheck() {
191 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); 199 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
192 } 200 }
193 201
194 } // namespace base 202 } // namespace base
OLDNEW
« base/system_monitor/system_monitor.h ('K') | « base/system_monitor/system_monitor.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698