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