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/disk_info_mac.h" | |
10 #include "chrome/browser/system_monitor/media_storage_util.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 | |
13 @implementation ImageCaptureCameraInterface | |
14 | |
15 - (id)init:(ICCameraDevice*)camera_device { | |
16 camera_ = camera_device; | |
sail
2012/12/13 02:14:00
init methods should be of the form:
if ((self =
Greg Billock
2012/12/14 00:39:59
Done.
| |
17 camera_.delegate = self; | |
18 return self; | |
19 } | |
20 | |
21 - (void)open { | |
22 [camera_ requestOpenSession]; | |
23 } | |
24 | |
25 - (void)close { | |
26 [camera_ requestCloseSession]; | |
27 camera_.delegate = NULL; | |
28 } | |
29 | |
30 - (void)setListener:(ImageCaptureDeviceListener*)listener { | |
31 listener_ = listener; | |
32 } | |
33 | |
34 - (void)DownloadFile:(const std::string&)name | |
35 localPath:(const FilePath&)local_path { | |
36 // Find the file with that name and start download. | |
37 for (ICCameraItem* item in [camera_ mediaFiles]) { | |
38 std::string itemName = base::SysNSStringToUTF8([item name]); | |
39 if (itemName == name) { | |
40 NSMutableDictionary* options = | |
41 [NSMutableDictionary dictionaryWithCapacity:3]; | |
42 NSString* fileURLString = | |
sail
2012/12/13 02:14:00
fileURLString and filename are confusing, one is t
Greg Billock
2012/12/14 00:39:59
Done.
| |
43 [NSString stringWithUTF8String:local_path.DirName().value().c_str()]; | |
sail
2012/12/13 02:14:00
base::mac::FilePathToNSString(), more below
Greg Billock
2012/12/14 00:39:59
Ah ha! That's what I was wanting. Is there an NSUR
| |
44 [options setObject:[NSURL fileURLWithPath:fileURLString isDirectory:YES] | |
45 forKey:ICDownloadsDirectoryURL]; | |
sail
2012/12/13 02:14:00
align colons, more below
Greg Billock
2012/12/14 00:39:59
Done.
Greg Billock
2012/12/14 00:39:59
Done.
| |
46 NSString* filename = | |
47 [NSString stringWithUTF8String:local_path.BaseName().value().c_str()]; | |
48 [options setObject:filename forKey:ICSaveAsFilename]; | |
49 [options setObject:[NSNumber numberWithBool:YES] forKey:ICOverwrite]; | |
50 | |
51 [camera_ requestDownloadFile:(ICCameraFile*)item | |
sail
2012/12/13 02:14:00
no C style casts
should use base::mac::ObjCCastStr
Greg Billock
2012/12/14 00:39:59
Done.
| |
52 options:options | |
53 downloadDelegate:self | |
54 didDownloadSelector: | |
55 @selector(didDownloadFile:error:options:contextInfo:) | |
56 contextInfo:NULL]; | |
57 return; | |
58 } | |
59 } | |
60 | |
61 if (listener_) | |
62 listener_->DownloadedFile(name, base::PLATFORM_FILE_ERROR_NOT_FOUND); | |
63 } | |
64 | |
65 // Delegates for ICCameraDeviceDelegate | |
66 | |
67 - (void)cameraDevice:(ICCameraDevice*)camera didAddItem:(ICCameraItem*)item { | |
68 std::string name = base::SysNSStringToUTF8([item name]); | |
69 base::PlatformFileInfo info; | |
70 info.size = 0; | |
sail
2012/12/13 02:14:00
this is already initialized in the constructor, sa
Greg Billock
2012/12/14 00:39:59
Done.
| |
71 info.is_directory = false; | |
72 if ([[item UTI] isEqualToString:(NSString*)kUTTypeFolder]) | |
sail
2012/12/13 02:14:00
no C style casts, use base::mac::CFToNSCast(kUTTyp
Greg Billock
2012/12/14 00:39:59
Done.
| |
73 info.is_directory = true; | |
74 else | |
75 info.size = [(ICCameraFile*)item fileSize]; | |
sail
2012/12/13 02:14:00
no C style casts,
Greg Billock
2012/12/14 00:39:59
Done.
| |
76 info.is_symbolic_link = false; | |
77 info.last_modified = | |
78 base::Time::FromDoubleT([[item modificationDate] timeIntervalSince1970]); | |
sail
2012/12/13 02:14:00
maybe move this to a utility function above, NSDat
Greg Billock
2012/12/14 00:39:59
Done.
| |
79 info.creation_time = | |
80 base::Time::FromDoubleT([[item creationDate] timeIntervalSince1970]); | |
81 info.last_accessed = info.last_modified; | |
82 | |
83 if (listener_) | |
84 listener_->ItemAdded(name, info); | |
85 } | |
86 | |
87 - (void)cameraDevice:(ICCameraDevice*)camera didAddItems:(NSArray*)items { | |
88 for (ICCameraItem* item in items) | |
89 [self cameraDevice:camera didAddItem:item]; | |
90 } | |
91 | |
92 - (void)didRemoveDevice:(ICDevice*)device { | |
93 device.delegate = NULL; | |
94 // Note: handled by ICDeviceBrowser::didRemoveDevice | |
sail
2012/12/13 02:14:00
It's not clear what the ready/open etc.. are used
Greg Billock
2012/12/14 00:39:59
Yeah, I'm not sure yet what kind of error handling
| |
95 [device.userData setObject:[NSNumber numberWithBool:NO] forKey:@"ready"]; | |
96 [device.userData setObject:[NSNumber numberWithBool:NO] forKey:@"open"]; | |
97 if (listener_) { | |
98 listener_->DeviceRemoved(); | |
99 [self setListener:nil]; | |
100 } | |
101 } | |
102 | |
103 // Notifies that a session was opened with the given device; potentially | |
104 // with an error. | |
105 - (void)device:(ICDevice*)device didOpenSessionWithError:(NSError*)error { | |
106 if (error == NULL) | |
sail
2012/12/13 02:14:00
Objective-C uses nil
in this caes this should just
Greg Billock
2012/12/14 00:39:59
Done.
| |
107 [device.userData setObject:[NSNumber numberWithBool:YES] forKey:@"open"]; | |
108 else | |
109 [device.userData setObject:error forKey:@"error"]; | |
110 } | |
111 | |
112 // Notifies that the device is ready for commands (i.e. download images) | |
sail
2012/12/13 02:14:00
i.e. -> e.g.
period at end
Greg Billock
2012/12/14 00:39:59
Removed.
On 2012/12/13 02:14:00, sail wrote:
| |
113 - (void)deviceDidBecomeReady:(ICDevice*)device { | |
114 [device.userData setObject:[NSNumber numberWithBool:YES] forKey:@"ready"]; | |
115 } | |
116 | |
117 - (void)device:(ICDevice*)device didEncounterError:(NSError*)error { | |
118 [device.userData setObject:error forKey:@"error"]; | |
119 [device.userData setObject:[NSNumber numberWithBool:NO] forKey:@"open"]; | |
120 } | |
121 | |
122 // All metadata is now loaded. | |
123 - (void)deviceDidBecomeReadyWithCompleteContentCatalog:(ICDevice*)device { | |
124 if (device.type & ICDeviceTypeCamera) { | |
sail
2012/12/13 02:14:00
no braces
Greg Billock
2012/12/14 00:39:59
Removed
On 2012/12/13 02:14:00, sail wrote:
| |
125 [device.userData setValue:(id)kCFBooleanTrue forKey:@"complete"]; | |
sail
2012/12/13 02:14:00
No C-style casts
Greg Billock
2012/12/14 00:39:59
Removed
On 2012/12/13 02:14:00, sail wrote:
| |
126 } | |
127 if (listener_) | |
128 listener_->NoMoreItems(); | |
129 } | |
130 | |
131 // Delegates for ICCameraDeviceDownloadDelegate | |
sail
2012/12/13 02:14:00
not needed
Greg Billock
2012/12/14 00:39:59
Done.
| |
132 | |
133 void RenameFileAndReturn(const std::string& name, | |
sail
2012/12/13 02:14:00
move this to an anonymous namespace above
Greg Billock
2012/12/14 00:39:59
Done.
| |
134 const FilePath& downloaded_filename, | |
135 const FilePath& desired_filename, | |
136 ImageCaptureCameraInterface* caller) { | |
137 int64 edsize = 0; | |
sail
2012/12/13 02:14:00
need to assert that this is on the file thread
Greg Billock
2012/12/14 00:39:59
file_util does this assert
On 2012/12/13 02:14:00
| |
138 file_util::GetFileSize(downloaded_filename, &edsize); | |
139 bool error = file_util::ReplaceFile(downloaded_filename, desired_filename); | |
140 [caller DidRenameDownloadFile:name withError:error]; | |
sail
2012/12/13 02:14:00
is the caller thread safe? do you want to do this
Greg Billock
2012/12/14 00:39:59
I need to think more about this. The calling situa
sail
2012/12/14 01:26:32
This shouldn't be check in as is. If you'd like to
| |
141 } | |
142 | |
143 - (void)didDownloadFile:(ICCameraFile*)file error:(NSError*)error | |
sail
2012/12/13 02:14:00
one argument per line and line up colons
Greg Billock
2012/12/14 00:39:59
Done.
| |
144 options:(NSDictionary*)options contextInfo:(void*)contextInfo { | |
145 std::string name = base::SysNSStringToUTF8([file name]); | |
146 base::PlatformFileError err = base::PLATFORM_FILE_OK; | |
147 | |
148 // ImageCapture does not save the file into the name we give it in the | |
149 // options. It picks a new name according to it's best lights, so we need | |
150 // to rename the file. | |
151 std::string savedFilename = | |
152 base::SysNSStringToUTF8([options objectForKey:ICSavedFilename]); | |
153 std::string saveAsFilename = | |
154 base::SysNSStringToUTF8([options objectForKey:ICSaveAsFilename]); | |
155 if (!error && (savedFilename != saveAsFilename)) { | |
156 FilePath save_dir(base::SysNSStringToUTF8( | |
157 [[options objectForKey:ICDownloadsDirectoryURL] path])); | |
158 FilePath saveAsPath = save_dir.Append(saveAsFilename); | |
159 FilePath savedPath = save_dir.Append(savedFilename); | |
160 // !!! should really pass a weak pointer or a refptr or something. | |
sail
2012/12/13 02:14:00
how about just retaining self here and releasing o
Greg Billock
2012/12/14 00:39:59
That'd work. The other problem is the listener. I
| |
161 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | |
162 base::Bind(&RenameFileAndReturn, name, savedPath, saveAsPath, self)); | |
163 return; | |
164 } | |
165 | |
166 if (error) | |
sail
2012/12/13 02:14:00
maybe just have an early return at the beginning,
Greg Billock
2012/12/14 00:39:59
Done.
| |
167 err = base::PLATFORM_FILE_ERROR_FAILED; | |
168 | |
169 if (listener_) | |
170 listener_->DownloadedFile(name, err); | |
171 } | |
172 | |
173 - (void)DidRenameDownloadFile:(const std::string&)name | |
174 withError:(bool)rename_error { | |
175 if (listener_) { | |
176 listener_->DownloadedFile(name, | |
177 rename_error ? base::PLATFORM_FILE_OK | |
178 : base::PLATFORM_FILE_ERROR_FAILED); | |
179 } | |
180 } | |
181 | |
182 @end // ImageCaptureCameraInterface | |
OLD | NEW |