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

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

Issue 563363002: Only set remote id during url node creation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Tests 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/enhanced_bookmark_model.h" 5 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h"
6 6
7 #include <iomanip> 7 #include <iomanip>
8 #include <sstream> 8 #include <sstream>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/rand_util.h" 12 #include "base/rand_util.h"
13 #include "components/bookmarks/browser/bookmark_model.h" 13 #include "components/bookmarks/browser/bookmark_model.h"
14 #include "components/bookmarks/browser/bookmark_node.h" 14 #include "components/bookmarks/browser/bookmark_node.h"
15 #include "components/enhanced_bookmarks/proto/metadata.pb.h" 15 #include "components/enhanced_bookmarks/proto/metadata.pb.h"
16 #include "url/gurl.h" 16 #include "url/gurl.h"
17 17
18 namespace { 18 namespace {
19 const char* kBookmarkBarId = "f_bookmarks_bar"; 19 const char* kBookmarkBarId = "f_bookmarks_bar";
20 20
21 const char* kIdKey = "stars.id"; 21 const char* kIdKey = "stars.id";
22 const char* kImageDataKey = "stars.imageData"; 22 const char* kImageDataKey = "stars.imageData";
23 const char* kNoteKey = "stars.note"; 23 const char* kNoteKey = "stars.note";
24 const char* kPageDataKey = "stars.pageData"; 24 const char* kPageDataKey = "stars.pageData";
25 const char* kUserEditKey = "stars.userEdit";
26 const char* kVersionKey = "stars.version"; 25 const char* kVersionKey = "stars.version";
27 26
28 const char* kFolderPrefix = "ebf_";
29 const char* kBookmarkPrefix = "ebc_"; 27 const char* kBookmarkPrefix = "ebc_";
30 28
31 // Helper method for working with bookmark metainfo. 29 // Helper method for working with bookmark metainfo.
32 std::string DataForMetaInfoField(const BookmarkNode* node, 30 std::string DataForMetaInfoField(const BookmarkNode* node,
33 const std::string& field) { 31 const std::string& field) {
34 std::string value; 32 std::string value;
35 if (!node->GetMetaInfo(field, &value)) 33 if (!node->GetMetaInfo(field, &value))
36 return std::string(); 34 return std::string();
37 35
38 std::string decoded; 36 std::string decoded;
(...skipping 16 matching lines...) Expand all
55 return false; 53 return false;
56 54
57 *out_url = url; 55 *out_url = url;
58 *width = info.width(); 56 *width = info.width();
59 *height = info.height(); 57 *height = info.height();
60 return true; 58 return true;
61 } 59 }
62 60
63 // Generate a random remote id, with a prefix that depends on whether the node 61 // Generate a random remote id, with a prefix that depends on whether the node
64 // is a folder or a bookmark. 62 // is a folder or a bookmark.
65 std::string GenerateRemoteId(bool is_folder) { 63 std::string GenerateRemoteId() {
66 std::stringstream random_id; 64 std::stringstream random_id;
67 // Add prefix depending on whether the node is a folder or not. 65 random_id << kBookmarkPrefix;
68 if (is_folder)
69 random_id << kFolderPrefix;
70 else
71 random_id << kBookmarkPrefix;
72 66
73 // Generate 32 digit hex string random suffix. 67 // Generate 32 digit hex string random suffix.
74 random_id << std::hex << std::setfill('0') << std::setw(16); 68 random_id << std::hex << std::setfill('0') << std::setw(16);
75 random_id << base::RandUint64() << base::RandUint64(); 69 random_id << base::RandUint64() << base::RandUint64();
76 return random_id.str(); 70 return random_id.str();
77 } 71 }
78 } // namespace 72 } // namespace
79 73
80 namespace enhanced_bookmarks { 74 namespace enhanced_bookmarks {
81 75
82 EnhancedBookmarkModel::EnhancedBookmarkModel(BookmarkModel* bookmark_model, 76 EnhancedBookmarkModel::EnhancedBookmarkModel(BookmarkModel* bookmark_model,
83 const std::string& version) 77 const std::string& version)
84 : bookmark_model_(bookmark_model), version_(version) { 78 : bookmark_model_(bookmark_model), version_(version) {
85 } 79 }
86 80
87 EnhancedBookmarkModel::~EnhancedBookmarkModel() { 81 EnhancedBookmarkModel::~EnhancedBookmarkModel() {
88 } 82 }
89 83
90 // Moves |node| to |new_parent| and inserts it at the given |index|. 84 // Moves |node| to |new_parent| and inserts it at the given |index|.
91 void EnhancedBookmarkModel::Move(const BookmarkNode* node, 85 void EnhancedBookmarkModel::Move(const BookmarkNode* node,
92 const BookmarkNode* new_parent, 86 const BookmarkNode* new_parent,
93 int index) { 87 int index) {
94 // TODO(rfevang): Update meta info placement fields.
95 bookmark_model_->Move(node, new_parent, index); 88 bookmark_model_->Move(node, new_parent, index);
96 } 89 }
97 90
98 // Adds a new folder node at the specified position. 91 // Adds a new folder node at the specified position.
99 const BookmarkNode* EnhancedBookmarkModel::AddFolder( 92 const BookmarkNode* EnhancedBookmarkModel::AddFolder(
100 const BookmarkNode* parent, 93 const BookmarkNode* parent,
101 int index, 94 int index,
102 const base::string16& title) { 95 const base::string16& title) {
103 BookmarkNode::MetaInfoMap meta_info; 96 return bookmark_model_->AddFolder(parent, index, title);
104 meta_info[kIdKey] = GenerateRemoteId(true);
105
106 // TODO(rfevang): Set meta info placement fields.
107 return bookmark_model_->AddFolderWithMetaInfo(
108 parent, index, title, &meta_info);
109 } 97 }
110 98
111 // Adds a url at the specified position. 99 // Adds a url at the specified position.
112 const BookmarkNode* EnhancedBookmarkModel::AddURL( 100 const BookmarkNode* EnhancedBookmarkModel::AddURL(
113 const BookmarkNode* parent, 101 const BookmarkNode* parent,
114 int index, 102 int index,
115 const base::string16& title, 103 const base::string16& title,
116 const GURL& url, 104 const GURL& url,
117 const base::Time& creation_time) { 105 const base::Time& creation_time) {
118 BookmarkNode::MetaInfoMap meta_info; 106 BookmarkNode::MetaInfoMap meta_info;
119 meta_info[kIdKey] = GenerateRemoteId(false); 107 meta_info[kIdKey] = GenerateRemoteId();
120
121 // TODO(rfevang): Set meta info placement fields.
122 return bookmark_model_->AddURLWithCreationTimeAndMetaInfo( 108 return bookmark_model_->AddURLWithCreationTimeAndMetaInfo(
123 parent, index, title, url, creation_time, &meta_info); 109 parent, index, title, url, creation_time, &meta_info);
124 } 110 }
125 111
126 std::string EnhancedBookmarkModel::GetRemoteId(const BookmarkNode* node) { 112 std::string EnhancedBookmarkModel::GetRemoteId(const BookmarkNode* node) {
127 if (node == bookmark_model_->bookmark_bar_node()) 113 if (node == bookmark_model_->bookmark_bar_node())
128 return kBookmarkBarId; 114 return kBookmarkBarId;
129 115
130 // Permanent nodes other than the bookmarks bar don't have ids.
131 DCHECK(!bookmark_model_->is_permanent_node(node));
132
133 std::string id; 116 std::string id;
134 if (!node->GetMetaInfo(kIdKey, &id) || id.empty()) 117 if (!node->GetMetaInfo(kIdKey, &id))
135 return SetRemoteId(node); 118 return std::string();
noyau1 2014/09/15 08:08:02 If existing bookmarks do not have IDs yet, this wi
Mark 2014/09/15 17:18:56 We will create ids server-side for clustering and
noyau (Ping after 24h) 2014/09/16 07:33:04 Yes, yes I understand that. All I'm saying is that
136 return id; 119 return id;
137 } 120 }
138 121
139 std::string EnhancedBookmarkModel::SetRemoteId(const BookmarkNode* node) {
140 std::string remote_id = GenerateRemoteId(node->is_folder());
141 SetMetaInfo(node, kIdKey, remote_id, false);
142 return remote_id;
143 }
144
145 void EnhancedBookmarkModel::SetDescription(const BookmarkNode* node, 122 void EnhancedBookmarkModel::SetDescription(const BookmarkNode* node,
146 const std::string& description) { 123 const std::string& description) {
147 SetMetaInfo(node, kNoteKey, description, true); 124 SetMetaInfo(node, kNoteKey, description);
148 } 125 }
149 126
150 std::string EnhancedBookmarkModel::GetDescription(const BookmarkNode* node) { 127 std::string EnhancedBookmarkModel::GetDescription(const BookmarkNode* node) {
151 // First, look for a custom note set by the user. 128 // First, look for a custom note set by the user.
152 std::string description; 129 std::string description;
153 if (node->GetMetaInfo(kNoteKey, &description) && !description.empty()) 130 if (node->GetMetaInfo(kNoteKey, &description) && !description.empty())
154 return description; 131 return description;
155 132
156 // If none are present, return the snippet. 133 // If none are present, return the snippet.
157 return GetSnippet(node); 134 return GetSnippet(node);
(...skipping 24 matching lines...) Expand all
182 info->set_height(height); 159 info->set_height(height);
183 data.set_allocated_original_info(info.release()); 160 data.set_allocated_original_info(info.release());
184 161
185 std::string output; 162 std::string output;
186 bool result = data.SerializePartialToString(&output); 163 bool result = data.SerializePartialToString(&output);
187 if (!result) 164 if (!result)
188 return false; 165 return false;
189 166
190 std::string encoded; 167 std::string encoded;
191 base::Base64Encode(output, &encoded); 168 base::Base64Encode(output, &encoded);
192 SetMetaInfo(node, kImageDataKey, encoded, true); 169 SetMetaInfo(node, kImageDataKey, encoded);
193 // Ensure that the bookmark has a stars.id, to trigger the server processing. 170 // Ensure that the bookmark has a stars.id, to trigger the server processing.
194 GetRemoteId(node); 171 GetRemoteId(node);
195 return true; 172 return true;
196 } 173 }
197 174
198 bool EnhancedBookmarkModel::GetOriginalImage(const BookmarkNode* node, 175 bool EnhancedBookmarkModel::GetOriginalImage(const BookmarkNode* node,
199 GURL* url, 176 GURL* url,
200 int* width, 177 int* width,
201 int* height) { 178 int* height) {
202 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); 179 std::string decoded(DataForMetaInfoField(node, kImageDataKey));
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 return data.snippet(); 223 return data.snippet();
247 } 224 }
248 225
249 void EnhancedBookmarkModel::SetVersionSuffix( 226 void EnhancedBookmarkModel::SetVersionSuffix(
250 const std::string& version_suffix) { 227 const std::string& version_suffix) {
251 version_suffix_ = version_suffix; 228 version_suffix_ = version_suffix;
252 } 229 }
253 230
254 void EnhancedBookmarkModel::SetMetaInfo(const BookmarkNode* node, 231 void EnhancedBookmarkModel::SetMetaInfo(const BookmarkNode* node,
255 const std::string& field, 232 const std::string& field,
256 const std::string& value, 233 const std::string& value) {
257 bool user_edit) {
258 DCHECK(!bookmark_model_->is_permanent_node(node)); 234 DCHECK(!bookmark_model_->is_permanent_node(node));
259 235
260 BookmarkNode::MetaInfoMap meta_info; 236 BookmarkNode::MetaInfoMap meta_info;
261 const BookmarkNode::MetaInfoMap* old_meta_info = node->GetMetaInfoMap(); 237 const BookmarkNode::MetaInfoMap* old_meta_info = node->GetMetaInfoMap();
262 if (old_meta_info) 238 if (old_meta_info)
263 meta_info.insert(old_meta_info->begin(), old_meta_info->end()); 239 meta_info.insert(old_meta_info->begin(), old_meta_info->end());
264 240
265 // Don't update anything if the value to set is already there. 241 // Don't update anything if the value to set is already there.
266 BookmarkNode::MetaInfoMap::iterator it = meta_info.find(field); 242 BookmarkNode::MetaInfoMap::iterator it = meta_info.find(field);
267 if (it != meta_info.end() && it->second == value) 243 if (it != meta_info.end() && it->second == value)
268 return; 244 return;
269 245
270 meta_info[field] = value; 246 meta_info[field] = value;
271 meta_info[kVersionKey] = GetVersionString(); 247 meta_info[kVersionKey] = GetVersionString();
272 meta_info[kUserEditKey] = user_edit ? "true" : "false";
273 bookmark_model_->SetNodeMetaInfoMap(node, meta_info); 248 bookmark_model_->SetNodeMetaInfoMap(node, meta_info);
274 } 249 }
275 250
276 std::string EnhancedBookmarkModel::GetVersionString() { 251 std::string EnhancedBookmarkModel::GetVersionString() {
277 if (version_suffix_.empty()) 252 if (version_suffix_.empty())
278 return version_; 253 return version_;
279 return version_ + '/' + version_suffix_; 254 return version_ + '/' + version_suffix_;
280 } 255 }
281 256
282 bool EnhancedBookmarkModel::SetAllImages(const BookmarkNode* node, 257 bool EnhancedBookmarkModel::SetAllImages(const BookmarkNode* node,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 if (!result) 307 if (!result)
333 return false; 308 return false;
334 309
335 std::string encoded; 310 std::string encoded;
336 base::Base64Encode(output, &encoded); 311 base::Base64Encode(output, &encoded);
337 bookmark_model_->SetNodeMetaInfo(node, kImageDataKey, encoded); 312 bookmark_model_->SetNodeMetaInfo(node, kImageDataKey, encoded);
338 return true; 313 return true;
339 } 314 }
340 315
341 } // namespace enhanced_bookmarks 316 } // namespace enhanced_bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698