OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/bookmark_codec.h" | |
6 | |
7 #include "base/string_util.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/bookmark_bar_model.h" | |
10 #include "googleurl/src/gurl.h" | |
11 | |
12 #include "generated_resources.h" | |
13 | |
14 // Key names. | |
15 static const wchar_t* kRootsKey = L"roots"; | |
16 static const wchar_t* kRootFolderNameKey = L"bookmark_bar"; | |
17 static const wchar_t* kOtherBookmarFolderNameKey = L"other"; | |
18 static const wchar_t* kVersionKey = L"version"; | |
19 static const wchar_t* kTypeKey = L"type"; | |
20 static const wchar_t* kNameKey = L"name"; | |
21 static const wchar_t* kDateAddedKey = L"date_added"; | |
22 static const wchar_t* kURLKey = L"url"; | |
23 static const wchar_t* kDateModifiedKey = L"date_modified"; | |
24 static const wchar_t* kChildrenKey = L"children"; | |
25 | |
26 // Possible values for kTypeKey. | |
27 static const wchar_t* kTypeURL = L"url"; | |
28 static const wchar_t* kTypeFolder = L"folder"; | |
29 | |
30 // Current version of the file. | |
31 static const int kCurrentVersion = 1; | |
32 | |
33 Value* BookmarkCodec::Encode(BookmarkBarModel* model) { | |
34 return Encode(model->GetBookmarkBarNode(), model->other_node()); | |
35 } | |
36 | |
37 Value* BookmarkCodec::Encode(BookmarkBarNode* bookmark_bar_node, | |
38 BookmarkBarNode* other_folder_node) { | |
39 DictionaryValue* roots = new DictionaryValue(); | |
40 roots->Set(kRootFolderNameKey, EncodeNode(bookmark_bar_node)); | |
41 roots->Set(kOtherBookmarFolderNameKey, EncodeNode(other_folder_node)); | |
42 | |
43 DictionaryValue* main = new DictionaryValue(); | |
44 main->SetInteger(kVersionKey, kCurrentVersion); | |
45 main->Set(kRootsKey, roots); | |
46 return main; | |
47 } | |
48 | |
49 bool BookmarkCodec::Decode(BookmarkBarModel* model, const Value& value) { | |
50 if (value.GetType() != Value::TYPE_DICTIONARY) | |
51 return false; // Unexpected type. | |
52 | |
53 const DictionaryValue& d_value = static_cast<const DictionaryValue&>(value); | |
54 | |
55 int version; | |
56 if (!d_value.GetInteger(kVersionKey, &version) || version != kCurrentVersion) | |
57 return false; // Unknown version. | |
58 | |
59 Value* roots; | |
60 if (!d_value.Get(kRootsKey, &roots)) | |
61 return false; // No roots. | |
62 | |
63 if (roots->GetType() != Value::TYPE_DICTIONARY) | |
64 return false; // Invalid type for roots. | |
65 | |
66 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots); | |
67 Value* root_folder_value; | |
68 Value* other_folder_value; | |
69 if (!roots_d_value->Get(kRootFolderNameKey, &root_folder_value) || | |
70 root_folder_value->GetType() != Value::TYPE_DICTIONARY || | |
71 !roots_d_value->Get(kOtherBookmarFolderNameKey, &other_folder_value) || | |
72 other_folder_value->GetType() != Value::TYPE_DICTIONARY) | |
73 return false; // Invalid type for root folder and/or other folder. | |
74 | |
75 DecodeNode(model, *static_cast<DictionaryValue*>(root_folder_value), | |
76 NULL, model->GetBookmarkBarNode()); | |
77 DecodeNode(model, *static_cast<DictionaryValue*>(other_folder_value), | |
78 NULL, model->other_node()); | |
79 // Need to reset the type as decoding resets the type to FOLDER. Similarly | |
80 // we need to reset the title as the title is persisted and restored from | |
81 // the file. | |
82 model->GetBookmarkBarNode()->type_ = history::StarredEntry::BOOKMARK_BAR; | |
83 model->other_node()->type_ = history::StarredEntry::OTHER; | |
84 model->GetBookmarkBarNode()->SetTitle( | |
85 l10n_util::GetString(IDS_BOOMARK_BAR_FOLDER_NAME)); | |
86 model->other_node()->SetTitle( | |
87 l10n_util::GetString(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); | |
88 return true; | |
89 } | |
90 | |
91 Value* BookmarkCodec::EncodeNode(BookmarkBarNode* node) { | |
92 DictionaryValue* value = new DictionaryValue(); | |
93 value->SetString(kNameKey, node->GetTitle()); | |
94 value->SetString(kDateAddedKey, | |
95 Int64ToWString(node->date_added().ToInternalValue())); | |
96 if (node->GetType() == history::StarredEntry::URL) { | |
97 value->SetString(kTypeKey, kTypeURL); | |
98 value->SetString(kURLKey, | |
99 UTF8ToWide(node->GetURL().possibly_invalid_spec())); | |
100 } else { | |
101 value->SetString(kTypeKey, kTypeFolder); | |
102 value->SetString(kDateModifiedKey, | |
103 Int64ToWString(node->date_group_modified(). | |
104 ToInternalValue())); | |
105 | |
106 ListValue* child_values = new ListValue(); | |
107 value->Set(kChildrenKey, child_values); | |
108 for (int i = 0; i < node->GetChildCount(); ++i) | |
109 child_values->Append(EncodeNode(node->GetChild(i))); | |
110 } | |
111 return value; | |
112 } | |
113 | |
114 bool BookmarkCodec::DecodeChildren(BookmarkBarModel* model, | |
115 const ListValue& child_value_list, | |
116 BookmarkBarNode* parent) { | |
117 for (size_t i = 0; i < child_value_list.GetSize(); ++i) { | |
118 Value* child_value; | |
119 if (!child_value_list.Get(i, &child_value)) | |
120 return false; | |
121 | |
122 if (child_value->GetType() != Value::TYPE_DICTIONARY) | |
123 return false; | |
124 | |
125 if (!DecodeNode(model, *static_cast<DictionaryValue*>(child_value), parent, | |
126 NULL)) { | |
127 return false; | |
128 } | |
129 } | |
130 return true; | |
131 } | |
132 | |
133 bool BookmarkCodec::DecodeNode(BookmarkBarModel* model, | |
134 const DictionaryValue& value, | |
135 BookmarkBarNode* parent, | |
136 BookmarkBarNode* node) { | |
137 bool created_node = (node == NULL); | |
138 std::wstring title; | |
139 if (!value.GetString(kNameKey, &title)) | |
140 return false; | |
141 | |
142 // TODO(sky): this should be more flexible. Don't hoark if we can't parse it | |
143 // all. | |
144 std::wstring date_added_string; | |
145 if (!value.GetString(kDateAddedKey, &date_added_string)) | |
146 return false; | |
147 | |
148 std::wstring type_string; | |
149 if (!value.GetString(kTypeKey, &type_string)) | |
150 return false; | |
151 | |
152 if (type_string != kTypeURL && type_string != kTypeFolder) | |
153 return false; // Unknown type. | |
154 | |
155 if (type_string == kTypeURL) { | |
156 std::wstring url_string; | |
157 if (!value.GetString(kURLKey, &url_string)) | |
158 return false; | |
159 // TODO(sky): this should ignore the node if not a valid URL. | |
160 if (!node) | |
161 node = new BookmarkBarNode(model, GURL(url_string)); | |
162 if (parent) | |
163 parent->Add(parent->GetChildCount(), node); | |
164 node->type_ = history::StarredEntry::URL; | |
165 } else { | |
166 std::wstring last_modified_date; | |
167 if (!value.GetString(kDateModifiedKey, &last_modified_date)) | |
168 return false; | |
169 | |
170 Value* child_values; | |
171 if (!value.Get(kChildrenKey, &child_values)) | |
172 return false; | |
173 | |
174 if (child_values->GetType() != Value::TYPE_LIST) | |
175 return false; | |
176 | |
177 if (!node) | |
178 node = new BookmarkBarNode(model, GURL()); | |
179 node->type_ = history::StarredEntry::USER_GROUP; | |
180 node->date_group_modified_ = | |
181 Time::FromInternalValue(StringToInt64(last_modified_date)); | |
182 | |
183 if (parent) | |
184 parent->Add(parent->GetChildCount(), node); | |
185 | |
186 if (!DecodeChildren(model, *static_cast<ListValue*>(child_values), node)) | |
187 return false; | |
188 } | |
189 | |
190 node->SetTitle(title); | |
191 node->date_added_ = | |
192 Time::FromInternalValue(StringToInt64(date_added_string)); | |
193 return true; | |
194 } | |
195 | |
OLD | NEW |