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 "chrome/browser/system_monitor/image_capture_device_manager.h" |
| 6 |
| 7 #import <ImageCaptureCore/ImageCaptureCore.h> |
| 8 |
| 9 #import "chrome/browser/system_monitor/image_capture_device.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/system_monitor/system_monitor.h" |
| 12 #include "chrome/browser/system_monitor/disk_info_mac.h" |
| 13 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 chrome::ImageCaptureDeviceManager* g_image_capture_device_manager = NULL; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 // This class is the surface for the Mac ICDeviceBrowser ImageCaptureCore API. |
| 23 // Owned by the ChromeBrowserParts and has browser process lifetime. Upon |
| 24 // creation, it gets a list of attached media volumes (asynchronously) which |
| 25 // it will eventually forward to the SystemMonitor as removable storage |
| 26 // notifications. It will also set up an ImageCaptureCore listener to be |
| 27 // told when new devices/volumes are discovered and existing ones are removed. |
| 28 @interface ImageCaptureDeviceManagerImpl |
| 29 : NSObject<ICDeviceBrowserDelegate> { |
| 30 @private |
| 31 scoped_nsobject<ICDeviceBrowser> deviceBrowser_; |
| 32 scoped_nsobject<NSMutableArray> cameras_; |
| 33 } |
| 34 |
| 35 - (void)close; |
| 36 |
| 37 // The UUIDs passed here are available in the device attach notifications |
| 38 // given through SystemMonitor. They're gotten by cracking the device ID |
| 39 // and taking the unique ID output. |
| 40 - (ImageCaptureDevice*)deviceForUUID:(const std::string&)uuid; |
| 41 |
| 42 @end |
| 43 |
| 44 @implementation ImageCaptureDeviceManagerImpl |
| 45 |
| 46 - (id)init { |
| 47 if ((self = [super init])) { |
| 48 cameras_.reset([[NSMutableArray alloc] init]); |
| 49 |
| 50 deviceBrowser_.reset([[ICDeviceBrowser alloc] init]); |
| 51 [deviceBrowser_ setDelegate:self]; |
| 52 deviceBrowser_.get().browsedDeviceTypeMask = |
| 53 [deviceBrowser_ browsedDeviceTypeMask] | |
| 54 ICDeviceTypeMaskCamera | ICDeviceLocationTypeMaskLocal; |
| 55 [deviceBrowser_ start]; |
| 56 } |
| 57 return self; |
| 58 } |
| 59 |
| 60 - (void)close { |
| 61 [deviceBrowser_ setDelegate:nil]; |
| 62 [deviceBrowser_ stop]; |
| 63 deviceBrowser_.reset(); |
| 64 cameras_.reset(); |
| 65 } |
| 66 |
| 67 - (ImageCaptureDevice*) deviceForUUID:(const std::string&)uuid { |
| 68 for (ICCameraDevice* camera in cameras_.get()) { |
| 69 NSString* camera_id = [camera UUIDString]; |
| 70 if (base::SysNSStringToUTF8(camera_id) == uuid) { |
| 71 return [[[ImageCaptureDevice alloc] |
| 72 initWithCameraDevice:camera] autorelease]; |
| 73 } |
| 74 } |
| 75 return nil; |
| 76 } |
| 77 |
| 78 - (void)deviceBrowser:(ICDeviceBrowser*)browser |
| 79 didAddDevice:(ICDevice*)addedDevice |
| 80 moreComing:(BOOL)moreComing { |
| 81 if (!(addedDevice.type & ICDeviceTypeCamera)) |
| 82 return; |
| 83 |
| 84 ICCameraDevice* cameraDevice = |
| 85 base::mac::ObjCCastStrict<ICCameraDevice>(addedDevice); |
| 86 |
| 87 [cameras_ addObject:addedDevice]; |
| 88 |
| 89 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( |
| 90 chrome::MediaStorageUtil::MakeDeviceId( |
| 91 chrome::MediaStorageUtil::MAC_IMAGE_CAPTURE, |
| 92 base::SysNSStringToUTF8([cameraDevice UUIDString])), |
| 93 base::SysNSStringToUTF16([cameraDevice name]), |
| 94 base::SysNSStringToUTF8([cameraDevice mountPoint])); |
| 95 } |
| 96 |
| 97 - (void)deviceBrowser:(ICDeviceBrowser*)browser |
| 98 didRemoveDevice:(ICDevice*)device |
| 99 moreGoing:(BOOL)moreGoing { |
| 100 if (!(device.type & ICDeviceTypeCamera)) |
| 101 return; |
| 102 |
| 103 [cameras_ removeObject:device]; |
| 104 |
| 105 base::SystemMonitor::Get()->ProcessRemovableStorageDetached( |
| 106 chrome::MediaStorageUtil::MakeDeviceId( |
| 107 chrome::MediaStorageUtil::MAC_IMAGE_CAPTURE, |
| 108 base::SysNSStringToUTF8([device UUIDString]))); |
| 109 } |
| 110 |
| 111 @end // ImageCaptureDeviceManagerImpl |
| 112 |
| 113 namespace chrome { |
| 114 |
| 115 ImageCaptureDeviceManager::ImageCaptureDeviceManager() { |
| 116 device_browser_.reset([[ImageCaptureDeviceManagerImpl alloc] init]); |
| 117 g_image_capture_device_manager = this; |
| 118 } |
| 119 |
| 120 ImageCaptureDeviceManager::~ImageCaptureDeviceManager() { |
| 121 g_image_capture_device_manager = NULL; |
| 122 [device_browser_ close]; |
| 123 } |
| 124 |
| 125 // static |
| 126 ImageCaptureDevice* ImageCaptureDeviceManager::deviceForUUID( |
| 127 const std::string& uuid) { |
| 128 ImageCaptureDeviceManagerImpl* manager = |
| 129 base::mac::ObjCCastStrict<ImageCaptureDeviceManagerImpl>( |
| 130 g_image_capture_device_manager->device_browser_); |
| 131 return [manager deviceForUUID:uuid]; |
| 132 } |
| 133 |
| 134 id<ICDeviceBrowserDelegate> ImageCaptureDeviceManager::device_browser() { |
| 135 return device_browser_.get(); |
| 136 } |
| 137 |
| 138 } // namespace chrome |
OLD | NEW |