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