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" |
| 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 std::string search_engine_url; | |
|
Peter Kasting
2014/11/17 21:30:36
Nit: Declare this below the comment instead of abo
Tapu Ghose
2014/12/07 02:49:54
Done.
| |
| 158 | |
| 159 // If bookmark contains a valid replaceable url and a keyword then import | |
| 160 // it as search engine. | |
| 161 if (is_bookmark && post_data.empty() && | |
| 162 CanImportURLAsSearchEngine(url, &search_engine_url) && | |
| 163 !shortcut.empty()) { | |
| 164 importer::SearchEngineInfo search_engine_info; | |
| 165 search_engine_info.url.assign(base::UTF8ToUTF16(search_engine_url)); | |
| 166 search_engine_info.keyword = shortcut; | |
| 167 search_engine_info.display_name = title; | |
| 168 search_engines->push_back(search_engine_info); | |
| 169 continue; | |
| 170 } | |
| 171 | |
| 153 if (is_bookmark) | 172 if (is_bookmark) |
| 154 last_folder_is_empty = false; | 173 last_folder_is_empty = false; |
| 155 | 174 |
| 156 if (is_bookmark && | 175 if (is_bookmark && |
| 157 post_data.empty() && | 176 post_data.empty() && |
| 158 (valid_url_callback.is_null() || valid_url_callback.Run(url))) { | 177 (valid_url_callback.is_null() || valid_url_callback.Run(url))) { |
| 159 if (toolbar_folder_index > path.size() && !path.empty()) { | 178 if (toolbar_folder_index > path.size() && !path.empty()) { |
| 160 NOTREACHED(); // error in parsing. | 179 NOTREACHED(); // error in parsing. |
| 161 break; | 180 break; |
| 162 } | 181 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 // Parent folder include current one, so it's not empty. | 250 // Parent folder include current one, so it's not empty. |
| 232 last_folder_is_empty = false; | 251 last_folder_is_empty = false; |
| 233 } | 252 } |
| 234 | 253 |
| 235 if (toolbar_folder_index > path.size()) | 254 if (toolbar_folder_index > path.size()) |
| 236 toolbar_folder_index = 0; | 255 toolbar_folder_index = 0; |
| 237 } | 256 } |
| 238 } | 257 } |
| 239 } | 258 } |
| 240 | 259 |
| 260 bool CanImportURLAsSearchEngine(const GURL& url, | |
| 261 std::string* search_engine_url) { | |
| 262 std::string url_spec = url.possibly_invalid_spec(); | |
| 263 | |
| 264 if (url_spec.empty()) | |
| 265 return false; | |
| 266 | |
| 267 url_spec = net::UnescapeURLComponent(url_spec, | |
| 268 net::UnescapeRule::URL_SPECIAL_CHARS); | |
|
Peter Kasting
2014/11/17 21:30:36
Do you have a testcase that will fail without this
Tapu Ghose
2014/12/07 02:49:54
As I mentioned in Patch Set 4, unescaping the url
| |
| 269 | |
| 270 // Replace replacement terms ("%s") in |url_spec| with {searchTerms}. | |
| 271 url_spec = | |
| 272 TemplateURLRef::DisplayURLToURLRef(base::UTF8ToUTF16(url_spec)); | |
| 273 | |
| 274 TemplateURLData data; | |
| 275 data.SetURL(url_spec); | |
| 276 *search_engine_url = url_spec; | |
| 277 return TemplateURL(data).SupportsReplacement(SearchTermsData()); | |
| 278 } | |
| 279 | |
| 241 namespace internal { | 280 namespace internal { |
| 242 | 281 |
| 243 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { | 282 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { |
| 244 const char kCharset[] = "charset="; | 283 const char kCharset[] = "charset="; |
| 245 if (StartsWithASCII(line, "<META", false) && | 284 if (StartsWithASCII(line, "<META", false) && |
| 246 (line.find("CONTENT=\"") != std::string::npos || | 285 (line.find("CONTENT=\"") != std::string::npos || |
| 247 line.find("content=\"") != std::string::npos)) { | 286 line.find("content=\"") != std::string::npos)) { |
| 248 size_t begin = line.find(kCharset); | 287 size_t begin = line.find(kCharset); |
| 249 if (begin == std::string::npos) | 288 if (begin == std::string::npos) |
| 250 return false; | 289 return false; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 433 *url = GURL(value); | 472 *url = GURL(value); |
| 434 } | 473 } |
| 435 } | 474 } |
| 436 | 475 |
| 437 return true; | 476 return true; |
| 438 } | 477 } |
| 439 | 478 |
| 440 } // namespace internal | 479 } // namespace internal |
| 441 | 480 |
| 442 } // namespace bookmark_html_reader | 481 } // namespace bookmark_html_reader |
| OLD | NEW |