| OLD | NEW |
| 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 "chrome/browser/system_monitor/removable_storage_notifications.h" | 5 #include "chrome/browser/system_monitor/removable_storage_notifications.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/system_monitor/removable_storage_observer.h" | 9 #include "chrome/browser/system_monitor/removable_storage_observer.h" |
| 10 | 10 |
| 11 namespace chrome { | 11 namespace chrome { |
| 12 | 12 |
| 13 static RemovableStorageNotifications* | 13 static RemovableStorageNotifications* |
| 14 g_removable_storage_notifications = NULL; | 14 g_removable_storage_notifications = NULL; |
| 15 | 15 |
| 16 RemovableStorageNotifications::StorageInfo::StorageInfo() { | 16 RemovableStorageNotifications::StorageInfo::StorageInfo() { |
| 17 } | 17 } |
| 18 | 18 |
| 19 RemovableStorageNotifications::StorageInfo::StorageInfo( | 19 RemovableStorageNotifications::StorageInfo::StorageInfo( |
| 20 const std::string& id, | 20 const std::string& id, |
| 21 const string16& device_name, | 21 const string16& device_name, |
| 22 const base::FilePath::StringType& device_location) | 22 const base::FilePath::StringType& device_location) |
| 23 : device_id(id), | 23 : device_id(id), |
| 24 name(device_name), | 24 name(device_name), |
| 25 location(device_location) { | 25 location(device_location) { |
| 26 } | 26 } |
| 27 | 27 |
| 28 RemovableStorageNotifications::Receiver::~Receiver() { |
| 29 } |
| 30 |
| 31 class RemovableStorageNotifications::ReceiverImpl |
| 32 : public RemovableStorageNotifications::Receiver { |
| 33 public: |
| 34 explicit ReceiverImpl(RemovableStorageNotifications* notifications) |
| 35 : notifications_(notifications) {} |
| 36 |
| 37 virtual ~ReceiverImpl() {} |
| 38 |
| 39 void ProcessAttach(const std::string& id, |
| 40 const string16& name, |
| 41 const base::FilePath::StringType& location) OVERRIDE; |
| 42 |
| 43 void ProcessDetach(const std::string& id) OVERRIDE; |
| 44 |
| 45 private: |
| 46 RemovableStorageNotifications* notifications_; |
| 47 }; |
| 48 |
| 49 void RemovableStorageNotifications::ReceiverImpl::ProcessAttach( |
| 50 const std::string& id, |
| 51 const string16& name, |
| 52 const base::FilePath::StringType& location) { |
| 53 notifications_->ProcessAttach( |
| 54 RemovableStorageNotifications::StorageInfo(id, name, location)); |
| 55 } |
| 56 |
| 57 void RemovableStorageNotifications::ReceiverImpl::ProcessDetach( |
| 58 const std::string& id) { |
| 59 notifications_->ProcessDetach(id); |
| 60 } |
| 61 |
| 28 RemovableStorageNotifications::RemovableStorageNotifications() | 62 RemovableStorageNotifications::RemovableStorageNotifications() |
| 29 : observer_list_( | 63 : observer_list_( |
| 30 new ObserverListThreadSafe<RemovableStorageObserver>()) { | 64 new ObserverListThreadSafe<RemovableStorageObserver>()) { |
| 65 receiver_.reset(new ReceiverImpl(this)); |
| 66 |
| 31 DCHECK(!g_removable_storage_notifications); | 67 DCHECK(!g_removable_storage_notifications); |
| 32 g_removable_storage_notifications = this; | 68 g_removable_storage_notifications = this; |
| 33 } | 69 } |
| 34 | 70 |
| 35 RemovableStorageNotifications::~RemovableStorageNotifications() { | 71 RemovableStorageNotifications::~RemovableStorageNotifications() { |
| 36 DCHECK_EQ(this, g_removable_storage_notifications); | 72 DCHECK_EQ(this, g_removable_storage_notifications); |
| 37 g_removable_storage_notifications = NULL; | 73 g_removable_storage_notifications = NULL; |
| 38 } | 74 } |
| 39 | 75 |
| 76 RemovableStorageNotifications::Receiver* |
| 77 RemovableStorageNotifications::receiver() const { |
| 78 return receiver_.get(); |
| 79 } |
| 80 |
| 40 void RemovableStorageNotifications::ProcessAttach( | 81 void RemovableStorageNotifications::ProcessAttach( |
| 41 const std::string& id, | 82 const StorageInfo& info) { |
| 42 const string16& name, | |
| 43 const base::FilePath::StringType& location) { | |
| 44 StorageInfo info(id, name, location); | |
| 45 { | 83 { |
| 46 base::AutoLock lock(storage_lock_); | 84 base::AutoLock lock(storage_lock_); |
| 47 if (ContainsKey(storage_map_, id)) { | 85 if (ContainsKey(storage_map_, info.device_id)) { |
| 48 // This can happen if our unique id scheme fails. Ignore the incoming | 86 // This can happen if our unique id scheme fails. Ignore the incoming |
| 49 // non-unique attachment. | 87 // non-unique attachment. |
| 50 return; | 88 return; |
| 51 } | 89 } |
| 52 storage_map_.insert(std::make_pair(id, info)); | 90 storage_map_.insert(std::make_pair(info.device_id, info)); |
| 53 } | 91 } |
| 54 | 92 |
| 55 DVLOG(1) << "RemovableStorageAttached with name " << UTF16ToUTF8(name) | 93 DVLOG(1) << "RemovableStorageAttached with name " << UTF16ToUTF8(info.name) |
| 56 << " and id " << id; | 94 << " and id " << info.device_id; |
| 57 observer_list_->Notify( | 95 observer_list_->Notify( |
| 58 &RemovableStorageObserver::OnRemovableStorageAttached, info); | 96 &RemovableStorageObserver::OnRemovableStorageAttached, info); |
| 59 } | 97 } |
| 60 | 98 |
| 61 void RemovableStorageNotifications::ProcessDetach( | 99 void RemovableStorageNotifications::ProcessDetach(const std::string& id) { |
| 62 const std::string& id) { | |
| 63 StorageInfo info; | 100 StorageInfo info; |
| 64 { | 101 { |
| 65 base::AutoLock lock(storage_lock_); | 102 base::AutoLock lock(storage_lock_); |
| 66 RemovableStorageMap::iterator it = storage_map_.find(id); | 103 RemovableStorageMap::iterator it = storage_map_.find(id); |
| 67 if (it == storage_map_.end()) | 104 if (it == storage_map_.end()) |
| 68 return; | 105 return; |
| 69 info = it->second; | 106 info = it->second; |
| 70 storage_map_.erase(it); | 107 storage_map_.erase(it); |
| 71 } | 108 } |
| 72 | 109 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 95 void RemovableStorageNotifications::RemoveObserver( | 132 void RemovableStorageNotifications::RemoveObserver( |
| 96 RemovableStorageObserver* obs) { | 133 RemovableStorageObserver* obs) { |
| 97 observer_list_->RemoveObserver(obs); | 134 observer_list_->RemoveObserver(obs); |
| 98 } | 135 } |
| 99 | 136 |
| 100 RemovableStorageNotifications* RemovableStorageNotifications::GetInstance() { | 137 RemovableStorageNotifications* RemovableStorageNotifications::GetInstance() { |
| 101 return g_removable_storage_notifications; | 138 return g_removable_storage_notifications; |
| 102 } | 139 } |
| 103 | 140 |
| 104 } // namespace chrome | 141 } // namespace chrome |
| OLD | NEW |