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

Side by Side Diff: chrome/browser/system_monitor/removable_storage_notifications.cc

Issue 12147002: Add a receiver interface to RemovableStorageNotifications. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 10 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 "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 FilePath::StringType& device_location) 22 const 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 class RemovableStorageNotifications::ReceiverImpl
29 : public RemovableStorageNotifications::Receiver {
30 public:
31 explicit ReceiverImpl(RemovableStorageNotifications* notifications)
32 : notifications_(notifications) {}
33
34 virtual ~ReceiverImpl() {}
35
36 void ProcessAttach(
37 const RemovableStorageNotifications::StorageInfo& info) OVERRIDE;
38
39 void ProcessDetach(const std::string& id) OVERRIDE;
40
41 private:
42 RemovableStorageNotifications* notifications_;
43 };
44
45 void RemovableStorageNotifications::ReceiverImpl::ProcessAttach(
46 const RemovableStorageNotifications::StorageInfo& info) {
47 notifications_->ProcessAttach(info);
48 }
49
50 void RemovableStorageNotifications::ReceiverImpl::ProcessDetach(
51 const std::string& id) {
52 notifications_->ProcessDetach(id);
53 }
54
28 RemovableStorageNotifications::RemovableStorageNotifications() 55 RemovableStorageNotifications::RemovableStorageNotifications()
29 : observer_list_( 56 : observer_list_(
30 new ObserverListThreadSafe<RemovableStorageObserver>()) { 57 new ObserverListThreadSafe<RemovableStorageObserver>()) {
58 receiver_.reset(new ReceiverImpl(this));
59
31 DCHECK(!g_removable_storage_notifications); 60 DCHECK(!g_removable_storage_notifications);
32 g_removable_storage_notifications = this; 61 g_removable_storage_notifications = this;
33 } 62 }
34 63
35 RemovableStorageNotifications::~RemovableStorageNotifications() { 64 RemovableStorageNotifications::~RemovableStorageNotifications() {
36 DCHECK_EQ(this, g_removable_storage_notifications); 65 DCHECK_EQ(this, g_removable_storage_notifications);
37 g_removable_storage_notifications = NULL; 66 g_removable_storage_notifications = NULL;
38 } 67 }
39 68
40 void RemovableStorageNotifications::ProcessAttach( 69 void RemovableStorageNotifications::ProcessAttach(
41 const std::string& id, 70 const StorageInfo& info) {
42 const string16& name,
43 const FilePath::StringType& location) {
44 StorageInfo info(id, name, location);
45 { 71 {
46 base::AutoLock lock(storage_lock_); 72 base::AutoLock lock(storage_lock_);
47 if (ContainsKey(storage_map_, id)) { 73 if (ContainsKey(storage_map_, info.device_id)) {
48 // This can happen if our unique id scheme fails. Ignore the incoming 74 // This can happen if our unique id scheme fails. Ignore the incoming
49 // non-unique attachment. 75 // non-unique attachment.
50 return; 76 return;
51 } 77 }
52 storage_map_.insert(std::make_pair(id, info)); 78 storage_map_.insert(std::make_pair(info.device_id, info));
53 } 79 }
54 80
55 DVLOG(1) << "RemovableStorageAttached with name " << UTF16ToUTF8(name) 81 DVLOG(1) << "RemovableStorageAttached with name " << UTF16ToUTF8(info.name)
56 << " and id " << id; 82 << " and id " << info.device_id;
57 observer_list_->Notify( 83 observer_list_->Notify(
58 &RemovableStorageObserver::OnRemovableStorageAttached, info); 84 &RemovableStorageObserver::OnRemovableStorageAttached, info);
59 } 85 }
60 86
61 void RemovableStorageNotifications::ProcessDetach( 87 void RemovableStorageNotifications::ProcessDetach(const std::string& id) {
62 const std::string& id) {
63 StorageInfo info; 88 StorageInfo info;
64 { 89 {
65 base::AutoLock lock(storage_lock_); 90 base::AutoLock lock(storage_lock_);
66 RemovableStorageMap::iterator it = storage_map_.find(id); 91 RemovableStorageMap::iterator it = storage_map_.find(id);
67 if (it == storage_map_.end()) 92 if (it == storage_map_.end())
68 return; 93 return;
69 info = it->second; 94 info = it->second;
70 storage_map_.erase(it); 95 storage_map_.erase(it);
71 } 96 }
72 97
(...skipping 22 matching lines...) Expand all
95 void RemovableStorageNotifications::RemoveObserver( 120 void RemovableStorageNotifications::RemoveObserver(
96 RemovableStorageObserver* obs) { 121 RemovableStorageObserver* obs) {
97 observer_list_->RemoveObserver(obs); 122 observer_list_->RemoveObserver(obs);
98 } 123 }
99 124
100 RemovableStorageNotifications* RemovableStorageNotifications::GetInstance() { 125 RemovableStorageNotifications* RemovableStorageNotifications::GetInstance() {
101 return g_removable_storage_notifications; 126 return g_removable_storage_notifications;
102 } 127 }
103 128
104 } // namespace chrome 129 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698