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/external_process_importer_client.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/importer/external_process_importer_host.h"
11 #include "chrome/browser/importer/in_process_importer_bridge.h"
12 #include "chrome/common/importer/firefox_importer_utils.h"
13 #include "chrome/common/importer/imported_bookmark_entry.h"
14 #include "chrome/common/importer/profile_import_process_messages.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/utility_process_host.h"
17 #include "grit/generated_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
20 using content::BrowserThread
;
21 using content::UtilityProcessHost
;
23 ExternalProcessImporterClient::ExternalProcessImporterClient(
24 ExternalProcessImporterHost
* importer_host
,
25 const importer::SourceProfile
& source_profile
,
27 InProcessImporterBridge
* bridge
)
28 : total_bookmarks_count_(0),
29 total_history_rows_count_(0),
30 total_favicons_count_(0),
31 process_importer_host_(importer_host
),
32 source_profile_(source_profile
),
36 process_importer_host_
->NotifyImportStarted();
39 void ExternalProcessImporterClient::Start() {
40 AddRef(); // balanced in Cleanup.
41 BrowserThread::ID thread_id
;
42 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id
));
43 BrowserThread::PostTask(
44 BrowserThread::IO
, FROM_HERE
,
45 base::Bind(&ExternalProcessImporterClient::StartProcessOnIOThread
,
50 void ExternalProcessImporterClient::Cancel() {
55 BrowserThread::PostTask(
56 BrowserThread::IO
, FROM_HERE
,
58 &ExternalProcessImporterClient::CancelImportProcessOnIOThread
,
63 void ExternalProcessImporterClient::OnProcessCrashed(int exit_code
) {
64 DLOG(ERROR
) << __FUNCTION__
;
68 process_importer_host_
->Cancel();
71 bool ExternalProcessImporterClient::OnMessageReceived(
72 const IPC::Message
& message
) {
74 IPC_BEGIN_MESSAGE_MAP(ExternalProcessImporterClient
, message
)
75 // Notification messages about the state of the import process.
76 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_Import_Started
,
78 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_Import_Finished
,
80 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_ImportItem_Started
,
82 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_ImportItem_Finished
,
84 // Data messages containing items to be written to the user profile.
85 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyHistoryImportStart
,
87 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyHistoryImportGroup
,
89 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyHomePageImportReady
,
90 OnHomePageImportReady
)
91 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyBookmarksImportStart
,
92 OnBookmarksImportStart
)
93 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyBookmarksImportGroup
,
94 OnBookmarksImportGroup
)
95 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyFaviconsImportStart
,
96 OnFaviconsImportStart
)
97 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyFaviconsImportGroup
,
98 OnFaviconsImportGroup
)
99 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyPasswordFormReady
,
100 OnPasswordFormImportReady
)
101 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyKeywordsReady
,
102 OnKeywordsImportReady
)
103 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyFirefoxSearchEngData
,
104 OnFirefoxSearchEngineDataReceived
)
106 IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyIE7PasswordInfo
,
107 OnIE7PasswordReceived
)
109 IPC_MESSAGE_UNHANDLED(handled
= false)
110 IPC_END_MESSAGE_MAP()
114 void ExternalProcessImporterClient::OnImportStart() {
118 bridge_
->NotifyStarted();
121 void ExternalProcessImporterClient::OnImportFinished(
122 bool succeeded
, const std::string
& error_msg
) {
127 LOG(WARNING
) << "Import failed. Error: " << error_msg
;
131 void ExternalProcessImporterClient::OnImportItemStart(int item_data
) {
135 bridge_
->NotifyItemStarted(static_cast<importer::ImportItem
>(item_data
));
138 void ExternalProcessImporterClient::OnImportItemFinished(int item_data
) {
142 importer::ImportItem import_item
=
143 static_cast<importer::ImportItem
>(item_data
);
144 bridge_
->NotifyItemEnded(import_item
);
145 BrowserThread::PostTask(
146 BrowserThread::IO
, FROM_HERE
,
147 base::Bind(&ExternalProcessImporterClient::NotifyItemFinishedOnIOThread
,
152 void ExternalProcessImporterClient::OnHistoryImportStart(
153 size_t total_history_rows_count
) {
157 total_history_rows_count_
= total_history_rows_count
;
158 history_rows_
.reserve(total_history_rows_count
);
161 void ExternalProcessImporterClient::OnHistoryImportGroup(
162 const std::vector
<ImporterURLRow
>& history_rows_group
,
167 history_rows_
.insert(history_rows_
.end(), history_rows_group
.begin(),
168 history_rows_group
.end());
169 if (history_rows_
.size() == total_history_rows_count_
)
170 bridge_
->SetHistoryItems(history_rows_
,
171 static_cast<importer::VisitSource
>(visit_source
));
174 void ExternalProcessImporterClient::OnHomePageImportReady(
175 const GURL
& home_page
) {
179 bridge_
->AddHomePage(home_page
);
182 void ExternalProcessImporterClient::OnBookmarksImportStart(
183 const string16
& first_folder_name
,
184 size_t total_bookmarks_count
) {
188 bookmarks_first_folder_name_
= first_folder_name
;
189 total_bookmarks_count_
= total_bookmarks_count
;
190 bookmarks_
.reserve(total_bookmarks_count
);
193 void ExternalProcessImporterClient::OnBookmarksImportGroup(
194 const std::vector
<ImportedBookmarkEntry
>& bookmarks_group
) {
198 // Collect sets of bookmarks from importer process until we have reached
199 // total_bookmarks_count_:
200 bookmarks_
.insert(bookmarks_
.end(), bookmarks_group
.begin(),
201 bookmarks_group
.end());
202 if (bookmarks_
.size() == total_bookmarks_count_
)
203 bridge_
->AddBookmarks(bookmarks_
, bookmarks_first_folder_name_
);
206 void ExternalProcessImporterClient::OnFaviconsImportStart(
207 size_t total_favicons_count
) {
211 total_favicons_count_
= total_favicons_count
;
212 favicons_
.reserve(total_favicons_count
);
215 void ExternalProcessImporterClient::OnFaviconsImportGroup(
216 const std::vector
<ImportedFaviconUsage
>& favicons_group
) {
220 favicons_
.insert(favicons_
.end(), favicons_group
.begin(),
221 favicons_group
.end());
222 if (favicons_
.size() == total_favicons_count_
)
223 bridge_
->SetFavicons(favicons_
);
226 void ExternalProcessImporterClient::OnPasswordFormImportReady(
227 const content::PasswordForm
& form
) {
231 bridge_
->SetPasswordForm(form
);
234 void ExternalProcessImporterClient::OnKeywordsImportReady(
235 const std::vector
<importer::URLKeywordInfo
>& url_keywords
,
236 bool unique_on_host_and_path
) {
239 bridge_
->SetKeywords(url_keywords
, unique_on_host_and_path
);
242 void ExternalProcessImporterClient::OnFirefoxSearchEngineDataReceived(
243 const std::vector
<std::string
> search_engine_data
) {
246 bridge_
->SetFirefoxSearchEnginesXMLData(search_engine_data
);
250 void ExternalProcessImporterClient::OnIE7PasswordReceived(
251 const importer::ImporterIE7PasswordInfo
& importer_password_info
) {
254 bridge_
->AddIE7PasswordInfo(importer_password_info
);
258 ExternalProcessImporterClient::~ExternalProcessImporterClient() {}
260 void ExternalProcessImporterClient::Cleanup() {
264 if (process_importer_host_
)
265 process_importer_host_
->NotifyImportEnded();
269 void ExternalProcessImporterClient::CancelImportProcessOnIOThread() {
270 if (utility_process_host_
.get())
271 utility_process_host_
->Send(new ProfileImportProcessMsg_CancelImport());
274 void ExternalProcessImporterClient::NotifyItemFinishedOnIOThread(
275 importer::ImportItem import_item
) {
276 utility_process_host_
->Send(
277 new ProfileImportProcessMsg_ReportImportItemFinished(import_item
));
280 void ExternalProcessImporterClient::StartProcessOnIOThread(
281 BrowserThread::ID thread_id
) {
282 utility_process_host_
= UtilityProcessHost::Create(
283 this, BrowserThread::GetMessageLoopProxyForThread(thread_id
).get())
285 utility_process_host_
->DisableSandbox();
287 #if defined(OS_MACOSX)
288 base::EnvironmentMap env
;
289 std::string dylib_path
= GetFirefoxDylibPath().value();
290 if (!dylib_path
.empty())
291 env
["DYLD_FALLBACK_LIBRARY_PATH"] = dylib_path
;
292 utility_process_host_
->SetEnv(env
);
295 // Dictionary of all localized strings that could be needed by the importer
296 // in the external process.
297 DictionaryValue localized_strings
;
298 localized_strings
.SetString(
299 base::IntToString(IDS_BOOKMARK_GROUP
),
300 l10n_util::GetStringUTF8(IDS_BOOKMARK_GROUP
));
301 localized_strings
.SetString(
302 base::IntToString(IDS_BOOKMARK_GROUP_FROM_FIREFOX
),
303 l10n_util::GetStringUTF8(IDS_BOOKMARK_GROUP_FROM_FIREFOX
));
304 localized_strings
.SetString(
305 base::IntToString(IDS_BOOKMARK_GROUP_FROM_SAFARI
),
306 l10n_util::GetStringUTF8(IDS_BOOKMARK_GROUP_FROM_SAFARI
));
307 localized_strings
.SetString(
308 base::IntToString(IDS_IMPORT_FROM_FIREFOX
),
309 l10n_util::GetStringUTF8(IDS_IMPORT_FROM_FIREFOX
));
310 localized_strings
.SetString(
311 base::IntToString(IDS_IMPORT_FROM_ICEWEASEL
),
312 l10n_util::GetStringUTF8(IDS_IMPORT_FROM_ICEWEASEL
));
313 localized_strings
.SetString(
314 base::IntToString(IDS_IMPORT_FROM_SAFARI
),
315 l10n_util::GetStringUTF8(IDS_IMPORT_FROM_SAFARI
));
316 localized_strings
.SetString(
317 base::IntToString(IDS_BOOKMARK_BAR_FOLDER_NAME
),
318 l10n_util::GetStringUTF8(IDS_BOOKMARK_BAR_FOLDER_NAME
));
320 utility_process_host_
->Send(new ProfileImportProcessMsg_StartImport(
321 source_profile_
, items_
, localized_strings
));