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
;
19 // Helper method for working with bookmark metainfo.
20 std::string
DataForMetaInfoField(const BookmarkNode
* node
,
21 const std::string
& field
) {
22 const BookmarkNode::MetaInfoMap
* map
= node
->GetMetaInfoMap();
26 BookmarkNode::MetaInfoMap::const_iterator it
= map
->find(field
);
31 bool result
= base::Base64Decode((*it
).second
, &decoded
);
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
);
51 // Helper method for working with ImageData_ImageInfo.
52 bool PopulateImageData(const ImageData_ImageInfo
& info
,
56 if (!info
.has_url() || !info
.has_width() || !info
.has_height())
64 *width
= info
.width();
65 *height
= info
.height();
71 namespace enhanced_bookmarks
{
73 const char* kPageDataKey
= "stars.pageData";
74 const char* kImageDataKey
= "stars.imageData";
75 const char* kIdDataKey
= "stars.id";
76 const char* kNoteKey
= "stars.note";
78 std::string
RemoteIdFromBookmark(BookmarkModel
* bookmark_model
,
79 const BookmarkNode
* node
) {
80 const BookmarkNode::MetaInfoMap
* map
= node
->GetMetaInfoMap();
82 return SetRemoteIdOnBookmark(bookmark_model
, node
);
84 BookmarkNode::MetaInfoMap::const_iterator it
= map
->find(kIdDataKey
);
86 return SetRemoteIdOnBookmark(bookmark_model
, node
);
88 DCHECK(it
->second
.length());
92 void SetDescriptionForBookmark(BookmarkModel
* bookmark_model
,
93 const BookmarkNode
* node
,
94 const std::string
& description
) {
95 bookmark_model
->SetNodeMetaInfo(node
, kNoteKey
, description
);
98 std::string
DescriptionFromBookmark(const BookmarkNode
* node
) {
99 const BookmarkNode::MetaInfoMap
* map
= node
->GetMetaInfoMap();
103 // First, look for a custom note set by the user.
104 BookmarkNode::MetaInfoMap::const_iterator it
= map
->find(kNoteKey
);
105 if (it
!= map
->end() && it
->second
!= "")
108 // If none are present, return the snippet.
109 return SnippetFromBookmark(node
);
112 bool SetOriginalImageForBookmark(BookmarkModel
* bookmark_model
,
113 const BookmarkNode
* node
,
117 DCHECK(url
.is_valid());
119 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
122 // Try to populate the imageData with the existing data.
124 // If the parsing fails, something is wrong. Immediately fail.
125 bool result
= data
.ParseFromString(decoded
);
130 scoped_ptr
<ImageData_ImageInfo
> info(new ImageData_ImageInfo
);
131 info
->set_url(url
.spec());
132 info
->set_width(width
);
133 info
->set_height(height
);
134 data
.set_allocated_original_info(info
.release());
137 bool result
= data
.SerializePartialToString(&output
);
142 base::Base64Encode(output
, &encoded
);
143 bookmark_model
->SetNodeMetaInfo(node
, kImageDataKey
, encoded
);
144 // Ensure that the bookmark has a stars.id, to trigger the server processing.
145 RemoteIdFromBookmark(bookmark_model
, node
);
149 bool OriginalImageFromBookmark(const BookmarkNode
* node
,
153 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
158 bool result
= data
.ParseFromString(decoded
);
162 if (!data
.has_original_info())
165 return PopulateImageData(data
.original_info(), url
, width
, height
);
168 bool ThumbnailImageFromBookmark(const BookmarkNode
* node
,
172 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
177 bool result
= data
.ParseFromString(decoded
);
181 if (!data
.has_thumbnail_info())
184 return PopulateImageData(data
.thumbnail_info(), url
, width
, height
);
187 std::string
SnippetFromBookmark(const BookmarkNode
* node
) {
188 std::string
decoded(DataForMetaInfoField(node
, kPageDataKey
));
193 bool result
= data
.ParseFromString(decoded
);
197 return data
.snippet();
200 bool SetAllImagesForBookmark(BookmarkModel
* bookmark_model
,
201 const BookmarkNode
* node
,
202 const GURL
& image_url
,
205 const GURL
& thumbnail_url
,
207 int thumbnail_height
) {
208 DCHECK(image_url
.is_valid() || image_url
.is_empty());
209 DCHECK(thumbnail_url
.is_valid() || thumbnail_url
.is_empty());
210 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
213 // Try to populate the imageData with the existing data.
215 // If the parsing fails, something is wrong. Immediately fail.
216 bool result
= data
.ParseFromString(decoded
);
221 if (image_url
.is_empty()) {
222 data
.release_original_info();
224 // Regardless of whether an image info exists, we make a new one.
225 // Intentially make a raw pointer.
226 ImageData_ImageInfo
* info
= new ImageData_ImageInfo
;
227 info
->set_url(image_url
.spec());
228 info
->set_width(image_width
);
229 info
->set_height(image_height
);
230 // This method consumes the raw pointer.
231 data
.set_allocated_original_info(info
);
234 if (thumbnail_url
.is_empty()) {
235 data
.release_thumbnail_info();
237 // Regardless of whether an image info exists, we make a new one.
238 // Intentially make a raw pointer.
239 ImageData_ImageInfo
* info
= new ImageData_ImageInfo
;
240 info
->set_url(thumbnail_url
.spec());
241 info
->set_width(thumbnail_width
);
242 info
->set_height(thumbnail_height
);
243 // This method consumes the raw pointer.
244 data
.set_allocated_thumbnail_info(info
);
247 bool result
= data
.SerializePartialToString(&output
);
252 base::Base64Encode(output
, &encoded
);
253 bookmark_model
->SetNodeMetaInfo(node
, kImageDataKey
, encoded
);
257 } // namespace enhanced_bookmarks