| 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 (StartsWithASCII(line, kHrTag, false)) { | 122 while (base::StartsWithASCII(line, kHrTag, false)) { |
| 123 line.erase(0, arraysize(kHrTag) - 1); | 123 line.erase(0, arraysize(kHrTag) - 1); |
| 124 base::TrimString(line, " ", &line); | 124 base::TrimString(line, " ", &line); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Get the encoding of the bookmark file. | 127 // Get the encoding of the bookmark file. |
| 128 if (internal::ParseCharsetFromLine(line, &charset)) | 128 if (internal::ParseCharsetFromLine(line, &charset)) |
| 129 continue; | 129 continue; |
| 130 | 130 |
| 131 // Get the folder name. | 131 // Get the folder name. |
| 132 if (internal::ParseFolderNameFromLine(line, | 132 if (internal::ParseFolderNameFromLine(line, |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 198 |
| 199 // Save the favicon. DataURLToFaviconUsage will handle the case where | 199 // Save the favicon. DataURLToFaviconUsage will handle the case where |
| 200 // there is no favicon. | 200 // there is no favicon. |
| 201 if (favicons) | 201 if (favicons) |
| 202 DataURLToFaviconUsage(url, favicon, favicons); | 202 DataURLToFaviconUsage(url, favicon, favicons); |
| 203 | 203 |
| 204 continue; | 204 continue; |
| 205 } | 205 } |
| 206 | 206 |
| 207 // Bookmarks in sub-folder are encapsulated with <DL> tag. | 207 // Bookmarks in sub-folder are encapsulated with <DL> tag. |
| 208 if (StartsWithASCII(line, "<DL>", false)) { | 208 if (base::StartsWithASCII(line, "<DL>", false)) { |
| 209 has_subfolder = true; | 209 has_subfolder = true; |
| 210 if (!last_folder.empty()) { | 210 if (!last_folder.empty()) { |
| 211 path.push_back(last_folder); | 211 path.push_back(last_folder); |
| 212 last_folder.clear(); | 212 last_folder.clear(); |
| 213 } | 213 } |
| 214 if (last_folder_on_toolbar && !toolbar_folder_index) | 214 if (last_folder_on_toolbar && !toolbar_folder_index) |
| 215 toolbar_folder_index = path.size(); | 215 toolbar_folder_index = path.size(); |
| 216 | 216 |
| 217 // Mark next folder empty as initial state. | 217 // Mark next folder empty as initial state. |
| 218 last_folder_is_empty = true; | 218 last_folder_is_empty = true; |
| 219 } else if (StartsWithASCII(line, "</DL>", false)) { | 219 } else if (base::StartsWithASCII(line, "</DL>", false)) { |
| 220 if (path.empty()) | 220 if (path.empty()) |
| 221 break; // Mismatch <DL>. | 221 break; // Mismatch <DL>. |
| 222 | 222 |
| 223 base::string16 folder_title = path.back(); | 223 base::string16 folder_title = path.back(); |
| 224 path.pop_back(); | 224 path.pop_back(); |
| 225 | 225 |
| 226 if (last_folder_is_empty) { | 226 if (last_folder_is_empty) { |
| 227 // Empty folder should be added explicitly. | 227 // Empty folder should be added explicitly. |
| 228 ImportedBookmarkEntry entry; | 228 ImportedBookmarkEntry entry; |
| 229 entry.is_folder = true; | 229 entry.is_folder = true; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 TemplateURLData data; | 271 TemplateURLData data; |
| 272 data.SetURL(url_spec); | 272 data.SetURL(url_spec); |
| 273 *search_engine_url = url_spec; | 273 *search_engine_url = url_spec; |
| 274 return TemplateURL(data).SupportsReplacement(SearchTermsData()); | 274 return TemplateURL(data).SupportsReplacement(SearchTermsData()); |
| 275 } | 275 } |
| 276 | 276 |
| 277 namespace internal { | 277 namespace internal { |
| 278 | 278 |
| 279 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { | 279 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { |
| 280 const char kCharset[] = "charset="; | 280 const char kCharset[] = "charset="; |
| 281 if (StartsWithASCII(line, "<META", false) && | 281 if (base::StartsWithASCII(line, "<META", false) && |
| 282 (line.find("CONTENT=\"") != std::string::npos || | 282 (line.find("CONTENT=\"") != std::string::npos || |
| 283 line.find("content=\"") != std::string::npos)) { | 283 line.find("content=\"") != std::string::npos)) { |
| 284 size_t begin = line.find(kCharset); | 284 size_t begin = line.find(kCharset); |
| 285 if (begin == std::string::npos) | 285 if (begin == std::string::npos) |
| 286 return false; | 286 return false; |
| 287 begin += std::string(kCharset).size(); | 287 begin += std::string(kCharset).size(); |
| 288 size_t end = line.find_first_of('\"', begin); | 288 size_t end = line.find_first_of('\"', begin); |
| 289 *charset = line.substr(begin, end - begin); | 289 *charset = line.substr(begin, end - begin); |
| 290 return true; | 290 return true; |
| 291 } | 291 } |
| 292 return false; | 292 return false; |
| 293 } | 293 } |
| 294 | 294 |
| 295 bool ParseFolderNameFromLine(const std::string& line, | 295 bool ParseFolderNameFromLine(const std::string& line, |
| 296 const std::string& charset, | 296 const std::string& charset, |
| 297 base::string16* folder_name, | 297 base::string16* folder_name, |
| 298 bool* is_toolbar_folder, | 298 bool* is_toolbar_folder, |
| 299 base::Time* add_date) { | 299 base::Time* add_date) { |
| 300 const char kFolderOpen[] = "<DT><H3"; | 300 const char kFolderOpen[] = "<DT><H3"; |
| 301 const char kFolderClose[] = "</H3>"; | 301 const char kFolderClose[] = "</H3>"; |
| 302 const char kToolbarFolderAttribute[] = "PERSONAL_TOOLBAR_FOLDER"; | 302 const char kToolbarFolderAttribute[] = "PERSONAL_TOOLBAR_FOLDER"; |
| 303 const char kAddDateAttribute[] = "ADD_DATE"; | 303 const char kAddDateAttribute[] = "ADD_DATE"; |
| 304 | 304 |
| 305 if (!StartsWithASCII(line, kFolderOpen, true)) | 305 if (!base::StartsWithASCII(line, kFolderOpen, true)) |
| 306 return false; | 306 return false; |
| 307 | 307 |
| 308 size_t end = line.find(kFolderClose); | 308 size_t end = line.find(kFolderClose); |
| 309 size_t tag_end = line.rfind('>', end) + 1; | 309 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. | 310 // 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)) | 311 if (end == std::string::npos || tag_end < arraysize(kFolderOpen)) |
| 312 return false; | 312 return false; |
| 313 | 313 |
| 314 base::CodepageToUTF16(line.substr(tag_end, end - tag_end), charset.c_str(), | 314 base::CodepageToUTF16(line.substr(tag_end, end - tag_end), charset.c_str(), |
| 315 base::OnStringConversionError::SKIP, folder_name); | 315 base::OnStringConversionError::SKIP, folder_name); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 const char kAddDateAttribute[] = "ADD_DATE"; | 354 const char kAddDateAttribute[] = "ADD_DATE"; |
| 355 const char kPostDataAttribute[] = "POST_DATA"; | 355 const char kPostDataAttribute[] = "POST_DATA"; |
| 356 | 356 |
| 357 title->clear(); | 357 title->clear(); |
| 358 *url = GURL(); | 358 *url = GURL(); |
| 359 *favicon = GURL(); | 359 *favicon = GURL(); |
| 360 shortcut->clear(); | 360 shortcut->clear(); |
| 361 post_data->clear(); | 361 post_data->clear(); |
| 362 *add_date = base::Time(); | 362 *add_date = base::Time(); |
| 363 | 363 |
| 364 if (!StartsWithASCII(line, kItemOpen, true)) | 364 if (!base::StartsWithASCII(line, kItemOpen, true)) |
| 365 return false; | 365 return false; |
| 366 | 366 |
| 367 size_t end = line.find(kItemClose); | 367 size_t end = line.find(kItemClose); |
| 368 size_t tag_end = line.rfind('>', end) + 1; | 368 size_t tag_end = line.rfind('>', end) + 1; |
| 369 if (end == std::string::npos || tag_end < arraysize(kItemOpen)) | 369 if (end == std::string::npos || tag_end < arraysize(kItemOpen)) |
| 370 return false; // No end tag or start tag is broken. | 370 return false; // No end tag or start tag is broken. |
| 371 | 371 |
| 372 std::string attribute_list = line.substr(arraysize(kItemOpen), | 372 std::string attribute_list = line.substr(arraysize(kItemOpen), |
| 373 tag_end - arraysize(kItemOpen) - 1); | 373 tag_end - arraysize(kItemOpen) - 1); |
| 374 | 374 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 GURL* url) { | 430 GURL* url) { |
| 431 const char kItemOpen[] = "<DT><A"; | 431 const char kItemOpen[] = "<DT><A"; |
| 432 const char kItemClose[] = "</"; | 432 const char kItemClose[] = "</"; |
| 433 const char kHrefAttributeUpper[] = "HREF"; | 433 const char kHrefAttributeUpper[] = "HREF"; |
| 434 const char kHrefAttributeLower[] = "href"; | 434 const char kHrefAttributeLower[] = "href"; |
| 435 | 435 |
| 436 title->clear(); | 436 title->clear(); |
| 437 *url = GURL(); | 437 *url = GURL(); |
| 438 | 438 |
| 439 // Case-insensitive check of open tag. | 439 // Case-insensitive check of open tag. |
| 440 if (!StartsWithASCII(line, kItemOpen, false)) | 440 if (!base::StartsWithASCII(line, kItemOpen, false)) |
| 441 return false; | 441 return false; |
| 442 | 442 |
| 443 // Find any close tag. | 443 // Find any close tag. |
| 444 size_t end = line.find(kItemClose); | 444 size_t end = line.find(kItemClose); |
| 445 size_t tag_end = line.rfind('>', end) + 1; | 445 size_t tag_end = line.rfind('>', end) + 1; |
| 446 if (end == std::string::npos || tag_end < arraysize(kItemOpen)) | 446 if (end == std::string::npos || tag_end < arraysize(kItemOpen)) |
| 447 return false; // No end tag or start tag is broken. | 447 return false; // No end tag or start tag is broken. |
| 448 | 448 |
| 449 std::string attribute_list = line.substr(arraysize(kItemOpen), | 449 std::string attribute_list = line.substr(arraysize(kItemOpen), |
| 450 tag_end - arraysize(kItemOpen) - 1); | 450 tag_end - arraysize(kItemOpen) - 1); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 469 *url = GURL(value); | 469 *url = GURL(value); |
| 470 } | 470 } |
| 471 } | 471 } |
| 472 | 472 |
| 473 return true; | 473 return true; |
| 474 } | 474 } |
| 475 | 475 |
| 476 } // namespace internal | 476 } // namespace internal |
| 477 | 477 |
| 478 } // namespace bookmark_html_reader | 478 } // namespace bookmark_html_reader |
| OLD | NEW |