| OLD | NEW |
| 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 #import "content/browser/web_contents/web_drag_source_mac.h" | 5 #import "content/browser/web_contents/web_drag_source_mac.h" |
| 6 | 6 |
| 7 #include <sys/param.h> | 7 #include <sys/param.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 using content::DropData; | 45 using content::DropData; |
| 46 using content::PromiseFileFinalizer; | 46 using content::PromiseFileFinalizer; |
| 47 using content::RenderViewHostImpl; | 47 using content::RenderViewHostImpl; |
| 48 | 48 |
| 49 namespace { | 49 namespace { |
| 50 | 50 |
| 51 // An unofficial standard pasteboard title type to be provided alongside the | 51 // An unofficial standard pasteboard title type to be provided alongside the |
| 52 // |NSURLPboardType|. | 52 // |NSURLPboardType|. |
| 53 NSString* const kNSURLTitlePboardType = @"public.url-name"; | 53 NSString* const kNSURLTitlePboardType = @"public.url-name"; |
| 54 | 54 |
| 55 // Converts a base::string16 into a FilePath. Use this method instead of | |
| 56 // -[NSString fileSystemRepresentation] to prevent exceptions from being thrown. | |
| 57 // See http://crbug.com/78782 for more info. | |
| 58 base::FilePath FilePathFromFilename(const base::string16& filename) { | |
| 59 NSString* str = SysUTF16ToNSString(filename); | |
| 60 char buf[MAXPATHLEN]; | |
| 61 if (![str getFileSystemRepresentation:buf maxLength:sizeof(buf)]) | |
| 62 return base::FilePath(); | |
| 63 return base::FilePath(buf); | |
| 64 } | |
| 65 | |
| 66 // Returns a filename appropriate for the drop data | |
| 67 // TODO(viettrungluu): Refactor to make it common across platforms, | |
| 68 // and move it somewhere sensible. | |
| 69 base::FilePath GetFileNameFromDragData(const DropData& drop_data) { | |
| 70 base::FilePath file_name( | |
| 71 FilePathFromFilename(drop_data.file_description_filename)); | |
| 72 | |
| 73 // Images without ALT text will only have a file extension so we need to | |
| 74 // synthesize one from the provided extension and URL. | |
| 75 if (file_name.empty()) { | |
| 76 // Retrieve the name from the URL. | |
| 77 base::string16 suggested_filename = | |
| 78 net::GetSuggestedFilename(drop_data.url, "", "", "", "", ""); | |
| 79 const std::string extension = file_name.Extension(); | |
| 80 file_name = FilePathFromFilename(suggested_filename); | |
| 81 file_name = file_name.ReplaceExtension(extension); | |
| 82 } | |
| 83 | |
| 84 return file_name; | |
| 85 } | |
| 86 | |
| 87 // This helper's sole task is to write out data for a promised file; the caller | 55 // This helper's sole task is to write out data for a promised file; the caller |
| 88 // is responsible for opening the file. It takes the drop data and an open file | 56 // is responsible for opening the file. It takes the drop data and an open file |
| 89 // stream. | 57 // stream. |
| 90 void PromiseWriterHelper(const DropData& drop_data, | 58 void PromiseWriterHelper(const DropData& drop_data, |
| 91 base::File file) { | 59 base::File file) { |
| 92 DCHECK(file.IsValid()); | 60 DCHECK(file.IsValid()); |
| 93 file.WriteAtCurrentPos(drop_data.file_contents.data(), | 61 file.WriteAtCurrentPos(drop_data.file_contents.data(), |
| 94 drop_data.file_contents.length()); | 62 drop_data.file_contents.length()); |
| 95 } | 63 } |
| 96 | 64 |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 owner:contentsView_]; | 332 owner:contentsView_]; |
| 365 } | 333 } |
| 366 | 334 |
| 367 // MIME type. | 335 // MIME type. |
| 368 std::string mimeType; | 336 std::string mimeType; |
| 369 | 337 |
| 370 // File. | 338 // File. |
| 371 if (!dropData_->file_contents.empty() || | 339 if (!dropData_->file_contents.empty() || |
| 372 !dropData_->download_metadata.empty()) { | 340 !dropData_->download_metadata.empty()) { |
| 373 if (dropData_->download_metadata.empty()) { | 341 if (dropData_->download_metadata.empty()) { |
| 374 downloadFileName_ = GetFileNameFromDragData(*dropData_); | 342 base::Optional<base::FilePath> suggestedFilename = |
| 375 net::GetMimeTypeFromExtension(downloadFileName_.Extension(), &mimeType); | 343 dropData_->GetSafeFilenameForImageFileContents(); |
| 344 if (suggestedFilename) { |
| 345 downloadFileName_ = std::move(*suggestedFilename); |
| 346 net::GetMimeTypeFromExtension(downloadFileName_.Extension(), &mimeType); |
| 347 } |
| 376 } else { | 348 } else { |
| 377 base::string16 mimeType16; | 349 base::string16 mimeType16; |
| 378 base::FilePath fileName; | 350 base::FilePath fileName; |
| 379 if (content::ParseDownloadMetadata( | 351 if (content::ParseDownloadMetadata( |
| 380 dropData_->download_metadata, | 352 dropData_->download_metadata, |
| 381 &mimeType16, | 353 &mimeType16, |
| 382 &fileName, | 354 &fileName, |
| 383 &downloadURL_)) { | 355 &downloadURL_)) { |
| 384 // Generate the file name based on both mime type and proposed file | 356 // Generate the file name based on both mime type and proposed file |
| 385 // name. | 357 // name. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 - (NSImage*)dragImage { | 440 - (NSImage*)dragImage { |
| 469 if (dragImage_) | 441 if (dragImage_) |
| 470 return dragImage_; | 442 return dragImage_; |
| 471 | 443 |
| 472 // Default to returning a generic image. | 444 // Default to returning a generic image. |
| 473 return content::GetContentClient()->GetNativeImageNamed( | 445 return content::GetContentClient()->GetNativeImageNamed( |
| 474 IDR_DEFAULT_FAVICON).ToNSImage(); | 446 IDR_DEFAULT_FAVICON).ToNSImage(); |
| 475 } | 447 } |
| 476 | 448 |
| 477 @end // @implementation WebDragSource (Private) | 449 @end // @implementation WebDragSource (Private) |
| OLD | NEW |