Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/importer/in_process_importer_bridge.h" | 5 #include "chrome/browser/importer/in_process_importer_bridge.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/bookmarks/imported_bookmark_entry.h" | 10 #include "chrome/browser/bookmarks/imported_bookmark_entry.h" |
| 10 #include "chrome/browser/favicon/imported_favicon_usage.h" | 11 #include "chrome/browser/favicon/imported_favicon_usage.h" |
| 11 #include "chrome/browser/importer/importer_host.h" | 12 #include "chrome/browser/importer/importer_host.h" |
| 12 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "chrome/browser/search_engines/template_url_parser.h" | |
| 15 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | |
| 16 #include "chrome/browser/search_engines/template_url_service.h" | |
| 13 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/common/password_form.h" | 18 #include "content/public/common/password_form.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 16 | 20 |
| 17 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
| 18 #include "components/webdata/encryptor/ie7_password.h" | 22 #include "components/webdata/encryptor/ie7_password.h" |
| 19 #endif | 23 #endif |
| 20 | 24 |
| 21 #include <iterator> | 25 #include <iterator> |
| 22 | 26 |
| 23 using content::BrowserThread; | 27 using content::BrowserThread; |
| 24 | 28 |
| 29 namespace { | |
| 30 | |
| 31 // FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from | |
| 32 // the search URL when importing search engines. | |
| 33 class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter { | |
| 34 public: | |
| 35 FirefoxURLParameterFilter() {} | |
| 36 virtual ~FirefoxURLParameterFilter() {} | |
| 37 | |
| 38 // TemplateURLParser::ParameterFilter method. | |
| 39 virtual bool KeepParameter(const std::string& key, | |
| 40 const std::string& value) OVERRIDE { | |
| 41 std::string low_value = StringToLowerASCII(value); | |
| 42 if (low_value.find("mozilla") != std::string::npos || | |
| 43 low_value.find("firefox") != std::string::npos || | |
| 44 low_value.find("moz:") != std::string::npos) { | |
| 45 return false; | |
| 46 } | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter); | |
| 52 }; | |
| 53 | |
| 54 // Creates a TemplateURL with the |keyword| and |url|. |title| may be empty. | |
| 55 // This function transfers ownership of the created TemplateURL to the caller. | |
| 56 TemplateURL* CreateTemplateURL(const string16& title, | |
| 57 const string16& keyword, | |
| 58 const GURL& url) { | |
| 59 // Skip if the url is invalid. | |
| 60 if (!url.is_valid()) | |
| 61 return NULL; | |
| 62 | |
| 63 TemplateURLData data; | |
| 64 if (keyword.empty()) | |
| 65 data.SetKeyword(TemplateURLService::GenerateKeyword(url)); | |
| 66 else | |
| 67 data.SetKeyword(keyword); | |
| 68 // We set short name by using the title if it exists. | |
| 69 // Otherwise, we use the shortcut. | |
| 70 data.short_name = title.empty() ? keyword : title; | |
| 71 data.SetKeyword(keyword); | |
|
gab
2013/06/28 00:28:45
nit: Remove this line, keyword is already taken ca
ananta
2013/06/28 01:03:29
Done.
| |
| 72 data.SetURL(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url.spec()))); | |
|
gab
2013/06/28 00:28:45
What's the difference between taking TemplateURLRe
ananta
2013/06/28 01:03:29
The DisplayURLToURLRef function does some processi
gab
2013/06/28 01:55:07
I see thanks. re-lgtm.
| |
| 73 return new TemplateURL(NULL, data); | |
| 74 } | |
| 75 | |
| 76 // Parses the OpenSearch XML files in |xml_files| and populates |search_engines| | |
| 77 // with the resulting TemplateURLs. | |
| 78 void ParseSearchEnginesFromFirefoxXMLData( | |
| 79 const std::vector<std::string>& xml_data, | |
| 80 std::vector<TemplateURL*>* search_engines) { | |
| 81 DCHECK(search_engines); | |
| 82 | |
| 83 typedef std::map<std::string, TemplateURL*> SearchEnginesMap; | |
| 84 SearchEnginesMap search_engine_for_url; | |
| 85 std::string content; | |
| 86 FirefoxURLParameterFilter param_filter; | |
| 87 // The first XML file represents the default search engine in Firefox 3, so we | |
| 88 // need to keep it on top of the list. | |
| 89 SearchEnginesMap::const_iterator default_turl = search_engine_for_url.end(); | |
| 90 for (std::vector<std::string>::const_iterator xml_iter = | |
| 91 xml_data.begin(); xml_iter != xml_data.end(); ++xml_iter) { | |
| 92 TemplateURL* template_url = TemplateURLParser::Parse(NULL, true, | |
| 93 xml_iter->data(), xml_iter->length(), ¶m_filter); | |
| 94 if (template_url) { | |
| 95 SearchEnginesMap::iterator iter = | |
| 96 search_engine_for_url.find(template_url->url()); | |
| 97 if (iter == search_engine_for_url.end()) { | |
| 98 iter = search_engine_for_url.insert( | |
| 99 std::make_pair(template_url->url(), template_url)).first; | |
| 100 } else { | |
| 101 // We have already found a search engine with the same URL. We give | |
| 102 // priority to the latest one found, as GetSearchEnginesXMLFiles() | |
| 103 // returns a vector with first Firefox default search engines and then | |
| 104 // the user's ones. We want to give priority to the user ones. | |
| 105 delete iter->second; | |
| 106 iter->second = template_url; | |
| 107 } | |
| 108 if (default_turl == search_engine_for_url.end()) | |
| 109 default_turl = iter; | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 // Put the results in the |search_engines| vector. | |
| 114 for (SearchEnginesMap::iterator t_iter = search_engine_for_url.begin(); | |
| 115 t_iter != search_engine_for_url.end(); ++t_iter) { | |
| 116 if (t_iter == default_turl) | |
| 117 search_engines->insert(search_engines->begin(), default_turl->second); | |
| 118 else | |
| 119 search_engines->push_back(t_iter->second); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 } // namespace | |
| 124 | |
| 25 InProcessImporterBridge::InProcessImporterBridge( | 125 InProcessImporterBridge::InProcessImporterBridge( |
| 26 ProfileWriter* writer, | 126 ProfileWriter* writer, |
| 27 base::WeakPtr<ImporterHost> host) : writer_(writer), | 127 base::WeakPtr<ImporterHost> host) : writer_(writer), |
| 28 host_(host) { | 128 host_(host) { |
| 29 } | 129 } |
| 30 | 130 |
| 31 void InProcessImporterBridge::AddBookmarks( | 131 void InProcessImporterBridge::AddBookmarks( |
| 32 const std::vector<ImportedBookmarkEntry>& bookmarks, | 132 const std::vector<ImportedBookmarkEntry>& bookmarks, |
| 33 const string16& first_folder_name) { | 133 const string16& first_folder_name) { |
| 34 BrowserThread::PostTask( | 134 BrowserThread::PostTask( |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 61 | 161 |
| 62 void InProcessImporterBridge::SetHistoryItems( | 162 void InProcessImporterBridge::SetHistoryItems( |
| 63 const history::URLRows &rows, | 163 const history::URLRows &rows, |
| 64 history::VisitSource visit_source) { | 164 history::VisitSource visit_source) { |
| 65 BrowserThread::PostTask( | 165 BrowserThread::PostTask( |
| 66 BrowserThread::UI, FROM_HERE, | 166 BrowserThread::UI, FROM_HERE, |
| 67 base::Bind(&ProfileWriter::AddHistoryPage, writer_, rows, visit_source)); | 167 base::Bind(&ProfileWriter::AddHistoryPage, writer_, rows, visit_source)); |
| 68 } | 168 } |
| 69 | 169 |
| 70 void InProcessImporterBridge::SetKeywords( | 170 void InProcessImporterBridge::SetKeywords( |
| 71 const std::vector<TemplateURL*>& template_urls, | 171 const std::vector<importer::URLKeywordInfo>& url_keywords, |
| 72 bool unique_on_host_and_path) { | 172 bool unique_on_host_and_path) { |
| 73 ScopedVector<TemplateURL> owned_template_urls; | 173 ScopedVector<TemplateURL> owned_template_urls; |
| 74 std::copy(template_urls.begin(), template_urls.end(), | 174 for (size_t i = 0; i < url_keywords.size(); ++i) { |
| 75 std::back_inserter(owned_template_urls)); | 175 owned_template_urls.push_back( |
| 176 CreateTemplateURL(url_keywords[i].display_name, | |
| 177 url_keywords[i].keyword, | |
| 178 url_keywords[i].url)); | |
| 179 } | |
| 76 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 180 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 77 base::Bind(&ProfileWriter::AddKeywords, writer_, | 181 base::Bind(&ProfileWriter::AddKeywords, writer_, |
| 78 base::Passed(&owned_template_urls), unique_on_host_and_path)); | 182 base::Passed(&owned_template_urls), unique_on_host_and_path)); |
| 79 } | 183 } |
| 80 | 184 |
| 185 void InProcessImporterBridge::SetFirefoxSearchEnginesXMLData( | |
| 186 const std::vector<std::string>& search_engine_data) { | |
| 187 std::vector<TemplateURL*> search_engines; | |
| 188 ParseSearchEnginesFromFirefoxXMLData(search_engine_data, &search_engines); | |
| 189 | |
| 190 ScopedVector<TemplateURL> owned_template_urls; | |
| 191 std::copy(search_engines.begin(), search_engines.end(), | |
| 192 std::back_inserter(owned_template_urls)); | |
| 193 | |
| 194 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 195 base::Bind(&ProfileWriter::AddKeywords, writer_, | |
| 196 base::Passed(&owned_template_urls), true)); | |
| 197 } | |
| 198 | |
| 81 void InProcessImporterBridge::SetPasswordForm( | 199 void InProcessImporterBridge::SetPasswordForm( |
| 82 const content::PasswordForm& form) { | 200 const content::PasswordForm& form) { |
| 83 BrowserThread::PostTask( | 201 BrowserThread::PostTask( |
| 84 BrowserThread::UI, FROM_HERE, | 202 BrowserThread::UI, FROM_HERE, |
| 85 base::Bind(&ProfileWriter::AddPasswordForm, writer_, form)); | 203 base::Bind(&ProfileWriter::AddPasswordForm, writer_, form)); |
| 86 } | 204 } |
| 87 | 205 |
| 88 void InProcessImporterBridge::NotifyStarted() { | 206 void InProcessImporterBridge::NotifyStarted() { |
| 89 BrowserThread::PostTask( | 207 BrowserThread::PostTask( |
| 90 BrowserThread::UI, FROM_HERE, | 208 BrowserThread::UI, FROM_HERE, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 107 BrowserThread::PostTask( | 225 BrowserThread::PostTask( |
| 108 BrowserThread::UI, FROM_HERE, | 226 BrowserThread::UI, FROM_HERE, |
| 109 base::Bind(&ImporterHost::NotifyImportEnded, host_)); | 227 base::Bind(&ImporterHost::NotifyImportEnded, host_)); |
| 110 } | 228 } |
| 111 | 229 |
| 112 string16 InProcessImporterBridge::GetLocalizedString(int message_id) { | 230 string16 InProcessImporterBridge::GetLocalizedString(int message_id) { |
| 113 return l10n_util::GetStringUTF16(message_id); | 231 return l10n_util::GetStringUTF16(message_id); |
| 114 } | 232 } |
| 115 | 233 |
| 116 InProcessImporterBridge::~InProcessImporterBridge() {} | 234 InProcessImporterBridge::~InProcessImporterBridge() {} |
| OLD | NEW |