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 #include "components/bookmarks/browser/bookmark_node.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
17 // Whitespace characters to strip from bookmark titles.
18 const base::char16 kInvalidChars
[] = {
20 0x2028, // Line separator
21 0x2029, // Paragraph separator
27 // BookmarkNode ---------------------------------------------------------------
29 const int64
BookmarkNode::kInvalidSyncTransactionVersion
= -1;
31 BookmarkNode::BookmarkNode(const GURL
& url
)
36 BookmarkNode::BookmarkNode(int64 id
, const GURL
& url
)
41 BookmarkNode::~BookmarkNode() {
44 void BookmarkNode::SetTitle(const base::string16
& title
) {
45 // Replace newlines and other problematic whitespace characters in
46 // folder/bookmark names with spaces.
47 base::string16 trimmed_title
;
48 base::ReplaceChars(title
, kInvalidChars
, base::ASCIIToUTF16(" "),
50 ui::TreeNode
<BookmarkNode
>::SetTitle(trimmed_title
);
53 bool BookmarkNode::IsVisible() const {
57 bool BookmarkNode::GetMetaInfo(const std::string
& key
,
58 std::string
* value
) const {
62 MetaInfoMap::const_iterator it
= meta_info_map_
->find(key
);
63 if (it
== meta_info_map_
->end())
70 bool BookmarkNode::SetMetaInfo(const std::string
& key
,
71 const std::string
& value
) {
73 meta_info_map_
.reset(new MetaInfoMap
);
75 MetaInfoMap::iterator it
= meta_info_map_
->find(key
);
76 if (it
== meta_info_map_
->end()) {
77 (*meta_info_map_
)[key
] = value
;
80 // Key already in map, check if the value has changed.
81 if (it
->second
== value
)
87 bool BookmarkNode::DeleteMetaInfo(const std::string
& key
) {
90 bool erased
= meta_info_map_
->erase(key
) != 0;
91 if (meta_info_map_
->empty())
92 meta_info_map_
.reset();
96 void BookmarkNode::SetMetaInfoMap(const MetaInfoMap
& meta_info_map
) {
97 if (meta_info_map
.empty())
98 meta_info_map_
.reset();
100 meta_info_map_
.reset(new MetaInfoMap(meta_info_map
));
103 const BookmarkNode::MetaInfoMap
* BookmarkNode::GetMetaInfoMap() const {
104 return meta_info_map_
.get();
107 void BookmarkNode::Initialize(int64 id
) {
109 type_
= url_
.is_empty() ? FOLDER
: URL
;
110 date_added_
= base::Time::Now();
111 favicon_type_
= favicon_base::INVALID_ICON
;
112 favicon_state_
= INVALID_FAVICON
;
113 favicon_load_task_id_
= base::CancelableTaskTracker::kBadTaskId
;
114 meta_info_map_
.reset();
115 sync_transaction_version_
= kInvalidSyncTransactionVersion
;
118 void BookmarkNode::InvalidateFavicon() {
120 favicon_
= gfx::Image();
121 favicon_type_
= favicon_base::INVALID_ICON
;
122 favicon_state_
= INVALID_FAVICON
;
125 // BookmarkPermanentNode -------------------------------------------------------
127 BookmarkPermanentNode::BookmarkPermanentNode(int64 id
)
128 : BookmarkNode(id
, GURL()),
132 BookmarkPermanentNode::~BookmarkPermanentNode() {
135 bool BookmarkPermanentNode::IsVisible() const {
136 return visible_
|| !empty();
139 } // namespace bookmarks