Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(712)

Unified Diff: chrome/browser/system_monitor/image_capture_camera.mm

Issue 11442057: [Media Galleries] Add an ImageCaptureCore listener for Mac. (part 2) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Lots of fixes Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/system_monitor/image_capture_camera.mm
diff --git a/chrome/browser/system_monitor/image_capture_camera.mm b/chrome/browser/system_monitor/image_capture_camera.mm
new file mode 100644
index 0000000000000000000000000000000000000000..cc37c1e8d46f51999a62c31fa35ed446d08e823e
--- /dev/null
+++ b/chrome/browser/system_monitor/image_capture_camera.mm
@@ -0,0 +1,188 @@
+// 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_camera.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"
+
+namespace {
+
+// This continuation runs on the file thread to rename a saved file if needed.
+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];
+}
+
+base::Time NSDateToBaseTime(NSDate* date) {
+ return base::Time::FromDoubleT([date timeIntervalSince1970]);
+}
+
+} // namespace
+
+@implementation ImageCaptureCameraInterface
+
+- (id)initWithCameraDevice:(ICCameraDevice*)camera_device {
+ if ((self = [super init])) {
+ camera_.reset(camera_device);
sail 2012/12/14 01:26:32 camera_.reset([cameraDevice retain])
Greg Billock 2012/12/14 18:59:11 Done. Does that mean I need to release in the dest
sail 2012/12/14 19:27:32 scoped_nsobject is modeled after scoped_ptt. They
+ [camera_ setDelegate:self];
+ }
+ return self;
+}
+
+- (void)open {
+ [camera_ requestOpenSession];
+}
+
+- (void)close {
+ [camera_ requestCloseSession];
+ [camera_ setDelegate:nil];
+}
+
+- (void)setListener:(ImageCaptureDeviceListener*)listener {
+ listener_ = listener;
+}
+
+- (void)downloadFile:(const std::string&)name
+ localPath:(const FilePath&)localPath {
sail 2012/12/14 01:26:32 line up colons
Greg Billock 2012/12/14 18:59:11 Done.
+ // Find the file with that name and start download.
+ for (ICCameraItem* item in [camera_ mediaFiles]) {
+ std::string itemName = base::SysNSStringToUTF8([item name]);
+ if (itemName == name) {
+ // To create save options for ImageCapture, we need to
+ // split the target filename into directory/name
+ // and encode the directory as a URL.
+ NSString* saveDirectory =
+ base::mac::FilePathToNSString(localPath.DirName());
+ NSString* saveFilename =
+ base::mac::FilePathToNSString(localPath.BaseName());
+
+ NSMutableDictionary* options =
+ [NSMutableDictionary dictionaryWithCapacity:3];
+ [options setObject:[NSURL fileURLWithPath:saveDirectory isDirectory:YES]
+ forKey:ICDownloadsDirectoryURL];
+ [options setObject:saveFilename forKey:ICSaveAsFilename];
+ [options setObject:[NSNumber numberWithBool:YES] forKey:ICOverwrite];
+
+ [camera_ requestDownloadFile:base::mac::ObjCCastStrict<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
sail 2012/12/14 01:26:32 don't need this
Greg Billock 2012/12/14 18:59:11 Done.
+
+- (void)cameraDevice:(ICCameraDevice*)camera didAddItem:(ICCameraItem*)item {
+ std::string name = base::SysNSStringToUTF8([item name]);
+ base::PlatformFileInfo info;
+ if ([[item UTI] isEqualToString:base::mac::CFToNSCast(kUTTypeFolder)])
+ info.is_directory = true;
+ else
+ info.size = [base::mac::ObjCCastStrict<ICCameraFile>(item) fileSize];
+ info.last_modified = NSDateToBaseTime([item modificationDate]);
+ info.creation_time = NSDateToBaseTime([item creationDate]);
+ 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;
+ 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) {
+ if (listener_) {
+ listener_->DeviceRemoved();
+ [self setListener:nil];
+ }
+ }
+}
+
+- (void)device:(ICDevice*)device didEncounterError:(NSError*)error {
+ if (error) {
+ if (listener_) {
+ listener_->DeviceRemoved();
+ [self setListener:nil];
+ }
+ }
+}
+
+// When this message is received, all media metadata is now loaded.
+- (void)deviceDidBecomeReadyWithCompleteContentCatalog:(ICDevice*)device {
+ if (listener_)
+ listener_->NoMoreItems();
+}
+
+- (void)didDownloadFile:(ICCameraFile*)file error:(NSError*)error
+ options:(NSDictionary*)options contextInfo:(void*)contextInfo {
sail 2012/12/14 01:26:32 align colons, one argument per line
Greg Billock 2012/12/14 18:59:11 Done. I think I have them all now. Whew! :-) On 2
+ std::string name = base::SysNSStringToUTF8([file name]);
+ if (error) {
+ if (listener_)
+ listener_->DownloadedFile(name, base::PLATFORM_FILE_ERROR_FAILED);
+ return;
+ }
+
+ // ImageCapture does not save the file into the name we give it in the
+ // options. It picks a new name according to its best lights, so we need
+ // to rename the file.
+ std::string savedFilename =
+ base::SysNSStringToUTF8([options objectForKey:ICSavedFilename]);
+ std::string saveAsFilename =
+ base::SysNSStringToUTF8([options objectForKey:ICSaveAsFilename]);
+ if (savedFilename != saveAsFilename) {
+ FilePath save_dir(base::SysNSStringToUTF8(
+ [[options objectForKey:ICDownloadsDirectoryURL] path]));
+ FilePath saveAsPath = save_dir.Append(saveAsFilename);
+ FilePath savedPath = save_dir.Append(savedFilename);
+ [self retain];
+ content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
+ base::Bind(&RenameFileAndReturn, name, savedPath, saveAsPath, self));
+ return;
+ }
+
+ if (listener_)
+ listener_->DownloadedFile(name, base::PLATFORM_FILE_OK);
+}
+
+- (void)didRenameDownloadFile:(const std::string&)name
+ withError:(bool)renameError {
+ [self release];
+ // !!! need to mutex-protect this listener
sail 2012/12/14 01:26:32 This should be removed and just assert that you're
Greg Billock 2012/12/14 18:59:11 There's at least three threads involved: the block
+ if (listener_) {
+ listener_->DownloadedFile(name,
+ renameError ? base::PLATFORM_FILE_OK
+ : base::PLATFORM_FILE_ERROR_FAILED);
+ }
+}
+
+@end // ImageCaptureCameraInterface

Powered by Google App Engine
This is Rietveld 408576698