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

Side by Side Diff: chrome/browser/download/download_status_updater_mac.mm

Issue 10831340: Bulletproof download status update code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixin' Created 8 years, 4 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/download/download_status_updater.h" 5 #include "chrome/browser/download/download_status_updater.h"
6 6
7 #include "base/mac/foundation_util.h" 7 #include "base/mac/foundation_util.h"
8 #include "base/memory/scoped_nsobject.h" 8 #include "base/memory/scoped_nsobject.h"
9 #include "base/supports_user_data.h" 9 #include "base/supports_user_data.h"
10 #include "base/sys_string_conversions.h" 10 #include "base/sys_string_conversions.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 if (!result) { 68 if (!result) {
69 NSString** ref = static_cast<NSString**>( 69 NSString** ref = static_cast<NSString**>(
70 CFBundleGetDataPointerForName(foundation, 70 CFBundleGetDataPointerForName(foundation,
71 base::mac::NSToCFCast(string))); 71 base::mac::NSToCFCast(string)));
72 if (ref) { 72 if (ref) {
73 result = *ref; 73 result = *ref;
74 [cache setObject:result forKey:string]; 74 [cache setObject:result forKey:string];
75 } 75 }
76 } 76 }
77 77
78 if (!result) {
79 // Huh. At least return a local copy of the expected string.
80 result = string;
81 NSString* const kKeySuffix = @"Key";
82 if ([result hasSuffix:kKeySuffix])
83 result = [result substringToIndex:[result length] - [kKeySuffix length]];
84 }
85
78 return result; 86 return result;
79 } 87 }
80 88
81 } // namespace 89 } // namespace
82 90
83 @interface NSProgress : NSObject 91 @interface NSProgress : NSObject
84 92
85 - (id)initWithParent:(id)parent userInfo:(NSDictionary*)info; 93 - (id)initWithParent:(id)parent userInfo:(NSDictionary*)info;
86 @property(copy) NSString* kind; 94 @property(copy) NSString* kind;
87 95
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 float progress) { 176 float progress) {
169 DockIcon* dock_icon = [DockIcon sharedDockIcon]; 177 DockIcon* dock_icon = [DockIcon sharedDockIcon];
170 [dock_icon setDownloads:download_count]; 178 [dock_icon setDownloads:download_count];
171 [dock_icon setIndeterminate:!progress_known]; 179 [dock_icon setIndeterminate:!progress_known];
172 [dock_icon setProgress:progress]; 180 [dock_icon setProgress:progress];
173 [dock_icon updateIcon]; 181 [dock_icon updateIcon];
174 } 182 }
175 183
176 void CreateNSProgress(content::DownloadItem* download) { 184 void CreateNSProgress(content::DownloadItem* download) {
177 NSURL* source_url = [NSURL URLWithString: 185 NSURL* source_url = [NSURL URLWithString:
178 base::SysUTF8ToNSString(download->GetURL().spec())]; 186 base::SysUTF8ToNSString(download->GetURL().possibly_invalid_spec())];
179 FilePath destination_path = download->GetFullPath(); 187 FilePath destination_path = download->GetFullPath();
180 NSURL* destination_url = [NSURL fileURLWithPath: 188 NSURL* destination_url = [NSURL fileURLWithPath:
181 base::mac::FilePathToNSString(destination_path)]; 189 base::mac::FilePathToNSString(destination_path)];
182 190
183 // If there were an image to fly to the download folder in the dock, then 191 // If there were an image to fly to the download folder in the dock, then
184 // the keys in the userInfo to set would be: 192 // the keys in the userInfo to set would be:
185 // - @"NSProgressFlyToImageKey" : NSImage 193 // - @"NSProgressFlyToImageKey" : NSImage
186 // - kNSProgressFileIconOriginalRectKey : NSValue of NSRect in global coords 194 // - kNSProgressFileIconOriginalRectKey : NSValue of NSRect in global coords
187 195
188 NSDictionary* user_info = @{ 196 NSDictionary* user_info = @{
189 ProgressString(kNSProgressFileDownloadingSourceURLKey) : source_url,
190 ProgressString(kNSProgressFileLocationCanChangeKey) : @true, 197 ProgressString(kNSProgressFileLocationCanChangeKey) : @true,
191 ProgressString(kNSProgressFileOperationKindKey) : 198 ProgressString(kNSProgressFileOperationKindKey) :
192 ProgressString(kNSProgressFileOperationKindDownloading), 199 ProgressString(kNSProgressFileOperationKindDownloading),
193 ProgressString(kNSProgressFileURLKey) : destination_url 200 ProgressString(kNSProgressFileURLKey) : destination_url
194 }; 201 };
202
195 Class progress_class = NSClassFromString(@"NSProgress"); 203 Class progress_class = NSClassFromString(@"NSProgress");
196 NSProgress* progress = [progress_class performSelector:@selector(alloc)]; 204 NSProgress* progress = [progress_class performSelector:@selector(alloc)];
197 progress = [progress performSelector:@selector(initWithParent:userInfo:) 205 progress = [progress performSelector:@selector(initWithParent:userInfo:)
198 withObject:nil 206 withObject:nil
199 withObject:user_info]; 207 withObject:user_info];
200 progress.kind = ProgressString(kNSProgressKindFile); 208 progress.kind = ProgressString(kNSProgressKindFile);
201 209
210 if (source_url) {
benjhayden 2012/08/16 15:52:03 Could you do this right after making |user_info| b
Avi (use Gerrit) 2012/08/16 17:05:30 Unfortunately, NSDictionaries are not mutable so I
211 [progress setUserInfoObject:source_url forKey:
212 ProgressString(kNSProgressFileDownloadingSourceURLKey)];
213 }
214
202 progress.pausable = NO; 215 progress.pausable = NO;
203 progress.cancellable = YES; 216 progress.cancellable = YES;
204 [progress setCancellationHandler:^{ 217 [progress setCancellationHandler:^{
205 dispatch_async(dispatch_get_main_queue(), ^{ 218 dispatch_async(dispatch_get_main_queue(), ^{
206 download->Cancel(true); 219 download->Cancel(true);
207 }); 220 });
208 }]; 221 }];
209 222
210 progress.totalUnitCount = download->GetTotalBytes(); 223 progress.totalUnitCount = download->GetTotalBytes();
211 progress.completedUnitCount = download->GetReceivedBytes(); 224 progress.completedUnitCount = download->GetReceivedBytes();
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 } 291 }
279 292
280 // Notify the Finder. 293 // Notify the Finder.
281 NSString* parent_path = [download_path stringByDeletingLastPathComponent]; 294 NSString* parent_path = [download_path stringByDeletingLastPathComponent];
282 FNNotifyByPath( 295 FNNotifyByPath(
283 reinterpret_cast<const UInt8*>([parent_path fileSystemRepresentation]), 296 reinterpret_cast<const UInt8*>([parent_path fileSystemRepresentation]),
284 kFNDirectoryModifiedMessage, 297 kFNDirectoryModifiedMessage,
285 kNilOptions); 298 kNilOptions);
286 } 299 }
287 } 300 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698