| 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 "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 using content::PromiseFileFinalizer; | 42 using content::PromiseFileFinalizer; |
| 43 using content::RenderViewHostImpl; | 43 using content::RenderViewHostImpl; |
| 44 using net::FileStream; | 44 using net::FileStream; |
| 45 | 45 |
| 46 namespace { | 46 namespace { |
| 47 | 47 |
| 48 // An unofficial standard pasteboard title type to be provided alongside the | 48 // An unofficial standard pasteboard title type to be provided alongside the |
| 49 // |NSURLPboardType|. | 49 // |NSURLPboardType|. |
| 50 NSString* const kNSURLTitlePboardType = @"public.url-name"; | 50 NSString* const kNSURLTitlePboardType = @"public.url-name"; |
| 51 | 51 |
| 52 // Converts a string16 into a FilePath. Use this method instead of | 52 // Converts a base::string16 into a FilePath. Use this method instead of |
| 53 // -[NSString fileSystemRepresentation] to prevent exceptions from being thrown. | 53 // -[NSString fileSystemRepresentation] to prevent exceptions from being thrown. |
| 54 // See http://crbug.com/78782 for more info. | 54 // See http://crbug.com/78782 for more info. |
| 55 base::FilePath FilePathFromFilename(const string16& filename) { | 55 base::FilePath FilePathFromFilename(const base::string16& filename) { |
| 56 NSString* str = SysUTF16ToNSString(filename); | 56 NSString* str = SysUTF16ToNSString(filename); |
| 57 char buf[MAXPATHLEN]; | 57 char buf[MAXPATHLEN]; |
| 58 if (![str getFileSystemRepresentation:buf maxLength:sizeof(buf)]) | 58 if (![str getFileSystemRepresentation:buf maxLength:sizeof(buf)]) |
| 59 return base::FilePath(); | 59 return base::FilePath(); |
| 60 return base::FilePath(buf); | 60 return base::FilePath(buf); |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Returns a filename appropriate for the drop data | 63 // Returns a filename appropriate for the drop data |
| 64 // TODO(viettrungluu): Refactor to make it common across platforms, | 64 // TODO(viettrungluu): Refactor to make it common across platforms, |
| 65 // and move it somewhere sensible. | 65 // and move it somewhere sensible. |
| 66 base::FilePath GetFileNameFromDragData(const DropData& drop_data) { | 66 base::FilePath GetFileNameFromDragData(const DropData& drop_data) { |
| 67 base::FilePath file_name( | 67 base::FilePath file_name( |
| 68 FilePathFromFilename(drop_data.file_description_filename)); | 68 FilePathFromFilename(drop_data.file_description_filename)); |
| 69 | 69 |
| 70 // Images without ALT text will only have a file extension so we need to | 70 // Images without ALT text will only have a file extension so we need to |
| 71 // synthesize one from the provided extension and URL. | 71 // synthesize one from the provided extension and URL. |
| 72 if (file_name.empty()) { | 72 if (file_name.empty()) { |
| 73 // Retrieve the name from the URL. | 73 // Retrieve the name from the URL. |
| 74 string16 suggested_filename = | 74 base::string16 suggested_filename = |
| 75 net::GetSuggestedFilename(drop_data.url, "", "", "", "", ""); | 75 net::GetSuggestedFilename(drop_data.url, "", "", "", "", ""); |
| 76 const std::string extension = file_name.Extension(); | 76 const std::string extension = file_name.Extension(); |
| 77 file_name = FilePathFromFilename(suggested_filename); | 77 file_name = FilePathFromFilename(suggested_filename); |
| 78 file_name = file_name.ReplaceExtension(extension); | 78 file_name = file_name.ReplaceExtension(extension); |
| 79 } | 79 } |
| 80 | 80 |
| 81 return file_name; | 81 return file_name; |
| 82 } | 82 } |
| 83 | 83 |
| 84 // This helper's sole task is to write out data for a promised file; the caller | 84 // This helper's sole task is to write out data for a promised file; the caller |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 contentsView_ = nil; | 140 contentsView_ = nil; |
| 141 } | 141 } |
| 142 | 142 |
| 143 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal { | 143 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal { |
| 144 return dragOperationMask_; | 144 return dragOperationMask_; |
| 145 } | 145 } |
| 146 | 146 |
| 147 - (void)lazyWriteToPasteboard:(NSPasteboard*)pboard forType:(NSString*)type { | 147 - (void)lazyWriteToPasteboard:(NSPasteboard*)pboard forType:(NSString*)type { |
| 148 // NSHTMLPboardType requires the character set to be declared. Otherwise, it | 148 // NSHTMLPboardType requires the character set to be declared. Otherwise, it |
| 149 // assumes US-ASCII. Awesome. | 149 // assumes US-ASCII. Awesome. |
| 150 const string16 kHtmlHeader = ASCIIToUTF16( | 150 const base::string16 kHtmlHeader = ASCIIToUTF16( |
| 151 "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"); | 151 "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"); |
| 152 | 152 |
| 153 // Be extra paranoid; avoid crashing. | 153 // Be extra paranoid; avoid crashing. |
| 154 if (!dropData_) { | 154 if (!dropData_) { |
| 155 NOTREACHED(); | 155 NOTREACHED(); |
| 156 return; | 156 return; |
| 157 } | 157 } |
| 158 | 158 |
| 159 // HTML. | 159 // HTML. |
| 160 if ([type isEqualToString:NSHTMLPboardType] || | 160 if ([type isEqualToString:NSHTMLPboardType] || |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 // MIME type. | 379 // MIME type. |
| 380 std::string mimeType; | 380 std::string mimeType; |
| 381 | 381 |
| 382 // File. | 382 // File. |
| 383 if (!dropData_->file_contents.empty() || | 383 if (!dropData_->file_contents.empty() || |
| 384 !dropData_->download_metadata.empty()) { | 384 !dropData_->download_metadata.empty()) { |
| 385 if (dropData_->download_metadata.empty()) { | 385 if (dropData_->download_metadata.empty()) { |
| 386 downloadFileName_ = GetFileNameFromDragData(*dropData_); | 386 downloadFileName_ = GetFileNameFromDragData(*dropData_); |
| 387 net::GetMimeTypeFromExtension(downloadFileName_.Extension(), &mimeType); | 387 net::GetMimeTypeFromExtension(downloadFileName_.Extension(), &mimeType); |
| 388 } else { | 388 } else { |
| 389 string16 mimeType16; | 389 base::string16 mimeType16; |
| 390 base::FilePath fileName; | 390 base::FilePath fileName; |
| 391 if (content::ParseDownloadMetadata( | 391 if (content::ParseDownloadMetadata( |
| 392 dropData_->download_metadata, | 392 dropData_->download_metadata, |
| 393 &mimeType16, | 393 &mimeType16, |
| 394 &fileName, | 394 &fileName, |
| 395 &downloadURL_)) { | 395 &downloadURL_)) { |
| 396 // Generate the file name based on both mime type and proposed file | 396 // Generate the file name based on both mime type and proposed file |
| 397 // name. | 397 // name. |
| 398 std::string defaultName = | 398 std::string defaultName = |
| 399 content::GetContentClient()->browser()->GetDefaultDownloadName(); | 399 content::GetContentClient()->browser()->GetDefaultDownloadName(); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 - (NSImage*)dragImage { | 480 - (NSImage*)dragImage { |
| 481 if (dragImage_) | 481 if (dragImage_) |
| 482 return dragImage_; | 482 return dragImage_; |
| 483 | 483 |
| 484 // Default to returning a generic image. | 484 // Default to returning a generic image. |
| 485 return content::GetContentClient()->GetNativeImageNamed( | 485 return content::GetContentClient()->GetNativeImageNamed( |
| 486 IDR_DEFAULT_FAVICON).ToNSImage(); | 486 IDR_DEFAULT_FAVICON).ToNSImage(); |
| 487 } | 487 } |
| 488 | 488 |
| 489 @end // @implementation WebDragSource (Private) | 489 @end // @implementation WebDragSource (Private) |
| OLD | NEW |