bookmark_node_data_views.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2014 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. #include "components/bookmarks/browser/bookmark_node_data.h"
  5. #include "base/check.h"
  6. #include "base/no_destructor.h"
  7. #include "base/pickle.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "build/build_config.h"
  10. #include "ui/base/clipboard/clipboard_format_type.h"
  11. #include "ui/base/dragdrop/os_exchange_data.h"
  12. #include "url/url_constants.h"
  13. namespace bookmarks {
  14. // static
  15. const ui::ClipboardFormatType& BookmarkNodeData::GetBookmarkFormatType() {
  16. static const base::NoDestructor<ui::ClipboardFormatType> format(
  17. ui::ClipboardFormatType::GetType(
  18. BookmarkNodeData::kClipboardFormatString));
  19. return *format;
  20. }
  21. void BookmarkNodeData::Write(const base::FilePath& profile_path,
  22. ui::OSExchangeData* data) const {
  23. DCHECK(data);
  24. // If there is only one element and it is a URL, write the URL to the
  25. // clipboard.
  26. if (has_single_url()) {
  27. if (elements[0].url.SchemeIs(url::kJavaScriptScheme)) {
  28. data->SetString(base::UTF8ToUTF16(elements[0].url.spec()));
  29. } else {
  30. data->SetURL(elements[0].url, elements[0].title);
  31. }
  32. }
  33. base::Pickle data_pickle;
  34. WriteToPickle(profile_path, &data_pickle);
  35. data->SetPickledData(GetBookmarkFormatType(), data_pickle);
  36. }
  37. bool BookmarkNodeData::Read(const ui::OSExchangeData& data) {
  38. elements.clear();
  39. profile_path_.clear();
  40. if (data.HasCustomFormat(GetBookmarkFormatType())) {
  41. base::Pickle drag_data_pickle;
  42. if (data.GetPickledData(GetBookmarkFormatType(), &drag_data_pickle)) {
  43. if (!ReadFromPickle(&drag_data_pickle))
  44. return false;
  45. }
  46. } else {
  47. // See if there is a URL on the clipboard.
  48. GURL url;
  49. std::u16string title;
  50. if (data.GetURLAndTitle(ui::FilenameToURLPolicy::CONVERT_FILENAMES, &url,
  51. &title))
  52. ReadFromTuple(url, title);
  53. }
  54. return is_valid();
  55. }
  56. } // namespace bookmarks