OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/base64.h" | 7 #include "base/base64.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 path_(path), | 95 path_(path), |
96 favicons_map_(favicons_map), | 96 favicons_map_(favicons_map), |
97 observer_(observer) { | 97 observer_(observer) { |
98 } | 98 } |
99 | 99 |
100 // Writing bookmarks and favicons data to file. | 100 // Writing bookmarks and favicons data to file. |
101 void DoWrite() { | 101 void DoWrite() { |
102 if (!OpenFile()) | 102 if (!OpenFile()) |
103 return; | 103 return; |
104 | 104 |
105 Value* roots = NULL; | 105 base::Value* roots = NULL; |
106 if (!Write(kHeader) || | 106 if (!Write(kHeader) || |
107 bookmarks_->GetType() != Value::TYPE_DICTIONARY || | 107 bookmarks_->GetType() != base::Value::TYPE_DICTIONARY || |
108 !static_cast<DictionaryValue*>(bookmarks_.get())->Get( | 108 !static_cast<base::DictionaryValue*>(bookmarks_.get())->Get( |
109 BookmarkCodec::kRootsKey, &roots) || | 109 BookmarkCodec::kRootsKey, &roots) || |
110 roots->GetType() != Value::TYPE_DICTIONARY) { | 110 roots->GetType() != base::Value::TYPE_DICTIONARY) { |
111 NOTREACHED(); | 111 NOTREACHED(); |
112 return; | 112 return; |
113 } | 113 } |
114 | 114 |
115 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots); | 115 base::DictionaryValue* roots_d_value = |
116 Value* root_folder_value; | 116 static_cast<base::DictionaryValue*>(roots); |
117 Value* other_folder_value = NULL; | 117 base::Value* root_folder_value; |
118 Value* mobile_folder_value = NULL; | 118 base::Value* other_folder_value = NULL; |
| 119 base::Value* mobile_folder_value = NULL; |
119 if (!roots_d_value->Get(BookmarkCodec::kRootFolderNameKey, | 120 if (!roots_d_value->Get(BookmarkCodec::kRootFolderNameKey, |
120 &root_folder_value) || | 121 &root_folder_value) || |
121 root_folder_value->GetType() != Value::TYPE_DICTIONARY || | 122 root_folder_value->GetType() != base::Value::TYPE_DICTIONARY || |
122 !roots_d_value->Get(BookmarkCodec::kOtherBookmarkFolderNameKey, | 123 !roots_d_value->Get(BookmarkCodec::kOtherBookmarkFolderNameKey, |
123 &other_folder_value) || | 124 &other_folder_value) || |
124 other_folder_value->GetType() != Value::TYPE_DICTIONARY || | 125 other_folder_value->GetType() != base::Value::TYPE_DICTIONARY || |
125 !roots_d_value->Get(BookmarkCodec::kMobileBookmarkFolderNameKey, | 126 !roots_d_value->Get(BookmarkCodec::kMobileBookmarkFolderNameKey, |
126 &mobile_folder_value) || | 127 &mobile_folder_value) || |
127 mobile_folder_value->GetType() != Value::TYPE_DICTIONARY) { | 128 mobile_folder_value->GetType() != base::Value::TYPE_DICTIONARY) { |
128 NOTREACHED(); | 129 NOTREACHED(); |
129 return; // Invalid type for root folder and/or other folder. | 130 return; // Invalid type for root folder and/or other folder. |
130 } | 131 } |
131 | 132 |
132 IncrementIndent(); | 133 IncrementIndent(); |
133 | 134 |
134 if (!WriteNode(*static_cast<DictionaryValue*>(root_folder_value), | 135 if (!WriteNode(*static_cast<base::DictionaryValue*>(root_folder_value), |
135 BookmarkNode::BOOKMARK_BAR) || | 136 BookmarkNode::BOOKMARK_BAR) || |
136 !WriteNode(*static_cast<DictionaryValue*>(other_folder_value), | 137 !WriteNode(*static_cast<base::DictionaryValue*>(other_folder_value), |
137 BookmarkNode::OTHER_NODE) || | 138 BookmarkNode::OTHER_NODE) || |
138 !WriteNode(*static_cast<DictionaryValue*>(mobile_folder_value), | 139 !WriteNode(*static_cast<base::DictionaryValue*>(mobile_folder_value), |
139 BookmarkNode::MOBILE)) { | 140 BookmarkNode::MOBILE)) { |
140 return; | 141 return; |
141 } | 142 } |
142 | 143 |
143 DecrementIndent(); | 144 DecrementIndent(); |
144 | 145 |
145 Write(kFolderChildrenEnd); | 146 Write(kFolderChildrenEnd); |
146 Write(kNewline); | 147 Write(kNewline); |
147 // File stream close is forced so that unit test could read it. | 148 // File stream close is forced so that unit test could read it. |
148 file_stream_.reset(); | 149 file_stream_.reset(); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 // Converts a time string written to the JSON codec into a time_t string | 236 // Converts a time string written to the JSON codec into a time_t string |
236 // (used by bookmarks.html) and writes it. | 237 // (used by bookmarks.html) and writes it. |
237 bool WriteTime(const std::string& time_string) { | 238 bool WriteTime(const std::string& time_string) { |
238 int64 internal_value; | 239 int64 internal_value; |
239 base::StringToInt64(time_string, &internal_value); | 240 base::StringToInt64(time_string, &internal_value); |
240 return Write(base::Int64ToString( | 241 return Write(base::Int64ToString( |
241 base::Time::FromInternalValue(internal_value).ToTimeT())); | 242 base::Time::FromInternalValue(internal_value).ToTimeT())); |
242 } | 243 } |
243 | 244 |
244 // Writes the node and all its children, returning true on success. | 245 // Writes the node and all its children, returning true on success. |
245 bool WriteNode(const DictionaryValue& value, | 246 bool WriteNode(const base::DictionaryValue& value, |
246 BookmarkNode::Type folder_type) { | 247 BookmarkNode::Type folder_type) { |
247 std::string title, date_added_string, type_string; | 248 std::string title, date_added_string, type_string; |
248 if (!value.GetString(BookmarkCodec::kNameKey, &title) || | 249 if (!value.GetString(BookmarkCodec::kNameKey, &title) || |
249 !value.GetString(BookmarkCodec::kDateAddedKey, &date_added_string) || | 250 !value.GetString(BookmarkCodec::kDateAddedKey, &date_added_string) || |
250 !value.GetString(BookmarkCodec::kTypeKey, &type_string) || | 251 !value.GetString(BookmarkCodec::kTypeKey, &type_string) || |
251 (type_string != BookmarkCodec::kTypeURL && | 252 (type_string != BookmarkCodec::kTypeURL && |
252 type_string != BookmarkCodec::kTypeFolder)) { | 253 type_string != BookmarkCodec::kTypeFolder)) { |
253 NOTREACHED(); | 254 NOTREACHED(); |
254 return false; | 255 return false; |
255 } | 256 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 !Write(title, CONTENT) || | 288 !Write(title, CONTENT) || |
288 !Write(kBookmarkEnd) || | 289 !Write(kBookmarkEnd) || |
289 !Write(kNewline)) { | 290 !Write(kNewline)) { |
290 return false; | 291 return false; |
291 } | 292 } |
292 return true; | 293 return true; |
293 } | 294 } |
294 | 295 |
295 // Folder. | 296 // Folder. |
296 std::string last_modified_date; | 297 std::string last_modified_date; |
297 const Value* child_values = NULL; | 298 const base::Value* child_values = NULL; |
298 if (!value.GetString(BookmarkCodec::kDateModifiedKey, | 299 if (!value.GetString(BookmarkCodec::kDateModifiedKey, |
299 &last_modified_date) || | 300 &last_modified_date) || |
300 !value.Get(BookmarkCodec::kChildrenKey, &child_values) || | 301 !value.Get(BookmarkCodec::kChildrenKey, &child_values) || |
301 child_values->GetType() != Value::TYPE_LIST) { | 302 child_values->GetType() != base::Value::TYPE_LIST) { |
302 NOTREACHED(); | 303 NOTREACHED(); |
303 return false; | 304 return false; |
304 } | 305 } |
305 if (folder_type != BookmarkNode::OTHER_NODE && | 306 if (folder_type != BookmarkNode::OTHER_NODE && |
306 folder_type != BookmarkNode::MOBILE) { | 307 folder_type != BookmarkNode::MOBILE) { |
307 // The other/mobile folder name are not written out. This gives the effect | 308 // The other/mobile folder name are not written out. This gives the effect |
308 // of making the contents of the 'other folder' be a sibling to the | 309 // of making the contents of the 'other folder' be a sibling to the |
309 // bookmark bar folder. | 310 // bookmark bar folder. |
310 if (!WriteIndent() || | 311 if (!WriteIndent() || |
311 !Write(kFolderStart) || | 312 !Write(kFolderStart) || |
(...skipping 14 matching lines...) Expand all Loading... |
326 !Write(kNewline) || | 327 !Write(kNewline) || |
327 !WriteIndent() || | 328 !WriteIndent() || |
328 !Write(kFolderChildren) || | 329 !Write(kFolderChildren) || |
329 !Write(kNewline)) { | 330 !Write(kNewline)) { |
330 return false; | 331 return false; |
331 } | 332 } |
332 IncrementIndent(); | 333 IncrementIndent(); |
333 } | 334 } |
334 | 335 |
335 // Write the children. | 336 // Write the children. |
336 const ListValue* children = static_cast<const ListValue*>(child_values); | 337 const base::ListValue* children = |
| 338 static_cast<const base::ListValue*>(child_values); |
337 for (size_t i = 0; i < children->GetSize(); ++i) { | 339 for (size_t i = 0; i < children->GetSize(); ++i) { |
338 const Value* child_value; | 340 const base::Value* child_value; |
339 if (!children->Get(i, &child_value) || | 341 if (!children->Get(i, &child_value) || |
340 child_value->GetType() != Value::TYPE_DICTIONARY) { | 342 child_value->GetType() != base::Value::TYPE_DICTIONARY) { |
341 NOTREACHED(); | 343 NOTREACHED(); |
342 return false; | 344 return false; |
343 } | 345 } |
344 if (!WriteNode(*static_cast<const DictionaryValue*>(child_value), | 346 if (!WriteNode(*static_cast<const base::DictionaryValue*>(child_value), |
345 BookmarkNode::FOLDER)) { | 347 BookmarkNode::FOLDER)) { |
346 return false; | 348 return false; |
347 } | 349 } |
348 } | 350 } |
349 if (folder_type != BookmarkNode::OTHER_NODE && | 351 if (folder_type != BookmarkNode::OTHER_NODE && |
350 folder_type != BookmarkNode::MOBILE) { | 352 folder_type != BookmarkNode::MOBILE) { |
351 // Close out the folder. | 353 // Close out the folder. |
352 DecrementIndent(); | 354 DecrementIndent(); |
353 if (!WriteIndent() || | 355 if (!WriteIndent() || |
354 !Write(kFolderChildrenEnd) || | 356 !Write(kFolderChildrenEnd) || |
355 !Write(kNewline)) { | 357 !Write(kNewline)) { |
356 return false; | 358 return false; |
357 } | 359 } |
358 } | 360 } |
359 return true; | 361 return true; |
360 } | 362 } |
361 | 363 |
362 // The BookmarkModel as a Value. This value was generated from the | 364 // The BookmarkModel as a base::Value. This value was generated from the |
363 // BookmarkCodec. | 365 // BookmarkCodec. |
364 scoped_ptr<Value> bookmarks_; | 366 scoped_ptr<base::Value> bookmarks_; |
365 | 367 |
366 // Path we're writing to. | 368 // Path we're writing to. |
367 base::FilePath path_; | 369 base::FilePath path_; |
368 | 370 |
369 // Map that stores favicon per URL. | 371 // Map that stores favicon per URL. |
370 scoped_ptr<BookmarkFaviconFetcher::URLFaviconMap> favicons_map_; | 372 scoped_ptr<BookmarkFaviconFetcher::URLFaviconMap> favicons_map_; |
371 | 373 |
372 // Observer to be notified on finish. | 374 // Observer to be notified on finish. |
373 BookmarksExportObserver* observer_; | 375 BookmarksExportObserver* observer_; |
374 | 376 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 // BookmarkModel isn't thread safe (nor would we want to lock it down | 503 // BookmarkModel isn't thread safe (nor would we want to lock it down |
502 // for the duration of the write), as such we make a copy of the | 504 // for the duration of the write), as such we make a copy of the |
503 // BookmarkModel using BookmarkCodec then write from that. | 505 // BookmarkModel using BookmarkCodec then write from that. |
504 if (fetcher == NULL) { | 506 if (fetcher == NULL) { |
505 fetcher = new BookmarkFaviconFetcher(profile, path, observer); | 507 fetcher = new BookmarkFaviconFetcher(profile, path, observer); |
506 fetcher->ExportBookmarks(); | 508 fetcher->ExportBookmarks(); |
507 } | 509 } |
508 } | 510 } |
509 | 511 |
510 } // namespace bookmark_html_writer | 512 } // namespace bookmark_html_writer |
OLD | NEW |