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/file_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/importer/external_process_importer_host.h"
11 #include "chrome/browser/search_engines/template_url.h"
12 #include "chrome/browser/search_engines/template_url_parser.h"
13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
14 #include "chrome/browser/search_engines/template_url_service.h"
15 #include "chrome/common/importer/imported_bookmark_entry.h"
16 #include "chrome/common/importer/imported_favicon_usage.h"
17 #include "components/autofill/core/common/password_form.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "ui/base/l10n/l10n_util.h"
22 #include "components/webdata/encryptor/ie7_password_win.h"
29 history::URLRows
ConvertImporterURLRowsToHistoryURLRows(
30 const std::vector
<ImporterURLRow
>& rows
) {
31 history::URLRows converted
;
32 converted
.reserve(rows
.size());
33 for (std::vector
<ImporterURLRow
>::const_iterator it
= rows
.begin();
34 it
!= rows
.end(); ++it
) {
35 history::URLRow
row(it
->url
);
36 row
.set_title(it
->title
);
37 row
.set_visit_count(it
->visit_count
);
38 row
.set_typed_count(it
->typed_count
);
39 row
.set_last_visit(it
->last_visit
);
40 row
.set_hidden(it
->hidden
);
41 converted
.push_back(row
);
46 history::VisitSource
ConvertImporterVisitSourceToHistoryVisitSource(
47 importer::VisitSource visit_source
) {
48 switch (visit_source
) {
49 case importer::VISIT_SOURCE_BROWSED
:
50 return history::SOURCE_BROWSED
;
51 case importer::VISIT_SOURCE_FIREFOX_IMPORTED
:
52 return history::SOURCE_FIREFOX_IMPORTED
;
53 case importer::VISIT_SOURCE_IE_IMPORTED
:
54 return history::SOURCE_IE_IMPORTED
;
55 case importer::VISIT_SOURCE_SAFARI_IMPORTED
:
56 return history::SOURCE_SAFARI_IMPORTED
;
59 return history::SOURCE_SYNCED
;
64 using content::BrowserThread
;
68 // FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from
69 // the search URL when importing search engines.
70 class FirefoxURLParameterFilter
: public TemplateURLParser::ParameterFilter
{
72 FirefoxURLParameterFilter() {}
73 virtual ~FirefoxURLParameterFilter() {}
75 // TemplateURLParser::ParameterFilter method.
76 virtual bool KeepParameter(const std::string
& key
,
77 const std::string
& value
) OVERRIDE
{
78 std::string low_value
= StringToLowerASCII(value
);
79 if (low_value
.find("mozilla") != std::string::npos
||
80 low_value
.find("firefox") != std::string::npos
||
81 low_value
.find("moz:") != std::string::npos
) {
88 DISALLOW_COPY_AND_ASSIGN(FirefoxURLParameterFilter
);
91 // Creates a TemplateURL with the |keyword| and |url|. |title| may be empty.
92 // This function transfers ownership of the created TemplateURL to the caller.
93 TemplateURL
* CreateTemplateURL(const base::string16
& title
,
94 const base::string16
& keyword
,
96 // Skip if the url is invalid.
100 TemplateURLData data
;
102 data
.SetKeyword(TemplateURLService::GenerateKeyword(url
));
104 data
.SetKeyword(keyword
);
105 // We set short name by using the title if it exists.
106 // Otherwise, we use the shortcut.
107 data
.short_name
= title
.empty() ? keyword
: title
;
109 TemplateURLRef::DisplayURLToURLRef(base::UTF8ToUTF16(url
.spec())));
110 return new TemplateURL(NULL
, data
);
113 // Parses the OpenSearch XML files in |xml_files| and populates |search_engines|
114 // with the resulting TemplateURLs.
115 void ParseSearchEnginesFromFirefoxXMLData(
116 const std::vector
<std::string
>& xml_data
,
117 std::vector
<TemplateURL
*>* search_engines
) {
118 DCHECK(search_engines
);
120 typedef std::map
<std::string
, TemplateURL
*> SearchEnginesMap
;
121 SearchEnginesMap search_engine_for_url
;
122 FirefoxURLParameterFilter param_filter
;
123 // The first XML file represents the default search engine in Firefox 3, so we
124 // need to keep it on top of the list.
125 SearchEnginesMap::const_iterator default_turl
= search_engine_for_url
.end();
126 for (std::vector
<std::string
>::const_iterator xml_iter
=
127 xml_data
.begin(); xml_iter
!= xml_data
.end(); ++xml_iter
) {
128 TemplateURL
* template_url
= TemplateURLParser::Parse(NULL
, true,
129 xml_iter
->data(), xml_iter
->length(), ¶m_filter
);
131 SearchEnginesMap::iterator iter
=
132 search_engine_for_url
.find(template_url
->url());
133 if (iter
== search_engine_for_url
.end()) {
134 iter
= search_engine_for_url
.insert(
135 std::make_pair(template_url
->url(), template_url
)).first
;
137 // We have already found a search engine with the same URL. We give
138 // priority to the latest one found, as GetSearchEnginesXMLFiles()
139 // returns a vector with first Firefox default search engines and then
140 // the user's ones. We want to give priority to the user ones.
142 iter
->second
= template_url
;
144 if (default_turl
== search_engine_for_url
.end())
149 // Put the results in the |search_engines| vector.
150 for (SearchEnginesMap::iterator t_iter
= search_engine_for_url
.begin();
151 t_iter
!= search_engine_for_url
.end(); ++t_iter
) {
152 if (t_iter
== default_turl
)
153 search_engines
->insert(search_engines
->begin(), default_turl
->second
);
155 search_engines
->push_back(t_iter
->second
);
161 InProcessImporterBridge::InProcessImporterBridge(
162 ProfileWriter
* writer
,
163 base::WeakPtr
<ExternalProcessImporterHost
> host
) : writer_(writer
),
167 void InProcessImporterBridge::AddBookmarks(
168 const std::vector
<ImportedBookmarkEntry
>& bookmarks
,
169 const base::string16
& first_folder_name
) {
170 BrowserThread::PostTask(
171 BrowserThread::UI
, FROM_HERE
,
172 base::Bind(&ProfileWriter::AddBookmarks
, writer_
, bookmarks
,
176 void InProcessImporterBridge::AddHomePage(const GURL
& home_page
) {
177 BrowserThread::PostTask(
178 BrowserThread::UI
, FROM_HERE
,
179 base::Bind(&ProfileWriter::AddHomepage
, writer_
, home_page
));
183 void InProcessImporterBridge::AddIE7PasswordInfo(
184 const importer::ImporterIE7PasswordInfo
& password_info
) {
185 IE7PasswordInfo ie7_password_info
;
186 ie7_password_info
.url_hash
= password_info
.url_hash
;
187 ie7_password_info
.encrypted_data
= password_info
.encrypted_data
;
188 ie7_password_info
.date_created
= password_info
.date_created
;
190 BrowserThread::PostTask(
191 BrowserThread::UI
, FROM_HERE
,
192 base::Bind(&ProfileWriter::AddIE7PasswordInfo
, writer_
,
197 void InProcessImporterBridge::SetFavicons(
198 const std::vector
<ImportedFaviconUsage
>& favicons
) {
199 BrowserThread::PostTask(
200 BrowserThread::UI
, FROM_HERE
,
201 base::Bind(&ProfileWriter::AddFavicons
, writer_
, favicons
));
204 void InProcessImporterBridge::SetHistoryItems(
205 const std::vector
<ImporterURLRow
>& rows
,
206 importer::VisitSource visit_source
) {
207 history::URLRows converted_rows
=
208 ConvertImporterURLRowsToHistoryURLRows(rows
);
209 history::VisitSource converted_visit_source
=
210 ConvertImporterVisitSourceToHistoryVisitSource(visit_source
);
211 BrowserThread::PostTask(BrowserThread::UI
,
213 base::Bind(&ProfileWriter::AddHistoryPage
,
216 converted_visit_source
));
219 void InProcessImporterBridge::SetKeywords(
220 const std::vector
<importer::URLKeywordInfo
>& url_keywords
,
221 bool unique_on_host_and_path
) {
222 ScopedVector
<TemplateURL
> owned_template_urls
;
223 for (size_t i
= 0; i
< url_keywords
.size(); ++i
) {
224 owned_template_urls
.push_back(
225 CreateTemplateURL(url_keywords
[i
].display_name
,
226 url_keywords
[i
].keyword
,
227 url_keywords
[i
].url
));
229 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
230 base::Bind(&ProfileWriter::AddKeywords
, writer_
,
231 base::Passed(&owned_template_urls
), unique_on_host_and_path
));
234 void InProcessImporterBridge::SetFirefoxSearchEnginesXMLData(
235 const std::vector
<std::string
>& search_engine_data
) {
236 std::vector
<TemplateURL
*> search_engines
;
237 ParseSearchEnginesFromFirefoxXMLData(search_engine_data
, &search_engines
);
239 ScopedVector
<TemplateURL
> owned_template_urls
;
240 std::copy(search_engines
.begin(), search_engines
.end(),
241 std::back_inserter(owned_template_urls
));
243 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
244 base::Bind(&ProfileWriter::AddKeywords
, writer_
,
245 base::Passed(&owned_template_urls
), true));
248 void InProcessImporterBridge::SetPasswordForm(
249 const autofill::PasswordForm
& form
) {
250 BrowserThread::PostTask(
251 BrowserThread::UI
, FROM_HERE
,
252 base::Bind(&ProfileWriter::AddPasswordForm
, writer_
, form
));
255 void InProcessImporterBridge::NotifyStarted() {
256 BrowserThread::PostTask(
257 BrowserThread::UI
, FROM_HERE
,
258 base::Bind(&ExternalProcessImporterHost::NotifyImportStarted
, host_
));
261 void InProcessImporterBridge::NotifyItemStarted(importer::ImportItem item
) {
262 BrowserThread::PostTask(
263 BrowserThread::UI
, FROM_HERE
,
264 base::Bind(&ExternalProcessImporterHost::NotifyImportItemStarted
,
268 void InProcessImporterBridge::NotifyItemEnded(importer::ImportItem item
) {
269 BrowserThread::PostTask(
270 BrowserThread::UI
, FROM_HERE
,
271 base::Bind(&ExternalProcessImporterHost::NotifyImportItemEnded
,
275 void InProcessImporterBridge::NotifyEnded() {
276 BrowserThread::PostTask(
277 BrowserThread::UI
, FROM_HERE
,
278 base::Bind(&ExternalProcessImporterHost::NotifyImportEnded
, host_
));
281 base::string16
InProcessImporterBridge::GetLocalizedString(int message_id
) {
282 return l10n_util::GetStringUTF16(message_id
);
285 InProcessImporterBridge::~InProcessImporterBridge() {}