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
);
85 if (it
== map
->end() || it
->second
.empty())
86 return SetRemoteIdOnBookmark(bookmark_model
, node
);
91 void SetDescriptionForBookmark(BookmarkModel
* bookmark_model
,
92 const BookmarkNode
* node
,
93 const std::string
& description
) {
94 bookmark_model
->SetNodeMetaInfo(node
, kNoteKey
, description
);
97 std::string
DescriptionFromBookmark(const BookmarkNode
* node
) {
98 const BookmarkNode::MetaInfoMap
* map
= node
->GetMetaInfoMap();
102 // First, look for a custom note set by the user.
103 BookmarkNode::MetaInfoMap::const_iterator it
= map
->find(kNoteKey
);
104 if (it
!= map
->end() && !it
->second
.empty())
107 // If none are present, return the snippet.
108 return SnippetFromBookmark(node
);
111 bool SetOriginalImageForBookmark(BookmarkModel
* bookmark_model
,
112 const BookmarkNode
* node
,
116 DCHECK(url
.is_valid());
118 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
121 // Try to populate the imageData with the existing data.
123 // If the parsing fails, something is wrong. Immediately fail.
124 bool result
= data
.ParseFromString(decoded
);
129 scoped_ptr
<ImageData_ImageInfo
> info(new ImageData_ImageInfo
);
130 info
->set_url(url
.spec());
131 info
->set_width(width
);
132 info
->set_height(height
);
133 data
.set_allocated_original_info(info
.release());
136 bool result
= data
.SerializePartialToString(&output
);
141 base::Base64Encode(output
, &encoded
);
142 bookmark_model
->SetNodeMetaInfo(node
, kImageDataKey
, encoded
);
143 // Ensure that the bookmark has a stars.id, to trigger the server processing.
144 RemoteIdFromBookmark(bookmark_model
, node
);
148 bool OriginalImageFromBookmark(const BookmarkNode
* node
,
152 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
157 bool result
= data
.ParseFromString(decoded
);
161 if (!data
.has_original_info())
164 return PopulateImageData(data
.original_info(), url
, width
, height
);
167 bool ThumbnailImageFromBookmark(const BookmarkNode
* node
,
171 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
176 bool result
= data
.ParseFromString(decoded
);
180 if (!data
.has_thumbnail_info())
183 return PopulateImageData(data
.thumbnail_info(), url
, width
, height
);
186 std::string
SnippetFromBookmark(const BookmarkNode
* node
) {
187 std::string
decoded(DataForMetaInfoField(node
, kPageDataKey
));
192 bool result
= data
.ParseFromString(decoded
);
196 return data
.snippet();
199 bool SetAllImagesForBookmark(BookmarkModel
* bookmark_model
,
200 const BookmarkNode
* node
,
201 const GURL
& image_url
,
204 const GURL
& thumbnail_url
,
206 int thumbnail_height
) {
207 DCHECK(image_url
.is_valid() || image_url
.is_empty());
208 DCHECK(thumbnail_url
.is_valid() || thumbnail_url
.is_empty());
209 std::string
decoded(DataForMetaInfoField(node
, kImageDataKey
));
212 // Try to populate the imageData with the existing data.
214 // If the parsing fails, something is wrong. Immediately fail.
215 bool result
= data
.ParseFromString(decoded
);
220 if (image_url
.is_empty()) {
221 data
.release_original_info();
223 // Regardless of whether an image info exists, we make a new one.
224 // Intentially make a raw pointer.
225 ImageData_ImageInfo
* info
= new ImageData_ImageInfo
;
226 info
->set_url(image_url
.spec());
227 info
->set_width(image_width
);
228 info
->set_height(image_height
);
229 // This method consumes the raw pointer.
230 data
.set_allocated_original_info(info
);
233 if (thumbnail_url
.is_empty()) {
234 data
.release_thumbnail_info();
236 // Regardless of whether an image info exists, we make a new one.
237 // Intentially make a raw pointer.
238 ImageData_ImageInfo
* info
= new ImageData_ImageInfo
;
239 info
->set_url(thumbnail_url
.spec());
240 info
->set_width(thumbnail_width
);
241 info
->set_height(thumbnail_height
);
242 // This method consumes the raw pointer.
243 data
.set_allocated_thumbnail_info(info
);
246 bool result
= data
.SerializePartialToString(&output
);
251 base::Base64Encode(output
, &encoded
);
252 bookmark_model
->SetNodeMetaInfo(node
, kImageDataKey
, encoded
);
256 } // namespace enhanced_bookmarks