cocoa_dnd_util.mm 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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
  3. // found in the LICENSE file.
  4. #import "ui/base/dragdrop/cocoa_dnd_util.h"
  5. #include "base/check_op.h"
  6. #include "base/strings/sys_string_conversions.h"
  7. #import "third_party/mozilla/NSPasteboard+Utils.h"
  8. #include "url/gurl.h"
  9. namespace ui {
  10. NSString* const kChromeDragDummyPboardType = @"org.chromium.drag-dummy-type";
  11. NSString* const kChromeDragImageHTMLPboardType = @"org.chromium.image-html";
  12. BOOL PopulateURLAndTitleFromPasteboard(GURL* url,
  13. std::u16string* title,
  14. NSPasteboard* pboard,
  15. BOOL convert_filenames) {
  16. CHECK(url);
  17. // Bail out early if there's no URL data.
  18. if (![pboard containsURLDataConvertingTextToURL:YES])
  19. return NO;
  20. // -getURLs:andTitles:convertingFilenames: will already validate URIs so we
  21. // don't need to again. The arrays returned are both of NSStrings.
  22. NSArray* url_array = nil;
  23. NSArray* title_array = nil;
  24. [pboard getURLs:&url_array
  25. andTitles:&title_array
  26. convertingFilenames:convert_filenames
  27. convertingTextToURL:YES];
  28. DCHECK_EQ([url_array count], [title_array count]);
  29. // It's possible that no URLs were actually provided!
  30. if (![url_array count])
  31. return NO;
  32. NSString* url_string = url_array[0];
  33. if ([url_string length]) {
  34. // Check again just to make sure to not assign NULL into a std::string,
  35. // which throws an exception.
  36. const char* utf8_url = [url_string UTF8String];
  37. if (utf8_url) {
  38. *url = GURL(utf8_url);
  39. // Extra paranoia check.
  40. if (title && [title_array count])
  41. *title = base::SysNSStringToUTF16(title_array[0]);
  42. }
  43. }
  44. return YES;
  45. }
  46. } // namespace ui