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

Side by Side Diff: chrome/browser/cocoa/web_drag_source.mm

Issue 255035: Mac: *Correctly* fill file extension as drag source. (Closed)
Patch Set: Updated comments per Nico's review. Created 11 years, 2 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
« 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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 #import "chrome/browser/cocoa/web_drag_source.h" 5 #import "chrome/browser/cocoa/web_drag_source.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/sys_string_conversions.h" 9 #include "base/sys_string_conversions.h"
10 #include "base/task.h" 10 #include "base/task.h"
(...skipping 11 matching lines...) Expand all
22 22
23 using base::SysNSStringToUTF8; 23 using base::SysNSStringToUTF8;
24 using base::SysUTF8ToNSString; 24 using base::SysUTF8ToNSString;
25 using base::SysUTF16ToNSString; 25 using base::SysUTF16ToNSString;
26 using net::FileStream; 26 using net::FileStream;
27 27
28 28
29 namespace { 29 namespace {
30 30
31 // Make a drag image from the drop data. 31 // Make a drag image from the drop data.
32 // TODO(viettrungluu@gmail.com): Move this somewhere more sensible. 32 // TODO(viettrungluu): Move this somewhere more sensible.
33 NSImage* MakeDragImage(const WebDropData* drop_data) { 33 NSImage* MakeDragImage(const WebDropData* drop_data) {
34 // TODO(viettrungluu@gmail.com): Just a stub for now. Make it do something. 34 // TODO(viettrungluu): Just a stub for now. Make it do something (see, e.g.,
35 // WebKit/WebKit/mac/Misc/WebNSViewExtras.m: |-_web_DragImageForElement:...|).
35 36
36 // Default to returning a generic image. 37 // Default to returning a generic image.
37 return nsimage_cache::ImageNamed(@"nav.pdf"); 38 return nsimage_cache::ImageNamed(@"nav.pdf");
38 } 39 }
39 40
40 // Returns a filename appropriate for the drop data (of form "FILENAME-seq.EXT" 41 // Returns a filename appropriate for the drop data (of form "FILENAME-seq.EXT"
41 // if seq > 0). 42 // if seq > 0).
42 // TODO(viettrungluu@gmail.com): Refactor to make it common across platforms, 43 // TODO(viettrungluu): Refactor to make it common across platforms,
43 // and move it somewhere sensible. 44 // and move it somewhere sensible.
44 FilePath GetFileNameFromDragData( 45 FilePath GetFileNameFromDragData(
45 const WebDropData& drop_data, unsigned seq) { 46 const WebDropData& drop_data, unsigned seq) {
46 // Images without ALT text will only have a file extension so we need to 47 // Images without ALT text will only have a file extension so we need to
47 // synthesize one from the provided extension and URL. 48 // synthesize one from the provided extension and URL.
48 FilePath file_name([SysUTF16ToNSString(drop_data.file_description_filename) 49 FilePath file_name([SysUTF16ToNSString(drop_data.file_description_filename)
49 fileSystemRepresentation]); 50 fileSystemRepresentation]);
50 file_name = file_name.BaseName().RemoveExtension(); 51 file_name = file_name.BaseName().RemoveExtension();
51 52
52 if (file_name.empty()) { 53 if (file_name.empty()) {
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 DCHECK(dropData_->url.is_valid()); 165 DCHECK(dropData_->url.is_valid());
165 [pboard setURLs:[NSArray 166 [pboard setURLs:[NSArray
166 arrayWithObject:SysUTF8ToNSString(dropData_->url.spec())] 167 arrayWithObject:SysUTF8ToNSString(dropData_->url.spec())]
167 withTitles:[NSArray arrayWithObject: 168 withTitles:[NSArray arrayWithObject:
168 SysUTF16ToNSString(dropData_->url_title)]]; 169 SysUTF16ToNSString(dropData_->url_title)]];
169 170
170 // File contents. 171 // File contents.
171 } else if ([type isEqualToString:NSFileContentsPboardType] || 172 } else if ([type isEqualToString:NSFileContentsPboardType] ||
172 [type isEqualToString:NSCreateFileContentsPboardType( 173 [type isEqualToString:NSCreateFileContentsPboardType(
173 SysUTF16ToNSString(dropData_->file_extension))]) { 174 SysUTF16ToNSString(dropData_->file_extension))]) {
174 // TODO(viettrungluu@gmail.com): find something which is known to accept 175 // TODO(viettrungluu: find something which is known to accept
175 // NSFileContentsPboardType to check that this actually works! 176 // NSFileContentsPboardType to check that this actually works!
176 scoped_nsobject<NSFileWrapper> file_wrapper( 177 scoped_nsobject<NSFileWrapper> file_wrapper(
177 [[NSFileWrapper alloc] initRegularFileWithContents:[NSData 178 [[NSFileWrapper alloc] initRegularFileWithContents:[NSData
178 dataWithBytes:dropData_->file_contents.data() 179 dataWithBytes:dropData_->file_contents.data()
179 length:dropData_->file_contents.length()]]); 180 length:dropData_->file_contents.length()]]);
180 [file_wrapper setPreferredFilename:SysUTF8ToNSString( 181 [file_wrapper setPreferredFilename:SysUTF8ToNSString(
181 GetFileNameFromDragData(*dropData_, 0).value())]; 182 GetFileNameFromDragData(*dropData_, 0).value())];
182 [pboard writeFileWrapper:file_wrapper]; 183 [pboard writeFileWrapper:file_wrapper];
183 184
184 // TIFF. 185 // TIFF.
185 } else if ([type isEqualToString:NSTIFFPboardType]) { 186 } else if ([type isEqualToString:NSTIFFPboardType]) {
186 // FIXME(viettrungluu@gmail.com): This is a bit odd since we rely on Cocoa 187 // TODO(viettrungluu): This is a bit odd since we rely on Cocoa to render
187 // to render our image into a TIFF. This is also suboptimal since this is 188 // our image into a TIFF. This is also suboptimal since this is all done
188 // all done synchronously. I'm not sure there's much we can easily do about 189 // synchronously. I'm not sure there's much we can easily do about it.
189 // it.
190 scoped_nsobject<NSImage> image( 190 scoped_nsobject<NSImage> image(
191 [[NSImage alloc] initWithData:[NSData 191 [[NSImage alloc] initWithData:[NSData
192 dataWithBytes:dropData_->file_contents.data() 192 dataWithBytes:dropData_->file_contents.data()
193 length:dropData_->file_contents.length()]]); 193 length:dropData_->file_contents.length()]]);
194 [pboard setData:[image TIFFRepresentation] forType:NSTIFFPboardType]; 194 [pboard setData:[image TIFFRepresentation] forType:NSTIFFPboardType];
195 195
196 // Plain text. 196 // Plain text.
197 } else if ([type isEqualToString:NSStringPboardType]) { 197 } else if ([type isEqualToString:NSStringPboardType]) {
198 DCHECK(!dropData_->plain_text.empty()); 198 DCHECK(!dropData_->plain_text.empty());
199 [pboard setString:SysUTF16ToNSString(dropData_->plain_text) 199 [pboard setString:SysUTF16ToNSString(dropData_->plain_text)
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 [pasteboard_ addTypes:[NSArray arrayWithObject:NSHTMLPboardType] 326 [pasteboard_ addTypes:[NSArray arrayWithObject:NSHTMLPboardType]
327 owner:contentsView_]; 327 owner:contentsView_];
328 328
329 // URL. 329 // URL.
330 if (dropData_->url.is_valid()) 330 if (dropData_->url.is_valid())
331 [pasteboard_ addTypes:[NSArray arrayWithObject:NSURLPboardType] 331 [pasteboard_ addTypes:[NSArray arrayWithObject:NSURLPboardType]
332 owner:contentsView_]; 332 owner:contentsView_];
333 333
334 // File. 334 // File.
335 if (!dropData_->file_contents.empty()) { 335 if (!dropData_->file_contents.empty()) {
336 NSString* file_ext = SysUTF16ToNSString(dropData_->file_extension); 336 // |dropData_->file_extension| comes with the '.', which we must strip.
337 NSString* fileExtension =
338 (dropData_->file_extension.length() > 0) ?
339 SysUTF16ToNSString(dropData_->file_extension.substr(1)) : @"";
337 340
338 // File contents (with and without specific type), file (HFS) promise, TIFF. 341 // File contents (with and without specific type), file (HFS) promise, TIFF.
339 // TODO(viettrungluu@gmail.com): others? 342 // TODO(viettrungluu): others?
340 [pasteboard_ addTypes:[NSArray arrayWithObjects: 343 [pasteboard_ addTypes:[NSArray arrayWithObjects:
341 NSFileContentsPboardType, 344 NSFileContentsPboardType,
342 NSCreateFileContentsPboardType(file_ext), 345 NSCreateFileContentsPboardType(fileExtension),
343 NSFilesPromisePboardType, 346 NSFilesPromisePboardType,
344 NSTIFFPboardType, 347 NSTIFFPboardType,
345 nil] 348 nil]
346 owner:contentsView_]; 349 owner:contentsView_];
347 350
348 // For the file promise, we need to specify the extension. 351 // For the file promise, we need to specify the extension.
349 [pasteboard_ setPropertyList:[NSArray arrayWithObject:file_ext] 352 [pasteboard_ setPropertyList:[NSArray arrayWithObject:fileExtension]
350 forType:NSFilesPromisePboardType]; 353 forType:NSFilesPromisePboardType];
351 } 354 }
352 355
353 // Plain text. 356 // Plain text.
354 if (!dropData_->plain_text.empty()) 357 if (!dropData_->plain_text.empty())
355 [pasteboard_ addTypes:[NSArray arrayWithObject:NSStringPboardType] 358 [pasteboard_ addTypes:[NSArray arrayWithObject:NSStringPboardType]
356 owner:contentsView_]; 359 owner:contentsView_];
357 } 360 }
358 361
359 - (NSImage*)dragImage { 362 - (NSImage*)dragImage {
360 return MakeDragImage(dropData_.get()); 363 return MakeDragImage(dropData_.get());
361 } 364 }
362 365
363 @end // @implementation WebDragSource (Private) 366 @end // @implementation WebDragSource (Private)
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