Chromium Code Reviews| Index: chrome/browser/system_monitor/image_capture_device_browser_mac.mm |
| diff --git a/chrome/browser/system_monitor/image_capture_device_browser_mac.mm b/chrome/browser/system_monitor/image_capture_device_browser_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e41549a8dc01e9c3f3c149c3f005ab06e274e531 |
| --- /dev/null |
| +++ b/chrome/browser/system_monitor/image_capture_device_browser_mac.mm |
| @@ -0,0 +1,277 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "chrome/browser/system_monitor/image_capture_device_browser_mac.h" |
| + |
| +#include "base/file_util.h" |
| +#include "base/system_monitor/system_monitor.h" |
| +#include "chrome/browser/system_monitor/disk_info_mac.h" |
| +#include "chrome/browser/system_monitor/media_storage_util.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +ImageCaptureDeviceBrowserMac* g_image_capture_device_browser; |
|
sail
2012/12/12 20:53:20
It doesn't seem like you really need a singleton.
Greg Billock
2012/12/13 00:31:58
Yes. I can't see where that's exposed, though. Wha
sail
2012/12/13 02:14:00
Ahh, you're right, k maybe have the C++ class be a
|
| +const char kRootPath[] = "/"; |
| + |
| +@implementation ImageCaptureDeviceBrowserMac |
| + |
| +@synthesize cameras = cameras_; |
| + |
| +- (id)init { |
| + cameras_ = [[NSMutableArray alloc] initWithCapacity:0]; |
| + |
| + device_browser_ = [[ICDeviceBrowser alloc] init]; |
| + device_browser_.delegate = self; |
| + device_browser_.browsedDeviceTypeMask = |
| + device_browser_.browsedDeviceTypeMask | |
| + ICDeviceTypeMaskCamera | ICDeviceLocationTypeMaskLocal; |
| + [device_browser_ start]; |
| + g_image_capture_device_browser = self; |
| + return self; |
| +} |
| + |
| +- (void)close { |
| + device_browser_.delegate = NULL; |
| + [device_browser_ stop]; |
| + [device_browser_ release]; |
| + [cameras_ release]; |
| +} |
| + |
| +- (ImageCaptureCameraInterface*)openDeviceByUUID:(std::string&)uuid { |
| + // TODO: refcount here? |
| + for (ICCameraDevice* camera in cameras_) { |
| + NSString* camera_id = [camera UUIDString]; |
| + if (base::SysNSStringToUTF8(camera_id) == uuid) { |
| + return [[ImageCaptureCameraInterface alloc] init:camera]; |
|
sail
2012/12/12 20:53:20
In Objective-C, the naming scheme implies the owne
Greg Billock
2012/12/13 00:31:58
OK, I think I got this. Also changed the name to u
sail
2012/12/13 02:14:00
Actually, you should do one or the other.
If you n
Greg Billock
2012/12/14 00:39:59
Done.
|
| + } |
| + } |
| + return nil; |
| +} |
| + |
| ++ (ImageCaptureDeviceBrowserMac*)Get { |
| + return g_image_capture_device_browser; |
| +} |
| + |
| +// Method delegates for device added and removed |
|
sail
2012/12/12 20:53:20
don't need this
Greg Billock
2012/12/13 00:31:58
Done.
|
| +// |
| +// Device browser maintains list of cameras as key-value pairs, so delegate |
| +// must call willChangeValueForKey to modify list |
| +- (void)deviceBrowser:(ICDeviceBrowser*)browser |
| + didAddDevice:(ICDevice*)addedDevice moreComing:(BOOL)moreComing { |
| + if (addedDevice.type & ICDeviceTypeCamera) { |
|
sail
2012/12/12 20:53:20
should do early return instead
Greg Billock
2012/12/13 00:31:58
Done.
|
| + ICCameraDevice* camera_device = (ICCameraDevice*)addedDevice; |
|
sail
2012/12/12 20:53:20
local variable names in Objective-C classes should
Greg Billock
2012/12/13 00:31:58
Done.
|
| + |
| + NSString* name = [addedDevice name]; |
| + NSString* mount_point = [camera_device mountPoint]; |
| + NSString* uuid = [camera_device UUIDString]; |
| + |
| + // implement manual observer notification for the cameras property |
| + [self willChangeValueForKey:@"cameras"]; |
| + [cameras_ addObject:addedDevice]; |
| + [self didChangeValueForKey:@"cameras"]; |
| + |
| + chrome::DiskInfoMac info = chrome::DiskInfoMac::BuildDiskInfoFromICDevice( |
| + base::SysNSStringToUTF8(uuid), |
| + base::SysNSStringToUTF16(name), |
| + FilePath(base::SysNSStringToUTF8(mount_point))); |
| + base::SystemMonitor::Get()->ProcessRemovableStorageAttached( |
| + chrome::MediaStorageUtil::MakeDeviceId(info.type(), info.device_id()), |
| + info.device_name(), info.mount_point().value()); |
| + } |
| +} |
| + |
| +- (void)deviceBrowser:(ICDeviceBrowser*)browser |
| + didRemoveDevice:(ICDevice*)device moreGoing:(BOOL)moreGoing { |
| + if (device.type & ICDeviceTypeCamera) { |
| + NSString* name = [device name]; |
| + |
| + ICCameraDevice* camera_device = (ICCameraDevice*)device; |
| + NSString* mount_point = [camera_device mountPoint]; |
| + NSString* uuid = [camera_device UUIDString]; |
| + |
| + // implement manual observer notification for the cameras property |
| + [self willChangeValueForKey:@"cameras"]; |
|
sail
2012/12/12 20:53:20
Are you planning on using key value observers? I'd
Greg Billock
2012/12/13 00:31:58
No. This is boilerplate from the ImageCapture samp
sail
2012/12/13 02:14:00
Yea, it's safe to remove
Greg Billock
2012/12/14 00:39:59
Done.
|
| + [cameras_ removeObject:device]; |
| + [self didChangeValueForKey:@"cameras"]; |
| + |
| + chrome::DiskInfoMac info = chrome::DiskInfoMac::BuildDiskInfoFromICDevice( |
| + base::SysNSStringToUTF8(uuid), |
| + base::SysNSStringToUTF16(name), |
| + FilePath(base::SysNSStringToUTF8(mount_point))); |
| + base::SystemMonitor::Get()->ProcessRemovableStorageDetached( |
| + chrome::MediaStorageUtil::MakeDeviceId(info.type(), info.device_id())); |
| + } |
| +} |
| + |
| +@end // ImageCaptureDeviceBrowserMac |
| + |
| +@implementation ImageCaptureCameraInterface |
| + |
| +- (id)init:(ICCameraDevice*)camera_device { |
| + camera_ = camera_device; |
| + camera_.delegate = self; |
| + return self; |
| +} |
| + |
| +- (void)open { |
| + [camera_ requestOpenSession]; |
| +} |
| + |
| +- (void)close { |
| + [camera_ requestCloseSession]; |
| + camera_.delegate = NULL; |
| +} |
| + |
| +- (void)setListener:(ImageCaptureDeviceListener*)listener { |
| + listener_ = listener; |
| +} |
| + |
| +- (void)DownloadFile:(const std::string&)name |
| + localPath:(const FilePath&)local_path { |
| + // Find the file with that name and start download. |
| + for (ICCameraItem* item in [camera_ mediaFiles]) { |
| + std::string item_name = base::SysNSStringToUTF8([item name]); |
| + if (item_name == name) { |
| + NSMutableDictionary* options = |
| + [NSMutableDictionary dictionaryWithCapacity:3]; |
| + NSString* fileURLString = |
| + [NSString stringWithUTF8String:local_path.DirName().value().c_str()]; |
| + [options setObject:[NSURL fileURLWithPath:fileURLString isDirectory:YES] |
| + forKey:ICDownloadsDirectoryURL]; |
| + NSString* filename = |
| + [NSString stringWithUTF8String:local_path.BaseName().value().c_str()]; |
| + [options setObject:filename forKey:ICSaveAsFilename]; |
| + [options setObject:[NSNumber numberWithBool:YES] forKey:ICOverwrite]; |
| + |
| + [camera_ requestDownloadFile:(ICCameraFile*)item |
| + options:options |
| + downloadDelegate:self |
| + didDownloadSelector: |
| + @selector(didDownloadFile:error:options:contextInfo:) |
| + contextInfo:NULL]; |
| + return; |
| + } |
| + } |
| + |
| + if (listener_) |
| + listener_->DownloadedFile(name, base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| +} |
| + |
| +// Delegates for ICCameraDeviceDelegate |
| + |
| +- (void)cameraDevice:(ICCameraDevice*)camera didAddItem:(ICCameraItem*)item { |
| + std::string name = base::SysNSStringToUTF8([item name]); |
| + base::PlatformFileInfo info; |
| + info.size = 0; |
| + info.is_directory = false; |
| + if ([[item UTI] isEqualToString:(NSString*)kUTTypeFolder]) |
| + info.is_directory = true; |
| + else |
| + info.size = [(ICCameraFile*)item fileSize]; |
| + info.is_symbolic_link = false; |
| + info.last_modified = |
| + base::Time::FromDoubleT([[item modificationDate] timeIntervalSince1970]); |
| + info.creation_time = |
| + base::Time::FromDoubleT([[item creationDate] timeIntervalSince1970]); |
| + info.last_accessed = info.last_modified; |
| + |
| + if (listener_) |
| + listener_->ItemAdded(name, info); |
| +} |
| + |
| +- (void)cameraDevice:(ICCameraDevice*)camera didAddItems:(NSArray*)items { |
| + for (ICCameraItem* item in items) |
| + [self cameraDevice:camera didAddItem:item]; |
| +} |
| + |
| +- (void)didRemoveDevice:(ICDevice*)device { |
| + device.delegate = NULL; |
| + // Note: handled by ICDeviceBrowser::didRemoveDevice |
| + [device.userData setObject:[NSNumber numberWithBool:NO] forKey:@"ready"]; |
| + [device.userData setObject:[NSNumber numberWithBool:NO] forKey:@"open"]; |
| + if (listener_) { |
| + listener_->DeviceRemoved(); |
| + [self setListener:nil]; |
| + } |
| +} |
| + |
| +// Notifies that a session was opened with the given device; potentially |
| +// with an error. |
| +- (void)device:(ICDevice*)device didOpenSessionWithError:(NSError*)error { |
| + if (error == NULL) |
| + [device.userData setObject:[NSNumber numberWithBool:YES] forKey:@"open"]; |
| + else |
| + [device.userData setObject:error forKey:@"error"]; |
| +} |
| + |
| +// Notifies that the device is ready for commands (i.e. download images) |
| +- (void)deviceDidBecomeReady:(ICDevice*)device { |
| + [device.userData setObject:[NSNumber numberWithBool:YES] forKey:@"ready"]; |
| +} |
| + |
| +- (void)device:(ICDevice*)device didEncounterError:(NSError*)error { |
| + [device.userData setObject:error forKey:@"error"]; |
| + [device.userData setObject:[NSNumber numberWithBool:NO] forKey:@"open"]; |
| +} |
| + |
| +// All metadata is now loaded. |
| +- (void)deviceDidBecomeReadyWithCompleteContentCatalog:(ICDevice*)device { |
| + if (device.type & ICDeviceTypeCamera) { |
| + [device.userData setValue:(id)kCFBooleanTrue forKey:@"complete"]; |
| + } |
| + if (listener_) |
| + listener_->NoMoreItems(); |
| +} |
| + |
| +// Delegates for ICCameraDeviceDownloadDelegate |
| + |
| +void RenameFileAndReturn(const std::string& name, |
| + const FilePath& downloaded_filename, |
| + const FilePath& desired_filename, |
| + ImageCaptureCameraInterface* caller) { |
| + int64 edsize = 0; |
| + file_util::GetFileSize(downloaded_filename, &edsize); |
| + bool error = file_util::ReplaceFile(downloaded_filename, desired_filename); |
| + [caller DidRenameDownloadFile:name withError:error]; |
| +} |
| + |
| +- (void)didDownloadFile:(ICCameraFile*)file error:(NSError*)error |
| + options:(NSDictionary*)options contextInfo:(void*)contextInfo { |
| + std::string name = base::SysNSStringToUTF8([file name]); |
| + base::PlatformFileError err = base::PLATFORM_FILE_OK; |
| + |
| + // ImageCapture does not save the file into the name we give it in the |
| + // options. It picks a new name according to it's best lights, so we need |
| + // to rename the file. |
| + std::string saved_filename = |
| + base::SysNSStringToUTF8([options objectForKey:ICSavedFilename]); |
| + std::string save_as_filename = |
| + base::SysNSStringToUTF8([options objectForKey:ICSaveAsFilename]); |
| + if (!error && (saved_filename != save_as_filename)) { |
| + FilePath save_dir(base::SysNSStringToUTF8( |
| + [[options objectForKey:ICDownloadsDirectoryURL] path])); |
| + FilePath save_as_path = save_dir.Append(save_as_filename); |
| + FilePath saved_path = save_dir.Append(saved_filename); |
| + // !!! should really pass a weak pointer or a refptr or something. |
| + content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&RenameFileAndReturn, name, saved_path, save_as_path, self)); |
| + return; |
| + } |
| + |
| + if (error) |
| + err = base::PLATFORM_FILE_ERROR_FAILED; |
| + |
| + if (listener_) |
| + listener_->DownloadedFile(name, err); |
| +} |
| + |
| +- (void)DidRenameDownloadFile:(const std::string&)name |
| + withError:(bool)rename_error { |
| + if (listener_) { |
| + listener_->DownloadedFile(name, |
| + rename_error ? base::PLATFORM_FILE_OK |
| + : base::PLATFORM_FILE_ERROR_FAILED); |
| + } |
| +} |
| + |
| +@end // ImageCaptureCameraInterface |