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