| 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 return true; |
| 47 } |
| 48 |
| 49 private: |
| 50 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter); |
| 51 }; |
| 52 |
| 53 // Creates a TemplateURL with the |keyword| and |url|. |title| may be empty. |
| 54 // This function transfers ownership of the created TemplateURL to the caller. |
| 55 TemplateURL* CreateTemplateURL(const string16& title, |
| 56 const string16& keyword, |
| 57 const GURL& url) { |
| 58 // Skip if the url is invalid. |
| 59 if (!url.is_valid()) |
| 60 return NULL; |
| 61 |
| 62 TemplateURLData data; |
| 63 if (keyword.empty()) { |
| 64 data.short_name = title; |
| 65 data.SetKeyword(TemplateURLService::GenerateKeyword(url)); |
| 66 data.SetURL(url.spec()); |
| 67 data.show_in_default_list = true; |
| 68 } else { |
| 69 // We set short name by using the title if it exists. |
| 70 // Otherwise, we use the shortcut. |
| 71 data.short_name = title.empty() ? keyword : title; |
| 72 data.SetKeyword(keyword); |
| 73 data.SetURL(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url.spec()))); |
| 74 } |
| 75 return new TemplateURL(NULL, data); |
| 76 } |
| 77 |
| 78 // Parses the OpenSearch XML files in |xml_files| and populates |search_engines| |
| 79 // with the resulting TemplateURLs. |
| 80 void ParseSearchEnginesFromXMLData( |
| 81 const std::vector<std::string>& xml_data, |
| 82 std::vector<TemplateURL*>* search_engines) { |
| 83 DCHECK(search_engines); |
| 84 |
| 85 typedef std::map<std::string, TemplateURL*> SearchEnginesMap; |
| 86 SearchEnginesMap search_engine_for_url; |
| 87 std::string content; |
| 88 // The first XML file represents the default search engine in Firefox 3, so we |
| 89 // need to keep it on top of the list. |
| 90 SearchEnginesMap::const_iterator default_turl = search_engine_for_url.end(); |
| 91 for (std::vector<std::string>::const_iterator xml_iter = |
| 92 xml_data.begin(); xml_iter != xml_data.end(); ++xml_iter) { |
| 93 FirefoxURLParameterFilter param_filter; |
| 94 TemplateURL* template_url = TemplateURLParser::Parse(NULL, true, |
| 95 xml_iter->data(), xml_iter->length(), ¶m_filter); |
| 96 if (template_url) { |
| 97 SearchEnginesMap::iterator iter = |
| 98 search_engine_for_url.find(template_url->url()); |
| 99 if (iter == search_engine_for_url.end()) { |
| 100 iter = search_engine_for_url.insert( |
| 101 std::make_pair(template_url->url(), template_url)).first; |
| 102 } else { |
| 103 // We have already found a search engine with the same URL. We give |
| 104 // priority to the latest one found, as GetSearchEnginesXMLFiles() |
| 105 // returns a vector with first Firefox default search engines and then |
| 106 // the user's ones. We want to give priority to the user ones. |
| 107 delete iter->second; |
| 108 iter->second = template_url; |
| 109 } |
| 110 if (default_turl == search_engine_for_url.end()) |
| 111 default_turl = iter; |
| 112 } |
| 113 } |
| 114 |
| 115 // Put the results in the |search_engines| vector. |
| 116 for (SearchEnginesMap::iterator t_iter = search_engine_for_url.begin(); |
| 117 t_iter != search_engine_for_url.end(); ++t_iter) { |
| 118 if (t_iter == default_turl) |
| 119 search_engines->insert(search_engines->begin(), default_turl->second); |
| 120 else |
| 121 search_engines->push_back(t_iter->second); |
| 122 } |
| 123 } |
| 124 |
| 125 } |
| 126 |
| 25 InProcessImporterBridge::InProcessImporterBridge( | 127 InProcessImporterBridge::InProcessImporterBridge( |
| 26 ProfileWriter* writer, | 128 ProfileWriter* writer, |
| 27 base::WeakPtr<ImporterHost> host) : writer_(writer), | 129 base::WeakPtr<ImporterHost> host) : writer_(writer), |
| 28 host_(host) { | 130 host_(host) { |
| 29 } | 131 } |
| 30 | 132 |
| 31 void InProcessImporterBridge::AddBookmarks( | 133 void InProcessImporterBridge::AddBookmarks( |
| 32 const std::vector<ImportedBookmarkEntry>& bookmarks, | 134 const std::vector<ImportedBookmarkEntry>& bookmarks, |
| 33 const string16& first_folder_name) { | 135 const string16& first_folder_name) { |
| 34 BrowserThread::PostTask( | 136 BrowserThread::PostTask( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 61 | 163 |
| 62 void InProcessImporterBridge::SetHistoryItems( | 164 void InProcessImporterBridge::SetHistoryItems( |
| 63 const history::URLRows &rows, | 165 const history::URLRows &rows, |
| 64 history::VisitSource visit_source) { | 166 history::VisitSource visit_source) { |
| 65 BrowserThread::PostTask( | 167 BrowserThread::PostTask( |
| 66 BrowserThread::UI, FROM_HERE, | 168 BrowserThread::UI, FROM_HERE, |
| 67 base::Bind(&ProfileWriter::AddHistoryPage, writer_, rows, visit_source)); | 169 base::Bind(&ProfileWriter::AddHistoryPage, writer_, rows, visit_source)); |
| 68 } | 170 } |
| 69 | 171 |
| 70 void InProcessImporterBridge::SetKeywords( | 172 void InProcessImporterBridge::SetKeywords( |
| 71 const std::vector<TemplateURL*>& template_urls, | 173 const std::vector<importer::URLKeywordInfo>& url_keywords, |
| 72 bool unique_on_host_and_path) { | 174 bool unique_on_host_and_path) { |
| 73 ScopedVector<TemplateURL> owned_template_urls; | 175 ScopedVector<TemplateURL> owned_template_urls; |
| 74 std::copy(template_urls.begin(), template_urls.end(), | 176 for (size_t i = 0; i < url_keywords.size(); ++i) { |
| 75 std::back_inserter(owned_template_urls)); | 177 owned_template_urls.push_back( |
| 178 CreateTemplateURL(url_keywords[i].display_name, |
| 179 url_keywords[i].keyword, |
| 180 url_keywords[i].url)); |
| 181 } |
| 76 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 182 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 77 base::Bind(&ProfileWriter::AddKeywords, writer_, | 183 base::Bind(&ProfileWriter::AddKeywords, writer_, |
| 78 base::Passed(&owned_template_urls), unique_on_host_and_path)); | 184 base::Passed(&owned_template_urls), unique_on_host_and_path)); |
| 79 } | 185 } |
| 80 | 186 |
| 187 void InProcessImporterBridge::SetFirefoxSearchEnginesXMLData( |
| 188 const std::vector<std::string>& search_engine_data) { |
| 189 std::vector<TemplateURL*> search_engines; |
| 190 ParseSearchEnginesFromXMLData(search_engine_data, &search_engines); |
| 191 |
| 192 ScopedVector<TemplateURL> owned_template_urls; |
| 193 std::copy(search_engines.begin(), search_engines.end(), |
| 194 std::back_inserter(owned_template_urls)); |
| 195 |
| 196 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 197 base::Bind(&ProfileWriter::AddKeywords, writer_, |
| 198 base::Passed(&owned_template_urls), true)); |
| 199 } |
| 200 |
| 81 void InProcessImporterBridge::SetPasswordForm( | 201 void InProcessImporterBridge::SetPasswordForm( |
| 82 const content::PasswordForm& form) { | 202 const content::PasswordForm& form) { |
| 83 BrowserThread::PostTask( | 203 BrowserThread::PostTask( |
| 84 BrowserThread::UI, FROM_HERE, | 204 BrowserThread::UI, FROM_HERE, |
| 85 base::Bind(&ProfileWriter::AddPasswordForm, writer_, form)); | 205 base::Bind(&ProfileWriter::AddPasswordForm, writer_, form)); |
| 86 } | 206 } |
| 87 | 207 |
| 88 void InProcessImporterBridge::NotifyStarted() { | 208 void InProcessImporterBridge::NotifyStarted() { |
| 89 BrowserThread::PostTask( | 209 BrowserThread::PostTask( |
| 90 BrowserThread::UI, FROM_HERE, | 210 BrowserThread::UI, FROM_HERE, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 107 BrowserThread::PostTask( | 227 BrowserThread::PostTask( |
| 108 BrowserThread::UI, FROM_HERE, | 228 BrowserThread::UI, FROM_HERE, |
| 109 base::Bind(&ImporterHost::NotifyImportEnded, host_)); | 229 base::Bind(&ImporterHost::NotifyImportEnded, host_)); |
| 110 } | 230 } |
| 111 | 231 |
| 112 string16 InProcessImporterBridge::GetLocalizedString(int message_id) { | 232 string16 InProcessImporterBridge::GetLocalizedString(int message_id) { |
| 113 return l10n_util::GetStringUTF16(message_id); | 233 return l10n_util::GetStringUTF16(message_id); |
| 114 } | 234 } |
| 115 | 235 |
| 116 InProcessImporterBridge::~InProcessImporterBridge() {} | 236 InProcessImporterBridge::~InProcessImporterBridge() {} |
| OLD | NEW |