bookmark_node.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_H_
  5. #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include "base/guid.h"
  11. #include "base/strings/string_piece.h"
  12. #include "base/task/cancelable_task_tracker.h"
  13. #include "base/time/time.h"
  14. #include "components/bookmarks/browser/titled_url_node.h"
  15. #include "ui/base/models/tree_node_model.h"
  16. #include "ui/gfx/image/image.h"
  17. #include "url/gurl.h"
  18. namespace bookmarks {
  19. class BookmarkModel;
  20. // BookmarkNode ---------------------------------------------------------------
  21. // BookmarkNode contains information about a starred entry: title, URL, favicon,
  22. // id and type. BookmarkNodes are returned from BookmarkModel.
  23. class BookmarkNode : public ui::TreeNode<BookmarkNode>, public TitledUrlNode {
  24. public:
  25. enum Type {
  26. URL,
  27. FOLDER,
  28. BOOKMARK_BAR,
  29. OTHER_NODE,
  30. MOBILE
  31. };
  32. enum FaviconState {
  33. INVALID_FAVICON,
  34. LOADING_FAVICON,
  35. LOADED_FAVICON,
  36. };
  37. typedef std::map<std::string, std::string> MetaInfoMap;
  38. // TODO(crbug.com/1026195): Make these constants of type base::GUID once there
  39. // exists a constexpr constructor.
  40. static const char kRootNodeGuid[];
  41. static const char kBookmarkBarNodeGuid[];
  42. static const char kOtherBookmarksNodeGuid[];
  43. static const char kMobileBookmarksNodeGuid[];
  44. static const char kManagedNodeGuid[];
  45. // A bug in sync caused some problematic GUIDs to be produced.
  46. static const char kBannedGuidDueToPastSyncBug[];
  47. // Creates a new node with |id|, |guid| and |url|.
  48. BookmarkNode(int64_t id, const base::GUID& guid, const GURL& url);
  49. BookmarkNode(const BookmarkNode&) = delete;
  50. BookmarkNode& operator=(const BookmarkNode&) = delete;
  51. ~BookmarkNode() override;
  52. // Returns true if the node is a BookmarkPermanentNode (which does not include
  53. // the root).
  54. bool is_permanent_node() const { return is_permanent_node_; }
  55. // Set the node's internal title. Note that this neither invokes observers
  56. // nor updates any bookmark model this node may be in. For that functionality,
  57. // BookmarkModel::SetTitle(..) should be used instead.
  58. void SetTitle(const std::u16string& title) override;
  59. // Returns an unique id for this node.
  60. // For bookmark nodes that are managed by the bookmark model, the IDs are
  61. // persisted across sessions.
  62. int64_t id() const { return id_; }
  63. void set_id(int64_t id) { id_ = id; }
  64. // Returns this node's GUID, which is guaranteed to be valid.
  65. // For bookmark nodes that are managed by the bookmark model, the GUIDs are
  66. // persisted across sessions and stable throughout the lifetime of the
  67. // bookmark.
  68. const base::GUID& guid() const { return guid_; }
  69. const GURL& url() const { return url_; }
  70. void set_url(const GURL& url) { url_ = url; }
  71. // Returns the favicon's URL. Return null if there is no favicon associated
  72. // with this bookmark.
  73. const GURL* icon_url() const { return icon_url_.get(); }
  74. Type type() const { return type_; }
  75. // Returns the time the node was added.
  76. const base::Time& date_added() const { return date_added_; }
  77. void set_date_added(const base::Time& date) { date_added_ = date; }
  78. // Returns the last time the folder was modified. This is only maintained
  79. // for folders (including the bookmark bar and other folder).
  80. const base::Time& date_folder_modified() const {
  81. return date_folder_modified_;
  82. }
  83. void set_date_folder_modified(const base::Time& date) {
  84. date_folder_modified_ = date;
  85. }
  86. // Convenience for testing if this node represents a folder. A folder is a
  87. // node whose type is not URL.
  88. bool is_folder() const { return type_ != URL; }
  89. bool is_url() const { return type_ == URL; }
  90. bool is_favicon_loaded() const { return favicon_state_ == LOADED_FAVICON; }
  91. bool is_favicon_loading() const { return favicon_state_ == LOADING_FAVICON; }
  92. // Accessor method for controlling the visibility of a bookmark node/sub-tree.
  93. // Note that visibility is not propagated down the tree hierarchy so if a
  94. // parent node is marked as invisible, a child node may return "Visible". This
  95. // function is primarily useful when traversing the model to generate a UI
  96. // representation but we may want to suppress some nodes.
  97. virtual bool IsVisible() const;
  98. // Gets/sets/deletes value of |key| in the meta info represented by
  99. // |meta_info_str_|. Return true if key is found in meta info for gets or
  100. // meta info is changed indeed for sets/deletes.
  101. bool GetMetaInfo(const std::string& key, std::string* value) const;
  102. bool SetMetaInfo(const std::string& key, const std::string& value);
  103. bool DeleteMetaInfo(const std::string& key);
  104. void SetMetaInfoMap(const MetaInfoMap& meta_info_map);
  105. // Returns NULL if there are no values in the map.
  106. const MetaInfoMap* GetMetaInfoMap() const;
  107. // TitledUrlNode interface methods.
  108. const std::u16string& GetTitledUrlNodeTitle() const override;
  109. const GURL& GetTitledUrlNodeUrl() const override;
  110. std::vector<base::StringPiece16> GetTitledUrlNodeAncestorTitles()
  111. const override;
  112. // Returns the last time the bookmark was opened. This is only maintained
  113. // for urls (no folders).
  114. base::Time date_last_used() const { return date_last_used_; }
  115. void set_date_last_used(const base::Time date_last_used) {
  116. date_last_used_ = date_last_used;
  117. }
  118. protected:
  119. BookmarkNode(int64_t id,
  120. const base::GUID& guid,
  121. const GURL& url,
  122. Type type,
  123. bool is_permanent_node);
  124. private:
  125. friend class BookmarkModel;
  126. // Called when the favicon becomes invalid.
  127. void InvalidateFavicon();
  128. // Sets the favicon's URL.
  129. void set_icon_url(const GURL& icon_url) {
  130. icon_url_ = std::make_unique<GURL>(icon_url);
  131. }
  132. // Returns the favicon. In nearly all cases you should use the method
  133. // BookmarkModel::GetFavicon rather than this one as it takes care of
  134. // loading the favicon if it isn't already loaded.
  135. const gfx::Image& favicon() const { return favicon_; }
  136. void set_favicon(const gfx::Image& icon) { favicon_ = icon; }
  137. FaviconState favicon_state() const { return favicon_state_; }
  138. void set_favicon_state(FaviconState state) { favicon_state_ = state; }
  139. base::CancelableTaskTracker::TaskId favicon_load_task_id() const {
  140. return favicon_load_task_id_;
  141. }
  142. void set_favicon_load_task_id(base::CancelableTaskTracker::TaskId id) {
  143. favicon_load_task_id_ = id;
  144. }
  145. // The unique identifier for this node.
  146. int64_t id_;
  147. // The GUID for this node. A BookmarkNode GUID is immutable and differs from
  148. // the |id_| in that it is consistent across different clients and
  149. // stable throughout the lifetime of the bookmark, with the exception of nodes
  150. // added to the Managed Bookmarks folder, whose GUIDs are re-assigned at
  151. // start-up every time.
  152. const base::GUID guid_;
  153. // The URL of this node. BookmarkModel maintains maps off this URL, so changes
  154. // to the URL must be done through the BookmarkModel.
  155. GURL url_;
  156. // The type of this node. See enum above.
  157. const Type type_;
  158. // Date of when this node was created.
  159. base::Time date_added_;
  160. // Date of the last modification. Only used for folders.
  161. base::Time date_folder_modified_;
  162. // The favicon of this node.
  163. gfx::Image favicon_;
  164. // The URL of the node's favicon.
  165. std::unique_ptr<GURL> icon_url_;
  166. // The loading state of the favicon.
  167. FaviconState favicon_state_ = INVALID_FAVICON;
  168. // If not base::CancelableTaskTracker::kBadTaskId, it indicates
  169. // we're loading the
  170. // favicon and the task is tracked by CancelableTaskTracker.
  171. base::CancelableTaskTracker::TaskId favicon_load_task_id_ =
  172. base::CancelableTaskTracker::kBadTaskId;
  173. // A map that stores arbitrary meta information about the node.
  174. std::unique_ptr<MetaInfoMap> meta_info_map_;
  175. const bool is_permanent_node_;
  176. base::Time date_last_used_;
  177. };
  178. // BookmarkPermanentNode -------------------------------------------------------
  179. // Node used for the permanent folders (excluding the root).
  180. class BookmarkPermanentNode : public BookmarkNode {
  181. public:
  182. // Permanent nodes are well-known, it's not allowed to create arbitrary ones.
  183. static std::unique_ptr<BookmarkPermanentNode> CreateManagedBookmarks(
  184. int64_t id);
  185. BookmarkPermanentNode(const BookmarkPermanentNode&) = delete;
  186. BookmarkPermanentNode& operator=(const BookmarkPermanentNode&) = delete;
  187. ~BookmarkPermanentNode() override;
  188. // BookmarkNode overrides:
  189. bool IsVisible() const override;
  190. private:
  191. friend class BookmarkLoadDetails;
  192. // Permanent nodes are well-known, it's not allowed to create arbitrary ones.
  193. static std::unique_ptr<BookmarkPermanentNode> CreateBookmarkBar(
  194. int64_t id,
  195. bool visible_when_empty);
  196. static std::unique_ptr<BookmarkPermanentNode> CreateOtherBookmarks(
  197. int64_t id,
  198. bool visible_when_empty);
  199. static std::unique_ptr<BookmarkPermanentNode> CreateMobileBookmarks(
  200. int64_t id,
  201. bool visible_when_empty);
  202. // Constructor is private to disallow the construction of permanent nodes
  203. // other than the well-known ones, see factory methods.
  204. BookmarkPermanentNode(int64_t id,
  205. Type type,
  206. const base::GUID& guid,
  207. const std::u16string& title,
  208. bool visible_when_empty);
  209. const bool visible_when_empty_;
  210. };
  211. // If you are looking for gMock printing via PrintTo(), please check
  212. // bookmark_test_util.h.
  213. } // namespace bookmarks
  214. #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_H_