| 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/bookmarks/browser/bookmark_codec.h" | 5 #include "components/bookmarks/browser/bookmark_codec.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/json/json_string_value_serializer.h" | 12 #include "base/json/json_string_value_serializer.h" |
| 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "components/bookmarks/browser/bookmark_model.h" | 17 #include "components/bookmarks/browser/bookmark_model.h" |
| 17 #include "grit/components_strings.h" | 18 #include "grit/components_strings.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 20 | 21 |
| 21 using base::Time; | 22 using base::Time; |
| 22 | 23 |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 bool BookmarkCodec::DecodeNode(const base::DictionaryValue& value, | 266 bool BookmarkCodec::DecodeNode(const base::DictionaryValue& value, |
| 266 BookmarkNode* parent, | 267 BookmarkNode* parent, |
| 267 BookmarkNode* node) { | 268 BookmarkNode* node) { |
| 268 // If no |node| is specified, we'll create one and add it to the |parent|. | 269 // If no |node| is specified, we'll create one and add it to the |parent|. |
| 269 // Therefore, in that case, |parent| must be non-NULL. | 270 // Therefore, in that case, |parent| must be non-NULL. |
| 270 if (!node && !parent) { | 271 if (!node && !parent) { |
| 271 NOTREACHED(); | 272 NOTREACHED(); |
| 272 return false; | 273 return false; |
| 273 } | 274 } |
| 274 | 275 |
| 276 // It's not valid to have both a node and a specified parent. |
| 277 if (node && parent) { |
| 278 NOTREACHED(); |
| 279 return false; |
| 280 } |
| 281 |
| 275 std::string id_string; | 282 std::string id_string; |
| 276 int64_t id = 0; | 283 int64_t id = 0; |
| 277 if (ids_valid_) { | 284 if (ids_valid_) { |
| 278 if (!value.GetString(kIdKey, &id_string) || | 285 if (!value.GetString(kIdKey, &id_string) || |
| 279 !base::StringToInt64(id_string, &id) || | 286 !base::StringToInt64(id_string, &id) || |
| 280 ids_.count(id) != 0) { | 287 ids_.count(id) != 0) { |
| 281 ids_valid_ = false; | 288 ids_valid_ = false; |
| 282 } else { | 289 } else { |
| 283 ids_.insert(id); | 290 ids_.insert(id); |
| 284 } | 291 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 307 if (!value.GetString(kURLKey, &url_string)) | 314 if (!value.GetString(kURLKey, &url_string)) |
| 308 return false; | 315 return false; |
| 309 | 316 |
| 310 GURL url = GURL(url_string); | 317 GURL url = GURL(url_string); |
| 311 if (!node && url.is_valid()) | 318 if (!node && url.is_valid()) |
| 312 node = new BookmarkNode(id, url); | 319 node = new BookmarkNode(id, url); |
| 313 else | 320 else |
| 314 return false; // Node invalid. | 321 return false; // Node invalid. |
| 315 | 322 |
| 316 if (parent) | 323 if (parent) |
| 317 parent->Add(node, parent->child_count()); | 324 parent->Add(base::WrapUnique(node), parent->child_count()); |
| 318 node->set_type(BookmarkNode::URL); | 325 node->set_type(BookmarkNode::URL); |
| 319 UpdateChecksumWithUrlNode(id_string, title, url_string); | 326 UpdateChecksumWithUrlNode(id_string, title, url_string); |
| 320 } else { | 327 } else { |
| 321 std::string last_modified_date; | 328 std::string last_modified_date; |
| 322 if (!value.GetString(kDateModifiedKey, &last_modified_date)) | 329 if (!value.GetString(kDateModifiedKey, &last_modified_date)) |
| 323 last_modified_date = base::Int64ToString(Time::Now().ToInternalValue()); | 330 last_modified_date = base::Int64ToString(Time::Now().ToInternalValue()); |
| 324 | 331 |
| 325 const base::Value* child_values; | 332 const base::Value* child_values; |
| 326 if (!value.Get(kChildrenKey, &child_values)) | 333 if (!value.Get(kChildrenKey, &child_values)) |
| 327 return false; | 334 return false; |
| 328 | 335 |
| 329 if (child_values->GetType() != base::Value::TYPE_LIST) | 336 if (child_values->GetType() != base::Value::TYPE_LIST) |
| 330 return false; | 337 return false; |
| 331 | 338 |
| 332 if (!node) { | 339 if (!node) { |
| 333 node = new BookmarkNode(id, GURL()); | 340 node = new BookmarkNode(id, GURL()); |
| 334 } else { | 341 } else { |
| 335 // If a new node is not created, explicitly assign ID to the existing one. | 342 // If a new node is not created, explicitly assign ID to the existing one. |
| 336 node->set_id(id); | 343 node->set_id(id); |
| 337 } | 344 } |
| 338 | 345 |
| 339 node->set_type(BookmarkNode::FOLDER); | 346 node->set_type(BookmarkNode::FOLDER); |
| 340 int64_t internal_time; | 347 int64_t internal_time; |
| 341 base::StringToInt64(last_modified_date, &internal_time); | 348 base::StringToInt64(last_modified_date, &internal_time); |
| 342 node->set_date_folder_modified(Time::FromInternalValue(internal_time)); | 349 node->set_date_folder_modified(Time::FromInternalValue(internal_time)); |
| 343 | 350 |
| 344 if (parent) | 351 if (parent) |
| 345 parent->Add(node, parent->child_count()); | 352 parent->Add(base::WrapUnique(node), parent->child_count()); |
| 346 | 353 |
| 347 UpdateChecksumWithFolderNode(id_string, title); | 354 UpdateChecksumWithFolderNode(id_string, title); |
| 348 | 355 |
| 349 const base::ListValue* child_l_values = nullptr; | 356 const base::ListValue* child_l_values = nullptr; |
| 350 if (!child_values->GetAsList(&child_l_values)) | 357 if (!child_values->GetAsList(&child_l_values)) |
| 351 return false; | 358 return false; |
| 352 if (!DecodeChildren(*child_l_values, node)) | 359 if (!DecodeChildren(*child_l_values, node)) |
| 353 return false; | 360 return false; |
| 354 } | 361 } |
| 355 | 362 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 base::MD5Init(&md5_context_); | 492 base::MD5Init(&md5_context_); |
| 486 } | 493 } |
| 487 | 494 |
| 488 void BookmarkCodec::FinalizeChecksum() { | 495 void BookmarkCodec::FinalizeChecksum() { |
| 489 base::MD5Digest digest; | 496 base::MD5Digest digest; |
| 490 base::MD5Final(&digest, &md5_context_); | 497 base::MD5Final(&digest, &md5_context_); |
| 491 computed_checksum_ = base::MD5DigestToBase16(digest); | 498 computed_checksum_ = base::MD5DigestToBase16(digest); |
| 492 } | 499 } |
| 493 | 500 |
| 494 } // namespace bookmarks | 501 } // namespace bookmarks |
| OLD | NEW |