Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_node.cc
blobf1a7945d31ecd052c9f88f6059b2be0239d2c86a
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"
7 #include <map>
8 #include <string>
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
13 namespace bookmarks {
15 namespace {
17 // Whitespace characters to strip from bookmark titles.
18 const base::char16 kInvalidChars[] = {
19 '\n', '\r', '\t',
20 0x2028, // Line separator
21 0x2029, // Paragraph separator
25 } // namespace
27 // BookmarkNode ---------------------------------------------------------------
29 const int64 BookmarkNode::kInvalidSyncTransactionVersion = -1;
31 BookmarkNode::BookmarkNode(const GURL& url)
32 : url_(url) {
33 Initialize(0);
36 BookmarkNode::BookmarkNode(int64 id, const GURL& url)
37 : url_(url) {
38 Initialize(id);
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(" "),
49 &trimmed_title);
50 ui::TreeNode<BookmarkNode>::SetTitle(trimmed_title);
53 bool BookmarkNode::IsVisible() const {
54 return true;
57 bool BookmarkNode::GetMetaInfo(const std::string& key,
58 std::string* value) const {
59 if (!meta_info_map_)
60 return false;
62 MetaInfoMap::const_iterator it = meta_info_map_->find(key);
63 if (it == meta_info_map_->end())
64 return false;
66 *value = it->second;
67 return true;
70 bool BookmarkNode::SetMetaInfo(const std::string& key,
71 const std::string& value) {
72 if (!meta_info_map_)
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;
78 return true;
80 // Key already in map, check if the value has changed.
81 if (it->second == value)
82 return false;
83 it->second = value;
84 return true;
87 bool BookmarkNode::DeleteMetaInfo(const std::string& key) {
88 if (!meta_info_map_)
89 return false;
90 bool erased = meta_info_map_->erase(key) != 0;
91 if (meta_info_map_->empty())
92 meta_info_map_.reset();
93 return erased;
96 void BookmarkNode::SetMetaInfoMap(const MetaInfoMap& meta_info_map) {
97 if (meta_info_map.empty())
98 meta_info_map_.reset();
99 else
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) {
108 id_ = 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() {
119 icon_url_ = GURL();
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()),
129 visible_(true) {
132 BookmarkPermanentNode::~BookmarkPermanentNode() {
135 bool BookmarkPermanentNode::IsVisible() const {
136 return visible_ || !empty();
139 } // namespace bookmarks