Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(606)

Side by Side Diff: components/enhanced_bookmarks/metadata_accessor.cc

Issue 336263003: Bring up of the metadata accessors for enhanced bookmarks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removing use of 'auto'. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "components/enhanced_bookmarks/metadata_accessor.h"
6
7 #include <iomanip>
8
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"
14
15 using namespace image::collections;
16
17 namespace {
18
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();
23 if (!map)
24 return "";
25
26 BookmarkNode::MetaInfoMap::const_iterator it = map->find(field);
27 if (it == map->end())
28 return "";
29
30 std::string decoded;
31 bool result = base::Base64Decode((*it).second, &decoded);
32 if (!result)
33 return "";
34
35 return decoded;
36 }
37
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.
52 bool PopulateImageData(const ImageData_ImageInfo& info,
53 GURL* out_url,
54 int* width,
55 int* height) {
56 if (!info.has_url() || !info.has_width() || !info.has_height())
57 return false;
58
59 GURL url(info.url());
60 if (!url.is_valid())
61 return false;
62
63 *out_url = url;
64 *width = info.width();
65 *height = info.height();
66 return true;
67 }
68
69 } // namespace
70
71 namespace enhanced_bookmarks {
72
73 const char* kPageDataKey = "stars.pageData";
74 const char* kImageDataKey = "stars.imageData";
75 const char* kIdDataKey = "stars.id";
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 }
91
92 void SetDescriptionForBookmark(BookmarkModel* bookmark_model,
93 const BookmarkNode* node,
94 const std::string& description) {
95 bookmark_model->SetNodeMetaInfo(node, kNoteKey, description);
96 }
97
98 std::string DescriptionFromBookmark(const BookmarkNode* node) {
99 const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap();
100 if (!map)
101 return "";
102
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 != "")
106 return it->second;
107
108 // If none are present, return the snippet.
109 return SnippetFromBookmark(node);
110 }
111
112 bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model,
113 const BookmarkNode* node,
114 const GURL& url,
115 int width,
116 int height) {
117 DCHECK(url.is_valid());
118
119 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
120 ImageData data;
121
122 // Try to populate the imageData with the existing data.
123 if (decoded != "") {
124 // If the parsing fails, something is wrong. Immediately fail.
125 bool result = data.ParseFromString(decoded);
126 if (!result)
127 return false;
128 }
129
130 // Regardless of whether an image info exists, we make a new one.
131 // Intentionally make a raw pointer.
132 ImageData_ImageInfo* info = new ImageData_ImageInfo;
133 info->set_url(url.spec());
134 info->set_width(width);
135 info->set_height(height);
136 // This method consumes the raw pointer.
Kibeom Kim (inactive) 2014/06/18 17:44:57 I think mentioning passing the ownership is more c
noyau (Ping after 24h) 2014/06/19 09:07:00 Removed the confusing comments and made the passin
137 data.set_allocated_original_info(info);
138
139 std::string output;
140 bool result = data.SerializePartialToString(&output);
141 if (!result)
142 return false;
143
144 std::string encoded;
145 base::Base64Encode(output, &encoded);
146 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
147 // Ensure that the bookmark has a stars.id, to trigger the server processing.
148 RemoteIdFromBookmark(bookmark_model, node);
149 return true;
150 }
151
152 bool OriginalImageFromBookmark(const BookmarkNode* node,
153 GURL* url,
154 int* width,
155 int* height) {
156 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
157 if (decoded == "")
158 return false;
159
160 ImageData data;
161 bool result = data.ParseFromString(decoded);
162 if (!result)
163 return false;
164
165 if (!data.has_original_info())
166 return false;
167
168 return PopulateImageData(data.original_info(), url, width, height);
169 }
170
171 bool ThumbnailImageFromBookmark(const BookmarkNode* node,
172 GURL* url,
173 int* width,
174 int* height) {
175 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
176 if (decoded == "")
177 return false;
178
179 ImageData data;
180 bool result = data.ParseFromString(decoded);
181 if (!result)
182 return false;
183
184 if (!data.has_thumbnail_info())
185 return false;
186
187 return PopulateImageData(data.thumbnail_info(), url, width, height);
188 }
189
190 std::string SnippetFromBookmark(const BookmarkNode* node) {
191 std::string decoded(DataForMetaInfoField(node, kPageDataKey));
192 if (decoded == "")
193 return decoded;
194
195 PageData data;
196 bool result = data.ParseFromString(decoded);
197 if (!result)
198 return "";
199
200 return data.snippet();
201 }
202
203 bool SetAllImagesForBookmark(BookmarkModel* bookmark_model,
204 const BookmarkNode* node,
205 const GURL& image_url,
206 int image_width,
207 int image_height,
208 const GURL& thumbnail_url,
209 int thumbnail_width,
210 int thumbnail_height) {
211 DCHECK(image_url.is_valid() || image_url.is_empty());
212 DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty());
213 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
214 ImageData data;
215
216 // Try to populate the imageData with the existing data.
217 if (decoded != "") {
218 // If the parsing fails, something is wrong. Immediately fail.
219 bool result = data.ParseFromString(decoded);
220 if (!result)
221 return false;
222 }
223
224 if (image_url.is_empty()) {
225 data.release_original_info();
226 } else {
227 // Regardless of whether an image info exists, we make a new one.
228 // Intentially make a raw pointer.
229 ImageData_ImageInfo* info = new ImageData_ImageInfo;
230 info->set_url(image_url.spec());
231 info->set_width(image_width);
232 info->set_height(image_height);
233 // This method consumes the raw pointer.
234 data.set_allocated_original_info(info);
235 }
236
237 if (thumbnail_url.is_empty()) {
238 data.release_thumbnail_info();
239 } else {
240 // Regardless of whether an image info exists, we make a new one.
241 // Intentially make a raw pointer.
242 ImageData_ImageInfo* info = new ImageData_ImageInfo;
243 info->set_url(thumbnail_url.spec());
244 info->set_width(thumbnail_width);
245 info->set_height(thumbnail_height);
246 // This method consumes the raw pointer.
247 data.set_allocated_thumbnail_info(info);
248 }
249 std::string output;
250 bool result = data.SerializePartialToString(&output);
251 if (!result)
252 return false;
253
254 std::string encoded;
255 base::Base64Encode(output, &encoded);
256 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
257 return true;
258 }
259
260 } // namespace enhanced_bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698