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

Side by Side Diff: chrome/browser/system_monitor/image_capture_device.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: Use constants Created 7 years, 12 months 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_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/26 18:52:37 DCHECK that this is called on the UI thread
Greg Billock 2013/01/03 20:59:55 Done.
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 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
58 listener_ = listener;
59 }
60
61 - (void)open {
62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
63 DCHECK(listener_);
64 [camera_ requestOpenSession];
65 }
66
67 - (void)close {
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
69 [camera_ requestCloseSession];
70 [camera_ setDelegate:nil];
71 listener_.reset();
72 }
73
74 - (void)downloadFile:(const std::string&)name
75 localPath:(const FilePath&)localPath {
76 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
77 // Find the file with that name and start download.
78 for (ICCameraItem* item in [camera_ mediaFiles]) {
79 std::string itemName = base::SysNSStringToUTF8([item name]);
80 if (itemName == name) {
81 // To create save options for ImageCapture, we need to
82 // split the target filename into directory/name
83 // and encode the directory as a URL.
84 NSString* saveDirectory =
85 base::mac::FilePathToNSString(localPath.DirName());
86 NSString* saveFilename =
87 base::mac::FilePathToNSString(localPath.BaseName());
88
89 NSMutableDictionary* options =
90 [NSMutableDictionary dictionaryWithCapacity:3];
91 [options setObject:[NSURL fileURLWithPath:saveDirectory isDirectory:YES]
92 forKey:ICDownloadsDirectoryURL];
93 [options setObject:saveFilename forKey:ICSaveAsFilename];
94 [options setObject:[NSNumber numberWithBool:YES] forKey:ICOverwrite];
95
96 [camera_ requestDownloadFile:base::mac::ObjCCastStrict<ICCameraFile>(item)
97 options:options
98 downloadDelegate:self
99 didDownloadSelector:
100 @selector(didDownloadFile:error:options:contextInfo:)
101 contextInfo:NULL];
102 return;
103 }
104 }
105
106 if (listener_)
107 listener_->DownloadedFile(name, base::PLATFORM_FILE_ERROR_NOT_FOUND);
108 }
109
110 - (void)cameraDevice:(ICCameraDevice*)camera didAddItem:(ICCameraItem*)item {
111 std::string name = base::SysNSStringToUTF8([item name]);
112 base::PlatformFileInfo info;
113 if ([[item UTI] isEqualToString:base::mac::CFToNSCast(kUTTypeFolder)])
114 info.is_directory = true;
115 else
116 info.size = [base::mac::ObjCCastStrict<ICCameraFile>(item) fileSize];
117 info.last_modified = NSDateToBaseTime([item modificationDate]);
118 info.creation_time = NSDateToBaseTime([item creationDate]);
119 info.last_accessed = info.last_modified;
120
121 if (listener_)
122 listener_->ItemAdded(name, info);
123 }
124
125 - (void)cameraDevice:(ICCameraDevice*)camera didAddItems:(NSArray*)items {
126 for (ICCameraItem* item in items)
127 [self cameraDevice:camera didAddItem:item];
128 }
129
130 - (void)didRemoveDevice:(ICDevice*)device {
131 device.delegate = NULL;
132 if (listener_)
133 listener_->DeviceRemoved();
134 }
135
136 // Notifies that a session was opened with the given device; potentially
137 // with an error.
138 - (void)device:(ICDevice*)device didOpenSessionWithError:(NSError*)error {
139 if (error)
140 [self didRemoveDevice:camera_];
141 }
142
143 - (void)device:(ICDevice*)device didEncounterError:(NSError*)error {
144 if (error && listener_)
145 listener_->DeviceRemoved();
146 }
147
148 // When this message is received, all media metadata is now loaded.
149 - (void)deviceDidBecomeReadyWithCompleteContentCatalog:(ICDevice*)device {
150 if (listener_)
151 listener_->NoMoreItems();
152 }
153
154 - (void)didDownloadFile:(ICCameraFile*)file
155 error:(NSError*)error
156 options:(NSDictionary*)options
157 contextInfo:(void*)contextInfo {
158 std::string name = base::SysNSStringToUTF8([file name]);
159
160 if (error) {
161 if (listener_)
162 listener_->DownloadedFile(name, base::PLATFORM_FILE_ERROR_FAILED);
163 return;
164 }
165
166 std::string savedFilename =
167 base::SysNSStringToUTF8([options objectForKey:ICSavedFilename]);
168 std::string saveAsFilename =
169 base::SysNSStringToUTF8([options objectForKey:ICSaveAsFilename]);
170 if (savedFilename == saveAsFilename) {
171 if (listener_)
172 listener_->DownloadedFile(name, base::PLATFORM_FILE_OK);
173 return;
174 }
175
176 // ImageCapture did not save the file into the name we give it in the
sail 2012/12/26 18:52:37 give -> gave
Greg Billock 2013/01/03 20:59:55 Done.
177 // options. It picks a new name according to its best lights, so we need
178 // to rename the file.
179 FilePath saveDir(base::SysNSStringToUTF8(
180 [[options objectForKey:ICDownloadsDirectoryURL] path]));
181 FilePath saveAsPath = saveDir.Append(saveAsFilename);
182 FilePath savedPath = saveDir.Append(savedFilename);
183 // Shared result value from file-copy closure to tell-listener closure.
184 base::PlatformFileError* copyResult = new base::PlatformFileError();
185 content::BrowserThread::PostTaskAndReply(
186 content::BrowserThread::FILE,
187 FROM_HERE,
188 base::Bind(&RenameFile, savedPath, saveAsPath, copyResult),
189 base::Bind(&ReturnRenameResultToListener, listener_, name, copyResult));
190 }
191
192 @end // ImageCaptureDevice
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698