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 #import "chrome/browser/system_monitor/image_capture_camera.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/system_monitor/system_monitor.h" |
| 9 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // This continuation runs on the file thread to rename a saved file if needed. |
| 15 void RenameFileAndReturn(const std::string& name, |
| 16 const FilePath& downloaded_filename, |
| 17 const FilePath& desired_filename, |
| 18 base::WeakPtr<ImageCaptureDeviceListener> listener) { |
| 19 int64 edsize = 0; |
| 20 file_util::GetFileSize(downloaded_filename, &edsize); |
| 21 bool error = file_util::ReplaceFile(downloaded_filename, desired_filename); |
| 22 |
| 23 if (listener) { |
| 24 listener->DownloadedFile(name, |
| 25 error ? base::PLATFORM_FILE_OK : base::PLATFORM_FILE_ERROR_FAILED); |
| 26 } |
| 27 } |
| 28 |
| 29 base::Time NSDateToBaseTime(NSDate* date) { |
| 30 return base::Time::FromDoubleT([date timeIntervalSince1970]); |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 @implementation ImageCaptureCameraInterface |
| 36 |
| 37 - (id)initWithCameraDevice:(ICCameraDevice*)cameraDevice { |
| 38 if ((self = [super init])) { |
| 39 camera_.reset([cameraDevice retain]); |
| 40 [camera_ setDelegate:self]; |
| 41 } |
| 42 return self; |
| 43 } |
| 44 |
| 45 - (void)open { |
| 46 [camera_ requestOpenSession]; |
| 47 } |
| 48 |
| 49 - (void)dealloc { |
| 50 // Make sure the session was closed before destruction. |
| 51 DCHECK(![camera_ delegate]); |
| 52 [super dealloc]; |
| 53 } |
| 54 |
| 55 - (void)close { |
| 56 [camera_ requestCloseSession]; |
| 57 [camera_ setDelegate:nil]; |
| 58 } |
| 59 |
| 60 - (void)setListener:(base::WeakPtr<ImageCaptureDeviceListener>)listener { |
| 61 listener_ = listener; |
| 62 } |
| 63 |
| 64 - (void)downloadFile:(const std::string&)name |
| 65 localPath:(const FilePath&)localPath { |
| 66 // Find the file with that name and start download. |
| 67 for (ICCameraItem* item in [camera_ mediaFiles]) { |
| 68 std::string itemName = base::SysNSStringToUTF8([item name]); |
| 69 if (itemName == name) { |
| 70 // To create save options for ImageCapture, we need to |
| 71 // split the target filename into directory/name |
| 72 // and encode the directory as a URL. |
| 73 NSString* saveDirectory = |
| 74 base::mac::FilePathToNSString(localPath.DirName()); |
| 75 NSString* saveFilename = |
| 76 base::mac::FilePathToNSString(localPath.BaseName()); |
| 77 |
| 78 NSMutableDictionary* options = |
| 79 [NSMutableDictionary dictionaryWithCapacity:3]; |
| 80 [options setObject:[NSURL fileURLWithPath:saveDirectory isDirectory:YES] |
| 81 forKey:ICDownloadsDirectoryURL]; |
| 82 [options setObject:saveFilename forKey:ICSaveAsFilename]; |
| 83 [options setObject:[NSNumber numberWithBool:YES] forKey:ICOverwrite]; |
| 84 |
| 85 [camera_ requestDownloadFile:base::mac::ObjCCastStrict<ICCameraFile>(item) |
| 86 options:options |
| 87 downloadDelegate:self |
| 88 didDownloadSelector: |
| 89 @selector(didDownloadFile:error:options:contextInfo:) |
| 90 contextInfo:NULL]; |
| 91 return; |
| 92 } |
| 93 } |
| 94 |
| 95 if (listener_) |
| 96 listener_->DownloadedFile(name, base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 97 } |
| 98 |
| 99 - (void)cameraDevice:(ICCameraDevice*)camera didAddItem:(ICCameraItem*)item { |
| 100 std::string name = base::SysNSStringToUTF8([item name]); |
| 101 base::PlatformFileInfo info; |
| 102 if ([[item UTI] isEqualToString:base::mac::CFToNSCast(kUTTypeFolder)]) |
| 103 info.is_directory = true; |
| 104 else |
| 105 info.size = [base::mac::ObjCCastStrict<ICCameraFile>(item) fileSize]; |
| 106 info.last_modified = NSDateToBaseTime([item modificationDate]); |
| 107 info.creation_time = NSDateToBaseTime([item creationDate]); |
| 108 info.last_accessed = info.last_modified; |
| 109 |
| 110 if (listener_) |
| 111 listener_->ItemAdded(name, info); |
| 112 } |
| 113 |
| 114 - (void)cameraDevice:(ICCameraDevice*)camera didAddItems:(NSArray*)items { |
| 115 for (ICCameraItem* item in items) |
| 116 [self cameraDevice:camera didAddItem:item]; |
| 117 } |
| 118 |
| 119 - (void)didRemoveDevice:(ICDevice*)device { |
| 120 device.delegate = NULL; |
| 121 if (listener_) { |
| 122 listener_->DeviceRemoved(); |
| 123 listener_.reset(); |
| 124 } |
| 125 } |
| 126 |
| 127 // Notifies that a session was opened with the given device; potentially |
| 128 // with an error. |
| 129 - (void)device:(ICDevice*)device didOpenSessionWithError:(NSError*)error { |
| 130 if (error) { |
| 131 [self didRemoveDevice:camera_]; |
| 132 if (listener_) { |
| 133 listener_->DeviceRemoved(); |
| 134 listener_.reset(); |
| 135 } |
| 136 } |
| 137 } |
| 138 |
| 139 - (void)device:(ICDevice*)device didEncounterError:(NSError*)error { |
| 140 if (error) { |
| 141 if (listener_) { |
| 142 listener_->DeviceRemoved(); |
| 143 listener_.reset(); |
| 144 } |
| 145 } |
| 146 } |
| 147 |
| 148 // When this message is received, all media metadata is now loaded. |
| 149 - (void)deviceDidBecomeReadyWithCompleteContentCatalog:(ICDevice*)device { |
| 150 if (listener_) |
| 151 listener_->NoMoreItems(); |
| 152 } |
| 153 |
| 154 - (void)didDownloadFile:(ICCameraFile*)file |
| 155 error:(NSError*)error |
| 156 options:(NSDictionary*)options |
| 157 contextInfo:(void*)contextInfo { |
| 158 std::string name = base::SysNSStringToUTF8([file name]); |
| 159 if (error) { |
| 160 if (listener_) |
| 161 listener_->DownloadedFile(name, base::PLATFORM_FILE_ERROR_FAILED); |
| 162 return; |
| 163 } |
| 164 |
| 165 std::string savedFilename = |
| 166 base::SysNSStringToUTF8([options objectForKey:ICSavedFilename]); |
| 167 std::string saveAsFilename = |
| 168 base::SysNSStringToUTF8([options objectForKey:ICSaveAsFilename]); |
| 169 if (savedFilename == saveAsFilename) { |
| 170 if (listener_) |
| 171 listener_->DownloadedFile(name, base::PLATFORM_FILE_OK); |
| 172 return; |
| 173 } |
| 174 |
| 175 // ImageCapture did not save the file into the name we give it in the |
| 176 // options. It picks a new name according to its best lights, so we need |
| 177 // to rename the file. |
| 178 FilePath save_dir(base::SysNSStringToUTF8( |
| 179 [[options objectForKey:ICDownloadsDirectoryURL] path])); |
| 180 FilePath saveAsPath = save_dir.Append(saveAsFilename); |
| 181 FilePath savedPath = save_dir.Append(savedFilename); |
| 182 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, |
| 183 base::Bind(&RenameFileAndReturn, |
| 184 name, savedPath, saveAsPath, listener_)); |
| 185 } |
| 186 |
| 187 @end // ImageCaptureCameraInterface |
OLD | NEW |