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

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

Issue 476573004: Set version field when changes are made to enhanced bookmarks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Merge with ImageService CL Created 6 years, 3 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
OLDNEW
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 // Moves |node| to |new_parent| and inserts it at the given |index|.
75 void EnhancedBookmarkModel::Move(const BookmarkNode* node,
76 const BookmarkNode* new_parent,
77 int index) {
78 // TODO(rfevang): Update meta info placement fields.
79 bookmark_model_->Move(node, new_parent, index);
80 }
81
82 // Adds a new folder node at the specified position.
83 const BookmarkNode* EnhancedBookmarkModel::AddFolder(
84 const BookmarkNode* parent,
85 int index,
86 const base::string16& title,
87 const BookmarkNode::MetaInfoMap* meta_info) {
88 // TODO(rfevang): Set meta info placement fields.
89 return bookmark_model_->AddFolderWithMetaInfo(
90 parent, index, title, meta_info);
91 }
92
93 // Adds a url at the specified position.
94 const BookmarkNode* EnhancedBookmarkModel::AddURL(
95 const BookmarkNode* parent,
96 int index,
97 const base::string16& title,
98 const GURL& url,
99 const base::Time& creation_time,
100 const BookmarkNode::MetaInfoMap* meta_info) {
101 // TODO(rfevang): Set meta info placement fields.
102 return bookmark_model_->AddURLWithCreationTimeAndMetaInfo(
103 parent, index, title, url, creation_time, meta_info);
104 }
105
106 std::string EnhancedBookmarkModel::GetRemoteIdForNode(
107 const BookmarkNode* node) {
108 DCHECK(initialized_);
109
110 if (node == bookmark_model_->bookmark_bar_node())
111 return kBookmarkBarId;
112
113 // Permanent nodes other than the bookmarks bar don't have ids.
114 DCHECK(!bookmark_model_->is_permanent_node(node));
115
116 std::string id;
117 if (!node->GetMetaInfo(kIdKey, &id) || id.empty())
118 return SetRemoteIdForNode(node);
119 return id;
120 }
121
122 std::string EnhancedBookmarkModel::SetRemoteIdForNode(
Mark 2014/09/03 23:31:16 Hah, you guys have to use the std:: prefix in Chro
Rune Fevang 2014/09/04 01:03:13 Acknowledged.
123 const BookmarkNode* node) {
124 DCHECK(initialized_);
125
126 std::stringstream random_id;
127 // Add prefix depending on whether the node is a folder or not.
128 if (node->is_url())
129 random_id << "ebc_";
Mark 2014/09/03 23:31:16 Nit: These should be top-level constants.
Rune Fevang 2014/09/04 01:03:13 Done.
130 else
131 random_id << "ebf_";
132
133 // Generate 32 digit hex string random suffix.
134 random_id << std::hex << std::setfill('0') << std::setw(16);
135 random_id << base::RandUint64() << base::RandUint64();
Mark 2014/09/03 23:31:16 Base the first rand on time so that you are not in
Rune Fevang 2014/09/04 01:03:13 This function has a comment stating it is safe to
136 std::string random_id_str = random_id.str();
137
138 SetNodeMetaInfo(node, kIdKey, random_id_str);
139 return random_id_str;
140 }
141
142 void EnhancedBookmarkModel::SetDescriptionForNode(
143 const BookmarkNode* node,
144 const std::string& description) {
145 DCHECK(initialized_);
146 SetNodeMetaInfo(node, kNoteKey, description);
147 }
148
149 std::string EnhancedBookmarkModel::GetDescriptionForNode(
150 const BookmarkNode* node) {
151 DCHECK(initialized_);
103 // First, look for a custom note set by the user. 152 // First, look for a custom note set by the user.
104 BookmarkNode::MetaInfoMap::const_iterator it = map->find(kNoteKey); 153 std::string description;
105 if (it != map->end() && it->second != "") 154 if (node->GetMetaInfo(kNoteKey, &description) && !description.empty())
106 return it->second; 155 return description;
107 156
108 // If none are present, return the snippet. 157 // If none are present, return the snippet.
109 return SnippetFromBookmark(node); 158 return GetSnippetForNode(node);
110 } 159 }
111 160
112 bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model, 161 bool EnhancedBookmarkModel::SetOriginalImageForNode(const BookmarkNode* node,
113 const BookmarkNode* node, 162 const GURL& url,
114 const GURL& url, 163 int width,
115 int width, 164 int height) {
116 int height) { 165 DCHECK(initialized_);
117 DCHECK(url.is_valid()); 166 DCHECK(url.is_valid());
118 167
119 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); 168 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
120 ImageData data; 169 image::collections::ImageData data;
121 170
122 // Try to populate the imageData with the existing data. 171 // Try to populate the imageData with the existing data.
123 if (decoded != "") { 172 if (decoded != "") {
124 // If the parsing fails, something is wrong. Immediately fail. 173 // If the parsing fails, something is wrong. Immediately fail.
125 bool result = data.ParseFromString(decoded); 174 bool result = data.ParseFromString(decoded);
126 if (!result) 175 if (!result)
127 return false; 176 return false;
128 } 177 }
129 178
130 scoped_ptr<ImageData_ImageInfo> info(new ImageData_ImageInfo); 179 scoped_ptr<image::collections::ImageData_ImageInfo> info(
180 new image::collections::ImageData_ImageInfo);
131 info->set_url(url.spec()); 181 info->set_url(url.spec());
132 info->set_width(width); 182 info->set_width(width);
133 info->set_height(height); 183 info->set_height(height);
134 data.set_allocated_original_info(info.release()); 184 data.set_allocated_original_info(info.release());
135 185
136 std::string output; 186 std::string output;
137 bool result = data.SerializePartialToString(&output); 187 bool result = data.SerializePartialToString(&output);
138 if (!result) 188 if (!result)
139 return false; 189 return false;
140 190
141 std::string encoded; 191 std::string encoded;
142 base::Base64Encode(output, &encoded); 192 base::Base64Encode(output, &encoded);
143 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded); 193 SetNodeMetaInfo(node, kImageDataKey, encoded);
144 // Ensure that the bookmark has a stars.id, to trigger the server processing. 194 // Ensure that the bookmark has a stars.id, to trigger the server processing.
145 RemoteIdFromBookmark(bookmark_model, node); 195 GetRemoteIdForNode(node);
146 return true; 196 return true;
147 } 197 }
148 198
149 bool OriginalImageFromBookmark(const BookmarkNode* node, 199 bool EnhancedBookmarkModel::GetOriginalImageForNode(const BookmarkNode* node,
150 GURL* url, 200 GURL* url,
151 int* width, 201 int* width,
152 int* height) { 202 int* height) {
203 DCHECK(initialized_);
153 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); 204 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
154 if (decoded == "") 205 if (decoded == "")
155 return false; 206 return false;
156 207
157 ImageData data; 208 image::collections::ImageData data;
158 bool result = data.ParseFromString(decoded); 209 bool result = data.ParseFromString(decoded);
159 if (!result) 210 if (!result)
160 return false; 211 return false;
161 212
162 if (!data.has_original_info()) 213 if (!data.has_original_info())
163 return false; 214 return false;
164 215
165 return PopulateImageData(data.original_info(), url, width, height); 216 return PopulateImageData(data.original_info(), url, width, height);
166 } 217 }
167 218
168 bool ThumbnailImageFromBookmark(const BookmarkNode* node, 219 bool EnhancedBookmarkModel::GetThumbnailImageForNode(const BookmarkNode* node,
169 GURL* url, 220 GURL* url,
170 int* width, 221 int* width,
171 int* height) { 222 int* height) {
223 DCHECK(initialized_);
172 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); 224 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
173 if (decoded == "") 225 if (decoded == "")
174 return false; 226 return false;
175 227
176 ImageData data; 228 image::collections::ImageData data;
177 bool result = data.ParseFromString(decoded); 229 bool result = data.ParseFromString(decoded);
178 if (!result) 230 if (!result)
179 return false; 231 return false;
180 232
181 if (!data.has_thumbnail_info()) 233 if (!data.has_thumbnail_info())
182 return false; 234 return false;
183 235
184 return PopulateImageData(data.thumbnail_info(), url, width, height); 236 return PopulateImageData(data.thumbnail_info(), url, width, height);
185 } 237 }
186 238
187 std::string SnippetFromBookmark(const BookmarkNode* node) { 239 std::string EnhancedBookmarkModel::GetSnippetForNode(const BookmarkNode* node) {
240 DCHECK(initialized_);
188 std::string decoded(DataForMetaInfoField(node, kPageDataKey)); 241 std::string decoded(DataForMetaInfoField(node, kPageDataKey));
189 if (decoded == "") 242 if (decoded.empty())
190 return decoded; 243 return decoded;
191 244
192 PageData data; 245 image::collections::PageData data;
193 bool result = data.ParseFromString(decoded); 246 bool result = data.ParseFromString(decoded);
194 if (!result) 247 if (!result)
195 return ""; 248 return std::string();
196 249
197 return data.snippet(); 250 return data.snippet();
198 } 251 }
199 252
253 std::string EnhancedBookmarkModel::GetVersionForNode(const BookmarkNode* node) {
254 DCHECK(initialized_);
255 std::string version;
256 if (!node->GetMetaInfo(kVersionKey, &version))
257 return std::string();
258 return version;
259 }
260
261 void EnhancedBookmarkModel::SetNodeMetaInfo(const BookmarkNode* node,
262 const std::string& field,
263 const std::string& value) {
264 BookmarkNode::MetaInfoMap meta_info;
265 const BookmarkNode::MetaInfoMap* old_meta_info = node->GetMetaInfoMap();
266 if (old_meta_info)
267 meta_info.insert(old_meta_info->begin(), old_meta_info->end());
268
269 // Don't update anything if the value to set is already there.
270 BookmarkNode::MetaInfoMap::iterator it = meta_info.find(field);
271 if (it != meta_info.end() && it->second == value)
272 return;
273
274 meta_info[field] = value;
275 meta_info[kVersionKey] = version_;
276 bookmark_model_->SetNodeMetaInfoMap(node, meta_info);
277 }
278
200 bool SetAllImagesForBookmark(BookmarkModel* bookmark_model, 279 bool SetAllImagesForBookmark(BookmarkModel* bookmark_model,
201 const BookmarkNode* node, 280 const BookmarkNode* node,
202 const GURL& image_url, 281 const GURL& image_url,
203 int image_width, 282 int image_width,
204 int image_height, 283 int image_height,
205 const GURL& thumbnail_url, 284 const GURL& thumbnail_url,
206 int thumbnail_width, 285 int thumbnail_width,
207 int thumbnail_height) { 286 int thumbnail_height) {
208 DCHECK(image_url.is_valid() || image_url.is_empty()); 287 DCHECK(image_url.is_valid() || image_url.is_empty());
209 DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty()); 288 DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty());
210 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); 289 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
211 ImageData data; 290 image::collections::ImageData data;
212 291
213 // Try to populate the imageData with the existing data. 292 // Try to populate the imageData with the existing data.
214 if (decoded != "") { 293 if (decoded != "") {
215 // If the parsing fails, something is wrong. Immediately fail. 294 // If the parsing fails, something is wrong. Immediately fail.
216 bool result = data.ParseFromString(decoded); 295 bool result = data.ParseFromString(decoded);
217 if (!result) 296 if (!result)
218 return false; 297 return false;
219 } 298 }
220 299
221 if (image_url.is_empty()) { 300 if (image_url.is_empty()) {
222 data.release_original_info(); 301 data.release_original_info();
223 } else { 302 } else {
224 // Regardless of whether an image info exists, we make a new one. 303 // Regardless of whether an image info exists, we make a new one.
225 // Intentially make a raw pointer. 304 // Intentially make a raw pointer.
226 ImageData_ImageInfo* info = new ImageData_ImageInfo; 305 image::collections::ImageData_ImageInfo* info =
306 new image::collections::ImageData_ImageInfo;
227 info->set_url(image_url.spec()); 307 info->set_url(image_url.spec());
228 info->set_width(image_width); 308 info->set_width(image_width);
229 info->set_height(image_height); 309 info->set_height(image_height);
230 // This method consumes the raw pointer. 310 // This method consumes the raw pointer.
231 data.set_allocated_original_info(info); 311 data.set_allocated_original_info(info);
232 } 312 }
233 313
234 if (thumbnail_url.is_empty()) { 314 if (thumbnail_url.is_empty()) {
235 data.release_thumbnail_info(); 315 data.release_thumbnail_info();
236 } else { 316 } else {
237 // Regardless of whether an image info exists, we make a new one. 317 // Regardless of whether an image info exists, we make a new one.
238 // Intentially make a raw pointer. 318 // Intentially make a raw pointer.
239 ImageData_ImageInfo* info = new ImageData_ImageInfo; 319 image::collections::ImageData_ImageInfo* info =
320 new image::collections::ImageData_ImageInfo;
240 info->set_url(thumbnail_url.spec()); 321 info->set_url(thumbnail_url.spec());
241 info->set_width(thumbnail_width); 322 info->set_width(thumbnail_width);
242 info->set_height(thumbnail_height); 323 info->set_height(thumbnail_height);
243 // This method consumes the raw pointer. 324 // This method consumes the raw pointer.
244 data.set_allocated_thumbnail_info(info); 325 data.set_allocated_thumbnail_info(info);
245 } 326 }
246 std::string output; 327 std::string output;
247 bool result = data.SerializePartialToString(&output); 328 bool result = data.SerializePartialToString(&output);
248 if (!result) 329 if (!result)
249 return false; 330 return false;
250 331
251 std::string encoded; 332 std::string encoded;
252 base::Base64Encode(output, &encoded); 333 base::Base64Encode(output, &encoded);
253 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded); 334 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
254 return true; 335 return true;
255 } 336 }
256 337
257 } // namespace enhanced_bookmarks 338 } // namespace enhanced_bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698