| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/bookmarks/bookmark_codec.h" | 5 #include "chrome/browser/bookmarks/bookmark_codec.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 const wchar_t* BookmarkCodec::kDateModifiedKey = L"date_modified"; | 28 const wchar_t* BookmarkCodec::kDateModifiedKey = L"date_modified"; |
| 29 const wchar_t* BookmarkCodec::kChildrenKey = L"children"; | 29 const wchar_t* BookmarkCodec::kChildrenKey = L"children"; |
| 30 const wchar_t* BookmarkCodec::kTypeURL = L"url"; | 30 const wchar_t* BookmarkCodec::kTypeURL = L"url"; |
| 31 const wchar_t* BookmarkCodec::kTypeFolder = L"folder"; | 31 const wchar_t* BookmarkCodec::kTypeFolder = L"folder"; |
| 32 | 32 |
| 33 // Current version of the file. | 33 // Current version of the file. |
| 34 static const int kCurrentVersion = 1; | 34 static const int kCurrentVersion = 1; |
| 35 | 35 |
| 36 BookmarkCodec::BookmarkCodec() | 36 BookmarkCodec::BookmarkCodec() |
| 37 : ids_reassigned_(false), | 37 : ids_reassigned_(false), |
| 38 ids_missing_(false), | 38 ids_valid_(true), |
| 39 maximum_id_(0) { | 39 maximum_id_(0) { |
| 40 } | 40 } |
| 41 | 41 |
| 42 Value* BookmarkCodec::Encode(BookmarkModel* model) { | 42 Value* BookmarkCodec::Encode(BookmarkModel* model) { |
| 43 return Encode(model->GetBookmarkBarNode(), model->other_node()); | 43 return Encode(model->GetBookmarkBarNode(), model->other_node()); |
| 44 } | 44 } |
| 45 | 45 |
| 46 Value* BookmarkCodec::Encode(const BookmarkNode* bookmark_bar_node, | 46 Value* BookmarkCodec::Encode(const BookmarkNode* bookmark_bar_node, |
| 47 const BookmarkNode* other_folder_node) { | 47 const BookmarkNode* other_folder_node) { |
| 48 ids_reassigned_ = false; | 48 ids_reassigned_ = false; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 59 stored_checksum_ = computed_checksum_; | 59 stored_checksum_ = computed_checksum_; |
| 60 main->Set(kChecksumKey, Value::CreateStringValue(computed_checksum_)); | 60 main->Set(kChecksumKey, Value::CreateStringValue(computed_checksum_)); |
| 61 main->Set(kRootsKey, roots); | 61 main->Set(kRootsKey, roots); |
| 62 return main; | 62 return main; |
| 63 } | 63 } |
| 64 | 64 |
| 65 bool BookmarkCodec::Decode(BookmarkNode* bb_node, | 65 bool BookmarkCodec::Decode(BookmarkNode* bb_node, |
| 66 BookmarkNode* other_folder_node, | 66 BookmarkNode* other_folder_node, |
| 67 int64* max_id, | 67 int64* max_id, |
| 68 const Value& value) { | 68 const Value& value) { |
| 69 ids_.clear(); |
| 69 ids_reassigned_ = false; | 70 ids_reassigned_ = false; |
| 70 ids_missing_ = false; | 71 ids_valid_ = true; |
| 71 maximum_id_ = 0; | 72 maximum_id_ = 0; |
| 72 stored_checksum_.clear(); | 73 stored_checksum_.clear(); |
| 73 InitializeChecksum(); | 74 InitializeChecksum(); |
| 74 bool success = DecodeHelper(bb_node, other_folder_node, value); | 75 bool success = DecodeHelper(bb_node, other_folder_node, value); |
| 75 FinalizeChecksum(); | 76 FinalizeChecksum(); |
| 76 // If either the checksums differ or some IDs were missing, reassign IDs. | 77 // If either the checksums differ or some IDs were missing/not unique, |
| 77 if (ids_missing_ || computed_checksum() != stored_checksum()) | 78 // reassign IDs. |
| 79 if (!ids_valid_ || computed_checksum() != stored_checksum()) |
| 78 ReassignIDs(bb_node, other_folder_node); | 80 ReassignIDs(bb_node, other_folder_node); |
| 79 *max_id = maximum_id_ + 1; | 81 *max_id = maximum_id_ + 1; |
| 80 return success; | 82 return success; |
| 81 } | 83 } |
| 82 | 84 |
| 83 Value* BookmarkCodec::EncodeNode(const BookmarkNode* node) { | 85 Value* BookmarkCodec::EncodeNode(const BookmarkNode* node) { |
| 84 DictionaryValue* value = new DictionaryValue(); | 86 DictionaryValue* value = new DictionaryValue(); |
| 85 std::string id; | 87 std::string id; |
| 86 id = Int64ToString(node->id()); | 88 id = Int64ToString(node->id()); |
| 87 value->SetString(kIdKey, id); | 89 value->SetString(kIdKey, id); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 BookmarkNode* node) { | 184 BookmarkNode* node) { |
| 183 // If no |node| is specified, we'll create one and add it to the |parent|. | 185 // If no |node| is specified, we'll create one and add it to the |parent|. |
| 184 // Therefore, in that case, |parent| must be non-NULL. | 186 // Therefore, in that case, |parent| must be non-NULL. |
| 185 if (!node && !parent) { | 187 if (!node && !parent) { |
| 186 NOTREACHED(); | 188 NOTREACHED(); |
| 187 return false; | 189 return false; |
| 188 } | 190 } |
| 189 | 191 |
| 190 std::string id_string; | 192 std::string id_string; |
| 191 int64 id = 0; | 193 int64 id = 0; |
| 192 if (!value.GetString(kIdKey, &id_string) || !StringToInt64(id_string, &id)) | 194 if (ids_valid_) { |
| 193 ids_missing_ = true; | 195 if (!value.GetString(kIdKey, &id_string) || |
| 196 !StringToInt64(id_string, &id) || |
| 197 ids_.count(id) != 0) { |
| 198 ids_valid_ = false; |
| 199 } else { |
| 200 ids_.insert(id); |
| 201 } |
| 202 } |
| 194 | 203 |
| 195 maximum_id_ = std::max(maximum_id_, id); | 204 maximum_id_ = std::max(maximum_id_, id); |
| 196 | 205 |
| 197 std::wstring title; | 206 std::wstring title; |
| 198 value.GetString(kNameKey, &title); | 207 value.GetString(kNameKey, &title); |
| 199 | 208 |
| 200 std::wstring date_added_string; | 209 std::wstring date_added_string; |
| 201 if (!value.GetString(kDateAddedKey, &date_added_string)) | 210 if (!value.GetString(kDateAddedKey, &date_added_string)) |
| 202 date_added_string = Int64ToWString(Time::Now().ToInternalValue()); | 211 date_added_string = Int64ToWString(Time::Now().ToInternalValue()); |
| 203 base::Time date_added = base::Time::FromInternalValue( | 212 base::Time date_added = base::Time::FromInternalValue( |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 | 324 |
| 316 void BookmarkCodec::InitializeChecksum() { | 325 void BookmarkCodec::InitializeChecksum() { |
| 317 MD5Init(&md5_context_); | 326 MD5Init(&md5_context_); |
| 318 } | 327 } |
| 319 | 328 |
| 320 void BookmarkCodec::FinalizeChecksum() { | 329 void BookmarkCodec::FinalizeChecksum() { |
| 321 MD5Digest digest; | 330 MD5Digest digest; |
| 322 MD5Final(&digest, &md5_context_); | 331 MD5Final(&digest, &md5_context_); |
| 323 computed_checksum_ = MD5DigestToBase16(digest); | 332 computed_checksum_ = MD5DigestToBase16(digest); |
| 324 } | 333 } |
| OLD | NEW |