Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
Avi (use Gerrit)
2012/02/25 22:13:00
File-wide comments:
- I do not want to see CFRele
vandebo (ex-Chrome)
2012/02/28 00:51:33
ScopedCFTypeRef? Done.
Avi (use Gerrit)
2012/02/28 01:26:46
No problem. If all you're trying to do is do equal
| |
| 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 "content/browser/mac/media_device_notifications.h" | |
| 6 | |
| 7 #include <Carbon/Carbon.h> | |
| 8 #include <CoreFoundation/CoreFoundation.h> | |
| 9 | |
| 10 #include "base/file_path.h" | |
| 11 #include "base/mac/foundation_util.h" | |
| 12 #include "base/sys_string_conversions.h" | |
| 13 #include "base/system_monitor/system_monitor.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 bool GetDeviceInfo(unsigned long device_number, std::string* name, | |
| 20 FilePath* location) { | |
| 21 ICACopyObjectPropertyDictionaryPB properties_request; | |
| 22 properties_request.object = device_number; | |
| 23 CFDictionaryRef device_properties; | |
| 24 properties_request.theDict = &device_properties; | |
| 25 OSErr ret = ICACopyObjectPropertyDictionary(&properties_request, NULL); | |
| 26 CHECK_EQ(ret, noErr); | |
| 27 | |
| 28 // For now, we only support mass storage media devices. | |
|
Avi (use Gerrit)
2012/02/25 22:13:00
That's a huge limitation; e.g. iPhones are an ICA
vandebo (ex-Chrome)
2012/02/28 00:51:33
Changed to a todo. Supporting all of OSX's media
| |
| 29 CFStringRef volume = base::mac::GetValueFromDictionary<CFStringRef>( | |
| 30 device_properties, CFSTR("volume")); | |
|
Avi (use Gerrit)
2012/02/25 22:13:00
Is there a constant you can use?
vandebo (ex-Chrome)
2012/02/28 00:51:33
Not that I'm aware of. "volume" does not appear i
| |
| 31 if (volume == NULL) { | |
| 32 CFRelease(device_properties); | |
| 33 return false; | |
| 34 } | |
| 35 *location = FilePath("/Volumes/").Append(base::SysCFStringRefToUTF8(volume)); | |
|
Avi (use Gerrit)
2012/02/25 22:13:00
This scares me because this is almost correct and
vandebo (ex-Chrome)
2012/02/28 00:51:33
Done.
| |
| 36 | |
| 37 CFStringRef device_name = base::mac::GetValueFromDictionary<CFStringRef>( | |
| 38 device_properties, CFSTR("ICAUserAssignedDeviceNameKey")); | |
|
Avi (use Gerrit)
2012/02/25 22:13:00
kICAUserAssignedDeviceNameKey. Constants, please!
vandebo (ex-Chrome)
2012/02/28 00:51:33
Done. (Added my own, there doesn't seem to be one
| |
| 39 if (device_name == NULL) { | |
| 40 device_name = base::mac::GetValueFromDictionary<CFStringRef>( | |
| 41 device_properties, CFSTR("ifil")); | |
|
Avi (use Gerrit)
2012/02/25 22:13:00
kICAPropertyImageFilename?
vandebo (ex-Chrome)
2012/02/28 00:51:33
It's the wrong type. Changed to a constant though
| |
| 42 } | |
| 43 if (device_name == NULL) { | |
| 44 CFRelease(device_properties); | |
| 45 return false; | |
| 46 } | |
| 47 *name = base::SysCFStringRefToUTF8(device_name); | |
| 48 | |
| 49 CFRelease(device_properties); | |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 void MediaDeviceNotificationCallback(CFStringRef notification_type, | |
| 54 CFDictionaryRef notification_dictionary) { | |
| 55 bool attach = false; | |
| 56 if (CFStringCompare(notification_type, kICANotificationTypeDeviceAdded, 0) == | |
| 57 kCFCompareEqualTo) { | |
| 58 attach = true; | |
| 59 } else if (CFStringCompare(notification_type, | |
| 60 kICANotificationTypeDeviceRemoved, 0) != | |
| 61 kCFCompareEqualTo) { | |
| 62 return; | |
| 63 } | |
| 64 | |
| 65 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | |
| 66 | |
| 67 CFNumberRef device_number_object = | |
| 68 base::mac::GetValueFromDictionary<CFNumberRef>( | |
| 69 notification_dictionary, kICANotificationDeviceICAObjectKey); | |
| 70 unsigned long device_number; | |
| 71 CFNumberGetValue(device_number_object, kCFNumberIntType, &device_number); | |
|
Avi (use Gerrit)
2012/02/25 22:13:00
Objective C will likely be nice here.
vandebo (ex-Chrome)
2012/02/28 00:51:33
Done.
| |
| 72 if (attach) { | |
| 73 std::string device_name; | |
| 74 FilePath location; | |
| 75 if (GetDeviceInfo(device_number, &device_name, &location)) { | |
| 76 system_monitor->ProcessMediaDeviceAttached(device_number, device_name, | |
| 77 location); | |
| 78 } | |
| 79 } else { | |
| 80 system_monitor->ProcessMediaDeviceDetached(device_number); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | |
| 86 void StartMediaDeviceNotifications() { | |
| 87 CFStringRef events_of_interest_array[] = {kICANotificationTypeDeviceAdded, | |
| 88 kICANotificationTypeDeviceRemoved}; | |
| 89 CFArrayRef events_of_interest = | |
|
Avi (use Gerrit)
2012/02/25 22:13:00
Does ICARegisterForEventNotificationPB own this? D
vandebo (ex-Chrome)
2012/02/28 00:51:33
This sample app implies that ICARegisterForEventNo
Avi (use Gerrit)
2012/02/28 01:26:46
You got ICA sample code to compile?! I tried two d
vandebo (ex-Chrome)
2012/02/28 18:35:05
I didn't try compiling the sample code, just used
| |
| 90 CFArrayCreate(NULL, (const void**)&events_of_interest_array, 2, | |
| 91 &kCFTypeArrayCallBacks); | |
| 92 | |
| 93 ICARegisterForEventNotificationPB notification_request; | |
| 94 notification_request.objectOfInterest = 0; // Zero means all objects | |
| 95 notification_request.eventsOfInterest = events_of_interest; | |
| 96 notification_request.notificationProc = &MediaDeviceNotificationCallback; | |
| 97 notification_request.options = NULL; | |
| 98 OSErr err = ICARegisterForEventNotification(¬ification_request, NULL); | |
| 99 CHECK_EQ(err, noErr); | |
| 100 } | |
| 101 | |
| 102 } // namespace content | |
| OLD | NEW |