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