OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/enhanced_bookmarks/metadata_accessor.h" | 5 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h" |
6 | 6 |
7 #include <iomanip> | 7 #include <iomanip> |
| 8 #include <sstream> |
8 | 9 |
9 #include "base/base64.h" | 10 #include "base/base64.h" |
| 11 #include "base/logging.h" |
10 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
11 #include "components/bookmarks/browser/bookmark_model.h" | 13 #include "components/bookmarks/browser/bookmark_model.h" |
| 14 #include "components/bookmarks/browser/bookmark_node.h" |
12 #include "components/enhanced_bookmarks/proto/metadata.pb.h" | 15 #include "components/enhanced_bookmarks/proto/metadata.pb.h" |
13 #include "ui/base/models/tree_node_iterator.h" | 16 #include "url/gurl.h" |
14 | |
15 using namespace image::collections; | |
16 | 17 |
17 namespace { | 18 namespace { |
| 19 const char* kBookmarkBarId = "f_bookmarks_bar"; |
| 20 |
| 21 const char* kIdKey = "stars.id"; |
| 22 const char* kImageDataKey = "stars.imageData"; |
| 23 const char* kNoteKey = "stars.note"; |
| 24 const char* kPageDataKey = "stars.pageData"; |
| 25 const char* kUserEditKey = "stars.userEdit"; |
| 26 const char* kVersionKey = "stars.version"; |
| 27 |
| 28 const char* kFolderPrefix = "ebf_"; |
| 29 const char* kBookmarkPrefix = "ebc_"; |
18 | 30 |
19 // Helper method for working with bookmark metainfo. | 31 // Helper method for working with bookmark metainfo. |
20 std::string DataForMetaInfoField(const BookmarkNode* node, | 32 std::string DataForMetaInfoField(const BookmarkNode* node, |
21 const std::string& field) { | 33 const std::string& field) { |
22 const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap(); | 34 std::string value; |
23 if (!map) | 35 if (!node->GetMetaInfo(field, &value)) |
24 return ""; | 36 return std::string(); |
25 | |
26 BookmarkNode::MetaInfoMap::const_iterator it = map->find(field); | |
27 if (it == map->end()) | |
28 return ""; | |
29 | 37 |
30 std::string decoded; | 38 std::string decoded; |
31 bool result = base::Base64Decode((*it).second, &decoded); | 39 if (!base::Base64Decode(value, &decoded)) |
32 if (!result) | 40 return std::string(); |
33 return ""; | |
34 | 41 |
35 return decoded; | 42 return decoded; |
36 } | 43 } |
37 | 44 |
38 // Sets a new remote id on a bookmark. | |
39 std::string SetRemoteIdOnBookmark(BookmarkModel* bookmark_model, | |
40 const BookmarkNode* node) { | |
41 // Generate 16 digit hex string random id. | |
42 std::stringstream random_id; | |
43 random_id << std::hex << std::setfill('0') << std::setw(16); | |
44 random_id << base::RandUint64() << base::RandUint64(); | |
45 std::string random_id_str = random_id.str(); | |
46 bookmark_model->SetNodeMetaInfo( | |
47 node, enhanced_bookmarks::kIdDataKey, random_id_str); | |
48 return random_id_str; | |
49 } | |
50 | |
51 // Helper method for working with ImageData_ImageInfo. | 45 // Helper method for working with ImageData_ImageInfo. |
52 bool PopulateImageData(const ImageData_ImageInfo& info, | 46 bool PopulateImageData(const image::collections::ImageData_ImageInfo& info, |
53 GURL* out_url, | 47 GURL* out_url, |
54 int* width, | 48 int* width, |
55 int* height) { | 49 int* height) { |
56 if (!info.has_url() || !info.has_width() || !info.has_height()) | 50 if (!info.has_url() || !info.has_width() || !info.has_height()) |
57 return false; | 51 return false; |
58 | 52 |
59 GURL url(info.url()); | 53 GURL url(info.url()); |
60 if (!url.is_valid()) | 54 if (!url.is_valid()) |
61 return false; | 55 return false; |
62 | 56 |
63 *out_url = url; | 57 *out_url = url; |
64 *width = info.width(); | 58 *width = info.width(); |
65 *height = info.height(); | 59 *height = info.height(); |
66 return true; | 60 return true; |
67 } | 61 } |
68 | 62 |
| 63 // Generate a random remote id, with a prefix that depends on whether the node |
| 64 // is a folder or a bookmark. |
| 65 std::string GenerateRemoteId(bool is_folder) { |
| 66 std::stringstream random_id; |
| 67 // Add prefix depending on whether the node is a folder or not. |
| 68 if (is_folder) |
| 69 random_id << kFolderPrefix; |
| 70 else |
| 71 random_id << kBookmarkPrefix; |
| 72 |
| 73 // Generate 32 digit hex string random suffix. |
| 74 random_id << std::hex << std::setfill('0') << std::setw(16); |
| 75 random_id << base::RandUint64() << base::RandUint64(); |
| 76 return random_id.str(); |
| 77 } |
69 } // namespace | 78 } // namespace |
70 | 79 |
71 namespace enhanced_bookmarks { | 80 namespace enhanced_bookmarks { |
72 | 81 |
73 const char* kPageDataKey = "stars.pageData"; | 82 EnhancedBookmarkModel::EnhancedBookmarkModel(BookmarkModel* bookmark_model, |
74 const char* kImageDataKey = "stars.imageData"; | 83 const std::string& version) |
75 const char* kIdDataKey = "stars.id"; | 84 : bookmark_model_(bookmark_model), version_(version) { |
76 const char* kNoteKey = "stars.note"; | |
77 | |
78 std::string RemoteIdFromBookmark(BookmarkModel* bookmark_model, | |
79 const BookmarkNode* node) { | |
80 const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap(); | |
81 if (!map) | |
82 return SetRemoteIdOnBookmark(bookmark_model, node); | |
83 | |
84 BookmarkNode::MetaInfoMap::const_iterator it = map->find(kIdDataKey); | |
85 if (it == map->end()) | |
86 return SetRemoteIdOnBookmark(bookmark_model, node); | |
87 | |
88 DCHECK(it->second.length()); | |
89 return it->second; | |
90 } | 85 } |
91 | 86 |
92 void SetDescriptionForBookmark(BookmarkModel* bookmark_model, | 87 EnhancedBookmarkModel::~EnhancedBookmarkModel() { |
93 const BookmarkNode* node, | |
94 const std::string& description) { | |
95 bookmark_model->SetNodeMetaInfo(node, kNoteKey, description); | |
96 } | 88 } |
97 | 89 |
98 std::string DescriptionFromBookmark(const BookmarkNode* node) { | 90 // Moves |node| to |new_parent| and inserts it at the given |index|. |
99 const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap(); | 91 void EnhancedBookmarkModel::Move(const BookmarkNode* node, |
100 if (!map) | 92 const BookmarkNode* new_parent, |
101 return ""; | 93 int index) { |
| 94 // TODO(rfevang): Update meta info placement fields. |
| 95 bookmark_model_->Move(node, new_parent, index); |
| 96 } |
102 | 97 |
| 98 // Adds a new folder node at the specified position. |
| 99 const BookmarkNode* EnhancedBookmarkModel::AddFolder( |
| 100 const BookmarkNode* parent, |
| 101 int index, |
| 102 const base::string16& title) { |
| 103 BookmarkNode::MetaInfoMap meta_info; |
| 104 meta_info[kIdKey] = GenerateRemoteId(true); |
| 105 |
| 106 // TODO(rfevang): Set meta info placement fields. |
| 107 return bookmark_model_->AddFolderWithMetaInfo( |
| 108 parent, index, title, &meta_info); |
| 109 } |
| 110 |
| 111 // Adds a url at the specified position. |
| 112 const BookmarkNode* EnhancedBookmarkModel::AddURL( |
| 113 const BookmarkNode* parent, |
| 114 int index, |
| 115 const base::string16& title, |
| 116 const GURL& url, |
| 117 const base::Time& creation_time) { |
| 118 BookmarkNode::MetaInfoMap meta_info; |
| 119 meta_info[kIdKey] = GenerateRemoteId(false); |
| 120 |
| 121 // TODO(rfevang): Set meta info placement fields. |
| 122 return bookmark_model_->AddURLWithCreationTimeAndMetaInfo( |
| 123 parent, index, title, url, creation_time, &meta_info); |
| 124 } |
| 125 |
| 126 std::string EnhancedBookmarkModel::GetRemoteId(const BookmarkNode* node) { |
| 127 if (node == bookmark_model_->bookmark_bar_node()) |
| 128 return kBookmarkBarId; |
| 129 |
| 130 // Permanent nodes other than the bookmarks bar don't have ids. |
| 131 DCHECK(!bookmark_model_->is_permanent_node(node)); |
| 132 |
| 133 std::string id; |
| 134 if (!node->GetMetaInfo(kIdKey, &id) || id.empty()) |
| 135 return SetRemoteId(node); |
| 136 return id; |
| 137 } |
| 138 |
| 139 std::string EnhancedBookmarkModel::SetRemoteId(const BookmarkNode* node) { |
| 140 std::string remote_id = GenerateRemoteId(node->is_folder()); |
| 141 SetMetaInfo(node, kIdKey, remote_id, false); |
| 142 return remote_id; |
| 143 } |
| 144 |
| 145 void EnhancedBookmarkModel::SetDescription(const BookmarkNode* node, |
| 146 const std::string& description) { |
| 147 SetMetaInfo(node, kNoteKey, description, true); |
| 148 } |
| 149 |
| 150 std::string EnhancedBookmarkModel::GetDescription(const BookmarkNode* node) { |
103 // First, look for a custom note set by the user. | 151 // First, look for a custom note set by the user. |
104 BookmarkNode::MetaInfoMap::const_iterator it = map->find(kNoteKey); | 152 std::string description; |
105 if (it != map->end() && it->second != "") | 153 if (node->GetMetaInfo(kNoteKey, &description) && !description.empty()) |
106 return it->second; | 154 return description; |
107 | 155 |
108 // If none are present, return the snippet. | 156 // If none are present, return the snippet. |
109 return SnippetFromBookmark(node); | 157 return GetSnippet(node); |
110 } | 158 } |
111 | 159 |
112 bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model, | 160 bool EnhancedBookmarkModel::SetOriginalImage(const BookmarkNode* node, |
113 const BookmarkNode* node, | 161 const GURL& url, |
114 const GURL& url, | 162 int width, |
115 int width, | 163 int height) { |
116 int height) { | 164 DCHECK(node->is_url()); |
117 DCHECK(url.is_valid()); | 165 DCHECK(url.is_valid()); |
118 | 166 |
119 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); | 167 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); |
120 ImageData data; | 168 image::collections::ImageData data; |
121 | 169 |
122 // Try to populate the imageData with the existing data. | 170 // Try to populate the imageData with the existing data. |
123 if (decoded != "") { | 171 if (decoded != "") { |
124 // If the parsing fails, something is wrong. Immediately fail. | 172 // If the parsing fails, something is wrong. Immediately fail. |
125 bool result = data.ParseFromString(decoded); | 173 bool result = data.ParseFromString(decoded); |
126 if (!result) | 174 if (!result) |
127 return false; | 175 return false; |
128 } | 176 } |
129 | 177 |
130 scoped_ptr<ImageData_ImageInfo> info(new ImageData_ImageInfo); | 178 scoped_ptr<image::collections::ImageData_ImageInfo> info( |
| 179 new image::collections::ImageData_ImageInfo); |
131 info->set_url(url.spec()); | 180 info->set_url(url.spec()); |
132 info->set_width(width); | 181 info->set_width(width); |
133 info->set_height(height); | 182 info->set_height(height); |
134 data.set_allocated_original_info(info.release()); | 183 data.set_allocated_original_info(info.release()); |
135 | 184 |
136 std::string output; | 185 std::string output; |
137 bool result = data.SerializePartialToString(&output); | 186 bool result = data.SerializePartialToString(&output); |
138 if (!result) | 187 if (!result) |
139 return false; | 188 return false; |
140 | 189 |
141 std::string encoded; | 190 std::string encoded; |
142 base::Base64Encode(output, &encoded); | 191 base::Base64Encode(output, &encoded); |
143 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded); | 192 SetMetaInfo(node, kImageDataKey, encoded, true); |
144 // Ensure that the bookmark has a stars.id, to trigger the server processing. | 193 // Ensure that the bookmark has a stars.id, to trigger the server processing. |
145 RemoteIdFromBookmark(bookmark_model, node); | 194 GetRemoteId(node); |
146 return true; | 195 return true; |
147 } | 196 } |
148 | 197 |
149 bool OriginalImageFromBookmark(const BookmarkNode* node, | 198 bool EnhancedBookmarkModel::GetOriginalImage(const BookmarkNode* node, |
150 GURL* url, | 199 GURL* url, |
151 int* width, | 200 int* width, |
152 int* height) { | 201 int* height) { |
153 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); | 202 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); |
154 if (decoded == "") | 203 if (decoded == "") |
155 return false; | 204 return false; |
156 | 205 |
157 ImageData data; | 206 image::collections::ImageData data; |
158 bool result = data.ParseFromString(decoded); | 207 bool result = data.ParseFromString(decoded); |
159 if (!result) | 208 if (!result) |
160 return false; | 209 return false; |
161 | 210 |
162 if (!data.has_original_info()) | 211 if (!data.has_original_info()) |
163 return false; | 212 return false; |
164 | 213 |
165 return PopulateImageData(data.original_info(), url, width, height); | 214 return PopulateImageData(data.original_info(), url, width, height); |
166 } | 215 } |
167 | 216 |
168 bool ThumbnailImageFromBookmark(const BookmarkNode* node, | 217 bool EnhancedBookmarkModel::GetThumbnailImage(const BookmarkNode* node, |
169 GURL* url, | 218 GURL* url, |
170 int* width, | 219 int* width, |
171 int* height) { | 220 int* height) { |
172 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); | 221 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); |
173 if (decoded == "") | 222 if (decoded == "") |
174 return false; | 223 return false; |
175 | 224 |
176 ImageData data; | 225 image::collections::ImageData data; |
177 bool result = data.ParseFromString(decoded); | 226 bool result = data.ParseFromString(decoded); |
178 if (!result) | 227 if (!result) |
179 return false; | 228 return false; |
180 | 229 |
181 if (!data.has_thumbnail_info()) | 230 if (!data.has_thumbnail_info()) |
182 return false; | 231 return false; |
183 | 232 |
184 return PopulateImageData(data.thumbnail_info(), url, width, height); | 233 return PopulateImageData(data.thumbnail_info(), url, width, height); |
185 } | 234 } |
186 | 235 |
187 std::string SnippetFromBookmark(const BookmarkNode* node) { | 236 std::string EnhancedBookmarkModel::GetSnippet(const BookmarkNode* node) { |
188 std::string decoded(DataForMetaInfoField(node, kPageDataKey)); | 237 std::string decoded(DataForMetaInfoField(node, kPageDataKey)); |
189 if (decoded == "") | 238 if (decoded.empty()) |
190 return decoded; | 239 return decoded; |
191 | 240 |
192 PageData data; | 241 image::collections::PageData data; |
193 bool result = data.ParseFromString(decoded); | 242 bool result = data.ParseFromString(decoded); |
194 if (!result) | 243 if (!result) |
195 return ""; | 244 return std::string(); |
196 | 245 |
197 return data.snippet(); | 246 return data.snippet(); |
198 } | 247 } |
199 | 248 |
200 bool SetAllImagesForBookmark(BookmarkModel* bookmark_model, | 249 void EnhancedBookmarkModel::SetVersionSuffix( |
201 const BookmarkNode* node, | 250 const std::string& version_suffix) { |
202 const GURL& image_url, | 251 version_suffix_ = version_suffix; |
203 int image_width, | 252 } |
204 int image_height, | 253 |
205 const GURL& thumbnail_url, | 254 void EnhancedBookmarkModel::SetMetaInfo(const BookmarkNode* node, |
206 int thumbnail_width, | 255 const std::string& field, |
207 int thumbnail_height) { | 256 const std::string& value, |
| 257 bool user_edit) { |
| 258 DCHECK(!bookmark_model_->is_permanent_node(node)); |
| 259 |
| 260 BookmarkNode::MetaInfoMap meta_info; |
| 261 const BookmarkNode::MetaInfoMap* old_meta_info = node->GetMetaInfoMap(); |
| 262 if (old_meta_info) |
| 263 meta_info.insert(old_meta_info->begin(), old_meta_info->end()); |
| 264 |
| 265 // Don't update anything if the value to set is already there. |
| 266 BookmarkNode::MetaInfoMap::iterator it = meta_info.find(field); |
| 267 if (it != meta_info.end() && it->second == value) |
| 268 return; |
| 269 |
| 270 meta_info[field] = value; |
| 271 meta_info[kVersionKey] = GetVersionString(); |
| 272 meta_info[kUserEditKey] = user_edit ? "true" : "false"; |
| 273 bookmark_model_->SetNodeMetaInfoMap(node, meta_info); |
| 274 } |
| 275 |
| 276 std::string EnhancedBookmarkModel::GetVersionString() { |
| 277 if (version_suffix_.empty()) |
| 278 return version_; |
| 279 return version_ + '/' + version_suffix_; |
| 280 } |
| 281 |
| 282 bool EnhancedBookmarkModel::SetAllImages(const BookmarkNode* node, |
| 283 const GURL& image_url, |
| 284 int image_width, |
| 285 int image_height, |
| 286 const GURL& thumbnail_url, |
| 287 int thumbnail_width, |
| 288 int thumbnail_height) { |
| 289 DCHECK(node->is_url()); |
208 DCHECK(image_url.is_valid() || image_url.is_empty()); | 290 DCHECK(image_url.is_valid() || image_url.is_empty()); |
209 DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty()); | 291 DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty()); |
210 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); | 292 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); |
211 ImageData data; | 293 image::collections::ImageData data; |
212 | 294 |
213 // Try to populate the imageData with the existing data. | 295 // Try to populate the imageData with the existing data. |
214 if (decoded != "") { | 296 if (decoded != "") { |
215 // If the parsing fails, something is wrong. Immediately fail. | 297 // If the parsing fails, something is wrong. Immediately fail. |
216 bool result = data.ParseFromString(decoded); | 298 bool result = data.ParseFromString(decoded); |
217 if (!result) | 299 if (!result) |
218 return false; | 300 return false; |
219 } | 301 } |
220 | 302 |
221 if (image_url.is_empty()) { | 303 if (image_url.is_empty()) { |
222 data.release_original_info(); | 304 data.release_original_info(); |
223 } else { | 305 } else { |
224 // Regardless of whether an image info exists, we make a new one. | 306 // Regardless of whether an image info exists, we make a new one. |
225 // Intentially make a raw pointer. | 307 // Intentially make a raw pointer. |
226 ImageData_ImageInfo* info = new ImageData_ImageInfo; | 308 image::collections::ImageData_ImageInfo* info = |
| 309 new image::collections::ImageData_ImageInfo; |
227 info->set_url(image_url.spec()); | 310 info->set_url(image_url.spec()); |
228 info->set_width(image_width); | 311 info->set_width(image_width); |
229 info->set_height(image_height); | 312 info->set_height(image_height); |
230 // This method consumes the raw pointer. | 313 // This method consumes the raw pointer. |
231 data.set_allocated_original_info(info); | 314 data.set_allocated_original_info(info); |
232 } | 315 } |
233 | 316 |
234 if (thumbnail_url.is_empty()) { | 317 if (thumbnail_url.is_empty()) { |
235 data.release_thumbnail_info(); | 318 data.release_thumbnail_info(); |
236 } else { | 319 } else { |
237 // Regardless of whether an image info exists, we make a new one. | 320 // Regardless of whether an image info exists, we make a new one. |
238 // Intentially make a raw pointer. | 321 // Intentially make a raw pointer. |
239 ImageData_ImageInfo* info = new ImageData_ImageInfo; | 322 image::collections::ImageData_ImageInfo* info = |
| 323 new image::collections::ImageData_ImageInfo; |
240 info->set_url(thumbnail_url.spec()); | 324 info->set_url(thumbnail_url.spec()); |
241 info->set_width(thumbnail_width); | 325 info->set_width(thumbnail_width); |
242 info->set_height(thumbnail_height); | 326 info->set_height(thumbnail_height); |
243 // This method consumes the raw pointer. | 327 // This method consumes the raw pointer. |
244 data.set_allocated_thumbnail_info(info); | 328 data.set_allocated_thumbnail_info(info); |
245 } | 329 } |
246 std::string output; | 330 std::string output; |
247 bool result = data.SerializePartialToString(&output); | 331 bool result = data.SerializePartialToString(&output); |
248 if (!result) | 332 if (!result) |
249 return false; | 333 return false; |
250 | 334 |
251 std::string encoded; | 335 std::string encoded; |
252 base::Base64Encode(output, &encoded); | 336 base::Base64Encode(output, &encoded); |
253 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded); | 337 bookmark_model_->SetNodeMetaInfo(node, kImageDataKey, encoded); |
254 return true; | 338 return true; |
255 } | 339 } |
256 | 340 |
257 } // namespace enhanced_bookmarks | 341 } // namespace enhanced_bookmarks |
OLD | NEW |