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

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

Powered by Google App Engine
This is Rietveld 408576698