Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: chrome/utility/importer/bookmark_html_reader.cc

Issue 616763002: Importing certain bookmarks from firefox and HTML file as search engines. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed feedback. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 importer::SearchEngineInfo search_engine;
158 bool is_valid_replaceable_url =
159 CanImportURLAsSearchEngine(url, shortcut, title, &search_engine);
Ilya Sherman 2014/11/10 23:00:22 Optional nit: I'd probably inline this into the if
Tapu Ghose 2014/11/15 05:22:20 Done.
160
161 // If bookmark contains a valid replaceable url and a keyword then import
162 // it as search engine.
163 if (is_bookmark && post_data.empty() && is_valid_replaceable_url &&
164 !shortcut.empty()) {
165 search_engines->push_back(search_engine);
166 continue;
167 }
168
153 if (is_bookmark) 169 if (is_bookmark)
154 last_folder_is_empty = false; 170 last_folder_is_empty = false;
155 171
156 if (is_bookmark && 172 if (is_bookmark &&
157 post_data.empty() && 173 post_data.empty() &&
158 (valid_url_callback.is_null() || valid_url_callback.Run(url))) { 174 (valid_url_callback.is_null() || valid_url_callback.Run(url))) {
159 if (toolbar_folder_index > path.size() && !path.empty()) { 175 if (toolbar_folder_index > path.size() && !path.empty()) {
160 NOTREACHED(); // error in parsing. 176 NOTREACHED(); // error in parsing.
161 break; 177 break;
162 } 178 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 // Parent folder include current one, so it's not empty. 247 // Parent folder include current one, so it's not empty.
232 last_folder_is_empty = false; 248 last_folder_is_empty = false;
233 } 249 }
234 250
235 if (toolbar_folder_index > path.size()) 251 if (toolbar_folder_index > path.size())
236 toolbar_folder_index = 0; 252 toolbar_folder_index = 0;
237 } 253 }
238 } 254 }
239 } 255 }
240 256
257 bool CanImportURLAsSearchEngine(const GURL& url,
258 const base::string16& keyword,
259 const base::string16& title,
260 importer::SearchEngineInfo* search_engine) {
Peter Kasting 2014/11/11 22:42:11 Rather than taking a SearchEngineInfo*, it seems l
Tapu Ghose 2014/11/15 05:22:19 My understanding was that the caller indeed needs
261 std::string url_spec = url.possibly_invalid_spec();
262
263 if (url_spec.empty())
264 return false;
265
266 std::string raw_url = net::UnescapeURLComponent(
267 url_spec,
268 net::UnescapeRule::URL_SPECIAL_CHARS);
269
270 search_engine->url.assign(base::UTF8ToUTF16(raw_url));
271 search_engine->keyword = keyword;
272 search_engine->display_name = title;
273
274 const std::string kReplacementTerm("%s");
275 const std::string kSearchTerms("{searchTerms}");
276 // Replace replacement terms in the |raw_url| with {searchTerms}. This is
277 // necessary so that |raw_url| can be parsed for replacement terms by
278 // |ParseURL| which will be called through |SupportsReplacement|.
279 ReplaceSubstringsAfterOffset(&raw_url, 0, kReplacementTerm, kSearchTerms);
Peter Kasting 2014/11/11 22:42:12 Please use TemplateURLRef::DisplayURLToURLRef() in
Tapu Ghose 2014/11/15 05:22:19 Many many thanks for this suggestion.
280 TemplateURLData data;
281 data.SetURL(raw_url);
282 return TemplateURL(data).SupportsReplacement(SearchTermsData());
283 }
284
241 namespace internal { 285 namespace internal {
242 286
243 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { 287 bool ParseCharsetFromLine(const std::string& line, std::string* charset) {
244 const char kCharset[] = "charset="; 288 const char kCharset[] = "charset=";
245 if (StartsWithASCII(line, "<META", false) && 289 if (StartsWithASCII(line, "<META", false) &&
246 (line.find("CONTENT=\"") != std::string::npos || 290 (line.find("CONTENT=\"") != std::string::npos ||
247 line.find("content=\"") != std::string::npos)) { 291 line.find("content=\"") != std::string::npos)) {
248 size_t begin = line.find(kCharset); 292 size_t begin = line.find(kCharset);
249 if (begin == std::string::npos) 293 if (begin == std::string::npos)
250 return false; 294 return false;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 *url = GURL(value); 477 *url = GURL(value);
434 } 478 }
435 } 479 }
436 480
437 return true; 481 return true;
438 } 482 }
439 483
440 } // namespace internal 484 } // namespace internal
441 485
442 } // namespace bookmark_html_reader 486 } // namespace bookmark_html_reader
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698