| 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 #include "base/file_util.h" | |
| 10 #include "base/system_monitor/system_monitor.h" | |
| 11 #include "chrome/browser/system_monitor/disk_info_mac.h" | |
| 12 #import "chrome/browser/system_monitor/image_capture_device.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_ setBrowsedDeviceTypeMask: | |
| 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 // TODO(gbillock): use [cameraDevice mountPoint] here when possible. | |
| 90 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( | |
| 91 chrome::MediaStorageUtil::MakeDeviceId( | |
| 92 chrome::MediaStorageUtil::MAC_IMAGE_CAPTURE, | |
| 93 base::SysNSStringToUTF8([cameraDevice UUIDString])), | |
| 94 base::SysNSStringToUTF16([cameraDevice name]), ""); | |
| 95 } | |
| 96 | |
| 97 - (void)deviceBrowser:(ICDeviceBrowser*)browser | |
| 98 didRemoveDevice:(ICDevice*)device | |
| 99 moreGoing:(BOOL)moreGoing { | |
| 100 if (!(device.type & ICDeviceTypeCamera)) | |
| 101 return; | |
| 102 | |
| 103 std::string uuid = base::SysNSStringToUTF8([device UUIDString]); | |
| 104 | |
| 105 // May delete |device|. | |
| 106 [cameras_ removeObject:device]; | |
| 107 | |
| 108 base::SystemMonitor::Get()->ProcessRemovableStorageDetached( | |
| 109 chrome::MediaStorageUtil::MakeDeviceId( | |
| 110 chrome::MediaStorageUtil::MAC_IMAGE_CAPTURE, uuid)); | |
| 111 } | |
| 112 | |
| 113 @end // ImageCaptureDeviceManagerImpl | |
| 114 | |
| 115 namespace chrome { | |
| 116 | |
| 117 ImageCaptureDeviceManager::ImageCaptureDeviceManager() { | |
| 118 device_browser_.reset([[ImageCaptureDeviceManagerImpl alloc] init]); | |
| 119 g_image_capture_device_manager = this; | |
| 120 } | |
| 121 | |
| 122 ImageCaptureDeviceManager::~ImageCaptureDeviceManager() { | |
| 123 g_image_capture_device_manager = NULL; | |
| 124 [device_browser_ close]; | |
| 125 } | |
| 126 | |
| 127 // static | |
| 128 ImageCaptureDevice* ImageCaptureDeviceManager::deviceForUUID( | |
| 129 const std::string& uuid) { | |
| 130 ImageCaptureDeviceManagerImpl* manager = | |
| 131 g_image_capture_device_manager->device_browser_; | |
| 132 return [manager deviceForUUID:uuid]; | |
| 133 } | |
| 134 | |
| 135 id<ICDeviceBrowserDelegate> ImageCaptureDeviceManager::device_browser() { | |
| 136 return device_browser_.get(); | |
| 137 } | |
| 138 | |
| 139 } // namespace chrome | |
| OLD | NEW |