| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 using content::DropData; | 46 using content::DropData; |
| 47 using content::PromiseFileFinalizer; | 47 using content::PromiseFileFinalizer; |
| 48 using content::RenderViewHostImpl; | 48 using content::RenderViewHostImpl; |
| 49 | 49 |
| 50 namespace { | 50 namespace { |
| 51 | 51 |
| 52 // An unofficial standard pasteboard title type to be provided alongside the | 52 // An unofficial standard pasteboard title type to be provided alongside the |
| 53 // |NSURLPboardType|. | 53 // |NSURLPboardType|. |
| 54 NSString* const kNSURLTitlePboardType = @"public.url-name"; | 54 NSString* const kNSURLTitlePboardType = @"public.url-name"; |
| 55 | 55 |
| 56 // Converts a base::string16 into a FilePath. Use this method instead of | |
| 57 // -[NSString fileSystemRepresentation] to prevent exceptions from being thrown. | |
| 58 // See http://crbug.com/78782 for more info. | |
| 59 base::FilePath FilePathFromFilename(const base::string16& filename) { | |
| 60 NSString* str = SysUTF16ToNSString(filename); | |
| 61 char buf[MAXPATHLEN]; | |
| 62 if (![str getFileSystemRepresentation:buf maxLength:sizeof(buf)]) | |
| 63 return base::FilePath(); | |
| 64 return base::FilePath(buf); | |
| 65 } | |
| 66 | |
| 67 // Returns a filename appropriate for the drop data | |
| 68 // TODO(viettrungluu): Refactor to make it common across platforms, | |
| 69 // and move it somewhere sensible. | |
| 70 base::FilePath GetFileNameFromDragData(const DropData& drop_data) { | |
| 71 base::FilePath file_name( | |
| 72 FilePathFromFilename(drop_data.file_description_filename)); | |
| 73 | |
| 74 // Images without ALT text will only have a file extension so we need to | |
| 75 // synthesize one from the provided extension and URL. | |
| 76 if (file_name.empty()) { | |
| 77 // Retrieve the name from the URL. | |
| 78 base::string16 suggested_filename = | |
| 79 net::GetSuggestedFilename(drop_data.url, "", "", "", "", ""); | |
| 80 const std::string extension = file_name.Extension(); | |
| 81 file_name = FilePathFromFilename(suggested_filename); | |
| 82 file_name = file_name.ReplaceExtension(extension); | |
| 83 } | |
| 84 | |
| 85 return file_name; | |
| 86 } | |
| 87 | |
| 88 // This helper's sole task is to write out data for a promised file; the caller | 56 // This helper's sole task is to write out data for a promised file; the caller |
| 89 // is responsible for opening the file. It takes the drop data and an open file | 57 // is responsible for opening the file. It takes the drop data and an open file |
| 90 // stream. | 58 // stream. |
| 91 void PromiseWriterHelper(const DropData& drop_data, | 59 void PromiseWriterHelper(const DropData& drop_data, |
| 92 base::File file) { | 60 base::File file) { |
| 93 DCHECK(file.IsValid()); | 61 DCHECK(file.IsValid()); |
| 94 file.WriteAtCurrentPos(drop_data.file_contents.data(), | 62 file.WriteAtCurrentPos(drop_data.file_contents.data(), |
| 95 drop_data.file_contents.length()); | 63 drop_data.file_contents.length()); |
| 96 } | 64 } |
| 97 | 65 |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 owner:contentsView_]; | 350 owner:contentsView_]; |
| 383 } | 351 } |
| 384 | 352 |
| 385 // MIME type. | 353 // MIME type. |
| 386 std::string mimeType; | 354 std::string mimeType; |
| 387 | 355 |
| 388 // File. | 356 // File. |
| 389 if (!dropData_->file_contents.empty() || | 357 if (!dropData_->file_contents.empty() || |
| 390 !dropData_->download_metadata.empty()) { | 358 !dropData_->download_metadata.empty()) { |
| 391 if (dropData_->download_metadata.empty()) { | 359 if (dropData_->download_metadata.empty()) { |
| 392 downloadFileName_ = GetFileNameFromDragData(*dropData_); | 360 base::Optional<base::FilePath> suggestedFilename = |
| 393 net::GetMimeTypeFromExtension(downloadFileName_.Extension(), &mimeType); | 361 dropData_->GetSafeFilenameForImageFileContents(); |
| 362 if (suggestedFilename) { |
| 363 downloadFileName_ = std::move(*suggestedFilename); |
| 364 net::GetMimeTypeFromExtension(downloadFileName_.Extension(), &mimeType); |
| 365 } |
| 394 } else { | 366 } else { |
| 395 base::string16 mimeType16; | 367 base::string16 mimeType16; |
| 396 base::FilePath fileName; | 368 base::FilePath fileName; |
| 397 if (content::ParseDownloadMetadata( | 369 if (content::ParseDownloadMetadata( |
| 398 dropData_->download_metadata, | 370 dropData_->download_metadata, |
| 399 &mimeType16, | 371 &mimeType16, |
| 400 &fileName, | 372 &fileName, |
| 401 &downloadURL_)) { | 373 &downloadURL_)) { |
| 402 // Generate the file name based on both mime type and proposed file | 374 // Generate the file name based on both mime type and proposed file |
| 403 // name. | 375 // name. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 - (NSImage*)dragImage { | 458 - (NSImage*)dragImage { |
| 487 if (dragImage_) | 459 if (dragImage_) |
| 488 return dragImage_; | 460 return dragImage_; |
| 489 | 461 |
| 490 // Default to returning a generic image. | 462 // Default to returning a generic image. |
| 491 return content::GetContentClient()->GetNativeImageNamed( | 463 return content::GetContentClient()->GetNativeImageNamed( |
| 492 IDR_DEFAULT_FAVICON).ToNSImage(); | 464 IDR_DEFAULT_FAVICON).ToNSImage(); |
| 493 } | 465 } |
| 494 | 466 |
| 495 @end // @implementation WebDragSource (Private) | 467 @end // @implementation WebDragSource (Private) |
| OLD | NEW |