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

Unified 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: Created 6 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: chrome/utility/importer/bookmark_html_reader.cc
diff --git a/chrome/utility/importer/bookmark_html_reader.cc b/chrome/utility/importer/bookmark_html_reader.cc
index e11dc1d7761f68631aede24d93a78fe37839f162..cd0e29a3aa2ecbccfb6d04d36aba34852d8efb7e 100644
--- a/chrome/utility/importer/bookmark_html_reader.cc
+++ b/chrome/utility/importer/bookmark_html_reader.cc
@@ -10,6 +10,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/common/importer/imported_bookmark_entry.h"
#include "chrome/common/importer/imported_favicon_usage.h"
@@ -18,6 +19,7 @@
#include "net/base/escape.h"
#include "url/gurl.h"
#include "url/url_constants.h"
+#include "url/url_util.h"
namespace {
@@ -91,6 +93,7 @@ void ImportBookmarksFile(
const base::Callback<bool(const GURL&)>& valid_url_callback,
const base::FilePath& file_path,
std::vector<ImportedBookmarkEntry>* bookmarks,
+ std::vector<importer::URLKeywordInfo>* url_keywords,
std::vector<ImportedFaviconUsage>* favicons) {
std::string content;
base::ReadFileToString(file_path, &content);
@@ -153,14 +156,32 @@ void ImportBookmarksFile(
if (is_bookmark)
last_folder_is_empty = false;
+ base::string16 decoded_url;
+
if (is_bookmark &&
post_data.empty() &&
- (valid_url_callback.is_null() || valid_url_callback.Run(url))) {
+ (valid_url_callback.is_null() || valid_url_callback.Run(url)
+ || CanImportURLAsSearchEngine(url, &decoded_url))) {
Ilya Sherman 2014/09/30 20:54:12 nit: The "||" should be on the previous line. ("g
Tapu Ghose 2014/10/07 02:02:44 Done.
if (toolbar_folder_index > path.size() && !path.empty()) {
NOTREACHED(); // error in parsing.
break;
}
+ // If bookmark contains a keyword then import it as search engine.
+ // If |url| is invalid, set raw_url property of |url_keyword_info|
+ // which will be used in importing as search engine.
+ if (!shortcut.empty()) {
Peter Kasting 2014/10/01 01:02:55 Doesn't this mean we'll also import non-replaceabl
Tapu Ghose 2014/10/07 02:02:44 Agree with you. Updated logic to address your comm
+ importer::URLKeywordInfo url_keyword_info;
+ if (url.is_valid())
+ url_keyword_info.url = url;
+ else
+ url_keyword_info.raw_url = decoded_url;
+ url_keyword_info.keyword.assign(shortcut);
+ url_keyword_info.display_name = title;
+ url_keywords->push_back(url_keyword_info);
+ continue;
+ }
+
ImportedBookmarkEntry entry;
entry.creation_time = add_date;
entry.url = url;
@@ -238,6 +259,36 @@ void ImportBookmarksFile(
}
}
+bool CanImportURLAsSearchEngine(const GURL& url, base::string16* decoded_url) {
+ // If |url| is valid then we are not supposed to be here.
Peter Kasting 2014/10/01 01:02:55 That implies callers shouldn't be calling us. I d
Tapu Ghose 2014/10/07 02:02:44 Updated comments.
+ if (url.is_valid())
+ return false;
+
+ std::string raw_url = url.possibly_invalid_spec();
+
+ // Decode raw_url.
+ url::RawCanonOutputW<1024> canon_output;
Ilya Sherman 2014/09/30 20:54:12 Hmm, how do you know that 1024 characters is enoug
Peter Kasting 2014/10/01 01:02:55 It's not necessarily enough. This is almost certa
Tapu Ghose 2014/10/07 02:02:44 Acknowledged.
Tapu Ghose 2014/10/07 02:02:44 Acknowledged.
+ url::DecodeURLEscapeSequences(raw_url.c_str(), raw_url.size(), &canon_output);
Peter Kasting 2014/10/01 01:02:55 Why can't we use the higher-level unescaping funct
Tapu Ghose 2014/10/07 02:02:44 My bad. I could not find higher-level unescaping f
+ decoded_url->assign(base::string16(canon_output.data(),
+ canon_output.length()));
+
+ raw_url = base::UTF16ToUTF8(*decoded_url);
+ const std::string kReplacementTerm("%s");
+
+ if (raw_url.find(kReplacementTerm) == std::string::npos)
+ return false;
+
+ // Substitute replacement term with arbitrary value. Return false if the
+ // resulted output is an invalid url.
+ size_t n = 0;
+ const std::string kReplacementValue("val");
+ while ((n = raw_url.find(kReplacementTerm, n)) != std::string::npos) {
+ raw_url.replace(n, kReplacementTerm.size(), kReplacementValue);
+ n += kReplacementValue.size();
+ }
Ilya Sherman 2014/09/30 20:54:12 Please use a utility function from base/strings/st
Peter Kasting 2014/10/01 01:02:55 Furthermore, there's already code in Chrome that a
Tapu Ghose 2014/10/07 02:02:44 Before writing my custom code I tried to use Repla
Tapu Ghose 2014/10/07 02:02:44 Replaced my own version with ReplaceSubstringsAfte
Ilya Sherman 2014/10/07 22:08:22 That sounds like a dependency issue, caused by the
Tapu Ghose 2014/10/12 00:58:19 Tried after adding '../components/search_engines.g
Ilya Sherman 2014/10/14 00:41:49 Hmm, based on local testing, it looks like you nee
Tapu Ghose 2014/10/19 02:46:33 After modifying .gyp* and BUILD.gn do I need to ru
+ return GURL(raw_url).is_valid();
+}
+
namespace internal {
bool ParseCharsetFromLine(const std::string& line, std::string* charset) {

Powered by Google App Engine
This is Rietveld 408576698