| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 const Value& value) { | 127 const Value& value) { |
| 128 if (value.GetType() != Value::TYPE_DICTIONARY) | 128 if (value.GetType() != Value::TYPE_DICTIONARY) |
| 129 return false; // Unexpected type. | 129 return false; // Unexpected type. |
| 130 | 130 |
| 131 const DictionaryValue& d_value = static_cast<const DictionaryValue&>(value); | 131 const DictionaryValue& d_value = static_cast<const DictionaryValue&>(value); |
| 132 | 132 |
| 133 int version; | 133 int version; |
| 134 if (!d_value.GetInteger(kVersionKey, &version) || version != kCurrentVersion) | 134 if (!d_value.GetInteger(kVersionKey, &version) || version != kCurrentVersion) |
| 135 return false; // Unknown version. | 135 return false; // Unknown version. |
| 136 | 136 |
| 137 Value* checksum_value; | 137 const Value* checksum_value; |
| 138 if (d_value.Get(kChecksumKey, &checksum_value)) { | 138 if (d_value.Get(kChecksumKey, &checksum_value)) { |
| 139 if (checksum_value->GetType() != Value::TYPE_STRING) | 139 if (checksum_value->GetType() != Value::TYPE_STRING) |
| 140 return false; | 140 return false; |
| 141 StringValue* checksum_value_str = static_cast<StringValue*>(checksum_value); | 141 const StringValue* checksum_value_str = |
| 142 static_cast<const StringValue*>(checksum_value); |
| 142 if (!checksum_value_str->GetAsString(&stored_checksum_)) | 143 if (!checksum_value_str->GetAsString(&stored_checksum_)) |
| 143 return false; | 144 return false; |
| 144 } | 145 } |
| 145 | 146 |
| 146 Value* roots; | 147 const Value* roots; |
| 147 if (!d_value.Get(kRootsKey, &roots)) | 148 if (!d_value.Get(kRootsKey, &roots)) |
| 148 return false; // No roots. | 149 return false; // No roots. |
| 149 | 150 |
| 150 if (roots->GetType() != Value::TYPE_DICTIONARY) | 151 if (roots->GetType() != Value::TYPE_DICTIONARY) |
| 151 return false; // Invalid type for roots. | 152 return false; // Invalid type for roots. |
| 152 | 153 |
| 153 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots); | 154 const DictionaryValue* roots_d_value = |
| 154 Value* root_folder_value; | 155 static_cast<const DictionaryValue*>(roots); |
| 155 Value* other_folder_value; | 156 const Value* root_folder_value; |
| 157 const Value* other_folder_value; |
| 156 if (!roots_d_value->Get(kRootFolderNameKey, &root_folder_value) || | 158 if (!roots_d_value->Get(kRootFolderNameKey, &root_folder_value) || |
| 157 root_folder_value->GetType() != Value::TYPE_DICTIONARY || | 159 root_folder_value->GetType() != Value::TYPE_DICTIONARY || |
| 158 !roots_d_value->Get(kOtherBookmarkFolderNameKey, &other_folder_value) || | 160 !roots_d_value->Get(kOtherBookmarkFolderNameKey, &other_folder_value) || |
| 159 other_folder_value->GetType() != Value::TYPE_DICTIONARY) { | 161 other_folder_value->GetType() != Value::TYPE_DICTIONARY) { |
| 160 return false; // Invalid type for root folder and/or other | 162 return false; // Invalid type for root folder and/or other |
| 161 // folder. | 163 // folder. |
| 162 } | 164 } |
| 163 DecodeNode(*static_cast<DictionaryValue*>(root_folder_value), NULL, | 165 DecodeNode(*static_cast<const DictionaryValue*>(root_folder_value), NULL, |
| 164 bb_node); | 166 bb_node); |
| 165 DecodeNode(*static_cast<DictionaryValue*>(other_folder_value), NULL, | 167 DecodeNode(*static_cast<const DictionaryValue*>(other_folder_value), NULL, |
| 166 other_folder_node); | 168 other_folder_node); |
| 167 | 169 |
| 168 // Fail silently if we can't deserialize mobile bookmarks. We can't require | 170 // Fail silently if we can't deserialize mobile bookmarks. We can't require |
| 169 // them to exist in order to be backwards-compatible with older versions of | 171 // them to exist in order to be backwards-compatible with older versions of |
| 170 // chrome. | 172 // chrome. |
| 171 Value* mobile_folder_value; | 173 const Value* mobile_folder_value; |
| 172 if (roots_d_value->Get(kMobileBookmarkFolderNameKey, &mobile_folder_value) && | 174 if (roots_d_value->Get(kMobileBookmarkFolderNameKey, &mobile_folder_value) && |
| 173 mobile_folder_value->GetType() == Value::TYPE_DICTIONARY) { | 175 mobile_folder_value->GetType() == Value::TYPE_DICTIONARY) { |
| 174 DecodeNode(*static_cast<DictionaryValue*>(mobile_folder_value), NULL, | 176 DecodeNode(*static_cast<const DictionaryValue*>(mobile_folder_value), NULL, |
| 175 mobile_folder_node); | 177 mobile_folder_node); |
| 176 } else { | 178 } else { |
| 177 // If we didn't find the mobile folder, we're almost guaranteed to have a | 179 // If we didn't find the mobile folder, we're almost guaranteed to have a |
| 178 // duplicate id when we add the mobile folder. Consequently, if we don't | 180 // duplicate id when we add the mobile folder. Consequently, if we don't |
| 179 // intend to reassign ids in the future (ids_valid_ is still true), then at | 181 // intend to reassign ids in the future (ids_valid_ is still true), then at |
| 180 // least reassign the mobile bookmarks to avoid it colliding with anything | 182 // least reassign the mobile bookmarks to avoid it colliding with anything |
| 181 // else. | 183 // else. |
| 182 if (ids_valid_) | 184 if (ids_valid_) |
| 183 ReassignIDsHelper(mobile_folder_node); | 185 ReassignIDsHelper(mobile_folder_node); |
| 184 } | 186 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 | 282 |
| 281 if (parent) | 283 if (parent) |
| 282 parent->Add(node, parent->child_count()); | 284 parent->Add(node, parent->child_count()); |
| 283 node->set_type(BookmarkNode::URL); | 285 node->set_type(BookmarkNode::URL); |
| 284 UpdateChecksumWithUrlNode(id_string, title, url_string); | 286 UpdateChecksumWithUrlNode(id_string, title, url_string); |
| 285 } else { | 287 } else { |
| 286 std::string last_modified_date; | 288 std::string last_modified_date; |
| 287 if (!value.GetString(kDateModifiedKey, &last_modified_date)) | 289 if (!value.GetString(kDateModifiedKey, &last_modified_date)) |
| 288 last_modified_date = base::Int64ToString(Time::Now().ToInternalValue()); | 290 last_modified_date = base::Int64ToString(Time::Now().ToInternalValue()); |
| 289 | 291 |
| 290 Value* child_values; | 292 const Value* child_values; |
| 291 if (!value.Get(kChildrenKey, &child_values)) | 293 if (!value.Get(kChildrenKey, &child_values)) |
| 292 return false; | 294 return false; |
| 293 | 295 |
| 294 if (child_values->GetType() != Value::TYPE_LIST) | 296 if (child_values->GetType() != Value::TYPE_LIST) |
| 295 return false; | 297 return false; |
| 296 | 298 |
| 297 if (!node) { | 299 if (!node) { |
| 298 node = new BookmarkNode(id, GURL()); | 300 node = new BookmarkNode(id, GURL()); |
| 299 } else { | 301 } else { |
| 300 // If a new node is not created, explicitly assign ID to the existing one. | 302 // If a new node is not created, explicitly assign ID to the existing one. |
| 301 node->set_id(id); | 303 node->set_id(id); |
| 302 } | 304 } |
| 303 | 305 |
| 304 node->set_type(BookmarkNode::FOLDER); | 306 node->set_type(BookmarkNode::FOLDER); |
| 305 int64 internal_time; | 307 int64 internal_time; |
| 306 base::StringToInt64(last_modified_date, &internal_time); | 308 base::StringToInt64(last_modified_date, &internal_time); |
| 307 node->set_date_folder_modified(Time::FromInternalValue(internal_time)); | 309 node->set_date_folder_modified(Time::FromInternalValue(internal_time)); |
| 308 | 310 |
| 309 if (parent) | 311 if (parent) |
| 310 parent->Add(node, parent->child_count()); | 312 parent->Add(node, parent->child_count()); |
| 311 | 313 |
| 312 UpdateChecksumWithFolderNode(id_string, title); | 314 UpdateChecksumWithFolderNode(id_string, title); |
| 313 | 315 |
| 314 if (!DecodeChildren(*static_cast<ListValue*>(child_values), node)) | 316 if (!DecodeChildren(*static_cast<const ListValue*>(child_values), node)) |
| 315 return false; | 317 return false; |
| 316 } | 318 } |
| 317 | 319 |
| 318 node->SetTitle(title); | 320 node->SetTitle(title); |
| 319 node->set_date_added(date_added); | 321 node->set_date_added(date_added); |
| 320 return true; | 322 return true; |
| 321 } | 323 } |
| 322 | 324 |
| 323 void BookmarkCodec::ReassignIDs(BookmarkNode* bb_node, | 325 void BookmarkCodec::ReassignIDs(BookmarkNode* bb_node, |
| 324 BookmarkNode* other_node, | 326 BookmarkNode* other_node, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 | 369 |
| 368 void BookmarkCodec::InitializeChecksum() { | 370 void BookmarkCodec::InitializeChecksum() { |
| 369 base::MD5Init(&md5_context_); | 371 base::MD5Init(&md5_context_); |
| 370 } | 372 } |
| 371 | 373 |
| 372 void BookmarkCodec::FinalizeChecksum() { | 374 void BookmarkCodec::FinalizeChecksum() { |
| 373 base::MD5Digest digest; | 375 base::MD5Digest digest; |
| 374 base::MD5Final(&digest, &md5_context_); | 376 base::MD5Final(&digest, &md5_context_); |
| 375 computed_checksum_ = base::MD5DigestToBase16(digest); | 377 computed_checksum_ = base::MD5DigestToBase16(digest); |
| 376 } | 378 } |
| OLD | NEW |