Chromium Code Reviews| 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/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 "ui/base/models/tree_node_iterator.h" | |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 const char* kBookmarkBarId = "f_bookmarks_bar"; | 20 const char* kBookmarkBarId = "f_bookmarks_bar"; |
| 20 | 21 |
| 21 const char* kIdKey = "stars.id"; | 22 const char* kIdKey = "stars.id"; |
| 22 const char* kImageDataKey = "stars.imageData"; | 23 const char* kImageDataKey = "stars.imageData"; |
| 23 const char* kNoteKey = "stars.note"; | 24 const char* kNoteKey = "stars.note"; |
| 25 const char* kOldIdKey = "stars.oldId"; | |
| 24 const char* kPageDataKey = "stars.pageData"; | 26 const char* kPageDataKey = "stars.pageData"; |
| 25 const char* kUserEditKey = "stars.userEdit"; | |
| 26 const char* kVersionKey = "stars.version"; | 27 const char* kVersionKey = "stars.version"; |
| 27 | 28 |
| 28 const char* kFolderPrefix = "ebf_"; | |
| 29 const char* kBookmarkPrefix = "ebc_"; | 29 const char* kBookmarkPrefix = "ebc_"; |
| 30 | 30 |
| 31 // Helper method for working with bookmark metainfo. | 31 // Helper method for working with bookmark metainfo. |
| 32 std::string DataForMetaInfoField(const BookmarkNode* node, | 32 std::string DataForMetaInfoField(const BookmarkNode* node, |
| 33 const std::string& field) { | 33 const std::string& field) { |
| 34 std::string value; | 34 std::string value; |
| 35 if (!node->GetMetaInfo(field, &value)) | 35 if (!node->GetMetaInfo(field, &value)) |
| 36 return std::string(); | 36 return std::string(); |
| 37 | 37 |
| 38 std::string decoded; | 38 std::string decoded; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 55 return false; | 55 return false; |
| 56 | 56 |
| 57 *out_url = url; | 57 *out_url = url; |
| 58 *width = info.width(); | 58 *width = info.width(); |
| 59 *height = info.height(); | 59 *height = info.height(); |
| 60 return true; | 60 return true; |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Generate a random remote id, with a prefix that depends on whether the node | 63 // Generate a random remote id, with a prefix that depends on whether the node |
| 64 // is a folder or a bookmark. | 64 // is a folder or a bookmark. |
| 65 std::string GenerateRemoteId(bool is_folder) { | 65 std::string GenerateRemoteId() { |
| 66 std::stringstream random_id; | 66 std::stringstream random_id; |
| 67 // Add prefix depending on whether the node is a folder or not. | 67 random_id << kBookmarkPrefix; |
| 68 if (is_folder) | |
| 69 random_id << kFolderPrefix; | |
| 70 else | |
| 71 random_id << kBookmarkPrefix; | |
| 72 | 68 |
| 73 // Generate 32 digit hex string random suffix. | 69 // Generate 32 digit hex string random suffix. |
| 74 random_id << std::hex << std::setfill('0') << std::setw(16); | 70 random_id << std::hex << std::setfill('0') << std::setw(16); |
| 75 random_id << base::RandUint64() << base::RandUint64(); | 71 random_id << base::RandUint64() << base::RandUint64(); |
| 76 return random_id.str(); | 72 return random_id.str(); |
| 77 } | 73 } |
| 78 } // namespace | 74 } // namespace |
| 79 | 75 |
| 80 namespace enhanced_bookmarks { | 76 namespace enhanced_bookmarks { |
| 81 | 77 |
| 82 EnhancedBookmarkModel::EnhancedBookmarkModel(BookmarkModel* bookmark_model, | 78 EnhancedBookmarkModel::EnhancedBookmarkModel(BookmarkModel* bookmark_model, |
| 83 const std::string& version) | 79 const std::string& version) |
| 84 : bookmark_model_(bookmark_model), version_(version) { | 80 : bookmark_model_(bookmark_model), version_(version) { |
| 81 bookmark_model_->AddObserver(this); | |
| 82 if (bookmark_model_->loaded()) | |
| 83 InitializeIdMap(); | |
| 85 } | 84 } |
| 86 | 85 |
| 87 EnhancedBookmarkModel::~EnhancedBookmarkModel() { | 86 EnhancedBookmarkModel::~EnhancedBookmarkModel() { |
| 88 } | 87 } |
| 89 | 88 |
| 89 void EnhancedBookmarkModel::ShutDown() { | |
| 90 bookmark_model_->RemoveObserver(this); | |
| 91 bookmark_model_ = NULL; | |
| 92 } | |
| 93 | |
| 90 // Moves |node| to |new_parent| and inserts it at the given |index|. | 94 // Moves |node| to |new_parent| and inserts it at the given |index|. |
| 91 void EnhancedBookmarkModel::Move(const BookmarkNode* node, | 95 void EnhancedBookmarkModel::Move(const BookmarkNode* node, |
| 92 const BookmarkNode* new_parent, | 96 const BookmarkNode* new_parent, |
| 93 int index) { | 97 int index) { |
| 94 // TODO(rfevang): Update meta info placement fields. | |
| 95 bookmark_model_->Move(node, new_parent, index); | 98 bookmark_model_->Move(node, new_parent, index); |
| 96 } | 99 } |
| 97 | 100 |
| 98 // Adds a new folder node at the specified position. | 101 // Adds a new folder node at the specified position. |
| 99 const BookmarkNode* EnhancedBookmarkModel::AddFolder( | 102 const BookmarkNode* EnhancedBookmarkModel::AddFolder( |
| 100 const BookmarkNode* parent, | 103 const BookmarkNode* parent, |
| 101 int index, | 104 int index, |
| 102 const base::string16& title) { | 105 const base::string16& title) { |
| 103 BookmarkNode::MetaInfoMap meta_info; | 106 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 } | 107 } |
| 110 | 108 |
| 111 // Adds a url at the specified position. | 109 // Adds a url at the specified position. |
| 112 const BookmarkNode* EnhancedBookmarkModel::AddURL( | 110 const BookmarkNode* EnhancedBookmarkModel::AddURL( |
| 113 const BookmarkNode* parent, | 111 const BookmarkNode* parent, |
| 114 int index, | 112 int index, |
| 115 const base::string16& title, | 113 const base::string16& title, |
| 116 const GURL& url, | 114 const GURL& url, |
| 117 const base::Time& creation_time) { | 115 const base::Time& creation_time) { |
| 118 BookmarkNode::MetaInfoMap meta_info; | 116 BookmarkNode::MetaInfoMap meta_info; |
| 119 meta_info[kIdKey] = GenerateRemoteId(false); | 117 meta_info[kIdKey] = GenerateRemoteId(); |
| 120 | |
| 121 // TODO(rfevang): Set meta info placement fields. | |
| 122 return bookmark_model_->AddURLWithCreationTimeAndMetaInfo( | 118 return bookmark_model_->AddURLWithCreationTimeAndMetaInfo( |
| 123 parent, index, title, url, creation_time, &meta_info); | 119 parent, index, title, url, creation_time, &meta_info); |
| 124 } | 120 } |
| 125 | 121 |
| 126 std::string EnhancedBookmarkModel::GetRemoteId(const BookmarkNode* node) { | 122 std::string EnhancedBookmarkModel::GetRemoteId(const BookmarkNode* node) { |
| 127 if (node == bookmark_model_->bookmark_bar_node()) | 123 if (node == bookmark_model_->bookmark_bar_node()) |
| 128 return kBookmarkBarId; | 124 return kBookmarkBarId; |
| 129 | 125 |
| 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; | 126 std::string id; |
| 134 if (!node->GetMetaInfo(kIdKey, &id) || id.empty()) | 127 if (!node->GetMetaInfo(kIdKey, &id)) |
| 135 return SetRemoteId(node); | 128 return std::string(); |
| 136 return id; | 129 return id; |
| 137 } | 130 } |
| 138 | 131 |
| 139 std::string EnhancedBookmarkModel::SetRemoteId(const BookmarkNode* node) { | 132 const BookmarkNode* EnhancedBookmarkModel::BookmarkForRemoteId( |
| 140 std::string remote_id = GenerateRemoteId(node->is_folder()); | 133 const std::string& remote_id) { |
| 141 SetMetaInfo(node, kIdKey, remote_id, false); | 134 IdToNodeMap::iterator it = id_map_.find(remote_id); |
| 142 return remote_id; | 135 if (it != id_map_.end()) |
| 136 return it->second; | |
| 137 return NULL; | |
| 143 } | 138 } |
| 144 | 139 |
| 145 void EnhancedBookmarkModel::SetDescription(const BookmarkNode* node, | 140 void EnhancedBookmarkModel::SetDescription(const BookmarkNode* node, |
| 146 const std::string& description) { | 141 const std::string& description) { |
| 147 SetMetaInfo(node, kNoteKey, description, true); | 142 SetMetaInfo(node, kNoteKey, description); |
| 148 } | 143 } |
| 149 | 144 |
| 150 std::string EnhancedBookmarkModel::GetDescription(const BookmarkNode* node) { | 145 std::string EnhancedBookmarkModel::GetDescription(const BookmarkNode* node) { |
| 151 // First, look for a custom note set by the user. | 146 // First, look for a custom note set by the user. |
| 152 std::string description; | 147 std::string description; |
| 153 if (node->GetMetaInfo(kNoteKey, &description) && !description.empty()) | 148 if (node->GetMetaInfo(kNoteKey, &description) && !description.empty()) |
| 154 return description; | 149 return description; |
| 155 | 150 |
| 156 // If none are present, return the snippet. | 151 // If none are present, return the snippet. |
| 157 return GetSnippet(node); | 152 return GetSnippet(node); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 182 info->set_height(height); | 177 info->set_height(height); |
| 183 data.set_allocated_original_info(info.release()); | 178 data.set_allocated_original_info(info.release()); |
| 184 | 179 |
| 185 std::string output; | 180 std::string output; |
| 186 bool result = data.SerializePartialToString(&output); | 181 bool result = data.SerializePartialToString(&output); |
| 187 if (!result) | 182 if (!result) |
| 188 return false; | 183 return false; |
| 189 | 184 |
| 190 std::string encoded; | 185 std::string encoded; |
| 191 base::Base64Encode(output, &encoded); | 186 base::Base64Encode(output, &encoded); |
| 192 SetMetaInfo(node, kImageDataKey, encoded, true); | 187 SetMetaInfo(node, kImageDataKey, encoded); |
| 193 // Ensure that the bookmark has a stars.id, to trigger the server processing. | |
| 194 GetRemoteId(node); | |
| 195 return true; | 188 return true; |
| 196 } | 189 } |
| 197 | 190 |
| 198 bool EnhancedBookmarkModel::GetOriginalImage(const BookmarkNode* node, | 191 bool EnhancedBookmarkModel::GetOriginalImage(const BookmarkNode* node, |
| 199 GURL* url, | 192 GURL* url, |
| 200 int* width, | 193 int* width, |
| 201 int* height) { | 194 int* height) { |
| 202 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); | 195 std::string decoded(DataForMetaInfoField(node, kImageDataKey)); |
| 203 if (decoded == "") | 196 if (decoded == "") |
| 204 return false; | 197 return false; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 return std::string(); | 237 return std::string(); |
| 245 | 238 |
| 246 return data.snippet(); | 239 return data.snippet(); |
| 247 } | 240 } |
| 248 | 241 |
| 249 void EnhancedBookmarkModel::SetVersionSuffix( | 242 void EnhancedBookmarkModel::SetVersionSuffix( |
| 250 const std::string& version_suffix) { | 243 const std::string& version_suffix) { |
| 251 version_suffix_ = version_suffix; | 244 version_suffix_ = version_suffix; |
| 252 } | 245 } |
| 253 | 246 |
| 247 void EnhancedBookmarkModel::BookmarkModelChanged() { | |
| 248 } | |
| 249 | |
| 250 void EnhancedBookmarkModel::BookmarkModelLoaded(BookmarkModel* model, | |
| 251 bool ids_reassigned) { | |
| 252 InitializeIdMap(); | |
| 253 } | |
| 254 | |
| 255 void EnhancedBookmarkModel::BookmarkNodeAdded(BookmarkModel* model, | |
| 256 const BookmarkNode* parent, | |
| 257 int index) { | |
| 258 AddToIdMap(parent->GetChild(index)); | |
| 259 ResetDuplicateRemoteIds(); | |
| 260 } | |
| 261 | |
| 262 void EnhancedBookmarkModel::BookmarkNodeRemoved( | |
| 263 BookmarkModel* model, | |
| 264 const BookmarkNode* parent, | |
| 265 int old_index, | |
| 266 const BookmarkNode* node, | |
| 267 const std::set<GURL>& removed_urls) { | |
| 268 std::string remote_id = GetRemoteId(node); | |
| 269 id_map_.erase(remote_id); | |
| 270 } | |
| 271 | |
| 272 void EnhancedBookmarkModel::OnWillChangeBookmarkMetaInfo( | |
| 273 BookmarkModel* model, | |
| 274 const BookmarkNode* node) { | |
| 275 prev_remote_id_ = GetRemoteId(node); | |
| 276 } | |
| 277 | |
| 278 void EnhancedBookmarkModel::BookmarkMetaInfoChanged(BookmarkModel* model, | |
| 279 const BookmarkNode* node) { | |
| 280 std::string remote_id = GetRemoteId(node); | |
| 281 if (remote_id != prev_remote_id_) { | |
| 282 id_map_.erase(prev_remote_id_); | |
| 283 if (!remote_id.empty()) { | |
| 284 AddToIdMap(node); | |
| 285 ResetDuplicateRemoteIds(); | |
| 286 } | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 void EnhancedBookmarkModel::BookmarkAllUserNodesRemoved( | |
| 291 BookmarkModel* model, | |
| 292 const std::set<GURL>& removed_urls) { | |
| 293 id_map_.clear(); | |
| 294 // Re-initialize so non-user nodes with remote ids are present in the map. | |
| 295 InitializeIdMap(); | |
| 296 } | |
| 297 | |
| 298 void EnhancedBookmarkModel::InitializeIdMap() { | |
| 299 ui::TreeNodeIterator<const BookmarkNode> iterator( | |
| 300 bookmark_model_->root_node()); | |
| 301 while (iterator.has_next()) { | |
| 302 AddToIdMap(iterator.Next()); | |
| 303 } | |
| 304 ResetDuplicateRemoteIds(); | |
| 305 } | |
| 306 | |
| 307 void EnhancedBookmarkModel::AddToIdMap(const BookmarkNode* node) { | |
| 308 std::string remote_id = GetRemoteId(node); | |
| 309 if (remote_id.empty()) | |
| 310 return; | |
| 311 | |
| 312 // Try to insert the node. | |
| 313 std::pair<IdToNodeMap::iterator, bool> result = | |
| 314 id_map_.insert(make_pair(remote_id, node)); | |
| 315 if (!result.second) { | |
| 316 // Some node already had the same remote id, so add both nodes to the | |
| 317 // to-be-reset set. | |
| 318 nodes_to_reset_[result.first->second] = remote_id; | |
| 319 nodes_to_reset_[node] = remote_id; | |
|
noyau (Ping after 24h)
2014/09/17 08:24:35
Don't you need to remove the node you found in the
Rune Fevang
2014/09/17 22:51:04
It needs to stay there in case there are more dupl
noyau (Ping after 24h)
2014/09/18 08:43:21
Acknowledged.
| |
| 320 } | |
| 321 } | |
| 322 | |
| 323 void EnhancedBookmarkModel::ResetDuplicateRemoteIds() { | |
| 324 for (NodeToIdMap::iterator it = nodes_to_reset_.begin(); | |
| 325 it != nodes_to_reset_.end(); | |
| 326 ++it) { | |
| 327 BookmarkNode::MetaInfoMap meta_info; | |
| 328 meta_info[kIdKey] = ""; | |
| 329 meta_info[kOldIdKey] = it->second; | |
| 330 SetMultipleMetaInfo(it->first, meta_info); | |
| 331 } | |
| 332 nodes_to_reset_.clear(); | |
| 333 } | |
| 334 | |
| 254 void EnhancedBookmarkModel::SetMetaInfo(const BookmarkNode* node, | 335 void EnhancedBookmarkModel::SetMetaInfo(const BookmarkNode* node, |
| 255 const std::string& field, | 336 const std::string& field, |
| 256 const std::string& value, | 337 const std::string& value) { |
| 257 bool user_edit) { | |
| 258 DCHECK(!bookmark_model_->is_permanent_node(node)); | 338 DCHECK(!bookmark_model_->is_permanent_node(node)); |
| 259 | 339 |
| 260 BookmarkNode::MetaInfoMap meta_info; | 340 BookmarkNode::MetaInfoMap meta_info; |
| 261 const BookmarkNode::MetaInfoMap* old_meta_info = node->GetMetaInfoMap(); | 341 const BookmarkNode::MetaInfoMap* old_meta_info = node->GetMetaInfoMap(); |
| 262 if (old_meta_info) | 342 if (old_meta_info) |
| 263 meta_info.insert(old_meta_info->begin(), old_meta_info->end()); | 343 meta_info.insert(old_meta_info->begin(), old_meta_info->end()); |
| 264 | 344 |
| 265 // Don't update anything if the value to set is already there. | 345 // Don't update anything if the value to set is already there. |
| 266 BookmarkNode::MetaInfoMap::iterator it = meta_info.find(field); | 346 BookmarkNode::MetaInfoMap::iterator it = meta_info.find(field); |
| 267 if (it != meta_info.end() && it->second == value) | 347 if (it != meta_info.end() && it->second == value) |
| 268 return; | 348 return; |
| 269 | 349 |
| 270 meta_info[field] = value; | 350 meta_info[field] = value; |
| 271 meta_info[kVersionKey] = GetVersionString(); | 351 meta_info[kVersionKey] = GetVersionString(); |
| 272 meta_info[kUserEditKey] = user_edit ? "true" : "false"; | |
| 273 bookmark_model_->SetNodeMetaInfoMap(node, meta_info); | 352 bookmark_model_->SetNodeMetaInfoMap(node, meta_info); |
| 274 } | 353 } |
| 275 | 354 |
| 276 std::string EnhancedBookmarkModel::GetVersionString() { | 355 std::string EnhancedBookmarkModel::GetVersionString() { |
| 277 if (version_suffix_.empty()) | 356 if (version_suffix_.empty()) |
| 278 return version_; | 357 return version_; |
| 279 return version_ + '/' + version_suffix_; | 358 return version_ + '/' + version_suffix_; |
| 280 } | 359 } |
| 281 | 360 |
| 361 void EnhancedBookmarkModel::SetMultipleMetaInfo( | |
| 362 const BookmarkNode* node, | |
| 363 BookmarkNode::MetaInfoMap meta_info) { | |
| 364 DCHECK(!bookmark_model_->is_permanent_node(node)); | |
| 365 | |
| 366 // Don't update anything if every value is already set correctly. | |
| 367 if (node->GetMetaInfoMap()) { | |
| 368 bool changed = false; | |
| 369 const BookmarkNode::MetaInfoMap* old_meta_info = node->GetMetaInfoMap(); | |
| 370 for (BookmarkNode::MetaInfoMap::iterator it = meta_info.begin(); | |
| 371 it != meta_info.end(); | |
| 372 ++it) { | |
| 373 BookmarkNode::MetaInfoMap::const_iterator old_field = | |
| 374 old_meta_info->find(it->first); | |
| 375 if (old_field == old_meta_info->end() || | |
| 376 old_field->second == it->second) { | |
| 377 changed = true; | |
| 378 break; | |
| 379 } | |
| 380 } | |
| 381 if (!changed) | |
| 382 return; | |
| 383 | |
| 384 // Fill in the values that aren't changing | |
| 385 meta_info.insert(old_meta_info->begin(), old_meta_info->end()); | |
| 386 } | |
| 387 | |
| 388 meta_info[kVersionKey] = GetVersionString(); | |
| 389 bookmark_model_->SetNodeMetaInfoMap(node, meta_info); | |
| 390 } | |
| 391 | |
| 282 bool EnhancedBookmarkModel::SetAllImages(const BookmarkNode* node, | 392 bool EnhancedBookmarkModel::SetAllImages(const BookmarkNode* node, |
| 283 const GURL& image_url, | 393 const GURL& image_url, |
| 284 int image_width, | 394 int image_width, |
| 285 int image_height, | 395 int image_height, |
| 286 const GURL& thumbnail_url, | 396 const GURL& thumbnail_url, |
| 287 int thumbnail_width, | 397 int thumbnail_width, |
| 288 int thumbnail_height) { | 398 int thumbnail_height) { |
| 289 DCHECK(node->is_url()); | 399 DCHECK(node->is_url()); |
| 290 DCHECK(image_url.is_valid() || image_url.is_empty()); | 400 DCHECK(image_url.is_valid() || image_url.is_empty()); |
| 291 DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty()); | 401 DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 if (!result) | 442 if (!result) |
| 333 return false; | 443 return false; |
| 334 | 444 |
| 335 std::string encoded; | 445 std::string encoded; |
| 336 base::Base64Encode(output, &encoded); | 446 base::Base64Encode(output, &encoded); |
| 337 bookmark_model_->SetNodeMetaInfo(node, kImageDataKey, encoded); | 447 bookmark_model_->SetNodeMetaInfo(node, kImageDataKey, encoded); |
| 338 return true; | 448 return true; |
| 339 } | 449 } |
| 340 | 450 |
| 341 } // namespace enhanced_bookmarks | 451 } // namespace enhanced_bookmarks |
| OLD | NEW |