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
3 // found in the LICENSE file.
5 #include "chrome/browser/importer/in_process_importer_bridge.h"
8 #include "base/files/file_util.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/importer/external_process_importer_host.h"
12 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
13 #include "chrome/common/importer/imported_bookmark_entry.h"
14 #include "chrome/common/importer/imported_favicon_usage.h"
15 #include "chrome/common/importer/importer_autofill_form_data_entry.h"
16 #include "components/autofill/core/browser/webdata/autofill_entry.h"
17 #include "components/autofill/core/common/password_form.h"
18 #include "components/search_engines/template_url.h"
19 #include "components/search_engines/template_url_parser.h"
20 #include "components/search_engines/template_url_prepopulate_data.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "ui/base/l10n/l10n_util.h"
25 #include "components/os_crypt/ie7_password_win.h"
32 history::URLRows
ConvertImporterURLRowsToHistoryURLRows(
33 const std::vector
<ImporterURLRow
>& rows
) {
34 history::URLRows converted
;
35 converted
.reserve(rows
.size());
36 for (std::vector
<ImporterURLRow
>::const_iterator it
= rows
.begin();
37 it
!= rows
.end(); ++it
) {
38 history::URLRow
row(it
->url
);
39 row
.set_title(it
->title
);
40 row
.set_visit_count(it
->visit_count
);
41 row
.set_typed_count(it
->typed_count
);
42 row
.set_last_visit(it
->last_visit
);
43 row
.set_hidden(it
->hidden
);
44 converted
.push_back(row
);
49 history::VisitSource
ConvertImporterVisitSourceToHistoryVisitSource(
50 importer::VisitSource visit_source
) {
51 switch (visit_source
) {
52 case importer::VISIT_SOURCE_BROWSED
:
53 return history::SOURCE_BROWSED
;
54 case importer::VISIT_SOURCE_FIREFOX_IMPORTED
:
55 return history::SOURCE_FIREFOX_IMPORTED
;
56 case importer::VISIT_SOURCE_IE_IMPORTED
:
57 return history::SOURCE_IE_IMPORTED
;
58 case importer::VISIT_SOURCE_SAFARI_IMPORTED
:
59 return history::SOURCE_SAFARI_IMPORTED
;
62 return history::SOURCE_SYNCED
;
67 using content::BrowserThread
;
71 // FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from
72 // the search URL when importing search engines.
73 class FirefoxURLParameterFilter
: public TemplateURLParser::ParameterFilter
{
75 FirefoxURLParameterFilter() {}
76 ~FirefoxURLParameterFilter() override
{}
78 // TemplateURLParser::ParameterFilter method.
79 bool KeepParameter(const std::string
& key
,
80 const std::string
& value
) override
{
81 std::string low_value
= base::StringToLowerASCII(value
);
82 if (low_value
.find("mozilla") != std::string::npos
||
83 low_value
.find("firefox") != std::string::npos
||
84 low_value
.find("moz:") != std::string::npos
) {
91 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter
);
94 // Creates a TemplateURL with the |keyword| and |url|. |title| may be empty.
95 // This function transfers ownership of the created TemplateURL to the caller.
96 TemplateURL
* CreateTemplateURL(const base::string16
& title
,
97 const base::string16
& keyword
,
99 // Skip if the url is invalid.
103 TemplateURLData data
;
105 data
.SetKeyword(TemplateURL::GenerateKeyword(url
));
107 data
.SetKeyword(keyword
);
108 // We set short name by using the title if it exists.
109 // Otherwise, we use the shortcut.
110 data
.short_name
= title
.empty() ? keyword
: title
;
112 TemplateURLRef::DisplayURLToURLRef(base::UTF8ToUTF16(url
.spec())));
113 return new TemplateURL(data
);
116 // Parses the OpenSearch XML files in |xml_files| and populates |search_engines|
117 // with the resulting TemplateURLs.
118 void ParseSearchEnginesFromFirefoxXMLData(
119 const std::vector
<std::string
>& xml_data
,
120 std::vector
<TemplateURL
*>* search_engines
) {
121 DCHECK(search_engines
);
123 typedef std::map
<std::string
, TemplateURL
*> SearchEnginesMap
;
124 SearchEnginesMap search_engine_for_url
;
125 FirefoxURLParameterFilter param_filter
;
126 // The first XML file represents the default search engine in Firefox 3, so we
127 // need to keep it on top of the list.
128 SearchEnginesMap::const_iterator default_turl
= search_engine_for_url
.end();
129 for (std::vector
<std::string
>::const_iterator xml_iter
=
130 xml_data
.begin(); xml_iter
!= xml_data
.end(); ++xml_iter
) {
131 TemplateURL
* template_url
= TemplateURLParser::Parse(
132 UIThreadSearchTermsData(NULL
), true,
133 xml_iter
->data(), xml_iter
->length(), ¶m_filter
);
135 SearchEnginesMap::iterator iter
=
136 search_engine_for_url
.find(template_url
->url());
137 if (iter
== search_engine_for_url
.end()) {
138 iter
= search_engine_for_url
.insert(
139 std::make_pair(template_url
->url(), template_url
)).first
;
141 // We have already found a search engine with the same URL. We give
142 // priority to the latest one found, as GetSearchEnginesXMLFiles()
143 // returns a vector with first Firefox default search engines and then
144 // the user's ones. We want to give priority to the user ones.
146 iter
->second
= template_url
;
148 if (default_turl
== search_engine_for_url
.end())
153 // Put the results in the |search_engines| vector.
154 for (SearchEnginesMap::iterator t_iter
= search_engine_for_url
.begin();
155 t_iter
!= search_engine_for_url
.end(); ++t_iter
) {
156 if (t_iter
== default_turl
)
157 search_engines
->insert(search_engines
->begin(), default_turl
->second
);
159 search_engines
->push_back(t_iter
->second
);
165 InProcessImporterBridge::InProcessImporterBridge(
166 ProfileWriter
* writer
,
167 base::WeakPtr
<ExternalProcessImporterHost
> host
) : writer_(writer
),
171 void InProcessImporterBridge::AddBookmarks(
172 const std::vector
<ImportedBookmarkEntry
>& bookmarks
,
173 const base::string16
& first_folder_name
) {
174 BrowserThread::PostTask(
175 BrowserThread::UI
, FROM_HERE
,
176 base::Bind(&ProfileWriter::AddBookmarks
, writer_
, bookmarks
,
180 void InProcessImporterBridge::AddHomePage(const GURL
& home_page
) {
181 BrowserThread::PostTask(
182 BrowserThread::UI
, FROM_HERE
,
183 base::Bind(&ProfileWriter::AddHomepage
, writer_
, home_page
));
187 void InProcessImporterBridge::AddIE7PasswordInfo(
188 const importer::ImporterIE7PasswordInfo
& password_info
) {
189 IE7PasswordInfo ie7_password_info
;
190 ie7_password_info
.url_hash
= password_info
.url_hash
;
191 ie7_password_info
.encrypted_data
= password_info
.encrypted_data
;
192 ie7_password_info
.date_created
= password_info
.date_created
;
194 BrowserThread::PostTask(
195 BrowserThread::UI
, FROM_HERE
,
196 base::Bind(&ProfileWriter::AddIE7PasswordInfo
, writer_
,
201 void InProcessImporterBridge::SetFavicons(
202 const std::vector
<ImportedFaviconUsage
>& favicons
) {
203 BrowserThread::PostTask(
204 BrowserThread::UI
, FROM_HERE
,
205 base::Bind(&ProfileWriter::AddFavicons
, writer_
, favicons
));
208 void InProcessImporterBridge::SetHistoryItems(
209 const std::vector
<ImporterURLRow
>& rows
,
210 importer::VisitSource visit_source
) {
211 history::URLRows converted_rows
=
212 ConvertImporterURLRowsToHistoryURLRows(rows
);
213 history::VisitSource converted_visit_source
=
214 ConvertImporterVisitSourceToHistoryVisitSource(visit_source
);
215 BrowserThread::PostTask(BrowserThread::UI
,
217 base::Bind(&ProfileWriter::AddHistoryPage
,
220 converted_visit_source
));
223 void InProcessImporterBridge::SetKeywords(
224 const std::vector
<importer::URLKeywordInfo
>& url_keywords
,
225 bool unique_on_host_and_path
) {
226 ScopedVector
<TemplateURL
> owned_template_urls
;
227 for (size_t i
= 0; i
< url_keywords
.size(); ++i
) {
228 owned_template_urls
.push_back(
229 CreateTemplateURL(url_keywords
[i
].display_name
,
230 url_keywords
[i
].keyword
,
231 url_keywords
[i
].url
));
233 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
234 base::Bind(&ProfileWriter::AddKeywords
, writer_
,
235 base::Passed(&owned_template_urls
), unique_on_host_and_path
));
238 void InProcessImporterBridge::SetFirefoxSearchEnginesXMLData(
239 const std::vector
<std::string
>& search_engine_data
) {
240 std::vector
<TemplateURL
*> search_engines
;
241 ParseSearchEnginesFromFirefoxXMLData(search_engine_data
, &search_engines
);
243 ScopedVector
<TemplateURL
> owned_template_urls
;
244 std::copy(search_engines
.begin(), search_engines
.end(),
245 std::back_inserter(owned_template_urls
));
247 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
248 base::Bind(&ProfileWriter::AddKeywords
, writer_
,
249 base::Passed(&owned_template_urls
), true));
252 void InProcessImporterBridge::SetPasswordForm(
253 const autofill::PasswordForm
& form
) {
254 BrowserThread::PostTask(
255 BrowserThread::UI
, FROM_HERE
,
256 base::Bind(&ProfileWriter::AddPasswordForm
, writer_
, form
));
259 void InProcessImporterBridge::SetAutofillFormData(
260 const std::vector
<ImporterAutofillFormDataEntry
>& entries
) {
261 std::vector
<autofill::AutofillEntry
> autofill_entries
;
262 for (size_t i
= 0; i
< entries
.size(); ++i
) {
263 autofill_entries
.push_back(autofill::AutofillEntry(
264 autofill::AutofillKey(entries
[i
].name
, entries
[i
].value
),
265 entries
[i
].first_used
,
266 entries
[i
].last_used
));
269 BrowserThread::PostTask(BrowserThread::UI
,
271 base::Bind(&ProfileWriter::AddAutofillFormDataEntries
,
276 void InProcessImporterBridge::NotifyStarted() {
277 BrowserThread::PostTask(
278 BrowserThread::UI
, FROM_HERE
,
279 base::Bind(&ExternalProcessImporterHost::NotifyImportStarted
, host_
));
282 void InProcessImporterBridge::NotifyItemStarted(importer::ImportItem item
) {
283 BrowserThread::PostTask(
284 BrowserThread::UI
, FROM_HERE
,
285 base::Bind(&ExternalProcessImporterHost::NotifyImportItemStarted
,
289 void InProcessImporterBridge::NotifyItemEnded(importer::ImportItem item
) {
290 BrowserThread::PostTask(
291 BrowserThread::UI
, FROM_HERE
,
292 base::Bind(&ExternalProcessImporterHost::NotifyImportItemEnded
,
296 void InProcessImporterBridge::NotifyEnded() {
297 BrowserThread::PostTask(
298 BrowserThread::UI
, FROM_HERE
,
299 base::Bind(&ExternalProcessImporterHost::NotifyImportEnded
, host_
));
302 base::string16
InProcessImporterBridge::GetLocalizedString(int message_id
) {
303 return l10n_util::GetStringUTF16(message_id
);
306 InProcessImporterBridge::~InProcessImporterBridge() {}