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/enhanced_bookmarks/metadata_accessor.h"
9 #include "base/base64.h"
10 #include "base/rand_util.h"
11 #include "components/bookmarks/browser/bookmark_model.h"
12 #include "components/enhanced_bookmarks/proto/metadata.pb.h"
13 #include "ui/base/models/tree_node_iterator.h"
15 using namespace image::collections
;
16 using bookmarks::BookmarkModel
;
17 using bookmarks::BookmarkNode
;
21 // Helper method for working with bookmark metainfo.
22 std::string
DataForMetaInfoField(const BookmarkNode
* node
,
23 const std::string
& field
) {
24 const BookmarkNode::MetaInfoMap
* map
= node
->GetMetaInfoMap();
28 BookmarkNode::MetaInfoMap::const_iterator it
= map
->find(field
);
33 bool result
= base::Base64Decode((*it
).second
, &decoded
);
40 // Sets a new remote id on a bookmark.
41 std::string
SetRemoteIdOnBookmark(BookmarkModel
* bookmark_model
,
42 const BookmarkNode
* node
) {
43 // Generate 16 digit hex string random id.
44 std::stringstream random_id
;
45 random_id
<< std::hex
<< std::setfill('0') << std::setw(16);
46 random_id
<< base::RandUint64() << base::RandUint64();
47 std::string random_id_str
= random_id
.str();
48 bookmark_model
->SetNodeMetaInfo(
49 node
, enhanced_bookmarks::kIdDataKey
, random_id_str
);
53 // Helper method for working with ImageData_ImageInfo.
54 bool PopulateImageData(const ImageData_ImageInfo
& info
,
58 if (!info
.has_url() || !info
.has_width() || !info
.has_height())
66 *width
= info
.width();
67 *height
= info
.height();
73 namespace enhanced_bookmarks
{
75 const char* kPageDataKey
= "stars.pageData";
76 const char* kImageDataKey
= "stars.imageData";
77 const char* kIdDataKey
= "stars.id";
78 const char* kNoteKey
= "stars.note";
80 std::string
RemoteIdFromBookmark(BookmarkModel
* bookmark_model
,
81 const BookmarkNode
* node
) {
82 const BookmarkNode::MetaInfoMap
* map
= node
->GetMetaInfoMap();
84 return SetRemoteIdOnBookmark(bookmark_model
, node
);
86 BookmarkNode::MetaInfoMap::const_iterator it
= map
->find(kIdDataKey
);
87 if (it
== map
->end() || it
->second
.empty())
88 return SetRemoteIdOnBookmark(bookmark_model
, node
);
93 void SetDescriptionForBookmark(BookmarkModel
* bookmark_model
,
94 const BookmarkNode
* node
,
95 const std::string
& description
) {
96 bookmark_model
->SetNodeMetaInfo(node
, kNoteKey
, description
);
99 std::string
DescriptionFromBookmark(const BookmarkNode
* node
) {
100 const BookmarkNode::MetaInfoMap
* map
= node
->GetMetaInfoMap();
104 // First, look for a custom note set by the user.
105 BookmarkNode::MetaInfoMap::const_iterator it
= map
->find(kNoteKey
);
106 if (it
!= map
->end() && !it
->second
.empty())
109 // If none are present, return the snippet.
110 return SnippetFromBookmark(node
);
113 bool SetOriginalImageForBookmark(BookmarkModel
* bookmark_model
,
114 const BookmarkNode
* node
,
118 DCHECK(url
.is_valid());
120 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
123 // Try to populate the imageData with the existing data.
124 if (!decoded
.empty()) {
125 // If the parsing fails, something is wrong. Immediately fail.
126 bool result
= data
.ParseFromString(decoded
);
131 scoped_ptr
<ImageData_ImageInfo
> info(new ImageData_ImageInfo
);
132 info
->set_url(url
.spec());
133 info
->set_width(width
);
134 info
->set_height(height
);
135 data
.set_allocated_original_info(info
.release());
138 bool result
= data
.SerializePartialToString(&output
);
143 base::Base64Encode(output
, &encoded
);
144 bookmark_model
->SetNodeMetaInfo(node
, kImageDataKey
, encoded
);
145 // Ensure that the bookmark has a stars.id, to trigger the server processing.
146 RemoteIdFromBookmark(bookmark_model
, node
);
150 bool OriginalImageFromBookmark(const BookmarkNode
* node
,
154 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
159 bool result
= data
.ParseFromString(decoded
);
163 if (!data
.has_original_info())
166 return PopulateImageData(data
.original_info(), url
, width
, height
);
169 bool ThumbnailImageFromBookmark(const BookmarkNode
* node
,
173 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
178 bool result
= data
.ParseFromString(decoded
);
182 if (!data
.has_thumbnail_info())
185 return PopulateImageData(data
.thumbnail_info(), url
, width
, height
);
188 std::string
SnippetFromBookmark(const BookmarkNode
* node
) {
189 std::string
decoded(DataForMetaInfoField(node
, kPageDataKey
));
194 bool result
= data
.ParseFromString(decoded
);
198 return data
.snippet();
201 bool SetAllImagesForBookmark(BookmarkModel
* bookmark_model
,
202 const BookmarkNode
* node
,
203 const GURL
& image_url
,
206 const GURL
& thumbnail_url
,
208 int thumbnail_height
) {
209 DCHECK(image_url
.is_valid() || image_url
.is_empty());
210 DCHECK(thumbnail_url
.is_valid() || thumbnail_url
.is_empty());
211 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
214 // Try to populate the imageData with the existing data.
215 if (!decoded
.empty()) {
216 // If the parsing fails, something is wrong. Immediately fail.
217 bool result
= data
.ParseFromString(decoded
);
222 if (image_url
.is_empty()) {
223 data
.release_original_info();
225 // Regardless of whether an image info exists, we make a new one.
226 // Intentially make a raw pointer.
227 ImageData_ImageInfo
* info
= new ImageData_ImageInfo
;
228 info
->set_url(image_url
.spec());
229 info
->set_width(image_width
);
230 info
->set_height(image_height
);
231 // This method consumes the raw pointer.
232 data
.set_allocated_original_info(info
);
235 if (thumbnail_url
.is_empty()) {
236 data
.release_thumbnail_info();
238 // Regardless of whether an image info exists, we make a new one.
239 // Intentially make a raw pointer.
240 ImageData_ImageInfo
* info
= new ImageData_ImageInfo
;
241 info
->set_url(thumbnail_url
.spec());
242 info
->set_width(thumbnail_width
);
243 info
->set_height(thumbnail_height
);
244 // This method consumes the raw pointer.
245 data
.set_allocated_thumbnail_info(info
);
248 bool result
= data
.SerializePartialToString(&output
);
253 base::Base64Encode(output
, &encoded
);
254 bookmark_model
->SetNodeMetaInfo(node
, kImageDataKey
, encoded
);
258 } // namespace enhanced_bookmarks