Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: chrome/utility/importer/bookmark_html_reader.cc

Issue 1873403003: Read bookmarks from 'NETSCAPE-Bookmark-file's that don't have a beginning <DT> tag. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reverted to old interface Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/utility/importer/bookmark_html_reader.h" 5 #include "chrome/utility/importer/bookmark_html_reader.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 // We only have one URL per favicon for Firefox 2 bookmarks. 84 // We only have one URL per favicon for Firefox 2 bookmarks.
85 usage.urls.insert(link_url); 85 usage.urls.insert(link_url);
86 86
87 favicons->push_back(usage); 87 favicons->push_back(usage);
88 } 88 }
89 89
90 } // namespace 90 } // namespace
91 91
92 namespace bookmark_html_reader { 92 namespace bookmark_html_reader {
93 93
94 static std::string stripDt(const std::string& lineDt) {
95 // Remove "<DT>" if the line starts with "<DT>". This may not occur if
96 // "<DT>" was on the previous line. Liberally accept entries that do not
97 // have an opening "<DT>" at all.
98 std::string line = lineDt;
99 static const char kDtTag[] = "<DT>";
100 if (base::StartsWith(line, kDtTag,
101 base::CompareCase::INSENSITIVE_ASCII)) {
102 line.erase(0, arraysize(kDtTag) - 1);
103 base::TrimString(line, " ", &line);
104 }
105 return line;
106 }
107
94 void ImportBookmarksFile( 108 void ImportBookmarksFile(
95 const base::Callback<bool(void)>& cancellation_callback, 109 const base::Callback<bool(void)>& cancellation_callback,
96 const base::Callback<bool(const GURL&)>& valid_url_callback, 110 const base::Callback<bool(const GURL&)>& valid_url_callback,
97 const base::FilePath& file_path, 111 const base::FilePath& file_path,
98 std::vector<ImportedBookmarkEntry>* bookmarks, 112 std::vector<ImportedBookmarkEntry>* bookmarks,
99 std::vector<importer::SearchEngineInfo>* search_engines, 113 std::vector<importer::SearchEngineInfo>* search_engines,
100 favicon_base::FaviconUsageDataList* favicons) { 114 favicon_base::FaviconUsageDataList* favicons) {
101 std::string content; 115 std::string content;
102 base::ReadFileToString(file_path, &content); 116 base::ReadFileToString(file_path, &content);
103 std::vector<std::string> lines = base::SplitString( 117 std::vector<std::string> lines = base::SplitString(
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 if (begin == std::string::npos) 305 if (begin == std::string::npos)
292 return false; 306 return false;
293 begin += std::string(kCharset).size(); 307 begin += std::string(kCharset).size();
294 size_t end = line.find_first_of('\"', begin); 308 size_t end = line.find_first_of('\"', begin);
295 *charset = line.substr(begin, end - begin); 309 *charset = line.substr(begin, end - begin);
296 return true; 310 return true;
297 } 311 }
298 return false; 312 return false;
299 } 313 }
300 314
301 bool ParseFolderNameFromLine(const std::string& line, 315 bool ParseFolderNameFromLine(const std::string& lineDt,
302 const std::string& charset, 316 const std::string& charset,
303 base::string16* folder_name, 317 base::string16* folder_name,
304 bool* is_toolbar_folder, 318 bool* is_toolbar_folder,
305 base::Time* add_date) { 319 base::Time* add_date) {
306 const char kFolderOpen[] = "<DT><H3"; 320 const char kFolderOpen[] = "<H3";
307 const char kFolderClose[] = "</H3>"; 321 const char kFolderClose[] = "</H3>";
308 const char kToolbarFolderAttribute[] = "PERSONAL_TOOLBAR_FOLDER"; 322 const char kToolbarFolderAttribute[] = "PERSONAL_TOOLBAR_FOLDER";
309 const char kAddDateAttribute[] = "ADD_DATE"; 323 const char kAddDateAttribute[] = "ADD_DATE";
310 324
325 std::string line = stripDt(lineDt);
326
311 if (!base::StartsWith(line, kFolderOpen, base::CompareCase::SENSITIVE)) 327 if (!base::StartsWith(line, kFolderOpen, base::CompareCase::SENSITIVE))
312 return false; 328 return false;
313 329
314 size_t end = line.find(kFolderClose); 330 size_t end = line.find(kFolderClose);
315 size_t tag_end = line.rfind('>', end) + 1; 331 size_t tag_end = line.rfind('>', end) + 1;
316 // If no end tag or start tag is broken, we skip to find the folder name. 332 // If no end tag or start tag is broken, we skip to find the folder name.
317 if (end == std::string::npos || tag_end < arraysize(kFolderOpen)) 333 if (end == std::string::npos || tag_end < arraysize(kFolderOpen))
318 return false; 334 return false;
319 335
320 base::CodepageToUTF16(line.substr(tag_end, end - tag_end), charset.c_str(), 336 base::CodepageToUTF16(line.substr(tag_end, end - tag_end), charset.c_str(),
(...skipping 15 matching lines...) Expand all
336 352
337 if (GetAttribute(attribute_list, kToolbarFolderAttribute, &value) && 353 if (GetAttribute(attribute_list, kToolbarFolderAttribute, &value) &&
338 base::LowerCaseEqualsASCII(value, "true")) 354 base::LowerCaseEqualsASCII(value, "true"))
339 *is_toolbar_folder = true; 355 *is_toolbar_folder = true;
340 else 356 else
341 *is_toolbar_folder = false; 357 *is_toolbar_folder = false;
342 358
343 return true; 359 return true;
344 } 360 }
345 361
346 bool ParseBookmarkFromLine(const std::string& line, 362 bool ParseBookmarkFromLine(const std::string& lineDt,
347 const std::string& charset, 363 const std::string& charset,
348 base::string16* title, 364 base::string16* title,
349 GURL* url, 365 GURL* url,
350 GURL* favicon, 366 GURL* favicon,
351 base::string16* shortcut, 367 base::string16* shortcut,
352 base::Time* add_date, 368 base::Time* add_date,
353 base::string16* post_data) { 369 base::string16* post_data) {
354 const char kItemOpen[] = "<DT><A"; 370 const char kItemOpen[] = "<A";
355 const char kItemClose[] = "</A>"; 371 const char kItemClose[] = "</A>";
356 const char kFeedURLAttribute[] = "FEEDURL"; 372 const char kFeedURLAttribute[] = "FEEDURL";
357 const char kHrefAttribute[] = "HREF"; 373 const char kHrefAttribute[] = "HREF";
358 const char kIconAttribute[] = "ICON"; 374 const char kIconAttribute[] = "ICON";
359 const char kShortcutURLAttribute[] = "SHORTCUTURL"; 375 const char kShortcutURLAttribute[] = "SHORTCUTURL";
360 const char kAddDateAttribute[] = "ADD_DATE"; 376 const char kAddDateAttribute[] = "ADD_DATE";
361 const char kPostDataAttribute[] = "POST_DATA"; 377 const char kPostDataAttribute[] = "POST_DATA";
362 378
379 std::string line = stripDt(lineDt);
363 title->clear(); 380 title->clear();
364 *url = GURL(); 381 *url = GURL();
365 *favicon = GURL(); 382 *favicon = GURL();
366 shortcut->clear(); 383 shortcut->clear();
367 post_data->clear(); 384 post_data->clear();
368 *add_date = base::Time(); 385 *add_date = base::Time();
369 386
370 if (!base::StartsWith(line, kItemOpen, base::CompareCase::SENSITIVE)) 387 if (!base::StartsWith(line, kItemOpen, base::CompareCase::SENSITIVE))
371 return false; 388 return false;
372 389
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 // Post data. 440 // Post data.
424 if (GetAttribute(attribute_list, kPostDataAttribute, &value)) { 441 if (GetAttribute(attribute_list, kPostDataAttribute, &value)) {
425 base::CodepageToUTF16(value, charset.c_str(), 442 base::CodepageToUTF16(value, charset.c_str(),
426 base::OnStringConversionError::SKIP, post_data); 443 base::OnStringConversionError::SKIP, post_data);
427 *post_data = net::UnescapeForHTML(*post_data); 444 *post_data = net::UnescapeForHTML(*post_data);
428 } 445 }
429 446
430 return true; 447 return true;
431 } 448 }
432 449
433 bool ParseMinimumBookmarkFromLine(const std::string& line, 450 bool ParseMinimumBookmarkFromLine(const std::string& lineDt,
434 const std::string& charset, 451 const std::string& charset,
435 base::string16* title, 452 base::string16* title,
436 GURL* url) { 453 GURL* url) {
437 const char kItemOpen[] = "<DT><A"; 454 const char kItemOpen[] = "<A";
438 const char kItemClose[] = "</"; 455 const char kItemClose[] = "</";
439 const char kHrefAttributeUpper[] = "HREF"; 456 const char kHrefAttributeUpper[] = "HREF";
440 const char kHrefAttributeLower[] = "href"; 457 const char kHrefAttributeLower[] = "href";
441 458
459 std::string line = stripDt(lineDt);
442 title->clear(); 460 title->clear();
443 *url = GURL(); 461 *url = GURL();
444 462
445 // Case-insensitive check of open tag. 463 // Case-insensitive check of open tag.
446 if (!base::StartsWith(line, kItemOpen, base::CompareCase::INSENSITIVE_ASCII)) 464 if (!base::StartsWith(line, kItemOpen, base::CompareCase::INSENSITIVE_ASCII))
447 return false; 465 return false;
448 466
449 // Find any close tag. 467 // Find any close tag.
450 size_t end = line.find(kItemClose); 468 size_t end = line.find(kItemClose);
451 size_t tag_end = line.rfind('>', end) + 1; 469 size_t tag_end = line.rfind('>', end) + 1;
(...skipping 23 matching lines...) Expand all
475 *url = GURL(value); 493 *url = GURL(value);
476 } 494 }
477 } 495 }
478 496
479 return true; 497 return true;
480 } 498 }
481 499
482 } // namespace internal 500 } // namespace internal
483 501
484 } // namespace bookmark_html_reader 502 } // namespace bookmark_html_reader
OLDNEW
« no previous file with comments | « chrome/test/data/bookmark_html_reader/redditsaver.html ('k') | chrome/utility/importer/bookmark_html_reader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698