| 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" |
| 11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "chrome/common/importer/imported_bookmark_entry.h" | 15 #include "chrome/common/importer/imported_bookmark_entry.h" |
| 15 #include "chrome/common/importer/imported_favicon_usage.h" | 16 #include "chrome/common/importer/imported_favicon_usage.h" |
| 16 #include "chrome/utility/importer/favicon_reencode.h" | 17 #include "chrome/utility/importer/favicon_reencode.h" |
| 18 #include "components/search_engines/search_terms_data.h" |
| 19 #include "components/search_engines/template_url.h" |
| 17 #include "net/base/data_url.h" | 20 #include "net/base/data_url.h" |
| 18 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
| 19 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 20 #include "url/url_constants.h" | 23 #include "url/url_constants.h" |
| 21 | 24 |
| 22 namespace { | 25 namespace { |
| 23 | 26 |
| 24 // Fetches the given |attribute| value from the |attribute_list|. Returns true | 27 // Fetches the given |attribute| value from the |attribute_list|. Returns true |
| 25 // if successful, and |value| will contain the value. | 28 // if successful, and |value| will contain the value. |
| 26 bool GetAttribute(const std::string& attribute_list, | 29 bool GetAttribute(const std::string& attribute_list, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 87 |
| 85 } // namespace | 88 } // namespace |
| 86 | 89 |
| 87 namespace bookmark_html_reader { | 90 namespace bookmark_html_reader { |
| 88 | 91 |
| 89 void ImportBookmarksFile( | 92 void ImportBookmarksFile( |
| 90 const base::Callback<bool(void)>& cancellation_callback, | 93 const base::Callback<bool(void)>& cancellation_callback, |
| 91 const base::Callback<bool(const GURL&)>& valid_url_callback, | 94 const base::Callback<bool(const GURL&)>& valid_url_callback, |
| 92 const base::FilePath& file_path, | 95 const base::FilePath& file_path, |
| 93 std::vector<ImportedBookmarkEntry>* bookmarks, | 96 std::vector<ImportedBookmarkEntry>* bookmarks, |
| 97 std::vector<importer::SearchEngineInfo>* search_engines, |
| 94 std::vector<ImportedFaviconUsage>* favicons) { | 98 std::vector<ImportedFaviconUsage>* favicons) { |
| 95 std::string content; | 99 std::string content; |
| 96 base::ReadFileToString(file_path, &content); | 100 base::ReadFileToString(file_path, &content); |
| 97 std::vector<std::string> lines; | 101 std::vector<std::string> lines; |
| 98 base::SplitString(content, '\n', &lines); | 102 base::SplitString(content, '\n', &lines); |
| 99 | 103 |
| 100 base::string16 last_folder; | 104 base::string16 last_folder; |
| 101 bool last_folder_on_toolbar = false; | 105 bool last_folder_on_toolbar = false; |
| 102 bool last_folder_is_empty = true; | 106 bool last_folder_is_empty = true; |
| 103 bool has_subfolder = false; | 107 bool has_subfolder = false; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 base::string16 post_data; | 147 base::string16 post_data; |
| 144 bool is_bookmark; | 148 bool is_bookmark; |
| 145 // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based | 149 // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based |
| 146 // keywords yet. | 150 // keywords yet. |
| 147 is_bookmark = | 151 is_bookmark = |
| 148 internal::ParseBookmarkFromLine(line, charset, &title, | 152 internal::ParseBookmarkFromLine(line, charset, &title, |
| 149 &url, &favicon, &shortcut, | 153 &url, &favicon, &shortcut, |
| 150 &add_date, &post_data) || | 154 &add_date, &post_data) || |
| 151 internal::ParseMinimumBookmarkFromLine(line, charset, &title, &url); | 155 internal::ParseMinimumBookmarkFromLine(line, charset, &title, &url); |
| 152 | 156 |
| 157 // If bookmark contains a valid replaceable url and a keyword then import |
| 158 // it as search engine. |
| 159 std::string search_engine_url; |
| 160 if (is_bookmark && post_data.empty() && |
| 161 CanImportURLAsSearchEngine(url, &search_engine_url) && |
| 162 !shortcut.empty()) { |
| 163 importer::SearchEngineInfo search_engine_info; |
| 164 search_engine_info.url.assign(base::UTF8ToUTF16(search_engine_url)); |
| 165 search_engine_info.keyword = shortcut; |
| 166 search_engine_info.display_name = title; |
| 167 search_engines->push_back(search_engine_info); |
| 168 continue; |
| 169 } |
| 170 |
| 153 if (is_bookmark) | 171 if (is_bookmark) |
| 154 last_folder_is_empty = false; | 172 last_folder_is_empty = false; |
| 155 | 173 |
| 156 if (is_bookmark && | 174 if (is_bookmark && |
| 157 post_data.empty() && | 175 post_data.empty() && |
| 158 (valid_url_callback.is_null() || valid_url_callback.Run(url))) { | 176 (valid_url_callback.is_null() || valid_url_callback.Run(url))) { |
| 159 if (toolbar_folder_index > path.size() && !path.empty()) { | 177 if (toolbar_folder_index > path.size() && !path.empty()) { |
| 160 NOTREACHED(); // error in parsing. | 178 NOTREACHED(); // error in parsing. |
| 161 break; | 179 break; |
| 162 } | 180 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 // Parent folder include current one, so it's not empty. | 249 // Parent folder include current one, so it's not empty. |
| 232 last_folder_is_empty = false; | 250 last_folder_is_empty = false; |
| 233 } | 251 } |
| 234 | 252 |
| 235 if (toolbar_folder_index > path.size()) | 253 if (toolbar_folder_index > path.size()) |
| 236 toolbar_folder_index = 0; | 254 toolbar_folder_index = 0; |
| 237 } | 255 } |
| 238 } | 256 } |
| 239 } | 257 } |
| 240 | 258 |
| 259 bool CanImportURLAsSearchEngine(const GURL& url, |
| 260 std::string* search_engine_url) { |
| 261 std::string url_spec = url.possibly_invalid_spec(); |
| 262 |
| 263 if (url_spec.empty()) |
| 264 return false; |
| 265 |
| 266 url_spec = net::UnescapeURLComponent(url_spec, |
| 267 net::UnescapeRule::URL_SPECIAL_CHARS); |
| 268 |
| 269 // Replace replacement terms ("%s") in |url_spec| with {searchTerms}. |
| 270 url_spec = |
| 271 TemplateURLRef::DisplayURLToURLRef(base::UTF8ToUTF16(url_spec)); |
| 272 |
| 273 TemplateURLData data; |
| 274 data.SetURL(url_spec); |
| 275 *search_engine_url = url_spec; |
| 276 return TemplateURL(data).SupportsReplacement(SearchTermsData()); |
| 277 } |
| 278 |
| 241 namespace internal { | 279 namespace internal { |
| 242 | 280 |
| 243 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { | 281 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { |
| 244 const char kCharset[] = "charset="; | 282 const char kCharset[] = "charset="; |
| 245 if (StartsWithASCII(line, "<META", false) && | 283 if (StartsWithASCII(line, "<META", false) && |
| 246 (line.find("CONTENT=\"") != std::string::npos || | 284 (line.find("CONTENT=\"") != std::string::npos || |
| 247 line.find("content=\"") != std::string::npos)) { | 285 line.find("content=\"") != std::string::npos)) { |
| 248 size_t begin = line.find(kCharset); | 286 size_t begin = line.find(kCharset); |
| 249 if (begin == std::string::npos) | 287 if (begin == std::string::npos) |
| 250 return false; | 288 return false; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 *url = GURL(value); | 471 *url = GURL(value); |
| 434 } | 472 } |
| 435 } | 473 } |
| 436 | 474 |
| 437 return true; | 475 return true; |
| 438 } | 476 } |
| 439 | 477 |
| 440 } // namespace internal | 478 } // namespace internal |
| 441 | 479 |
| 442 } // namespace bookmark_html_reader | 480 } // namespace bookmark_html_reader |
| OLD | NEW |