bookmark_node_data.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_DATA_H_
  5. #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_DATA_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <string>
  9. #include <vector>
  10. #include "base/files/file_path.h"
  11. #include "base/time/time.h"
  12. #include "build/build_config.h"
  13. #include "components/bookmarks/browser/bookmark_node.h"
  14. #include "ui/base/clipboard/clipboard_buffer.h"
  15. #include "url/gurl.h"
  16. #if defined(TOOLKIT_VIEWS)
  17. #include "ui/base/clipboard/clipboard_format_type.h"
  18. #endif
  19. namespace base {
  20. class Pickle;
  21. class PickleIterator;
  22. }
  23. #if defined(TOOLKIT_VIEWS)
  24. namespace ui {
  25. class OSExchangeData;
  26. }
  27. #endif
  28. namespace bookmarks {
  29. class BookmarkModel;
  30. // BookmarkNodeData is used to represent the following:
  31. //
  32. // . A single URL.
  33. // . A single node from the bookmark model.
  34. // . A set of nodes from the bookmark model.
  35. //
  36. // BookmarkNodeData is used by bookmark related views to represent a dragged
  37. // bookmark or bookmarks.
  38. //
  39. // Typical usage when writing data for a drag is:
  40. // BookmarkNodeData data(node_user_is_dragging);
  41. // data.Write(os_exchange_data_for_drag);
  42. //
  43. // Typical usage to read is:
  44. // BookmarkNodeData data;
  45. // if (data.Read(os_exchange_data))
  46. // // data is valid, contents are in elements.
  47. struct BookmarkNodeData {
  48. // Element represents a single node.
  49. struct Element {
  50. Element();
  51. explicit Element(const BookmarkNode* node);
  52. Element(const Element& other);
  53. ~Element();
  54. // If true, this element represents a URL.
  55. bool is_url;
  56. // The URL, only valid if is_url is true.
  57. GURL url;
  58. // Title of the entry, used for both urls and folders.
  59. std::u16string title;
  60. // Date of when this node was created.
  61. base::Time date_added;
  62. // Date of the last modification. Only used for folders.
  63. base::Time date_folder_modified;
  64. // Children, only used for non-URL nodes.
  65. std::vector<Element> children;
  66. // Meta info for the bookmark node.
  67. BookmarkNode::MetaInfoMap meta_info_map;
  68. int64_t id() const { return id_; }
  69. private:
  70. friend struct BookmarkNodeData;
  71. #if !BUILDFLAG(IS_APPLE)
  72. // For reading/writing this Element.
  73. void WriteToPickle(base::Pickle* pickle) const;
  74. bool ReadFromPickle(base::PickleIterator* iterator);
  75. #endif
  76. // ID of the node.
  77. int64_t id_;
  78. };
  79. #if !BUILDFLAG(IS_APPLE)
  80. // The MIME type for the clipboard format for BookmarkNodeData. This type is
  81. // not used on the Mac.
  82. static const char kClipboardFormatString[];
  83. #endif
  84. BookmarkNodeData();
  85. BookmarkNodeData(const BookmarkNodeData& other);
  86. // Created a BookmarkNodeData populated from the arguments.
  87. explicit BookmarkNodeData(const BookmarkNode* node);
  88. explicit BookmarkNodeData(const std::vector<const BookmarkNode*>& nodes);
  89. ~BookmarkNodeData();
  90. #if defined(TOOLKIT_VIEWS)
  91. static const ui::ClipboardFormatType& GetBookmarkFormatType();
  92. #endif
  93. static bool ClipboardContainsBookmarks();
  94. // Reads bookmarks from the given vector.
  95. bool ReadFromVector(const std::vector<const BookmarkNode*>& nodes);
  96. // Creates a single-bookmark DragData from url/title pair.
  97. bool ReadFromTuple(const GURL& url, const std::u16string& title);
  98. // Writes bookmarks to the specified clipboard.
  99. void WriteToClipboard();
  100. // Reads bookmarks from the specified clipboard. Prefers data written via
  101. // WriteToClipboard() but will also attempt to read a plain bookmark.
  102. bool ReadFromClipboard(ui::ClipboardBuffer buffer);
  103. #if defined(TOOLKIT_VIEWS)
  104. // Writes elements to data. If there is only one element and it is a URL
  105. // the URL and title are written to the clipboard in a format other apps can
  106. // use.
  107. // |profile_path| is used to identify which profile the data came from. Use an
  108. // empty path to indicate that the data is not associated with any profile.
  109. void Write(const base::FilePath& profile_path,
  110. ui::OSExchangeData* data) const;
  111. // Restores this data from the clipboard, returning true on success.
  112. bool Read(const ui::OSExchangeData& data);
  113. #endif
  114. #if !BUILDFLAG(IS_APPLE)
  115. // Writes the data for a drag to |pickle|.
  116. void WriteToPickle(const base::FilePath& profile_path,
  117. base::Pickle* pickle) const;
  118. // Reads the data for a drag from a |pickle|.
  119. bool ReadFromPickle(base::Pickle* pickle);
  120. #endif
  121. // Returns the nodes represented by this DragData. If this DragData was
  122. // created from the same profile then the nodes from the model are returned.
  123. // If the nodes can't be found (may have been deleted), an empty vector is
  124. // returned.
  125. std::vector<const BookmarkNode*> GetNodes(
  126. BookmarkModel* model,
  127. const base::FilePath& profile_path) const;
  128. // Convenience for getting the first node. Returns NULL if the data doesn't
  129. // match any nodes or there is more than one node.
  130. const BookmarkNode* GetFirstNode(BookmarkModel* model,
  131. const base::FilePath& profile_path) const;
  132. // Do we contain valid data?
  133. bool is_valid() const { return !elements.empty(); }
  134. // Returns true if there is a single url.
  135. bool has_single_url() const { return size() == 1 && elements[0].is_url; }
  136. // Number of elements.
  137. size_t size() const { return elements.size(); }
  138. // Clears the data.
  139. void Clear();
  140. // Sets |profile_path_|. This is useful for the constructors/readers that
  141. // don't set it. This should only be called if the profile path is not
  142. // already set.
  143. void SetOriginatingProfilePath(const base::FilePath& profile_path);
  144. // Returns true if this data is from the specified profile path.
  145. bool IsFromProfilePath(const base::FilePath& profile_path) const;
  146. // The actual elements written to the clipboard.
  147. std::vector<Element> elements;
  148. private:
  149. // Path of the profile we originated from.
  150. base::FilePath profile_path_;
  151. };
  152. } // namespace bookmarks
  153. #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_DATA_H_