| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/stl_util.h" |
| 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/system_monitor/removable_storage_notifications.h" |
| 8 |
| 9 namespace chrome { |
| 10 |
| 11 RemovableStorageNotifications::RemovableStorageInfo::RemovableStorageInfo() { |
| 12 } |
| 13 |
| 14 RemovableStorageNotifications::RemovableStorageInfo::RemovableStorageInfo( |
| 15 const std::string& id, |
| 16 const string16& device_name, |
| 17 const FilePath::StringType& device_location) |
| 18 : device_id(id), |
| 19 name(device_name), |
| 20 location(device_location) { |
| 21 } |
| 22 |
| 23 RemovableStorageNotifications::RemovableStorageNotifications() |
| 24 : observer_list_( |
| 25 new ObserverListThreadSafe<RemovableStorageObserver>()) { |
| 26 } |
| 27 |
| 28 void RemovableStorageNotifications::ProcessRemovableStorageAttached( |
| 29 const std::string& id, |
| 30 const string16& name, |
| 31 const FilePath::StringType& location) { |
| 32 { |
| 33 base::AutoLock lock(removable_storage_lock_); |
| 34 if (ContainsKey(removable_storage_map_, id)) { |
| 35 // This can happen if our unique id scheme fails. Ignore the incoming |
| 36 // non-unique attachment. |
| 37 return; |
| 38 } |
| 39 RemovableStorageInfo info(id, name, location); |
| 40 removable_storage_map_.insert(std::make_pair(id, info)); |
| 41 } |
| 42 NotifyRemovableStorageAttached(id, name, location); |
| 43 } |
| 44 |
| 45 void RemovableStorageNotifications::ProcessRemovableStorageDetached( |
| 46 const std::string& id) { |
| 47 { |
| 48 base::AutoLock lock(removable_storage_lock_); |
| 49 RemovableStorageMap::iterator it = removable_storage_map_.find(id); |
| 50 if (it == removable_storage_map_.end()) |
| 51 return; |
| 52 removable_storage_map_.erase(it); |
| 53 } |
| 54 NotifyRemovableStorageDetached(id); |
| 55 } |
| 56 |
| 57 std::vector<RemovableStorageNotifications::RemovableStorageInfo> |
| 58 RemovableStorageNotifications::GetAttachedRemovableStorage() const { |
| 59 std::vector<RemovableStorageInfo> results; |
| 60 |
| 61 base::AutoLock lock(removable_storage_lock_); |
| 62 for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin(); |
| 63 it != removable_storage_map_.end(); |
| 64 ++it) { |
| 65 results.push_back(it->second); |
| 66 } |
| 67 return results; |
| 68 } |
| 69 |
| 70 void RemovableStorageNotifications::AddRemovableStorageObserver( |
| 71 RemovableStorageObserver* obs) { |
| 72 observer_list_->AddObserver(obs); |
| 73 } |
| 74 |
| 75 void RemovableStorageNotifications::RemoveRemovableStorageObserver( |
| 76 RemovableStorageObserver* obs) { |
| 77 observer_list_->RemoveObserver(obs); |
| 78 } |
| 79 |
| 80 void RemovableStorageNotifications::NotifyRemovableStorageAttached( |
| 81 const std::string& id, |
| 82 const string16& name, |
| 83 const FilePath::StringType& location) { |
| 84 DVLOG(1) << "RemovableStorageAttached with name " << UTF16ToUTF8(name) |
| 85 << " and id " << id; |
| 86 observer_list_->Notify( |
| 87 &RemovableStorageObserver::OnRemovableStorageAttached, id, name, location); |
| 88 } |
| 89 |
| 90 void RemovableStorageNotifications::NotifyRemovableStorageDetached( |
| 91 const std::string& id) { |
| 92 DVLOG(1) << "RemovableStorageDetached for id " << id; |
| 93 observer_list_->Notify( |
| 94 &RemovableStorageObserver::OnRemovableStorageDetached, id); |
| 95 } |
| 96 |
| 97 |
| 98 |
| 99 |
| 100 } // namespace chrome |
| OLD | NEW |