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

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 comments Created 6 years, 2 months 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"
17 #include "net/base/data_url.h" 18 #include "net/base/data_url.h"
18 #include "net/base/escape.h" 19 #include "net/base/escape.h"
19 #include "url/gurl.h" 20 #include "url/gurl.h"
20 #include "url/url_constants.h" 21 #include "url/url_constants.h"
21 22
22 namespace { 23 namespace {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 85
85 } // namespace 86 } // namespace
86 87
87 namespace bookmark_html_reader { 88 namespace bookmark_html_reader {
88 89
89 void ImportBookmarksFile( 90 void ImportBookmarksFile(
90 const base::Callback<bool(void)>& cancellation_callback, 91 const base::Callback<bool(void)>& cancellation_callback,
91 const base::Callback<bool(const GURL&)>& valid_url_callback, 92 const base::Callback<bool(const GURL&)>& valid_url_callback,
92 const base::FilePath& file_path, 93 const base::FilePath& file_path,
93 std::vector<ImportedBookmarkEntry>* bookmarks, 94 std::vector<ImportedBookmarkEntry>* bookmarks,
95 std::vector<importer::URLKeywordInfo>* search_engine_keywords,
94 std::vector<ImportedFaviconUsage>* favicons) { 96 std::vector<ImportedFaviconUsage>* favicons) {
95 std::string content; 97 std::string content;
96 base::ReadFileToString(file_path, &content); 98 base::ReadFileToString(file_path, &content);
97 std::vector<std::string> lines; 99 std::vector<std::string> lines;
98 base::SplitString(content, '\n', &lines); 100 base::SplitString(content, '\n', &lines);
99 101
100 base::string16 last_folder; 102 base::string16 last_folder;
101 bool last_folder_on_toolbar = false; 103 bool last_folder_on_toolbar = false;
102 bool last_folder_is_empty = true; 104 bool last_folder_is_empty = true;
103 bool has_subfolder = false; 105 bool has_subfolder = false;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 // keywords yet. 148 // keywords yet.
147 is_bookmark = 149 is_bookmark =
148 internal::ParseBookmarkFromLine(line, charset, &title, 150 internal::ParseBookmarkFromLine(line, charset, &title,
149 &url, &favicon, &shortcut, 151 &url, &favicon, &shortcut,
150 &add_date, &post_data) || 152 &add_date, &post_data) ||
151 internal::ParseMinimumBookmarkFromLine(line, charset, &title, &url); 153 internal::ParseMinimumBookmarkFromLine(line, charset, &title, &url);
152 154
153 if (is_bookmark) 155 if (is_bookmark)
154 last_folder_is_empty = false; 156 last_folder_is_empty = false;
155 157
158 importer::URLKeywordInfo search_engine_keyword;
159 bool is_valid_replaceable_url =
Peter Kasting 2014/10/07 23:17:38 Nit: Why are the variable and the function named t
Tapu Ghose 2014/10/12 00:58:20 Renamed to search_engine with type as SearchEngine
160 CanImportURLAsSearchEngine(url, shortcut,
161 title, &search_engine_keyword);
Ilya Sherman 2014/10/07 22:08:22 nit: This formatting looks off. Please run "git c
Peter Kasting 2014/10/07 23:17:38 No, this formatting is legal.
Tapu Ghose 2014/10/12 00:58:20 Acknowledged.
Tapu Ghose 2014/10/12 00:58:20 Acknowledged.
162
156 if (is_bookmark && 163 if (is_bookmark &&
157 post_data.empty() && 164 post_data.empty() &&
158 (valid_url_callback.is_null() || valid_url_callback.Run(url))) { 165 (valid_url_callback.is_null() || valid_url_callback.Run(url) ||
166 is_valid_replaceable_url)) {
159 if (toolbar_folder_index > path.size() && !path.empty()) { 167 if (toolbar_folder_index > path.size() && !path.empty()) {
160 NOTREACHED(); // error in parsing. 168 NOTREACHED(); // error in parsing.
161 break; 169 break;
162 } 170 }
163 171
172 // If bookmark contains a valid replaceable url and a keyword then import
173 // it as search engine.
174 if (is_valid_replaceable_url && !shortcut.empty()) {
175 search_engine_keywords->push_back(search_engine_keyword);
176 continue;
177 }
178
164 ImportedBookmarkEntry entry; 179 ImportedBookmarkEntry entry;
165 entry.creation_time = add_date; 180 entry.creation_time = add_date;
166 entry.url = url; 181 entry.url = url;
167 entry.title = title; 182 entry.title = title;
168 183
169 if (toolbar_folder_index) { 184 if (toolbar_folder_index) {
170 // The toolbar folder should be at the top level. 185 // The toolbar folder should be at the top level.
171 entry.in_toolbar = true; 186 entry.in_toolbar = true;
172 entry.path.assign(path.begin() + toolbar_folder_index - 1, path.end()); 187 entry.path.assign(path.begin() + toolbar_folder_index - 1, path.end());
173 } else { 188 } else {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 // Parent folder include current one, so it's not empty. 246 // Parent folder include current one, so it's not empty.
232 last_folder_is_empty = false; 247 last_folder_is_empty = false;
233 } 248 }
234 249
235 if (toolbar_folder_index > path.size()) 250 if (toolbar_folder_index > path.size())
236 toolbar_folder_index = 0; 251 toolbar_folder_index = 0;
237 } 252 }
238 } 253 }
239 } 254 }
240 255
256 bool CanImportURLAsSearchEngine(const GURL& url,
257 const base::string16& keyword,
258 const base::string16& title,
259 importer::URLKeywordInfo* search_engine_info) {
260 // |url| can be imported as search engine if it contains replacement term(s).
261 // Usually, a |url| containing replacement term in host is considered as
262 // invalid. Hence, return false for valid |url|.
Ilya Sherman 2014/10/07 22:08:22 Hmm, what if the replacement term is outside the h
Tapu Ghose 2014/10/12 00:58:20 Removed the condition. After looking at the flow i
263 if (url.is_valid())
264 return false;
265
266 std::string raw_url = net::UnescapeURLComponent(
267 url.possibly_invalid_spec(),
268 net::UnescapeRule::SPACES |
269 net::UnescapeRule::URL_SPECIAL_CHARS |
270 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE);
Ilya Sherman 2014/10/07 22:08:22 Hmm, why do you need to unescape spaces?
Peter Kasting 2014/10/07 23:17:38 And pluses? Does this need to be some sort of "fu
Tapu Ghose 2014/10/12 00:58:20 Removed REPLACE_PLUS_WITH_SPACE.
Tapu Ghose 2014/10/12 00:58:20 As I mentioned in Patch Set 1...... when I try to
Tapu Ghose 2014/11/06 00:23:52 Finally, the dependency issue has been resolved. I
271 search_engine_info->raw_url.assign(base::UTF8ToUTF16(raw_url));
272 search_engine_info->keyword = keyword;
273 search_engine_info->display_name = title;
274
275 const std::string kReplacementTerm("%s");
276 if (raw_url.find(kReplacementTerm) == std::string::npos)
277 return false;
278
279 // Substitute replacement term with arbitrary value. Return false if the
280 // resulted output is an invalid url.
281 const std::string kReplacementVal("val");
282 ReplaceSubstringsAfterOffset(&raw_url, 0, kReplacementTerm, kReplacementVal);
283 return GURL(raw_url).is_valid();
284 }
285
241 namespace internal { 286 namespace internal {
242 287
243 bool ParseCharsetFromLine(const std::string& line, std::string* charset) { 288 bool ParseCharsetFromLine(const std::string& line, std::string* charset) {
244 const char kCharset[] = "charset="; 289 const char kCharset[] = "charset=";
245 if (StartsWithASCII(line, "<META", false) && 290 if (StartsWithASCII(line, "<META", false) &&
246 (line.find("CONTENT=\"") != std::string::npos || 291 (line.find("CONTENT=\"") != std::string::npos ||
247 line.find("content=\"") != std::string::npos)) { 292 line.find("content=\"") != std::string::npos)) {
248 size_t begin = line.find(kCharset); 293 size_t begin = line.find(kCharset);
249 if (begin == std::string::npos) 294 if (begin == std::string::npos)
250 return false; 295 return false;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 *url = GURL(value); 478 *url = GURL(value);
434 } 479 }
435 } 480 }
436 481
437 return true; 482 return true;
438 } 483 }
439 484
440 } // namespace internal 485 } // namespace internal
441 486
442 } // namespace bookmark_html_reader 487 } // namespace bookmark_html_reader
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698