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

Side by Side Diff: chrome/browser/system_monitor/removable_device_notifications_mac.mm

Issue 10919185: Implement RemovableDeviceNotifications for Mac (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix threading 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 | Annotate | Revision Log
OLDNEW
(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 "chrome/browser/system_monitor/removable_device_notifications_mac.h"
6
7 #include "base/system_monitor/system_monitor.h"
8 #include "content/public/browser/browser_thread.h"
9
10 namespace chrome {
11
12 namespace {
13
14 void GetDiskInfoAndUpdateOnFileThead(
vandebo (ex-Chrome) 2012/09/10 18:45:03 nit: Thead -> Thread
sail 2012/09/10 21:58:43 Done.
15 const base::WeakPtr<RemovableDeviceNotificationsMac>& notifications,
16 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict,
17 bool should_remove) {
18 DiskInfoMac info = DiskInfoMac::BuildDiskInfoOnFileThread(dict);
19 if (info.device_id().empty())
20 return;
21
22 content::BrowserThread::PostTask(
23 content::BrowserThread::UI,
24 FROM_HERE,
25 base::Bind(&RemovableDeviceNotificationsMac::UpdateDisk,
26 notifications,
27 info,
28 should_remove));
29 }
30
31 void GetDiskInfoAndUpdate(
32 const base::WeakPtr<RemovableDeviceNotificationsMac>& notifications,
33 DADiskRef disk,
34 bool should_remove) {
35 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict(DADiskCopyDescription(disk));
36 content::BrowserThread::PostTask(
37 content::BrowserThread::FILE,
38 FROM_HERE,
39 base::Bind(GetDiskInfoAndUpdateOnFileThead,
40 notifications,
41 dict,
42 should_remove));
43 }
44
45 } // namespace
46
47 RemovableDeviceNotificationsMac::RemovableDeviceNotificationsMac() {
48 session_.reset(DASessionCreate(NULL));
49 DASessionScheduleWithRunLoop(
vandebo (ex-Chrome) 2012/09/10 18:45:03 I assume this means you have to register and recei
sail 2012/09/10 21:58:43 I think we can register on any thread that has a C
50 session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
51
52 DARegisterDiskAppearedCallback(
vandebo (ex-Chrome) 2012/09/10 18:45:03 Does this send notifications for already attached
sail 2012/09/10 21:58:43 Yep! Added comment too.
53 session_,
54 kDADiskDescriptionMatchVolumeMountable,
55 DiskAppearedCallback,
56 this);
57 DARegisterDiskDisappearedCallback(
58 session_,
59 kDADiskDescriptionMatchVolumeMountable,
60 DiskDisappearedCallback,
61 this);
62 DARegisterDiskDescriptionChangedCallback(
63 session_,
64 kDADiskDescriptionMatchVolumeMountable,
65 kDADiskDescriptionWatchVolumePath,
66 DiskDescriptionChangedCallback,
67 this);
68 }
69
70 RemovableDeviceNotificationsMac::~RemovableDeviceNotificationsMac() {
71 DASessionUnscheduleFromRunLoop(
72 session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
73 }
74
75 void RemovableDeviceNotificationsMac::UpdateDisk(
76 const DiskInfoMac& info,
77 bool should_remove) {
78 std::map<std::string, DiskInfoMac>::iterator it =
79 disk_info_map_.find(info.device_id());
80 if (it != disk_info_map_.end()) {
81 if (ShouldPostNotificationForDisk(it->second)) {
vandebo (ex-Chrome) 2012/09/10 18:45:03 DCHECK(should_remove) ?
sail 2012/09/10 21:58:43 I'm trying to handle the update case here. For ex
82 base::SystemMonitor::Get()->ProcessRemovableStorageDetached(
83 it->second.device_id());
84 }
85 }
86
87 if (should_remove) {
vandebo (ex-Chrome) 2012/09/10 18:45:03 looks like line 80 and 87 could be merged?
sail 2012/09/10 21:58:43 see above.
88 if (it != disk_info_map_.end())
89 disk_info_map_.erase(it);
90 } else {
91 disk_info_map_[info.device_id()] = info;
92 if (ShouldPostNotificationForDisk(info)) {
93 base::SystemMonitor::Get()->ProcessRemovableStorageAttached(
94 info.device_id(), info.display_name(), info.mount_point().value());
95 }
96 }
97 }
98
99 // static
100 void RemovableDeviceNotificationsMac::DiskAppearedCallback(
101 DADiskRef disk,
102 void* context) {
103 RemovableDeviceNotificationsMac* notifications =
104 static_cast<RemovableDeviceNotificationsMac*>(context);
105 GetDiskInfoAndUpdate(notifications->AsWeakPtr(), disk, false);
106 }
107
108 // static
109 void RemovableDeviceNotificationsMac::DiskDisappearedCallback(
110 DADiskRef disk,
111 void* context) {
112 RemovableDeviceNotificationsMac* notifications =
113 static_cast<RemovableDeviceNotificationsMac*>(context);
114 GetDiskInfoAndUpdate(notifications->AsWeakPtr(), disk, true);
115 }
116
117 // static
118 void RemovableDeviceNotificationsMac::DiskDescriptionChangedCallback(
119 DADiskRef disk,
120 CFArrayRef keys,
121 void *context) {
122 RemovableDeviceNotificationsMac* notifications =
123 static_cast<RemovableDeviceNotificationsMac*>(context);
124 GetDiskInfoAndUpdate(notifications->AsWeakPtr(), disk, false);
125 }
126
127 bool RemovableDeviceNotificationsMac::ShouldPostNotificationForDisk(
128 const DiskInfoMac& info) const {
129 return !info.bsd_name().empty() &&
130 !info.device_id().empty() &&
131 !info.display_name().empty() &&
132 !info.mount_point().empty() &&
133 (info.type() == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM ||
134 info.type() == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM);
Nico 2012/09/10 07:02:34 the other file mentioned FIXED_MASS_FOO. that's no
vandebo (ex-Chrome) 2012/09/10 18:45:03 SystemMonitor only notifies about removable storag
sail 2012/09/10 21:58:43 Yea, we only post notifications about removable st
sail 2012/09/10 21:58:43 I didn't understand this comment. Did you mean th
vandebo (ex-Chrome) 2012/09/10 22:53:05 No, I was replying to Nico's comment.
135 }
136
137 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698