1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/enhanced_bookmarks/metadata_accessor.h"
#include <iomanip>
#include "base/base64.h"
#include "base/rand_util.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/enhanced_bookmarks/proto/metadata.pb.h"
#include "ui/base/models/tree_node_iterator.h"
using namespace image::collections;
namespace {
// Helper method for working with bookmark metainfo.
std::string DataForMetaInfoField(const BookmarkNode* node,
const std::string& field) {
const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap();
if (!map)
return "";
BookmarkNode::MetaInfoMap::const_iterator it = map->find(field);
if (it == map->end())
return "";
std::string decoded;
bool result = base::Base64Decode((*it).second, &decoded);
if (!result)
return "";
return decoded;
}
// Sets a new remote id on a bookmark.
std::string SetRemoteIdOnBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node) {
// Generate 16 digit hex string random id.
std::stringstream random_id;
random_id << std::hex << std::setfill('0') << std::setw(16);
random_id << base::RandUint64() << base::RandUint64();
std::string random_id_str = random_id.str();
bookmark_model->SetNodeMetaInfo(
node, enhanced_bookmarks::kIdDataKey, random_id_str);
return random_id_str;
}
// Helper method for working with ImageData_ImageInfo.
bool PopulateImageData(const ImageData_ImageInfo& info,
GURL* out_url,
int* width,
int* height) {
if (!info.has_url() || !info.has_width() || !info.has_height())
return false;
GURL url(info.url());
if (!url.is_valid())
return false;
*out_url = url;
*width = info.width();
*height = info.height();
return true;
}
} // namespace
namespace enhanced_bookmarks {
const char* kPageDataKey = "stars.pageData";
const char* kImageDataKey = "stars.imageData";
const char* kIdDataKey = "stars.id";
const char* kNoteKey = "stars.note";
std::string RemoteIdFromBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node) {
const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap();
if (!map)
return SetRemoteIdOnBookmark(bookmark_model, node);
BookmarkNode::MetaInfoMap::const_iterator it = map->find(kIdDataKey);
if (it == map->end())
return SetRemoteIdOnBookmark(bookmark_model, node);
DCHECK(it->second.length());
return it->second;
}
void SetDescriptionForBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node,
const std::string& description) {
bookmark_model->SetNodeMetaInfo(node, kNoteKey, description);
}
std::string DescriptionFromBookmark(const BookmarkNode* node) {
const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap();
if (!map)
return "";
// First, look for a custom note set by the user.
BookmarkNode::MetaInfoMap::const_iterator it = map->find(kNoteKey);
if (it != map->end() && it->second != "")
return it->second;
// If none are present, return the snippet.
return SnippetFromBookmark(node);
}
bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node,
const GURL& url,
int width,
int height) {
DCHECK(url.is_valid());
std::string decoded(DataForMetaInfoField(node, kImageDataKey));
ImageData data;
// Try to populate the imageData with the existing data.
if (decoded != "") {
// If the parsing fails, something is wrong. Immediately fail.
bool result = data.ParseFromString(decoded);
if (!result)
return false;
}
scoped_ptr<ImageData_ImageInfo> info(new ImageData_ImageInfo);
info->set_url(url.spec());
info->set_width(width);
info->set_height(height);
data.set_allocated_original_info(info.release());
std::string output;
bool result = data.SerializePartialToString(&output);
if (!result)
return false;
std::string encoded;
base::Base64Encode(output, &encoded);
bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
// Ensure that the bookmark has a stars.id, to trigger the server processing.
RemoteIdFromBookmark(bookmark_model, node);
return true;
}
bool OriginalImageFromBookmark(const BookmarkNode* node,
GURL* url,
int* width,
int* height) {
std::string decoded(DataForMetaInfoField(node, kImageDataKey));
if (decoded == "")
return false;
ImageData data;
bool result = data.ParseFromString(decoded);
if (!result)
return false;
if (!data.has_original_info())
return false;
return PopulateImageData(data.original_info(), url, width, height);
}
bool ThumbnailImageFromBookmark(const BookmarkNode* node,
GURL* url,
int* width,
int* height) {
std::string decoded(DataForMetaInfoField(node, kImageDataKey));
if (decoded == "")
return false;
ImageData data;
bool result = data.ParseFromString(decoded);
if (!result)
return false;
if (!data.has_thumbnail_info())
return false;
return PopulateImageData(data.thumbnail_info(), url, width, height);
}
std::string SnippetFromBookmark(const BookmarkNode* node) {
std::string decoded(DataForMetaInfoField(node, kPageDataKey));
if (decoded == "")
return decoded;
PageData data;
bool result = data.ParseFromString(decoded);
if (!result)
return "";
return data.snippet();
}
bool SetAllImagesForBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node,
const GURL& image_url,
int image_width,
int image_height,
const GURL& thumbnail_url,
int thumbnail_width,
int thumbnail_height) {
DCHECK(image_url.is_valid() || image_url.is_empty());
DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty());
std::string decoded(DataForMetaInfoField(node, kImageDataKey));
ImageData data;
// Try to populate the imageData with the existing data.
if (decoded != "") {
// If the parsing fails, something is wrong. Immediately fail.
bool result = data.ParseFromString(decoded);
if (!result)
return false;
}
if (image_url.is_empty()) {
data.release_original_info();
} else {
// Regardless of whether an image info exists, we make a new one.
// Intentially make a raw pointer.
ImageData_ImageInfo* info = new ImageData_ImageInfo;
info->set_url(image_url.spec());
info->set_width(image_width);
info->set_height(image_height);
// This method consumes the raw pointer.
data.set_allocated_original_info(info);
}
if (thumbnail_url.is_empty()) {
data.release_thumbnail_info();
} else {
// Regardless of whether an image info exists, we make a new one.
// Intentially make a raw pointer.
ImageData_ImageInfo* info = new ImageData_ImageInfo;
info->set_url(thumbnail_url.spec());
info->set_width(thumbnail_width);
info->set_height(thumbnail_height);
// This method consumes the raw pointer.
data.set_allocated_thumbnail_info(info);
}
std::string output;
bool result = data.SerializePartialToString(&output);
if (!result)
return false;
std::string encoded;
base::Base64Encode(output, &encoded);
bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
return true;
}
} // namespace enhanced_bookmarks
|