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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |
| 153 if (is_bookmark) | 157 if (is_bookmark) |
| 154 last_folder_is_empty = false; | 158 last_folder_is_empty = false; |
| 155 | 159 |
| 160 importer::SearchEngineInfo search_engine; | |
| 161 bool is_valid_replaceable_url = | |
| 162 CanImportURLAsSearchEngine(url, shortcut, | |
| 163 title, &search_engine); | |
|
Ilya Sherman
2014/11/07 00:22:34
nit: Looks like this doesn't need to wrap.
Tapu Ghose
2014/11/09 14:03:07
Done.
| |
| 164 | |
| 156 if (is_bookmark && | 165 if (is_bookmark && |
| 157 post_data.empty() && | 166 post_data.empty() && |
| 158 (valid_url_callback.is_null() || valid_url_callback.Run(url))) { | 167 (valid_url_callback.is_null() || valid_url_callback.Run(url) || |
| 168 is_valid_replaceable_url)) { | |
|
Ilya Sherman
2014/11/07 00:22:34
Should you also check "!shortcut.empty()" here? I
Tapu Ghose
2014/11/09 14:03:07
Moved above the parent if-stmt.
| |
| 159 if (toolbar_folder_index > path.size() && !path.empty()) { | 169 if (toolbar_folder_index > path.size() && !path.empty()) { |
| 160 NOTREACHED(); // error in parsing. | 170 NOTREACHED(); // error in parsing. |
| 161 break; | 171 break; |
| 162 } | 172 } |
| 163 | 173 |
| 174 // If bookmark contains a valid replaceable url and a keyword then import | |
| 175 // it as search engine. | |
| 176 if (is_valid_replaceable_url && !shortcut.empty()) { | |
| 177 search_engines->push_back(search_engine); | |
| 178 continue; | |
| 179 } | |
| 180 | |
| 164 ImportedBookmarkEntry entry; | 181 ImportedBookmarkEntry entry; |
| 165 entry.creation_time = add_date; | 182 entry.creation_time = add_date; |
| 166 entry.url = url; | 183 entry.url = url; |
| 167 entry.title = title; | 184 entry.title = title; |
| 168 | 185 |
| 169 if (toolbar_folder_index) { | 186 if (toolbar_folder_index) { |
| 170 // The toolbar folder should be at the top level. | 187 // The toolbar folder should be at the top level. |
| 171 entry.in_toolbar = true; | 188 entry.in_toolbar = true; |
| 172 entry.path.assign(path.begin() + toolbar_folder_index - 1, path.end()); | 189 entry.path.assign(path.begin() + toolbar_folder_index - 1, path.end()); |
| 173 } else { | 190 } else { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 // Parent folder include current one, so it's not empty. | 248 // Parent folder include current one, so it's not empty. |
| 232 last_folder_is_empty = false; | 249 last_folder_is_empty = false; |
| 233 } | 250 } |
| 234 | 251 |
| 235 if (toolbar_folder_index > path.size()) | 252 if (toolbar_folder_index > path.size()) |
| 236 toolbar_folder_index = 0; | 253 toolbar_folder_index = 0; |
| 237 } | 254 } |
| 238 } | 255 } |
| 239 } | 256 } |
| 240 | 257 |
| 258 bool CanImportURLAsSearchEngine(const GURL& url, | |
| 259 const base::string16& keyword, | |
| 260 const base::string16& title, | |
| 261 importer::SearchEngineInfo* search_engine) { | |
| 262 std::string url_spec = | |
| 263 url.is_valid() ? url.spec() : url.possibly_invalid_spec(); | |
|
Ilya Sherman
2014/11/07 00:22:34
nit: Why not just always grab the possibly_invalid
Tapu Ghose
2014/11/09 14:03:07
Agreed.
| |
| 264 | |
| 265 if (url_spec.empty()) | |
| 266 return false; | |
| 267 | |
| 268 std::string raw_url = net::UnescapeURLComponent( | |
| 269 url_spec, | |
| 270 net::UnescapeRule::SPACES | | |
| 271 net::UnescapeRule::URL_SPECIAL_CHARS); | |
|
Ilya Sherman
2014/11/07 00:22:34
Do you really need both of these unescape rules?
Tapu Ghose
2014/11/09 14:03:07
I think unescaping the url is necessary. For insta
| |
| 272 | |
| 273 search_engine->url.assign(base::UTF8ToUTF16(raw_url)); | |
| 274 search_engine->keyword = keyword; | |
| 275 search_engine->display_name = title; | |
| 276 | |
| 277 const std::string kReplacementTerm("%s"); | |
| 278 const std::string kSearchTerms("{searchTerms}"); | |
| 279 // Replace replacement terms in the |raw_url| with {searchTerms}. This is | |
| 280 // necessary so that |raw_url| can be parsed for replacement terms by | |
| 281 // |ParseURL| which will be called through |SupportsReplacement|. | |
| 282 ReplaceSubstringsAfterOffset(&raw_url, 0, kReplacementTerm, kSearchTerms); | |
| 283 TemplateURLData data; | |
| 284 data.SetURL(raw_url); | |
| 285 SearchTermsData search_terms_data; | |
| 286 return TemplateURL(data).SupportsReplacement(search_terms_data); | |
|
Ilya Sherman
2014/11/07 00:22:34
nit: I believe that you can combine the above two
Tapu Ghose
2014/11/09 14:03:06
Done.
| |
| 287 } | |
| 288 | |
| 241 namespace internal { | 289 namespace internal { |
| 242 | 290 |
| 243 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { | 291 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { |
| 244 const char kCharset[] = "charset="; | 292 const char kCharset[] = "charset="; |
| 245 if (StartsWithASCII(line, "<META", false) && | 293 if (StartsWithASCII(line, "<META", false) && |
| 246 (line.find("CONTENT=\"") != std::string::npos || | 294 (line.find("CONTENT=\"") != std::string::npos || |
| 247 line.find("content=\"") != std::string::npos)) { | 295 line.find("content=\"") != std::string::npos)) { |
| 248 size_t begin = line.find(kCharset); | 296 size_t begin = line.find(kCharset); |
| 249 if (begin == std::string::npos) | 297 if (begin == std::string::npos) |
| 250 return false; | 298 return false; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 433 *url = GURL(value); | 481 *url = GURL(value); |
| 434 } | 482 } |
| 435 } | 483 } |
| 436 | 484 |
| 437 return true; | 485 return true; |
| 438 } | 486 } |
| 439 | 487 |
| 440 } // namespace internal | 488 } // namespace internal |
| 441 | 489 |
| 442 } // namespace bookmark_html_reader | 490 } // namespace bookmark_html_reader |
| OLD | NEW |