OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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_html_writer.h" | 5 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 } | 78 } |
79 | 79 |
80 virtual void Run() { | 80 virtual void Run() { |
81 if (!OpenFile()) | 81 if (!OpenFile()) |
82 return; | 82 return; |
83 | 83 |
84 Value* roots; | 84 Value* roots; |
85 if (!Write(kHeader) || | 85 if (!Write(kHeader) || |
86 bookmarks_->GetType() != Value::TYPE_DICTIONARY || | 86 bookmarks_->GetType() != Value::TYPE_DICTIONARY || |
87 !static_cast<DictionaryValue*>(bookmarks_.get())->Get( | 87 !static_cast<DictionaryValue*>(bookmarks_.get())->Get( |
88 BookmarkCodec::kRootsKey, &roots) || | 88 WideToUTF16Hack(BookmarkCodec::kRootsKey), &roots) || |
89 roots->GetType() != Value::TYPE_DICTIONARY) { | 89 roots->GetType() != Value::TYPE_DICTIONARY) { |
90 NOTREACHED(); | 90 NOTREACHED(); |
91 return; | 91 return; |
92 } | 92 } |
93 | 93 |
94 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots); | 94 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots); |
95 Value* root_folder_value; | 95 Value* root_folder_value; |
96 Value* other_folder_value; | 96 Value* other_folder_value; |
97 if (!roots_d_value->Get(BookmarkCodec::kRootFolderNameKey, | 97 if (!roots_d_value->Get(WideToUTF16Hack(BookmarkCodec::kRootFolderNameKey), |
98 &root_folder_value) || | 98 &root_folder_value) || |
99 root_folder_value->GetType() != Value::TYPE_DICTIONARY || | 99 root_folder_value->GetType() != Value::TYPE_DICTIONARY || |
100 !roots_d_value->Get(BookmarkCodec::kOtherBookmarFolderNameKey, | 100 !roots_d_value->Get( |
101 &other_folder_value) || | 101 WideToUTF16Hack(BookmarkCodec::kOtherBookmarFolderNameKey), |
| 102 &other_folder_value) || |
102 other_folder_value->GetType() != Value::TYPE_DICTIONARY) { | 103 other_folder_value->GetType() != Value::TYPE_DICTIONARY) { |
103 NOTREACHED(); | 104 NOTREACHED(); |
104 return; // Invalid type for root folder and/or other folder. | 105 return; // Invalid type for root folder and/or other folder. |
105 } | 106 } |
106 | 107 |
107 IncrementIndent(); | 108 IncrementIndent(); |
108 | 109 |
109 if (!WriteNode(*static_cast<DictionaryValue*>(root_folder_value), | 110 if (!WriteNode(*static_cast<DictionaryValue*>(root_folder_value), |
110 history::StarredEntry::BOOKMARK_BAR) || | 111 history::StarredEntry::BOOKMARK_BAR) || |
111 !WriteNode(*static_cast<DictionaryValue*>(other_folder_value), | 112 !WriteNode(*static_cast<DictionaryValue*>(other_folder_value), |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 // (used by bookmarks.html) and writes it. | 197 // (used by bookmarks.html) and writes it. |
197 bool WriteTime(const std::wstring& time_string) { | 198 bool WriteTime(const std::wstring& time_string) { |
198 base::Time time = base::Time::FromInternalValue( | 199 base::Time time = base::Time::FromInternalValue( |
199 StringToInt64(WideToUTF16Hack(time_string))); | 200 StringToInt64(WideToUTF16Hack(time_string))); |
200 return Write(Int64ToString(time.ToTimeT())); | 201 return Write(Int64ToString(time.ToTimeT())); |
201 } | 202 } |
202 | 203 |
203 // Writes the node and all its children, returning true on success. | 204 // Writes the node and all its children, returning true on success. |
204 bool WriteNode(const DictionaryValue& value, | 205 bool WriteNode(const DictionaryValue& value, |
205 history::StarredEntry::Type folder_type) { | 206 history::StarredEntry::Type folder_type) { |
206 std::wstring title, date_added_string, type_string; | 207 string16 title, date_added_string, type_string; |
207 if (!value.GetString(BookmarkCodec::kNameKey, &title) || | 208 if (!value.GetString(WideToUTF16Hack(BookmarkCodec::kNameKey), &title) || |
208 !value.GetString(BookmarkCodec::kDateAddedKey, &date_added_string) || | 209 !value.GetString(WideToUTF16Hack(BookmarkCodec::kDateAddedKey), |
209 !value.GetString(BookmarkCodec::kTypeKey, &type_string) || | 210 &date_added_string) || |
210 (type_string != BookmarkCodec::kTypeURL && | 211 !value.GetString(WideToUTF16Hack(BookmarkCodec::kTypeKey), |
211 type_string != BookmarkCodec::kTypeFolder)) { | 212 &type_string) || |
| 213 (type_string != WideToUTF16Hack(BookmarkCodec::kTypeURL) && |
| 214 type_string != WideToUTF16Hack(BookmarkCodec::kTypeFolder))) { |
212 NOTREACHED(); | 215 NOTREACHED(); |
213 return false; | 216 return false; |
214 } | 217 } |
215 | 218 |
216 if (type_string == BookmarkCodec::kTypeURL) { | 219 if (type_string == WideToUTF16Hack(BookmarkCodec::kTypeURL)) { |
217 std::wstring url_string; | 220 string16 url_string; |
218 if (!value.GetString(BookmarkCodec::kURLKey, &url_string)) { | 221 if (!value.GetString(WideToUTF16Hack(BookmarkCodec::kURLKey), |
| 222 &url_string)) { |
219 NOTREACHED(); | 223 NOTREACHED(); |
220 return false; | 224 return false; |
221 } | 225 } |
222 if (!WriteIndent() || | 226 if (!WriteIndent() || |
223 !Write(kBookmarkStart) || | 227 !Write(kBookmarkStart) || |
224 !Write(url_string, ATTRIBUTE_VALUE) || | 228 !Write(UTF16ToWideHack(url_string), ATTRIBUTE_VALUE) || |
225 !Write(kAddDate) || | 229 !Write(kAddDate) || |
226 !WriteTime(date_added_string) || | 230 !WriteTime(UTF16ToWideHack(date_added_string)) || |
227 !Write(kBookmarkAttributeEnd) || | 231 !Write(kBookmarkAttributeEnd) || |
228 !Write(title, CONTENT) || | 232 !Write(UTF16ToWideHack(title), CONTENT) || |
229 !Write(kBookmarkEnd) || | 233 !Write(kBookmarkEnd) || |
230 !Write(kNewline)) { | 234 !Write(kNewline)) { |
231 return false; | 235 return false; |
232 } | 236 } |
233 return true; | 237 return true; |
234 } | 238 } |
235 | 239 |
236 // Folder. | 240 // Folder. |
237 std::wstring last_modified_date; | 241 string16 last_modified_date; |
238 Value* child_values; | 242 Value* child_values; |
239 if (!value.GetString(BookmarkCodec::kDateModifiedKey, | 243 if (!value.GetString(WideToUTF16Hack(BookmarkCodec::kDateModifiedKey), |
240 &last_modified_date) || | 244 &last_modified_date) || |
241 !value.Get(BookmarkCodec::kChildrenKey, &child_values) || | 245 !value.Get(WideToUTF16Hack(BookmarkCodec::kChildrenKey), |
| 246 &child_values) || |
242 child_values->GetType() != Value::TYPE_LIST) { | 247 child_values->GetType() != Value::TYPE_LIST) { |
243 NOTREACHED(); | 248 NOTREACHED(); |
244 return false; | 249 return false; |
245 } | 250 } |
246 if (folder_type != history::StarredEntry::OTHER) { | 251 if (folder_type != history::StarredEntry::OTHER) { |
247 // The other folder name is not written out. This gives the effect of | 252 // The other folder name is not written out. This gives the effect of |
248 // making the contents of the 'other folder' be a sibling to the bookmark | 253 // making the contents of the 'other folder' be a sibling to the bookmark |
249 // bar folder. | 254 // bar folder. |
250 if (!WriteIndent() || | 255 if (!WriteIndent() || |
251 !Write(kFolderStart) || | 256 !Write(kFolderStart) || |
252 !WriteTime(date_added_string) || | 257 !WriteTime(UTF16ToWideHack(date_added_string)) || |
253 !Write(kLastModified) || | 258 !Write(kLastModified) || |
254 !WriteTime(last_modified_date)) { | 259 !WriteTime(UTF16ToWideHack(last_modified_date))) { |
255 return false; | 260 return false; |
256 } | 261 } |
257 if (folder_type == history::StarredEntry::BOOKMARK_BAR) { | 262 if (folder_type == history::StarredEntry::BOOKMARK_BAR) { |
258 if (!Write(kBookmarkBar)) | 263 if (!Write(kBookmarkBar)) |
259 return false; | 264 return false; |
260 title = L"Bookmark Bar"; | 265 title = ASCIIToUTF16("Bookmark Bar"); |
261 } else if (!Write(kFolderAttributeEnd)) { | 266 } else if (!Write(kFolderAttributeEnd)) { |
262 return false; | 267 return false; |
263 } | 268 } |
264 if (!Write(title, CONTENT) || | 269 if (!Write(UTF16ToWideHack(title), CONTENT) || |
265 !Write(kFolderEnd) || | 270 !Write(kFolderEnd) || |
266 !Write(kNewline) || | 271 !Write(kNewline) || |
267 !WriteIndent() || | 272 !WriteIndent() || |
268 !Write(kFolderChildren) || | 273 !Write(kFolderChildren) || |
269 !Write(kNewline)) { | 274 !Write(kNewline)) { |
270 return false; | 275 return false; |
271 } | 276 } |
272 IncrementIndent(); | 277 IncrementIndent(); |
273 } | 278 } |
274 | 279 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 BookmarkCodec codec; | 329 BookmarkCodec codec; |
325 scoped_ptr<Writer> writer(new Writer(codec.Encode(model), | 330 scoped_ptr<Writer> writer(new Writer(codec.Encode(model), |
326 FilePath::FromWStringHack(path))); | 331 FilePath::FromWStringHack(path))); |
327 if (thread) | 332 if (thread) |
328 thread->PostTask(FROM_HERE, writer.release()); | 333 thread->PostTask(FROM_HERE, writer.release()); |
329 else | 334 else |
330 writer->Run(); | 335 writer->Run(); |
331 } | 336 } |
332 | 337 |
333 } // namespace bookmark_html_writer | 338 } // namespace bookmark_html_writer |
OLD | NEW |