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" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/bookmarks/bookmark_model.h" | 12 #include "chrome/browser/bookmarks/bookmark_model.h" |
13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
14 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
15 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
16 | 16 |
17 using base::Time; | 17 using base::Time; |
18 | 18 |
19 const char* BookmarkCodec::kRootsKey = "roots"; | 19 const char* BookmarkCodec::kRootsKey = "roots"; |
20 const char* BookmarkCodec::kRootFolderNameKey = "bookmark_bar"; | 20 const char* BookmarkCodec::kRootFolderNameKey = "bookmark_bar"; |
21 const char* BookmarkCodec::kOtherBookmarkFolderNameKey = "other"; | 21 const char* BookmarkCodec::kOtherBookmarkFolderNameKey = "other"; |
| 22 const char* BookmarkCodec::kSyncedBookmarkFolderNameKey = "synced"; |
22 const char* BookmarkCodec::kVersionKey = "version"; | 23 const char* BookmarkCodec::kVersionKey = "version"; |
23 const char* BookmarkCodec::kChecksumKey = "checksum"; | 24 const char* BookmarkCodec::kChecksumKey = "checksum"; |
24 const char* BookmarkCodec::kIdKey = "id"; | 25 const char* BookmarkCodec::kIdKey = "id"; |
25 const char* BookmarkCodec::kTypeKey = "type"; | 26 const char* BookmarkCodec::kTypeKey = "type"; |
26 const char* BookmarkCodec::kNameKey = "name"; | 27 const char* BookmarkCodec::kNameKey = "name"; |
27 const char* BookmarkCodec::kDateAddedKey = "date_added"; | 28 const char* BookmarkCodec::kDateAddedKey = "date_added"; |
28 const char* BookmarkCodec::kURLKey = "url"; | 29 const char* BookmarkCodec::kURLKey = "url"; |
29 const char* BookmarkCodec::kDateModifiedKey = "date_modified"; | 30 const char* BookmarkCodec::kDateModifiedKey = "date_modified"; |
30 const char* BookmarkCodec::kChildrenKey = "children"; | 31 const char* BookmarkCodec::kChildrenKey = "children"; |
31 const char* BookmarkCodec::kTypeURL = "url"; | 32 const char* BookmarkCodec::kTypeURL = "url"; |
32 const char* BookmarkCodec::kTypeFolder = "folder"; | 33 const char* BookmarkCodec::kTypeFolder = "folder"; |
33 | 34 |
34 // Current version of the file. | 35 // Current version of the file. |
35 static const int kCurrentVersion = 1; | 36 static const int kCurrentVersion = 1; |
36 | 37 |
37 BookmarkCodec::BookmarkCodec() | 38 BookmarkCodec::BookmarkCodec() |
38 : ids_reassigned_(false), | 39 : ids_reassigned_(false), |
39 ids_valid_(true), | 40 ids_valid_(true), |
40 maximum_id_(0) { | 41 maximum_id_(0) { |
41 } | 42 } |
42 | 43 |
43 BookmarkCodec::~BookmarkCodec() {} | 44 BookmarkCodec::~BookmarkCodec() {} |
44 | 45 |
45 Value* BookmarkCodec::Encode(BookmarkModel* model) { | 46 Value* BookmarkCodec::Encode(BookmarkModel* model) { |
46 return Encode(model->GetBookmarkBarNode(), model->other_node()); | 47 return Encode(model->GetBookmarkBarNode(), model->other_node(), |
| 48 model->synced_node()); |
47 } | 49 } |
48 | 50 |
49 Value* BookmarkCodec::Encode(const BookmarkNode* bookmark_bar_node, | 51 Value* BookmarkCodec::Encode(const BookmarkNode* bookmark_bar_node, |
50 const BookmarkNode* other_folder_node) { | 52 const BookmarkNode* other_folder_node, |
| 53 const BookmarkNode* synced_folder_node) { |
51 ids_reassigned_ = false; | 54 ids_reassigned_ = false; |
52 InitializeChecksum(); | 55 InitializeChecksum(); |
53 DictionaryValue* roots = new DictionaryValue(); | 56 DictionaryValue* roots = new DictionaryValue(); |
54 roots->Set(kRootFolderNameKey, EncodeNode(bookmark_bar_node)); | 57 roots->Set(kRootFolderNameKey, EncodeNode(bookmark_bar_node)); |
55 roots->Set(kOtherBookmarkFolderNameKey, EncodeNode(other_folder_node)); | 58 roots->Set(kOtherBookmarkFolderNameKey, EncodeNode(other_folder_node)); |
| 59 roots->Set(kSyncedBookmarkFolderNameKey, EncodeNode(synced_folder_node)); |
56 | 60 |
57 DictionaryValue* main = new DictionaryValue(); | 61 DictionaryValue* main = new DictionaryValue(); |
58 main->SetInteger(kVersionKey, kCurrentVersion); | 62 main->SetInteger(kVersionKey, kCurrentVersion); |
59 FinalizeChecksum(); | 63 FinalizeChecksum(); |
60 // We are going to store the computed checksum. So set stored checksum to be | 64 // We are going to store the computed checksum. So set stored checksum to be |
61 // the same as computed checksum. | 65 // the same as computed checksum. |
62 stored_checksum_ = computed_checksum_; | 66 stored_checksum_ = computed_checksum_; |
63 main->Set(kChecksumKey, Value::CreateStringValue(computed_checksum_)); | 67 main->Set(kChecksumKey, Value::CreateStringValue(computed_checksum_)); |
64 main->Set(kRootsKey, roots); | 68 main->Set(kRootsKey, roots); |
65 return main; | 69 return main; |
66 } | 70 } |
67 | 71 |
68 bool BookmarkCodec::Decode(BookmarkNode* bb_node, | 72 bool BookmarkCodec::Decode(BookmarkNode* bb_node, |
69 BookmarkNode* other_folder_node, | 73 BookmarkNode* other_folder_node, |
| 74 BookmarkNode* synced_folder_node, |
70 int64* max_id, | 75 int64* max_id, |
71 const Value& value) { | 76 const Value& value) { |
72 ids_.clear(); | 77 ids_.clear(); |
73 ids_reassigned_ = false; | 78 ids_reassigned_ = false; |
74 ids_valid_ = true; | 79 ids_valid_ = true; |
75 maximum_id_ = 0; | 80 maximum_id_ = 0; |
76 stored_checksum_.clear(); | 81 stored_checksum_.clear(); |
77 InitializeChecksum(); | 82 InitializeChecksum(); |
78 bool success = DecodeHelper(bb_node, other_folder_node, value); | 83 bool success = DecodeHelper(bb_node, other_folder_node, synced_folder_node, |
| 84 value); |
79 FinalizeChecksum(); | 85 FinalizeChecksum(); |
80 // If either the checksums differ or some IDs were missing/not unique, | 86 // If either the checksums differ or some IDs were missing/not unique, |
81 // reassign IDs. | 87 // reassign IDs. |
82 if (!ids_valid_ || computed_checksum() != stored_checksum()) | 88 if (!ids_valid_ || computed_checksum() != stored_checksum()) |
83 ReassignIDs(bb_node, other_folder_node); | 89 ReassignIDs(bb_node, other_folder_node, synced_folder_node); |
84 *max_id = maximum_id_ + 1; | 90 *max_id = maximum_id_ + 1; |
85 return success; | 91 return success; |
86 } | 92 } |
87 | 93 |
88 Value* BookmarkCodec::EncodeNode(const BookmarkNode* node) { | 94 Value* BookmarkCodec::EncodeNode(const BookmarkNode* node) { |
89 DictionaryValue* value = new DictionaryValue(); | 95 DictionaryValue* value = new DictionaryValue(); |
90 std::string id = base::Int64ToString(node->id()); | 96 std::string id = base::Int64ToString(node->id()); |
91 value->SetString(kIdKey, id); | 97 value->SetString(kIdKey, id); |
92 const string16& title = node->GetTitle(); | 98 const string16& title = node->GetTitle(); |
93 value->SetString(kNameKey, title); | 99 value->SetString(kNameKey, title); |
(...skipping 14 matching lines...) Expand all Loading... |
108 ListValue* child_values = new ListValue(); | 114 ListValue* child_values = new ListValue(); |
109 value->Set(kChildrenKey, child_values); | 115 value->Set(kChildrenKey, child_values); |
110 for (int i = 0; i < node->child_count(); ++i) | 116 for (int i = 0; i < node->child_count(); ++i) |
111 child_values->Append(EncodeNode(node->GetChild(i))); | 117 child_values->Append(EncodeNode(node->GetChild(i))); |
112 } | 118 } |
113 return value; | 119 return value; |
114 } | 120 } |
115 | 121 |
116 bool BookmarkCodec::DecodeHelper(BookmarkNode* bb_node, | 122 bool BookmarkCodec::DecodeHelper(BookmarkNode* bb_node, |
117 BookmarkNode* other_folder_node, | 123 BookmarkNode* other_folder_node, |
| 124 BookmarkNode* synced_folder_node, |
118 const Value& value) { | 125 const Value& value) { |
119 if (value.GetType() != Value::TYPE_DICTIONARY) | 126 if (value.GetType() != Value::TYPE_DICTIONARY) |
120 return false; // Unexpected type. | 127 return false; // Unexpected type. |
121 | 128 |
122 const DictionaryValue& d_value = static_cast<const DictionaryValue&>(value); | 129 const DictionaryValue& d_value = static_cast<const DictionaryValue&>(value); |
123 | 130 |
124 int version; | 131 int version; |
125 if (!d_value.GetInteger(kVersionKey, &version) || version != kCurrentVersion) | 132 if (!d_value.GetInteger(kVersionKey, &version) || version != kCurrentVersion) |
126 return false; // Unknown version. | 133 return false; // Unknown version. |
127 | 134 |
128 Value* checksum_value; | 135 Value* checksum_value; |
129 if (d_value.Get(kChecksumKey, &checksum_value)) { | 136 if (d_value.Get(kChecksumKey, &checksum_value)) { |
130 if (checksum_value->GetType() != Value::TYPE_STRING) | 137 if (checksum_value->GetType() != Value::TYPE_STRING) |
131 return false; | 138 return false; |
132 StringValue* checksum_value_str = static_cast<StringValue*>(checksum_value); | 139 StringValue* checksum_value_str = static_cast<StringValue*>(checksum_value); |
133 if (!checksum_value_str->GetAsString(&stored_checksum_)) | 140 if (!checksum_value_str->GetAsString(&stored_checksum_)) |
134 return false; | 141 return false; |
135 } | 142 } |
136 | 143 |
137 Value* roots; | 144 Value* roots; |
138 if (!d_value.Get(kRootsKey, &roots)) | 145 if (!d_value.Get(kRootsKey, &roots)) |
139 return false; // No roots. | 146 return false; // No roots. |
140 | 147 |
141 if (roots->GetType() != Value::TYPE_DICTIONARY) | 148 if (roots->GetType() != Value::TYPE_DICTIONARY) |
142 return false; // Invalid type for roots. | 149 return false; // Invalid type for roots. |
143 | 150 |
144 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots); | 151 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots); |
145 Value* root_folder_value; | 152 Value* root_folder_value; |
146 Value* other_folder_value; | 153 Value* other_folder_value; |
| 154 Value* synced_folder_value; |
147 if (!roots_d_value->Get(kRootFolderNameKey, &root_folder_value) || | 155 if (!roots_d_value->Get(kRootFolderNameKey, &root_folder_value) || |
148 root_folder_value->GetType() != Value::TYPE_DICTIONARY || | 156 root_folder_value->GetType() != Value::TYPE_DICTIONARY || |
149 !roots_d_value->Get(kOtherBookmarkFolderNameKey, &other_folder_value) || | 157 !roots_d_value->Get(kOtherBookmarkFolderNameKey, &other_folder_value) || |
150 other_folder_value->GetType() != Value::TYPE_DICTIONARY) | 158 other_folder_value->GetType() != Value::TYPE_DICTIONARY || |
151 return false; // Invalid type for root folder and/or other folder. | 159 !roots_d_value->Get(kSyncedBookmarkFolderNameKey, &synced_folder_value) || |
| 160 synced_folder_value->GetType() != Value::TYPE_DICTIONARY) { |
| 161 return false; // Invalid type for root folder and/or synced and/or other |
| 162 // folder. |
| 163 } |
152 | 164 |
153 DecodeNode(*static_cast<DictionaryValue*>(root_folder_value), NULL, | 165 DecodeNode(*static_cast<DictionaryValue*>(root_folder_value), NULL, |
154 bb_node); | 166 bb_node); |
155 DecodeNode(*static_cast<DictionaryValue*>(other_folder_value), NULL, | 167 DecodeNode(*static_cast<DictionaryValue*>(other_folder_value), NULL, |
156 other_folder_node); | 168 other_folder_node); |
| 169 DecodeNode(*static_cast<DictionaryValue*>(synced_folder_value), NULL, |
| 170 synced_folder_node); |
157 // Need to reset the type as decoding resets the type to FOLDER. Similarly | 171 // Need to reset the type as decoding resets the type to FOLDER. Similarly |
158 // we need to reset the title as the title is persisted and restored from | 172 // we need to reset the title as the title is persisted and restored from |
159 // the file. | 173 // the file. |
160 bb_node->set_type(BookmarkNode::BOOKMARK_BAR); | 174 bb_node->set_type(BookmarkNode::BOOKMARK_BAR); |
161 other_folder_node->set_type(BookmarkNode::OTHER_NODE); | 175 other_folder_node->set_type(BookmarkNode::OTHER_NODE); |
| 176 synced_folder_node->set_type(BookmarkNode::SYNCED); |
162 bb_node->set_title(l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_FOLDER_NAME)); | 177 bb_node->set_title(l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_FOLDER_NAME)); |
163 other_folder_node->set_title( | 178 other_folder_node->set_title( |
164 l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); | 179 l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); |
| 180 synced_folder_node->set_title( |
| 181 l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_SYNCED_FOLDER_NAME)); |
165 | 182 |
166 return true; | 183 return true; |
167 } | 184 } |
168 | 185 |
169 bool BookmarkCodec::DecodeChildren(const ListValue& child_value_list, | 186 bool BookmarkCodec::DecodeChildren(const ListValue& child_value_list, |
170 BookmarkNode* parent) { | 187 BookmarkNode* parent) { |
171 for (size_t i = 0; i < child_value_list.GetSize(); ++i) { | 188 for (size_t i = 0; i < child_value_list.GetSize(); ++i) { |
172 Value* child_value; | 189 Value* child_value; |
173 if (!child_value_list.Get(i, &child_value)) | 190 if (!child_value_list.Get(i, &child_value)) |
174 return false; | 191 return false; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 if (!DecodeChildren(*static_cast<ListValue*>(child_values), node)) | 299 if (!DecodeChildren(*static_cast<ListValue*>(child_values), node)) |
283 return false; | 300 return false; |
284 } | 301 } |
285 | 302 |
286 node->set_title(title); | 303 node->set_title(title); |
287 node->set_date_added(date_added); | 304 node->set_date_added(date_added); |
288 return true; | 305 return true; |
289 } | 306 } |
290 | 307 |
291 void BookmarkCodec::ReassignIDs(BookmarkNode* bb_node, | 308 void BookmarkCodec::ReassignIDs(BookmarkNode* bb_node, |
292 BookmarkNode* other_node) { | 309 BookmarkNode* other_node, |
| 310 BookmarkNode* synced_node) { |
293 maximum_id_ = 0; | 311 maximum_id_ = 0; |
294 ReassignIDsHelper(bb_node); | 312 ReassignIDsHelper(bb_node); |
295 ReassignIDsHelper(other_node); | 313 ReassignIDsHelper(other_node); |
| 314 ReassignIDsHelper(synced_node); |
296 ids_reassigned_ = true; | 315 ids_reassigned_ = true; |
297 } | 316 } |
298 | 317 |
299 void BookmarkCodec::ReassignIDsHelper(BookmarkNode* node) { | 318 void BookmarkCodec::ReassignIDsHelper(BookmarkNode* node) { |
300 DCHECK(node); | 319 DCHECK(node); |
301 node->set_id(++maximum_id_); | 320 node->set_id(++maximum_id_); |
302 for (int i = 0; i < node->child_count(); ++i) | 321 for (int i = 0; i < node->child_count(); ++i) |
303 ReassignIDsHelper(node->GetChild(i)); | 322 ReassignIDsHelper(node->GetChild(i)); |
304 } | 323 } |
305 | 324 |
(...skipping 24 matching lines...) Expand all Loading... |
330 | 349 |
331 void BookmarkCodec::InitializeChecksum() { | 350 void BookmarkCodec::InitializeChecksum() { |
332 MD5Init(&md5_context_); | 351 MD5Init(&md5_context_); |
333 } | 352 } |
334 | 353 |
335 void BookmarkCodec::FinalizeChecksum() { | 354 void BookmarkCodec::FinalizeChecksum() { |
336 MD5Digest digest; | 355 MD5Digest digest; |
337 MD5Final(&digest, &md5_context_); | 356 MD5Final(&digest, &md5_context_); |
338 computed_checksum_ = MD5DigestToBase16(digest); | 357 computed_checksum_ = MD5DigestToBase16(digest); |
339 } | 358 } |
OLD | NEW |