Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_node.cc
blobcd9fe0e8c090593e1540c16c252c6c903f798730
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 {
15 // Whitespace characters to strip from bookmark titles.
16 const base::char16 kInvalidChars[] = {
17 '\n', '\r', '\t',
18 0x2028, // Line separator
19 0x2029, // Paragraph separator
23 } // namespace
25 // BookmarkNode ---------------------------------------------------------------
27 const int64 BookmarkNode::kInvalidSyncTransactionVersion = -1;
29 BookmarkNode::BookmarkNode(const GURL& url)
30 : url_(url) {
31 Initialize(0);
34 BookmarkNode::BookmarkNode(int64 id, const GURL& url)
35 : url_(url) {
36 Initialize(id);
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(" "),
47 &trimmed_title);
48 ui::TreeNode<BookmarkNode>::SetTitle(trimmed_title);
51 bool BookmarkNode::IsVisible() const {
52 return true;
55 bool BookmarkNode::GetMetaInfo(const std::string& key,
56 std::string* value) const {
57 if (!meta_info_map_)
58 return false;
60 MetaInfoMap::const_iterator it = meta_info_map_->find(key);
61 if (it == meta_info_map_->end())
62 return false;
64 *value = it->second;
65 return true;
68 bool BookmarkNode::SetMetaInfo(const std::string& key,
69 const std::string& value) {
70 if (!meta_info_map_)
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;
76 return true;
78 // Key already in map, check if the value has changed.
79 if (it->second == value)
80 return false;
81 it->second = value;
82 return true;
85 bool BookmarkNode::DeleteMetaInfo(const std::string& key) {
86 if (!meta_info_map_)
87 return false;
88 bool erased = meta_info_map_->erase(key) != 0;
89 if (meta_info_map_->empty())
90 meta_info_map_.reset();
91 return erased;
94 void BookmarkNode::SetMetaInfoMap(const MetaInfoMap& meta_info_map) {
95 if (meta_info_map.empty())
96 meta_info_map_.reset();
97 else
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) {
106 id_ = 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() {
117 icon_url_ = GURL();
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()),
127 visible_(true) {
130 BookmarkPermanentNode::~BookmarkPermanentNode() {
133 bool BookmarkPermanentNode::IsVisible() const {
134 return visible_ || !empty();