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

Side by Side Diff: chrome/browser/system_monitor/image_capture_device.mm

Issue 11817036: Revert 175938 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 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/mac/mac_util.h"
9 #include "base/system_monitor/system_monitor.h"
10 #include "chrome/browser/system_monitor/media_storage_util.h"
11 #include "content/public/browser/browser_thread.h"
12
13 namespace {
14
15 void RenameFile(const FilePath& downloaded_filename,
16 const FilePath& desired_filename,
17 base::PlatformFileError* result) {
18 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
19 bool success = file_util::ReplaceFile(downloaded_filename, desired_filename);
20 *result = success ? base::PLATFORM_FILE_OK
21 : base::PLATFORM_FILE_ERROR_NOT_FOUND;
22 }
23
24 void ReturnRenameResultToListener(
25 base::WeakPtr<ImageCaptureDeviceListener> listener,
26 const std::string& name,
27 base::PlatformFileError* result) {
28 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
29 scoped_ptr<base::PlatformFileError> result_deleter(result);
30 if (listener)
31 listener->DownloadedFile(name, *result);
32 }
33
34 base::Time NSDateToBaseTime(NSDate* date) {
35 return base::Time::FromDoubleT([date timeIntervalSince1970]);
36 }
37
38 } // namespace
39
40 @implementation ImageCaptureDevice
41
42 - (id)initWithCameraDevice:(ICCameraDevice*)cameraDevice {
43 if ((self = [super init])) {
44 camera_.reset([cameraDevice retain]);
45 [camera_ setDelegate:self];
46 }
47 return self;
48 }
49
50 - (void)dealloc {
51 // Make sure the session was closed and listener set to null
52 // before destruction.
53 DCHECK(![camera_ delegate]);
54 DCHECK(!listener_);
55 [super dealloc];
56 }
57
58 - (void)setListener:(base::WeakPtr<ImageCaptureDeviceListener>)listener {
59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
60 listener_ = listener;
61 }
62
63 - (void)open {
64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
65 DCHECK(listener_);
66 [camera_ requestOpenSession];
67 }
68
69 - (void)close {
70 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
71 [camera_ requestCloseSession];
72 [camera_ setDelegate:nil];
73 listener_.reset();
74 }
75
76 - (void)downloadFile:(const std::string&)name
77 localPath:(const FilePath&)localPath {
78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
79 // Find the file with that name and start download.
80 for (ICCameraItem* item in [camera_ mediaFiles]) {
81 std::string itemName = base::SysNSStringToUTF8([item name]);
82 if (itemName == name) {
83 // To create save options for ImageCapture, we need to
84 // split the target filename into directory/name
85 // and encode the directory as a URL.
86 NSString* saveDirectory =
87 base::mac::FilePathToNSString(localPath.DirName());
88 NSString* saveFilename =
89 base::mac::FilePathToNSString(localPath.BaseName());
90
91 NSMutableDictionary* options =
92 [NSMutableDictionary dictionaryWithCapacity:3];
93 [options setObject:[NSURL fileURLWithPath:saveDirectory isDirectory:YES]
94 forKey:ICDownloadsDirectoryURL];
95 [options setObject:saveFilename forKey:ICSaveAsFilename];
96 [options setObject:[NSNumber numberWithBool:YES] forKey:ICOverwrite];
97
98 [camera_ requestDownloadFile:base::mac::ObjCCastStrict<ICCameraFile>(item)
99 options:options
100 downloadDelegate:self
101 didDownloadSelector:
102 @selector(didDownloadFile:error:options:contextInfo:)
103 contextInfo:NULL];
104 return;
105 }
106 }
107
108 if (listener_)
109 listener_->DownloadedFile(name, base::PLATFORM_FILE_ERROR_NOT_FOUND);
110 }
111
112 - (void)cameraDevice:(ICCameraDevice*)camera didAddItem:(ICCameraItem*)item {
113 std::string name = base::SysNSStringToUTF8([item name]);
114 base::PlatformFileInfo info;
115 if ([[item UTI] isEqualToString:base::mac::CFToNSCast(kUTTypeFolder)])
116 info.is_directory = true;
117 else
118 info.size = [base::mac::ObjCCastStrict<ICCameraFile>(item) fileSize];
119 info.last_modified = NSDateToBaseTime([item modificationDate]);
120 info.creation_time = NSDateToBaseTime([item creationDate]);
121 info.last_accessed = info.last_modified;
122
123 if (listener_)
124 listener_->ItemAdded(name, info);
125 }
126
127 - (void)cameraDevice:(ICCameraDevice*)camera didAddItems:(NSArray*)items {
128 for (ICCameraItem* item in items)
129 [self cameraDevice:camera didAddItem:item];
130 }
131
132 - (void)didRemoveDevice:(ICDevice*)device {
133 device.delegate = NULL;
134 if (listener_)
135 listener_->DeviceRemoved();
136 }
137
138 // Notifies that a session was opened with the given device; potentially
139 // with an error.
140 - (void)device:(ICDevice*)device didOpenSessionWithError:(NSError*)error {
141 if (error)
142 [self didRemoveDevice:camera_];
143 }
144
145 - (void)device:(ICDevice*)device didEncounterError:(NSError*)error {
146 if (error && listener_)
147 listener_->DeviceRemoved();
148 }
149
150 // When this message is received, all media metadata is now loaded.
151 - (void)deviceDidBecomeReadyWithCompleteContentCatalog:(ICDevice*)device {
152 if (listener_)
153 listener_->NoMoreItems();
154 }
155
156 // For 10.7 and earlier, this method has the semantics of
157 // deviceDidBecomeReadyWithCompleteContentCatalog. For 10.8++, it has
158 // the semantics of notifying that the device can receive commands.
159 - (void)deviceDidBecomeReady:(ICDevice*)device {
160 if (listener_ && base::mac::IsOSLionOrEarlier())
161 listener_->NoMoreItems();
162 }
163
164 - (void)didDownloadFile:(ICCameraFile*)file
165 error:(NSError*)error
166 options:(NSDictionary*)options
167 contextInfo:(void*)contextInfo {
168 std::string name = base::SysNSStringToUTF8([file name]);
169
170 if (error) {
171 if (listener_)
172 listener_->DownloadedFile(name, base::PLATFORM_FILE_ERROR_FAILED);
173 return;
174 }
175
176 std::string savedFilename =
177 base::SysNSStringToUTF8([options objectForKey:ICSavedFilename]);
178 std::string saveAsFilename =
179 base::SysNSStringToUTF8([options objectForKey:ICSaveAsFilename]);
180 if (savedFilename == saveAsFilename) {
181 if (listener_)
182 listener_->DownloadedFile(name, base::PLATFORM_FILE_OK);
183 return;
184 }
185
186 // ImageCapture did not save the file into the name we gave it in the
187 // options. It picks a new name according to its best lights, so we need
188 // to rename the file.
189 FilePath saveDir(base::SysNSStringToUTF8(
190 [[options objectForKey:ICDownloadsDirectoryURL] path]));
191 FilePath saveAsPath = saveDir.Append(saveAsFilename);
192 FilePath savedPath = saveDir.Append(savedFilename);
193 // Shared result value from file-copy closure to tell-listener closure.
194 base::PlatformFileError* copyResult = new base::PlatformFileError();
195 content::BrowserThread::PostTaskAndReply(
196 content::BrowserThread::FILE,
197 FROM_HERE,
198 base::Bind(&RenameFile, savedPath, saveAsPath, copyResult),
199 base::Bind(&ReturnRenameResultToListener, listener_, name, copyResult));
200 }
201
202 @end // ImageCaptureDevice
OLDNEW
« no previous file with comments | « chrome/browser/system_monitor/image_capture_device.h ('k') | chrome/browser/system_monitor/image_capture_device_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698