cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / enhanced_bookmarks / metadata_accessor.cc
blob40c5de9ead4456b801c12791651431bdf393684b
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"
7 #include <iomanip>
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;
19 namespace {
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();
25 if (!map)
26 return "";
28 BookmarkNode::MetaInfoMap::const_iterator it = map->find(field);
29 if (it == map->end())
30 return "";
32 std::string decoded;
33 bool result = base::Base64Decode((*it).second, &decoded);
34 if (!result)
35 return "";
37 return 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);
50 return random_id_str;
53 // Helper method for working with ImageData_ImageInfo.
54 bool PopulateImageData(const ImageData_ImageInfo& info,
55 GURL* out_url,
56 int* width,
57 int* height) {
58 if (!info.has_url() || !info.has_width() || !info.has_height())
59 return false;
61 GURL url(info.url());
62 if (!url.is_valid())
63 return false;
65 *out_url = url;
66 *width = info.width();
67 *height = info.height();
68 return true;
71 } // namespace
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();
83 if (!map)
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);
90 return it->second;
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();
101 if (!map)
102 return "";
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())
107 return it->second;
109 // If none are present, return the snippet.
110 return SnippetFromBookmark(node);
113 bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model,
114 const BookmarkNode* node,
115 const GURL& url,
116 int width,
117 int height) {
118 DCHECK(url.is_valid());
120 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
121 ImageData data;
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);
127 if (!result)
128 return false;
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());
137 std::string output;
138 bool result = data.SerializePartialToString(&output);
139 if (!result)
140 return false;
142 std::string encoded;
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);
147 return true;
150 bool OriginalImageFromBookmark(const BookmarkNode* node,
151 GURL* url,
152 int* width,
153 int* height) {
154 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
155 if (decoded.empty())
156 return false;
158 ImageData data;
159 bool result = data.ParseFromString(decoded);
160 if (!result)
161 return false;
163 if (!data.has_original_info())
164 return false;
166 return PopulateImageData(data.original_info(), url, width, height);
169 bool ThumbnailImageFromBookmark(const BookmarkNode* node,
170 GURL* url,
171 int* width,
172 int* height) {
173 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
174 if (decoded.empty())
175 return false;
177 ImageData data;
178 bool result = data.ParseFromString(decoded);
179 if (!result)
180 return false;
182 if (!data.has_thumbnail_info())
183 return false;
185 return PopulateImageData(data.thumbnail_info(), url, width, height);
188 std::string SnippetFromBookmark(const BookmarkNode* node) {
189 std::string decoded(DataForMetaInfoField(node, kPageDataKey));
190 if (decoded.empty())
191 return decoded;
193 PageData data;
194 bool result = data.ParseFromString(decoded);
195 if (!result)
196 return "";
198 return data.snippet();
201 bool SetAllImagesForBookmark(BookmarkModel* bookmark_model,
202 const BookmarkNode* node,
203 const GURL& image_url,
204 int image_width,
205 int image_height,
206 const GURL& thumbnail_url,
207 int thumbnail_width,
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));
212 ImageData data;
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);
218 if (!result)
219 return false;
222 if (image_url.is_empty()) {
223 data.release_original_info();
224 } else {
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();
237 } else {
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);
247 std::string output;
248 bool result = data.SerializePartialToString(&output);
249 if (!result)
250 return false;
252 std::string encoded;
253 base::Base64Encode(output, &encoded);
254 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
255 return true;
258 } // namespace enhanced_bookmarks