| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 15 #include "chrome/browser/importer/external_process_importer_host.h" | 16 #include "chrome/browser/importer/external_process_importer_host.h" |
| 16 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | 17 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
| 17 #include "chrome/common/importer/imported_bookmark_entry.h" | 18 #include "chrome/common/importer/imported_bookmark_entry.h" |
| 18 #include "chrome/common/importer/importer_autofill_form_data_entry.h" | 19 #include "chrome/common/importer/importer_autofill_form_data_entry.h" |
| 19 #include "components/autofill/core/browser/webdata/autofill_entry.h" | 20 #include "components/autofill/core/browser/webdata/autofill_entry.h" |
| 20 #include "components/autofill/core/common/password_form.h" | 21 #include "components/autofill/core/common/password_form.h" |
| 21 #include "components/favicon_base/favicon_usage_data.h" | 22 #include "components/favicon_base/favicon_usage_data.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 return false; | 90 return false; |
| 90 } | 91 } |
| 91 return true; | 92 return true; |
| 92 } | 93 } |
| 93 | 94 |
| 94 private: | 95 private: |
| 95 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter); | 96 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter); |
| 96 }; | 97 }; |
| 97 | 98 |
| 98 // Attempts to create a TemplateURL from the provided data. |title| is optional. | 99 // Attempts to create a TemplateURL from the provided data. |title| is optional. |
| 99 // If TemplateURL creation fails, returns NULL. | 100 // If TemplateURL creation fails, returns null. |
| 100 // This function transfers ownership of the created TemplateURL to the caller. | 101 std::unique_ptr<TemplateURL> CreateTemplateURL(const base::string16& url, |
| 101 TemplateURL* CreateTemplateURL(const base::string16& url, | 102 const base::string16& keyword, |
| 102 const base::string16& keyword, | 103 const base::string16& title) { |
| 103 const base::string16& title) { | |
| 104 if (url.empty() || keyword.empty()) | 104 if (url.empty() || keyword.empty()) |
| 105 return NULL; | 105 return nullptr; |
| 106 TemplateURLData data; | 106 TemplateURLData data; |
| 107 data.SetKeyword(keyword); | 107 data.SetKeyword(keyword); |
| 108 // We set short name by using the title if it exists. | 108 // We set short name by using the title if it exists. |
| 109 // Otherwise, we use the shortcut. | 109 // Otherwise, we use the shortcut. |
| 110 data.SetShortName(title.empty() ? keyword : title); | 110 data.SetShortName(title.empty() ? keyword : title); |
| 111 data.SetURL(TemplateURLRef::DisplayURLToURLRef(url)); | 111 data.SetURL(TemplateURLRef::DisplayURLToURLRef(url)); |
| 112 return new TemplateURL(data); | 112 return base::MakeUnique<TemplateURL>(data); |
| 113 } | 113 } |
| 114 | 114 |
| 115 // Parses the OpenSearch XML files in |xml_files| and populates |search_engines| | 115 // Parses the OpenSearch XML files in |xml_files| and populates |search_engines| |
| 116 // with the resulting TemplateURLs. | 116 // with the resulting TemplateURLs. |
| 117 void ParseSearchEnginesFromFirefoxXMLData( | 117 void ParseSearchEnginesFromFirefoxXMLData( |
| 118 const std::vector<std::string>& xml_data, | 118 const std::vector<std::string>& xml_data, |
| 119 std::vector<TemplateURL*>* search_engines) { | 119 TemplateURLService::OwnedTemplateURLVector* search_engines) { |
| 120 DCHECK(search_engines); | 120 DCHECK(search_engines); |
| 121 | 121 |
| 122 typedef std::map<std::string, TemplateURL*> SearchEnginesMap; | 122 std::map<std::string, std::unique_ptr<TemplateURL>> search_engine_for_url; |
| 123 SearchEnginesMap search_engine_for_url; | |
| 124 FirefoxURLParameterFilter param_filter; | 123 FirefoxURLParameterFilter param_filter; |
| 125 // The first XML file represents the default search engine in Firefox 3, so we | 124 // The first XML file represents the default search engine in Firefox 3, so we |
| 126 // need to keep it on top of the list. | 125 // need to keep it on top of the list. |
| 127 SearchEnginesMap::const_iterator default_turl = search_engine_for_url.end(); | 126 auto default_turl = search_engine_for_url.end(); |
| 128 for (std::vector<std::string>::const_iterator xml_iter = | 127 for (std::vector<std::string>::const_iterator xml_iter = |
| 129 xml_data.begin(); xml_iter != xml_data.end(); ++xml_iter) { | 128 xml_data.begin(); xml_iter != xml_data.end(); ++xml_iter) { |
| 130 TemplateURL* template_url = TemplateURLParser::Parse( | 129 std::unique_ptr<TemplateURL> template_url = TemplateURLParser::Parse( |
| 131 UIThreadSearchTermsData(NULL), true, | 130 UIThreadSearchTermsData(nullptr), true, xml_iter->data(), |
| 132 xml_iter->data(), xml_iter->length(), ¶m_filter); | 131 xml_iter->length(), ¶m_filter); |
| 133 if (template_url) { | 132 if (template_url) { |
| 134 SearchEnginesMap::iterator iter = | 133 auto iter = search_engine_for_url.find(template_url->url()); |
| 135 search_engine_for_url.find(template_url->url()); | |
| 136 if (iter == search_engine_for_url.end()) { | 134 if (iter == search_engine_for_url.end()) { |
| 137 iter = search_engine_for_url.insert( | 135 iter = search_engine_for_url |
| 138 std::make_pair(template_url->url(), template_url)).first; | 136 .insert(std::make_pair(template_url->url(), |
| 137 std::move(template_url))) |
| 138 .first; |
| 139 } else { | 139 } else { |
| 140 // We have already found a search engine with the same URL. We give | 140 // We have already found a search engine with the same URL. We give |
| 141 // priority to the latest one found, as GetSearchEnginesXMLFiles() | 141 // priority to the latest one found, as GetSearchEnginesXMLFiles() |
| 142 // returns a vector with first Firefox default search engines and then | 142 // returns a vector with first Firefox default search engines and then |
| 143 // the user's ones. We want to give priority to the user ones. | 143 // the user's ones. We want to give priority to the user ones. |
| 144 delete iter->second; | 144 iter->second = std::move(template_url); |
| 145 iter->second = template_url; | |
| 146 } | 145 } |
| 147 if (default_turl == search_engine_for_url.end()) | 146 if (default_turl == search_engine_for_url.end()) |
| 148 default_turl = iter; | 147 default_turl = iter; |
| 149 } | 148 } |
| 150 } | 149 } |
| 151 | 150 |
| 152 // Put the results in the |search_engines| vector. | 151 // Put the results in the |search_engines| vector. |
| 153 for (SearchEnginesMap::iterator t_iter = search_engine_for_url.begin(); | 152 for (auto t_iter = search_engine_for_url.begin(); |
| 154 t_iter != search_engine_for_url.end(); ++t_iter) { | 153 t_iter != search_engine_for_url.end(); ++t_iter) { |
| 155 if (t_iter == default_turl) | 154 if (t_iter == default_turl) |
| 156 search_engines->insert(search_engines->begin(), default_turl->second); | 155 search_engines->insert(search_engines->begin(), |
| 156 std::move(default_turl->second)); |
| 157 else | 157 else |
| 158 search_engines->push_back(t_iter->second); | 158 search_engines->push_back(std::move(t_iter->second)); |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 | 161 |
| 162 } // namespace | 162 } // namespace |
| 163 | 163 |
| 164 InProcessImporterBridge::InProcessImporterBridge( | 164 InProcessImporterBridge::InProcessImporterBridge( |
| 165 ProfileWriter* writer, | 165 ProfileWriter* writer, |
| 166 base::WeakPtr<ExternalProcessImporterHost> host) : writer_(writer), | 166 base::WeakPtr<ExternalProcessImporterHost> host) : writer_(writer), |
| 167 host_(host) { | 167 host_(host) { |
| 168 } | 168 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 FROM_HERE, | 215 FROM_HERE, |
| 216 base::Bind(&ProfileWriter::AddHistoryPage, | 216 base::Bind(&ProfileWriter::AddHistoryPage, |
| 217 writer_, | 217 writer_, |
| 218 converted_rows, | 218 converted_rows, |
| 219 converted_visit_source)); | 219 converted_visit_source)); |
| 220 } | 220 } |
| 221 | 221 |
| 222 void InProcessImporterBridge::SetKeywords( | 222 void InProcessImporterBridge::SetKeywords( |
| 223 const std::vector<importer::SearchEngineInfo>& search_engines, | 223 const std::vector<importer::SearchEngineInfo>& search_engines, |
| 224 bool unique_on_host_and_path) { | 224 bool unique_on_host_and_path) { |
| 225 ScopedVector<TemplateURL> owned_template_urls; | 225 TemplateURLService::OwnedTemplateURLVector owned_template_urls; |
| 226 for (const auto& search_engine : search_engines) { | 226 for (const auto& search_engine : search_engines) { |
| 227 TemplateURL* owned_template_url = | 227 std::unique_ptr<TemplateURL> owned_template_url = CreateTemplateURL( |
| 228 CreateTemplateURL(search_engine.url, | 228 search_engine.url, search_engine.keyword, search_engine.display_name); |
| 229 search_engine.keyword, | |
| 230 search_engine.display_name); | |
| 231 if (owned_template_url) | 229 if (owned_template_url) |
| 232 owned_template_urls.push_back(owned_template_url); | 230 owned_template_urls.push_back(std::move(owned_template_url)); |
| 233 } | 231 } |
| 234 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 232 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 235 base::Bind(&ProfileWriter::AddKeywords, writer_, | 233 base::Bind(&ProfileWriter::AddKeywords, writer_, |
| 236 base::Passed(&owned_template_urls), unique_on_host_and_path)); | 234 base::Passed(&owned_template_urls), unique_on_host_and_path)); |
| 237 } | 235 } |
| 238 | 236 |
| 239 void InProcessImporterBridge::SetFirefoxSearchEnginesXMLData( | 237 void InProcessImporterBridge::SetFirefoxSearchEnginesXMLData( |
| 240 const std::vector<std::string>& search_engine_data) { | 238 const std::vector<std::string>& search_engine_data) { |
| 241 std::vector<TemplateURL*> search_engines; | 239 TemplateURLService::OwnedTemplateURLVector search_engines; |
| 242 ParseSearchEnginesFromFirefoxXMLData(search_engine_data, &search_engines); | 240 ParseSearchEnginesFromFirefoxXMLData(search_engine_data, &search_engines); |
| 243 | 241 |
| 244 ScopedVector<TemplateURL> owned_template_urls; | |
| 245 std::copy(search_engines.begin(), search_engines.end(), | |
| 246 std::back_inserter(owned_template_urls)); | |
| 247 | |
| 248 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 242 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 249 base::Bind(&ProfileWriter::AddKeywords, writer_, | 243 base::Bind(&ProfileWriter::AddKeywords, writer_, |
| 250 base::Passed(&owned_template_urls), true)); | 244 base::Passed(&search_engines), true)); |
| 251 } | 245 } |
| 252 | 246 |
| 253 void InProcessImporterBridge::SetPasswordForm( | 247 void InProcessImporterBridge::SetPasswordForm( |
| 254 const autofill::PasswordForm& form) { | 248 const autofill::PasswordForm& form) { |
| 255 BrowserThread::PostTask( | 249 BrowserThread::PostTask( |
| 256 BrowserThread::UI, FROM_HERE, | 250 BrowserThread::UI, FROM_HERE, |
| 257 base::Bind(&ProfileWriter::AddPasswordForm, writer_, form)); | 251 base::Bind(&ProfileWriter::AddPasswordForm, writer_, form)); |
| 258 } | 252 } |
| 259 | 253 |
| 260 void InProcessImporterBridge::SetAutofillFormData( | 254 void InProcessImporterBridge::SetAutofillFormData( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 BrowserThread::PostTask( | 292 BrowserThread::PostTask( |
| 299 BrowserThread::UI, FROM_HERE, | 293 BrowserThread::UI, FROM_HERE, |
| 300 base::Bind(&ExternalProcessImporterHost::NotifyImportEnded, host_)); | 294 base::Bind(&ExternalProcessImporterHost::NotifyImportEnded, host_)); |
| 301 } | 295 } |
| 302 | 296 |
| 303 base::string16 InProcessImporterBridge::GetLocalizedString(int message_id) { | 297 base::string16 InProcessImporterBridge::GetLocalizedString(int message_id) { |
| 304 return l10n_util::GetStringUTF16(message_id); | 298 return l10n_util::GetStringUTF16(message_id); |
| 305 } | 299 } |
| 306 | 300 |
| 307 InProcessImporterBridge::~InProcessImporterBridge() {} | 301 InProcessImporterBridge::~InProcessImporterBridge() {} |
| OLD | NEW |