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.
5 #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_H_
6 #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/task/cancelable_task_tracker.h"
10 #include "base/time/time.h"
11 #include "components/favicon_base/favicon_types.h"
12 #include "ui/base/models/tree_node_model.h"
13 #include "ui/gfx/image/image.h"
18 // BookmarkNode ---------------------------------------------------------------
20 // BookmarkNode contains information about a starred entry: title, URL, favicon,
21 // id and type. BookmarkNodes are returned from BookmarkModel.
22 class BookmarkNode
: public ui::TreeNode
<BookmarkNode
> {
38 typedef std::map
<std::string
, std::string
> MetaInfoMap
;
40 static const int64 kInvalidSyncTransactionVersion
;
42 // Creates a new node with an id of 0 and |url|.
43 explicit BookmarkNode(const GURL
& url
);
44 // Creates a new node with |id| and |url|.
45 BookmarkNode(int64 id
, const GURL
& url
);
47 virtual ~BookmarkNode();
49 // Set the node's internal title. Note that this neither invokes observers
50 // nor updates any bookmark model this node may be in. For that functionality,
51 // BookmarkModel::SetTitle(..) should be used instead.
52 virtual void SetTitle(const base::string16
& title
) OVERRIDE
;
54 // Returns an unique id for this node.
55 // For bookmark nodes that are managed by the bookmark model, the IDs are
56 // persisted across sessions.
57 int64
id() const { return id_
; }
58 void set_id(int64 id
) { id_
= id
; }
60 const GURL
& url() const { return url_
; }
61 void set_url(const GURL
& url
) { url_
= url
; }
63 // Returns the favicon's URL. Returns an empty URL if there is no favicon
64 // associated with this bookmark.
65 const GURL
& icon_url() const { return icon_url_
; }
67 Type
type() const { return type_
; }
68 void set_type(Type type
) { type_
= type
; }
70 // Returns the time the node was added.
71 const base::Time
& date_added() const { return date_added_
; }
72 void set_date_added(const base::Time
& date
) { date_added_
= date
; }
74 // Returns the last time the folder was modified. This is only maintained
75 // for folders (including the bookmark bar and other folder).
76 const base::Time
& date_folder_modified() const {
77 return date_folder_modified_
;
79 void set_date_folder_modified(const base::Time
& date
) {
80 date_folder_modified_
= date
;
83 // Convenience for testing if this node represents a folder. A folder is a
84 // node whose type is not URL.
85 bool is_folder() const { return type_
!= URL
; }
86 bool is_url() const { return type_
== URL
; }
88 bool is_favicon_loaded() const { return favicon_state_
== LOADED_FAVICON
; }
90 // Accessor method for controlling the visibility of a bookmark node/sub-tree.
91 // Note that visibility is not propagated down the tree hierarchy so if a
92 // parent node is marked as invisible, a child node may return "Visible". This
93 // function is primarily useful when traversing the model to generate a UI
94 // representation but we may want to suppress some nodes.
95 virtual bool IsVisible() const;
97 // Gets/sets/deletes value of |key| in the meta info represented by
98 // |meta_info_str_|. Return true if key is found in meta info for gets or
99 // meta info is changed indeed for sets/deletes.
100 bool GetMetaInfo(const std::string
& key
, std::string
* value
) const;
101 bool SetMetaInfo(const std::string
& key
, const std::string
& value
);
102 bool DeleteMetaInfo(const std::string
& key
);
103 void SetMetaInfoMap(const MetaInfoMap
& meta_info_map
);
104 // Returns NULL if there are no values in the map.
105 const MetaInfoMap
* GetMetaInfoMap() const;
107 void set_sync_transaction_version(int64 sync_transaction_version
) {
108 sync_transaction_version_
= sync_transaction_version
;
110 int64
sync_transaction_version() const {
111 return sync_transaction_version_
;
114 // TODO(sky): Consider adding last visit time here, it'll greatly simplify
115 // HistoryContentsProvider.
118 friend class BookmarkModel
;
120 // A helper function to initialize various fields during construction.
121 void Initialize(int64 id
);
123 // Called when the favicon becomes invalid.
124 void InvalidateFavicon();
126 // Sets the favicon's URL.
127 void set_icon_url(const GURL
& icon_url
) {
128 icon_url_
= icon_url
;
131 // Returns the favicon. In nearly all cases you should use the method
132 // BookmarkModel::GetFavicon rather than this one as it takes care of
133 // loading the favicon if it isn't already loaded.
134 const gfx::Image
& favicon() const { return favicon_
; }
135 void set_favicon(const gfx::Image
& icon
) { favicon_
= icon
; }
137 favicon_base::IconType
favicon_type() const { return favicon_type_
; }
138 void set_favicon_type(favicon_base::IconType type
) { favicon_type_
= type
; }
140 FaviconState
favicon_state() const { return favicon_state_
; }
141 void set_favicon_state(FaviconState state
) { favicon_state_
= state
; }
143 base::CancelableTaskTracker::TaskId
favicon_load_task_id() const {
144 return favicon_load_task_id_
;
146 void set_favicon_load_task_id(base::CancelableTaskTracker::TaskId id
) {
147 favicon_load_task_id_
= id
;
150 // The unique identifier for this node.
153 // The URL of this node. BookmarkModel maintains maps off this URL, so changes
154 // to the URL must be done through the BookmarkModel.
157 // The type of this node. See enum above.
160 // Date of when this node was created.
161 base::Time date_added_
;
163 // Date of the last modification. Only used for folders.
164 base::Time date_folder_modified_
;
166 // The favicon of this node.
169 // The type of favicon currently loaded.
170 favicon_base::IconType favicon_type_
;
172 // The URL of the node's favicon.
175 // The loading state of the favicon.
176 FaviconState favicon_state_
;
178 // If not base::CancelableTaskTracker::kBadTaskId, it indicates
180 // favicon and the task is tracked by CancelabelTaskTracker.
181 base::CancelableTaskTracker::TaskId favicon_load_task_id_
;
183 // A map that stores arbitrary meta information about the node.
184 scoped_ptr
<MetaInfoMap
> meta_info_map_
;
186 // The sync transaction version. Defaults to kInvalidSyncTransactionVersion.
187 int64 sync_transaction_version_
;
189 DISALLOW_COPY_AND_ASSIGN(BookmarkNode
);
192 // BookmarkPermanentNode -------------------------------------------------------
194 // Node used for the permanent folders (excluding the root).
195 class BookmarkPermanentNode
: public BookmarkNode
{
197 explicit BookmarkPermanentNode(int64 id
);
198 virtual ~BookmarkPermanentNode();
200 // WARNING: this code is used for other projects. Contact noyau@ for details.
201 void set_visible(bool value
) { visible_
= value
; }
203 // BookmarkNode overrides:
204 virtual bool IsVisible() const OVERRIDE
;
209 DISALLOW_COPY_AND_ASSIGN(BookmarkPermanentNode
);
212 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_H_