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

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: Added flags TODO 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* kUserEditKey = "stars.userEdit";
26 const char* kVersionKey = "stars.version";
27
28 const char* kFolderPrefix = "ebf_";
29 const char* kBookmarkPrefix = "ebc_";
18 30
19 // Helper method for working with bookmark metainfo. 31 // Helper method for working with bookmark metainfo.
20 std::string DataForMetaInfoField(const BookmarkNode* node, 32 std::string DataForMetaInfoField(const BookmarkNode* node,
21 const std::string& field) { 33 const std::string& field) {
22 const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap(); 34 std::string value;
23 if (!map) 35 if (!node->GetMetaInfo(field, &value))
24 return ""; 36 return std::string();
25
26 BookmarkNode::MetaInfoMap::const_iterator it = map->find(field);
27 if (it == map->end())
28 return "";
29 37
30 std::string decoded; 38 std::string decoded;
31 bool result = base::Base64Decode((*it).second, &decoded); 39 if (!base::Base64Decode(value, &decoded))
32 if (!result) 40 return std::string();
33 return "";
34 41
35 return decoded; 42 return decoded;
36 } 43 }
37 44
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. 45 // Helper method for working with ImageData_ImageInfo.
52 bool PopulateImageData(const ImageData_ImageInfo& info, 46 bool PopulateImageData(const image::collections::ImageData_ImageInfo& info,
53 GURL* out_url, 47 GURL* out_url,
54 int* width, 48 int* width,
55 int* height) { 49 int* height) {
56 if (!info.has_url() || !info.has_width() || !info.has_height()) 50 if (!info.has_url() || !info.has_width() || !info.has_height())
57 return false; 51 return false;
58 52
59 GURL url(info.url()); 53 GURL url(info.url());
60 if (!url.is_valid()) 54 if (!url.is_valid())
61 return false; 55 return false;
62 56
63 *out_url = url; 57 *out_url = url;
64 *width = info.width(); 58 *width = info.width();
65 *height = info.height(); 59 *height = info.height();
66 return true; 60 return true;
67 } 61 }
68
69 } // namespace 62 } // namespace
70 63
71 namespace enhanced_bookmarks { 64 namespace enhanced_bookmarks {
72 65
73 const char* kPageDataKey = "stars.pageData"; 66 EnhancedBookmarkModel::EnhancedBookmarkModel(BookmarkModel* bookmark_model,
74 const char* kImageDataKey = "stars.imageData"; 67 const std::string& version)
75 const char* kIdDataKey = "stars.id"; 68 : bookmark_model_(bookmark_model), version_(version) {
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 } 69 }
91 70
92 void SetDescriptionForBookmark(BookmarkModel* bookmark_model, 71 EnhancedBookmarkModel::~EnhancedBookmarkModel() {
93 const BookmarkNode* node,
94 const std::string& description) {
95 bookmark_model->SetNodeMetaInfo(node, kNoteKey, description);
96 } 72 }
97 73
98 std::string DescriptionFromBookmark(const BookmarkNode* node) { 74 // Moves |node| to |new_parent| and inserts it at the given |index|.
99 const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap(); 75 void EnhancedBookmarkModel::Move(const BookmarkNode* node,
100 if (!map) 76 const BookmarkNode* new_parent,
101 return ""; 77 int index) {
78 // TODO(rfevang): Update meta info placement fields.
79 bookmark_model_->Move(node, new_parent, index);
80 }
102 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(
noyau (Ping after 24h) 2014/09/05 12:04:43 Why did you rename everything to "Node"? I prefer
Rune Fevang 2014/09/06 00:01:56 Both bookmarks and folders have remote ids. I rena
noyau (Ping after 24h) 2014/09/08 09:04:18 I see, you consider folder are not bookmarks. I do
Rune Fevang 2014/09/08 19:29:19 How about omitting the ForBookmark/ForNode altoget
noyau (Ping after 24h) 2014/09/09 08:54:28 Duh, this solution is so obviously right!
107 const BookmarkNode* node) {
108 if (node == bookmark_model_->bookmark_bar_node())
109 return kBookmarkBarId;
110
111 // Permanent nodes other than the bookmarks bar don't have ids.
112 DCHECK(!bookmark_model_->is_permanent_node(node));
113
114 std::string id;
115 if (!node->GetMetaInfo(kIdKey, &id) || id.empty())
116 return SetRemoteIdForNode(node);
117 return id;
118 }
119
120 std::string EnhancedBookmarkModel::SetRemoteIdForNode(
121 const BookmarkNode* node) {
122 std::stringstream random_id;
123 // Add prefix depending on whether the node is a folder or not.
124 if (node->is_url())
125 random_id << kBookmarkPrefix;
126 else
127 random_id << kFolderPrefix;
128
129 // Generate 32 digit hex string random suffix.
130 random_id << std::hex << std::setfill('0') << std::setw(16);
131 random_id << base::RandUint64() << base::RandUint64();
132 std::string random_id_str = random_id.str();
133
134 SetNodeMetaInfo(node, kIdKey, random_id_str, false);
135 return random_id_str;
136 }
137
138 void EnhancedBookmarkModel::SetDescriptionForNode(
139 const BookmarkNode* node,
140 const std::string& description,
141 bool user_edit) {
142 SetNodeMetaInfo(node, kNoteKey, description, user_edit);
143 }
144
145 std::string EnhancedBookmarkModel::GetDescriptionForNode(
146 const BookmarkNode* node) {
103 // First, look for a custom note set by the user. 147 // First, look for a custom note set by the user.
104 BookmarkNode::MetaInfoMap::const_iterator it = map->find(kNoteKey); 148 std::string description;
105 if (it != map->end() && it->second != "") 149 if (node->GetMetaInfo(kNoteKey, &description) && !description.empty())
106 return it->second; 150 return description;
107 151
108 // If none are present, return the snippet. 152 // If none are present, return the snippet.
109 return SnippetFromBookmark(node); 153 return GetSnippetForNode(node);
110 } 154 }
111 155
112 bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model, 156 bool EnhancedBookmarkModel::SetOriginalImageForNode(const BookmarkNode* node,
113 const BookmarkNode* node, 157 const GURL& url,
114 const GURL& url, 158 int width,
115 int width, 159 int height,
116 int height) { 160 bool user_edit) {
117 DCHECK(url.is_valid()); 161 DCHECK(url.is_valid());
118 162
119 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); 163 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
120 ImageData data; 164 image::collections::ImageData data;
121 165
122 // Try to populate the imageData with the existing data. 166 // Try to populate the imageData with the existing data.
123 if (decoded != "") { 167 if (decoded != "") {
124 // If the parsing fails, something is wrong. Immediately fail. 168 // If the parsing fails, something is wrong. Immediately fail.
125 bool result = data.ParseFromString(decoded); 169 bool result = data.ParseFromString(decoded);
126 if (!result) 170 if (!result)
127 return false; 171 return false;
128 } 172 }
129 173
130 scoped_ptr<ImageData_ImageInfo> info(new ImageData_ImageInfo); 174 scoped_ptr<image::collections::ImageData_ImageInfo> info(
175 new image::collections::ImageData_ImageInfo);
131 info->set_url(url.spec()); 176 info->set_url(url.spec());
132 info->set_width(width); 177 info->set_width(width);
133 info->set_height(height); 178 info->set_height(height);
134 data.set_allocated_original_info(info.release()); 179 data.set_allocated_original_info(info.release());
135 180
136 std::string output; 181 std::string output;
137 bool result = data.SerializePartialToString(&output); 182 bool result = data.SerializePartialToString(&output);
138 if (!result) 183 if (!result)
139 return false; 184 return false;
140 185
141 std::string encoded; 186 std::string encoded;
142 base::Base64Encode(output, &encoded); 187 base::Base64Encode(output, &encoded);
143 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded); 188 SetNodeMetaInfo(node, kImageDataKey, encoded, user_edit);
144 // Ensure that the bookmark has a stars.id, to trigger the server processing. 189 // Ensure that the bookmark has a stars.id, to trigger the server processing.
145 RemoteIdFromBookmark(bookmark_model, node); 190 GetRemoteIdForNode(node);
146 return true; 191 return true;
147 } 192 }
148 193
149 bool OriginalImageFromBookmark(const BookmarkNode* node, 194 bool EnhancedBookmarkModel::GetOriginalImageForNode(const BookmarkNode* node,
150 GURL* url, 195 GURL* url,
151 int* width, 196 int* width,
152 int* height) { 197 int* height) {
153 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); 198 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
154 if (decoded == "") 199 if (decoded == "")
155 return false; 200 return false;
156 201
157 ImageData data; 202 image::collections::ImageData data;
158 bool result = data.ParseFromString(decoded); 203 bool result = data.ParseFromString(decoded);
159 if (!result) 204 if (!result)
160 return false; 205 return false;
161 206
162 if (!data.has_original_info()) 207 if (!data.has_original_info())
163 return false; 208 return false;
164 209
165 return PopulateImageData(data.original_info(), url, width, height); 210 return PopulateImageData(data.original_info(), url, width, height);
166 } 211 }
167 212
168 bool ThumbnailImageFromBookmark(const BookmarkNode* node, 213 bool EnhancedBookmarkModel::GetThumbnailImageForNode(const BookmarkNode* node,
169 GURL* url, 214 GURL* url,
170 int* width, 215 int* width,
171 int* height) { 216 int* height) {
172 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); 217 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
173 if (decoded == "") 218 if (decoded == "")
174 return false; 219 return false;
175 220
176 ImageData data; 221 image::collections::ImageData data;
177 bool result = data.ParseFromString(decoded); 222 bool result = data.ParseFromString(decoded);
178 if (!result) 223 if (!result)
179 return false; 224 return false;
180 225
181 if (!data.has_thumbnail_info()) 226 if (!data.has_thumbnail_info())
182 return false; 227 return false;
183 228
184 return PopulateImageData(data.thumbnail_info(), url, width, height); 229 return PopulateImageData(data.thumbnail_info(), url, width, height);
185 } 230 }
186 231
187 std::string SnippetFromBookmark(const BookmarkNode* node) { 232 std::string EnhancedBookmarkModel::GetSnippetForNode(const BookmarkNode* node) {
188 std::string decoded(DataForMetaInfoField(node, kPageDataKey)); 233 std::string decoded(DataForMetaInfoField(node, kPageDataKey));
189 if (decoded == "") 234 if (decoded.empty())
190 return decoded; 235 return decoded;
191 236
192 PageData data; 237 image::collections::PageData data;
193 bool result = data.ParseFromString(decoded); 238 bool result = data.ParseFromString(decoded);
194 if (!result) 239 if (!result)
195 return ""; 240 return std::string();
196 241
197 return data.snippet(); 242 return data.snippet();
198 } 243 }
199 244
245 void EnhancedBookmarkModel::SetVersionSuffix(
246 const std::string& version_suffix) {
247 version_suffix_ = version_suffix;
248 }
249
250 void EnhancedBookmarkModel::SetNodeMetaInfo(const BookmarkNode* node,
251 const std::string& field,
252 const std::string& value,
253 bool user_edit) {
254 BookmarkNode::MetaInfoMap meta_info;
255 const BookmarkNode::MetaInfoMap* old_meta_info = node->GetMetaInfoMap();
256 if (old_meta_info)
257 meta_info.insert(old_meta_info->begin(), old_meta_info->end());
Yaron 2014/09/05 03:16:53 Some of these fields are quite long. Isn't this a
Rune Fevang 2014/09/05 03:40:26 It is, however if the fields are set one at a time
Yaron 2014/09/05 04:07:37 Hmm. That's a little concerning re: sync. It's not
Rune Fevang 2014/09/05 05:04:58 The idea is that any write operations that "belong
Mark 2014/09/05 16:27:34 Sync operates on SyncEntity fidelity. There is no
Rune Fevang 2014/09/06 00:01:56 How do you distinguish between a partial write wit
258
259 // Don't update anything if the value to set is already there.
260 BookmarkNode::MetaInfoMap::iterator it = meta_info.find(field);
261 if (it != meta_info.end() && it->second == value)
262 return;
263
264 meta_info[field] = value;
265 meta_info[kVersionKey] = GetVersionString();
266 meta_info[kUserEditKey] = user_edit ? "true" : "false";
267 bookmark_model_->SetNodeMetaInfoMap(node, meta_info);
268 }
269
270 std::string EnhancedBookmarkModel::GetVersionString() {
271 if (version_suffix_.empty())
272 return version_;
273 return version_ + '/' + version_suffix_;
274 }
275
200 bool SetAllImagesForBookmark(BookmarkModel* bookmark_model, 276 bool SetAllImagesForBookmark(BookmarkModel* bookmark_model,
201 const BookmarkNode* node, 277 const BookmarkNode* node,
202 const GURL& image_url, 278 const GURL& image_url,
203 int image_width, 279 int image_width,
204 int image_height, 280 int image_height,
205 const GURL& thumbnail_url, 281 const GURL& thumbnail_url,
206 int thumbnail_width, 282 int thumbnail_width,
207 int thumbnail_height) { 283 int thumbnail_height) {
208 DCHECK(image_url.is_valid() || image_url.is_empty()); 284 DCHECK(image_url.is_valid() || image_url.is_empty());
209 DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty()); 285 DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty());
210 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); 286 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
211 ImageData data; 287 image::collections::ImageData data;
212 288
213 // Try to populate the imageData with the existing data. 289 // Try to populate the imageData with the existing data.
214 if (decoded != "") { 290 if (decoded != "") {
215 // If the parsing fails, something is wrong. Immediately fail. 291 // If the parsing fails, something is wrong. Immediately fail.
216 bool result = data.ParseFromString(decoded); 292 bool result = data.ParseFromString(decoded);
217 if (!result) 293 if (!result)
218 return false; 294 return false;
219 } 295 }
220 296
221 if (image_url.is_empty()) { 297 if (image_url.is_empty()) {
222 data.release_original_info(); 298 data.release_original_info();
223 } else { 299 } else {
224 // Regardless of whether an image info exists, we make a new one. 300 // Regardless of whether an image info exists, we make a new one.
225 // Intentially make a raw pointer. 301 // Intentially make a raw pointer.
226 ImageData_ImageInfo* info = new ImageData_ImageInfo; 302 image::collections::ImageData_ImageInfo* info =
303 new image::collections::ImageData_ImageInfo;
227 info->set_url(image_url.spec()); 304 info->set_url(image_url.spec());
228 info->set_width(image_width); 305 info->set_width(image_width);
229 info->set_height(image_height); 306 info->set_height(image_height);
230 // This method consumes the raw pointer. 307 // This method consumes the raw pointer.
231 data.set_allocated_original_info(info); 308 data.set_allocated_original_info(info);
232 } 309 }
233 310
234 if (thumbnail_url.is_empty()) { 311 if (thumbnail_url.is_empty()) {
235 data.release_thumbnail_info(); 312 data.release_thumbnail_info();
236 } else { 313 } else {
237 // Regardless of whether an image info exists, we make a new one. 314 // Regardless of whether an image info exists, we make a new one.
238 // Intentially make a raw pointer. 315 // Intentially make a raw pointer.
239 ImageData_ImageInfo* info = new ImageData_ImageInfo; 316 image::collections::ImageData_ImageInfo* info =
317 new image::collections::ImageData_ImageInfo;
240 info->set_url(thumbnail_url.spec()); 318 info->set_url(thumbnail_url.spec());
241 info->set_width(thumbnail_width); 319 info->set_width(thumbnail_width);
242 info->set_height(thumbnail_height); 320 info->set_height(thumbnail_height);
243 // This method consumes the raw pointer. 321 // This method consumes the raw pointer.
244 data.set_allocated_thumbnail_info(info); 322 data.set_allocated_thumbnail_info(info);
245 } 323 }
246 std::string output; 324 std::string output;
247 bool result = data.SerializePartialToString(&output); 325 bool result = data.SerializePartialToString(&output);
248 if (!result) 326 if (!result)
249 return false; 327 return false;
250 328
251 std::string encoded; 329 std::string encoded;
252 base::Base64Encode(output, &encoded); 330 base::Base64Encode(output, &encoded);
253 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded); 331 bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
254 return true; 332 return true;
255 } 333 }
256 334
257 } // namespace enhanced_bookmarks 335 } // namespace enhanced_bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698