1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/bookmarks/browser/bookmark_node.h"
#include <map>
#include <string>
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
namespace bookmarks {
namespace {
// Whitespace characters to strip from bookmark titles.
const base::char16 kInvalidChars[] = {
'\n', '\r', '\t',
0x2028, // Line separator
0x2029, // Paragraph separator
0
};
} // namespace
// BookmarkNode ---------------------------------------------------------------
const int64 BookmarkNode::kInvalidSyncTransactionVersion = -1;
BookmarkNode::BookmarkNode(const GURL& url)
: url_(url) {
Initialize(0);
}
BookmarkNode::BookmarkNode(int64 id, const GURL& url)
: url_(url) {
Initialize(id);
}
BookmarkNode::~BookmarkNode() {
}
void BookmarkNode::SetTitle(const base::string16& title) {
// Replace newlines and other problematic whitespace characters in
// folder/bookmark names with spaces.
base::string16 trimmed_title;
base::ReplaceChars(title, kInvalidChars, base::ASCIIToUTF16(" "),
&trimmed_title);
ui::TreeNode<BookmarkNode>::SetTitle(trimmed_title);
}
bool BookmarkNode::IsVisible() const {
return true;
}
bool BookmarkNode::GetMetaInfo(const std::string& key,
std::string* value) const {
if (!meta_info_map_)
return false;
MetaInfoMap::const_iterator it = meta_info_map_->find(key);
if (it == meta_info_map_->end())
return false;
*value = it->second;
return true;
}
bool BookmarkNode::SetMetaInfo(const std::string& key,
const std::string& value) {
if (!meta_info_map_)
meta_info_map_.reset(new MetaInfoMap);
MetaInfoMap::iterator it = meta_info_map_->find(key);
if (it == meta_info_map_->end()) {
(*meta_info_map_)[key] = value;
return true;
}
// Key already in map, check if the value has changed.
if (it->second == value)
return false;
it->second = value;
return true;
}
bool BookmarkNode::DeleteMetaInfo(const std::string& key) {
if (!meta_info_map_)
return false;
bool erased = meta_info_map_->erase(key) != 0;
if (meta_info_map_->empty())
meta_info_map_.reset();
return erased;
}
void BookmarkNode::SetMetaInfoMap(const MetaInfoMap& meta_info_map) {
if (meta_info_map.empty())
meta_info_map_.reset();
else
meta_info_map_.reset(new MetaInfoMap(meta_info_map));
}
const BookmarkNode::MetaInfoMap* BookmarkNode::GetMetaInfoMap() const {
return meta_info_map_.get();
}
void BookmarkNode::Initialize(int64 id) {
id_ = id;
type_ = url_.is_empty() ? FOLDER : URL;
date_added_ = base::Time::Now();
favicon_type_ = favicon_base::INVALID_ICON;
favicon_state_ = INVALID_FAVICON;
favicon_load_task_id_ = base::CancelableTaskTracker::kBadTaskId;
meta_info_map_.reset();
sync_transaction_version_ = kInvalidSyncTransactionVersion;
}
void BookmarkNode::InvalidateFavicon() {
icon_url_ = GURL();
favicon_ = gfx::Image();
favicon_type_ = favicon_base::INVALID_ICON;
favicon_state_ = INVALID_FAVICON;
}
// BookmarkPermanentNode -------------------------------------------------------
BookmarkPermanentNode::BookmarkPermanentNode(int64 id)
: BookmarkNode(id, GURL()),
visible_(true) {
}
BookmarkPermanentNode::~BookmarkPermanentNode() {
}
bool BookmarkPermanentNode::IsVisible() const {
return visible_ || !empty();
}
} // namespace bookmarks
|