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"
15 // Whitespace characters to strip from bookmark titles.
16 const base::char16 kInvalidChars
[] = {
18 0x2028, // Line separator
19 0x2029, // Paragraph separator
25 // BookmarkNode ---------------------------------------------------------------
27 const int64
BookmarkNode::kInvalidSyncTransactionVersion
= -1;
29 BookmarkNode::BookmarkNode(const GURL
& url
)
34 BookmarkNode::BookmarkNode(int64 id
, const GURL
& url
)
39 BookmarkNode::~BookmarkNode() {
42 void BookmarkNode::SetTitle(const base::string16
& title
) {
43 // Replace newlines and other problematic whitespace characters in
44 // folder/bookmark names with spaces.
45 base::string16 trimmed_title
;
46 base::ReplaceChars(title
, kInvalidChars
, base::ASCIIToUTF16(" "),
48 ui::TreeNode
<BookmarkNode
>::SetTitle(trimmed_title
);
51 bool BookmarkNode::IsVisible() const {
55 bool BookmarkNode::GetMetaInfo(const std::string
& key
,
56 std::string
* value
) const {
60 MetaInfoMap::const_iterator it
= meta_info_map_
->find(key
);
61 if (it
== meta_info_map_
->end())
68 bool BookmarkNode::SetMetaInfo(const std::string
& key
,
69 const std::string
& value
) {
71 meta_info_map_
.reset(new MetaInfoMap
);
73 MetaInfoMap::iterator it
= meta_info_map_
->find(key
);
74 if (it
== meta_info_map_
->end()) {
75 (*meta_info_map_
)[key
] = value
;
78 // Key already in map, check if the value has changed.
79 if (it
->second
== value
)
85 bool BookmarkNode::DeleteMetaInfo(const std::string
& key
) {
88 bool erased
= meta_info_map_
->erase(key
) != 0;
89 if (meta_info_map_
->empty())
90 meta_info_map_
.reset();
94 void BookmarkNode::SetMetaInfoMap(const MetaInfoMap
& meta_info_map
) {
95 if (meta_info_map
.empty())
96 meta_info_map_
.reset();
98 meta_info_map_
.reset(new MetaInfoMap(meta_info_map
));
101 const BookmarkNode::MetaInfoMap
* BookmarkNode::GetMetaInfoMap() const {
102 return meta_info_map_
.get();
105 void BookmarkNode::Initialize(int64 id
) {
107 type_
= url_
.is_empty() ? FOLDER
: URL
;
108 date_added_
= base::Time::Now();
109 favicon_type_
= favicon_base::INVALID_ICON
;
110 favicon_state_
= INVALID_FAVICON
;
111 favicon_load_task_id_
= base::CancelableTaskTracker::kBadTaskId
;
112 meta_info_map_
.reset();
113 sync_transaction_version_
= kInvalidSyncTransactionVersion
;
116 void BookmarkNode::InvalidateFavicon() {
118 favicon_
= gfx::Image();
119 favicon_type_
= favicon_base::INVALID_ICON
;
120 favicon_state_
= INVALID_FAVICON
;
123 // BookmarkPermanentNode -------------------------------------------------------
125 BookmarkPermanentNode::BookmarkPermanentNode(int64 id
)
126 : BookmarkNode(id
, GURL()),
130 BookmarkPermanentNode::~BookmarkPermanentNode() {
133 bool BookmarkPermanentNode::IsVisible() const {
134 return visible_
|| !empty();