Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/i18n/icu_string_conversions.h" | 9 #include "base/i18n/icu_string_conversions.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 (cancellation_callback.is_null() || !cancellation_callback.Run()); | 112 (cancellation_callback.is_null() || !cancellation_callback.Run()); |
| 113 ++i) { | 113 ++i) { |
| 114 std::string line; | 114 std::string line; |
| 115 base::TrimString(lines[i], " ", &line); | 115 base::TrimString(lines[i], " ", &line); |
| 116 | 116 |
| 117 // Remove "<HR>" if |line| starts with it. "<HR>" is the bookmark entries | 117 // Remove "<HR>" if |line| starts with it. "<HR>" is the bookmark entries |
| 118 // separator in Firefox that Chrome does not support. Note that there can be | 118 // separator in Firefox that Chrome does not support. Note that there can be |
| 119 // multiple "<HR>" tags at the beginning of a single line. | 119 // multiple "<HR>" tags at the beginning of a single line. |
| 120 // See http://crbug.com/257474. | 120 // See http://crbug.com/257474. |
| 121 static const char kHrTag[] = "<HR>"; | 121 static const char kHrTag[] = "<HR>"; |
| 122 while (base::StartsWithASCII(line, kHrTag, false)) { | 122 while (base::StartsWith(line, kHrTag, |
| 123 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 123 line.erase(0, arraysize(kHrTag) - 1); | 124 line.erase(0, arraysize(kHrTag) - 1); |
| 124 base::TrimString(line, " ", &line); | 125 base::TrimString(line, " ", &line); |
| 125 } | 126 } |
| 126 | 127 |
| 127 // Get the encoding of the bookmark file. | 128 // Get the encoding of the bookmark file. |
| 128 if (internal::ParseCharsetFromLine(line, &charset)) | 129 if (internal::ParseCharsetFromLine(line, &charset)) |
| 129 continue; | 130 continue; |
| 130 | 131 |
| 131 // Get the folder name. | 132 // Get the folder name. |
| 132 if (internal::ParseFolderNameFromLine(line, | 133 if (internal::ParseFolderNameFromLine(line, |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 | 199 |
| 199 // Save the favicon. DataURLToFaviconUsage will handle the case where | 200 // Save the favicon. DataURLToFaviconUsage will handle the case where |
| 200 // there is no favicon. | 201 // there is no favicon. |
| 201 if (favicons) | 202 if (favicons) |
| 202 DataURLToFaviconUsage(url, favicon, favicons); | 203 DataURLToFaviconUsage(url, favicon, favicons); |
| 203 | 204 |
| 204 continue; | 205 continue; |
| 205 } | 206 } |
| 206 | 207 |
| 207 // Bookmarks in sub-folder are encapsulated with <DL> tag. | 208 // Bookmarks in sub-folder are encapsulated with <DL> tag. |
| 208 if (base::StartsWithASCII(line, "<DL>", false)) { | 209 if (base::StartsWith(line, "<DL>", base::CompareCase::INSENSITIVE_ASCII)) { |
| 209 has_subfolder = true; | 210 has_subfolder = true; |
| 210 if (!last_folder.empty()) { | 211 if (!last_folder.empty()) { |
| 211 path.push_back(last_folder); | 212 path.push_back(last_folder); |
| 212 last_folder.clear(); | 213 last_folder.clear(); |
| 213 } | 214 } |
| 214 if (last_folder_on_toolbar && !toolbar_folder_index) | 215 if (last_folder_on_toolbar && !toolbar_folder_index) |
| 215 toolbar_folder_index = path.size(); | 216 toolbar_folder_index = path.size(); |
| 216 | 217 |
| 217 // Mark next folder empty as initial state. | 218 // Mark next folder empty as initial state. |
| 218 last_folder_is_empty = true; | 219 last_folder_is_empty = true; |
| 219 } else if (base::StartsWithASCII(line, "</DL>", false)) { | 220 } else if (base::StartsWith(line, "</DL>", |
| 221 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 220 if (path.empty()) | 222 if (path.empty()) |
| 221 break; // Mismatch <DL>. | 223 break; // Mismatch <DL>. |
| 222 | 224 |
| 223 base::string16 folder_title = path.back(); | 225 base::string16 folder_title = path.back(); |
| 224 path.pop_back(); | 226 path.pop_back(); |
| 225 | 227 |
| 226 if (last_folder_is_empty) { | 228 if (last_folder_is_empty) { |
| 227 // Empty folder should be added explicitly. | 229 // Empty folder should be added explicitly. |
| 228 ImportedBookmarkEntry entry; | 230 ImportedBookmarkEntry entry; |
| 229 entry.is_folder = true; | 231 entry.is_folder = true; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 TemplateURLData data; | 273 TemplateURLData data; |
| 272 data.SetURL(url_spec); | 274 data.SetURL(url_spec); |
| 273 *search_engine_url = url_spec; | 275 *search_engine_url = url_spec; |
| 274 return TemplateURL(data).SupportsReplacement(SearchTermsData()); | 276 return TemplateURL(data).SupportsReplacement(SearchTermsData()); |
| 275 } | 277 } |
| 276 | 278 |
| 277 namespace internal { | 279 namespace internal { |
| 278 | 280 |
| 279 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { | 281 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { |
| 280 const char kCharset[] = "charset="; | 282 const char kCharset[] = "charset="; |
| 281 if (base::StartsWithASCII(line, "<META", false) && | 283 if (base::StartsWith(line, "<META", base::CompareCase::INSENSITIVE_ASCII) && |
| 282 (line.find("CONTENT=\"") != std::string::npos || | 284 (line.find("CONTENT=\"") != std::string::npos || |
| 283 line.find("content=\"") != std::string::npos)) { | 285 line.find("content=\"") != std::string::npos)) { |
| 284 size_t begin = line.find(kCharset); | 286 size_t begin = line.find(kCharset); |
| 285 if (begin == std::string::npos) | 287 if (begin == std::string::npos) |
| 286 return false; | 288 return false; |
| 287 begin += std::string(kCharset).size(); | 289 begin += std::string(kCharset).size(); |
| 288 size_t end = line.find_first_of('\"', begin); | 290 size_t end = line.find_first_of('\"', begin); |
| 289 *charset = line.substr(begin, end - begin); | 291 *charset = line.substr(begin, end - begin); |
| 290 return true; | 292 return true; |
| 291 } | 293 } |
| 292 return false; | 294 return false; |
| 293 } | 295 } |
| 294 | 296 |
| 295 bool ParseFolderNameFromLine(const std::string& line, | 297 bool ParseFolderNameFromLine(const std::string& line, |
| 296 const std::string& charset, | 298 const std::string& charset, |
| 297 base::string16* folder_name, | 299 base::string16* folder_name, |
| 298 bool* is_toolbar_folder, | 300 bool* is_toolbar_folder, |
| 299 base::Time* add_date) { | 301 base::Time* add_date) { |
| 300 const char kFolderOpen[] = "<DT><H3"; | 302 const char kFolderOpen[] = "<DT><H3"; |
| 301 const char kFolderClose[] = "</H3>"; | 303 const char kFolderClose[] = "</H3>"; |
| 302 const char kToolbarFolderAttribute[] = "PERSONAL_TOOLBAR_FOLDER"; | 304 const char kToolbarFolderAttribute[] = "PERSONAL_TOOLBAR_FOLDER"; |
| 303 const char kAddDateAttribute[] = "ADD_DATE"; | 305 const char kAddDateAttribute[] = "ADD_DATE"; |
| 304 | 306 |
| 305 if (!base::StartsWithASCII(line, kFolderOpen, true)) | 307 if (!base::StartsWith(line, kFolderOpen, base::CompareCase::SENSITIVE)) |
|
Nico
2015/07/06 17:55:00
sure, let's treat tags as case-sensitive starting
| |
| 306 return false; | 308 return false; |
| 307 | 309 |
| 308 size_t end = line.find(kFolderClose); | 310 size_t end = line.find(kFolderClose); |
| 309 size_t tag_end = line.rfind('>', end) + 1; | 311 size_t tag_end = line.rfind('>', end) + 1; |
| 310 // If no end tag or start tag is broken, we skip to find the folder name. | 312 // If no end tag or start tag is broken, we skip to find the folder name. |
| 311 if (end == std::string::npos || tag_end < arraysize(kFolderOpen)) | 313 if (end == std::string::npos || tag_end < arraysize(kFolderOpen)) |
| 312 return false; | 314 return false; |
| 313 | 315 |
| 314 base::CodepageToUTF16(line.substr(tag_end, end - tag_end), charset.c_str(), | 316 base::CodepageToUTF16(line.substr(tag_end, end - tag_end), charset.c_str(), |
| 315 base::OnStringConversionError::SKIP, folder_name); | 317 base::OnStringConversionError::SKIP, folder_name); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 354 const char kAddDateAttribute[] = "ADD_DATE"; | 356 const char kAddDateAttribute[] = "ADD_DATE"; |
| 355 const char kPostDataAttribute[] = "POST_DATA"; | 357 const char kPostDataAttribute[] = "POST_DATA"; |
| 356 | 358 |
| 357 title->clear(); | 359 title->clear(); |
| 358 *url = GURL(); | 360 *url = GURL(); |
| 359 *favicon = GURL(); | 361 *favicon = GURL(); |
| 360 shortcut->clear(); | 362 shortcut->clear(); |
| 361 post_data->clear(); | 363 post_data->clear(); |
| 362 *add_date = base::Time(); | 364 *add_date = base::Time(); |
| 363 | 365 |
| 364 if (!base::StartsWithASCII(line, kItemOpen, true)) | 366 if (!base::StartsWith(line, kItemOpen, base::CompareCase::SENSITIVE)) |
| 365 return false; | 367 return false; |
| 366 | 368 |
| 367 size_t end = line.find(kItemClose); | 369 size_t end = line.find(kItemClose); |
| 368 size_t tag_end = line.rfind('>', end) + 1; | 370 size_t tag_end = line.rfind('>', end) + 1; |
| 369 if (end == std::string::npos || tag_end < arraysize(kItemOpen)) | 371 if (end == std::string::npos || tag_end < arraysize(kItemOpen)) |
| 370 return false; // No end tag or start tag is broken. | 372 return false; // No end tag or start tag is broken. |
| 371 | 373 |
| 372 std::string attribute_list = line.substr(arraysize(kItemOpen), | 374 std::string attribute_list = line.substr(arraysize(kItemOpen), |
| 373 tag_end - arraysize(kItemOpen) - 1); | 375 tag_end - arraysize(kItemOpen) - 1); |
| 374 | 376 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 430 GURL* url) { | 432 GURL* url) { |
| 431 const char kItemOpen[] = "<DT><A"; | 433 const char kItemOpen[] = "<DT><A"; |
| 432 const char kItemClose[] = "</"; | 434 const char kItemClose[] = "</"; |
| 433 const char kHrefAttributeUpper[] = "HREF"; | 435 const char kHrefAttributeUpper[] = "HREF"; |
| 434 const char kHrefAttributeLower[] = "href"; | 436 const char kHrefAttributeLower[] = "href"; |
| 435 | 437 |
| 436 title->clear(); | 438 title->clear(); |
| 437 *url = GURL(); | 439 *url = GURL(); |
| 438 | 440 |
| 439 // Case-insensitive check of open tag. | 441 // Case-insensitive check of open tag. |
| 440 if (!base::StartsWithASCII(line, kItemOpen, false)) | 442 if (!base::StartsWith(line, kItemOpen, base::CompareCase::INSENSITIVE_ASCII)) |
|
Nico
2015/07/06 17:55:00
…or don't! I guess <DT><A is different from <DT><A
| |
| 441 return false; | 443 return false; |
| 442 | 444 |
| 443 // Find any close tag. | 445 // Find any close tag. |
| 444 size_t end = line.find(kItemClose); | 446 size_t end = line.find(kItemClose); |
| 445 size_t tag_end = line.rfind('>', end) + 1; | 447 size_t tag_end = line.rfind('>', end) + 1; |
| 446 if (end == std::string::npos || tag_end < arraysize(kItemOpen)) | 448 if (end == std::string::npos || tag_end < arraysize(kItemOpen)) |
| 447 return false; // No end tag or start tag is broken. | 449 return false; // No end tag or start tag is broken. |
| 448 | 450 |
| 449 std::string attribute_list = line.substr(arraysize(kItemOpen), | 451 std::string attribute_list = line.substr(arraysize(kItemOpen), |
| 450 tag_end - arraysize(kItemOpen) - 1); | 452 tag_end - arraysize(kItemOpen) - 1); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 469 *url = GURL(value); | 471 *url = GURL(value); |
| 470 } | 472 } |
| 471 } | 473 } |
| 472 | 474 |
| 473 return true; | 475 return true; |
| 474 } | 476 } |
| 475 | 477 |
| 476 } // namespace internal | 478 } // namespace internal |
| 477 | 479 |
| 478 } // namespace bookmark_html_reader | 480 } // namespace bookmark_html_reader |
| OLD | NEW |