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/utility/importer/external_process_importer_bridge.h"
8 #include "base/debug/dump_without_crashing.h"
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/task_runner.h"
13 #include "base/values.h"
14 #include "chrome/common/importer/imported_bookmark_entry.h"
15 #include "chrome/common/importer/imported_favicon_usage.h"
16 #include "chrome/common/importer/importer_data_types.h"
17 #include "chrome/common/importer/profile_import_process_messages.h"
18 #include "components/autofill/core/common/password_form.h"
19 #include "ipc/ipc_sender.h"
23 // Rather than sending all import items over IPC at once we chunk them into
24 // separate requests. This avoids the case of a large import causing
25 // oversized IPC messages.
26 const int kNumBookmarksToSend
= 100;
27 const int kNumHistoryRowsToSend
= 100;
28 const int kNumFaviconsToSend
= 100;
29 const int kNumAutofillFormDataToSend
= 100;
31 // http://crbug.com/404012. Let's see where the empty fields come from.
32 void CheckForEmptyUsernameAndPassword(const autofill::PasswordForm
& form
) {
33 if (form
.username_value
.empty() &&
34 form
.password_value
.empty() &&
35 !form
.blacklisted_by_user
) {
36 base::debug::DumpWithoutCrashing();
41 ExternalProcessImporterBridge::ExternalProcessImporterBridge(
42 const base::DictionaryValue
& localized_strings
,
44 base::TaskRunner
* task_runner
)
46 task_runner_(task_runner
) {
47 // Bridge needs to make its own copy because OS 10.6 autoreleases the
48 // localized_strings value that is passed in (see http://crbug.com/46003 ).
49 localized_strings_
.reset(localized_strings
.DeepCopy());
52 void ExternalProcessImporterBridge::AddBookmarks(
53 const std::vector
<ImportedBookmarkEntry
>& bookmarks
,
54 const base::string16
& first_folder_name
) {
55 Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportStart(
56 first_folder_name
, bookmarks
.size()));
58 // |bookmarks_left| is required for the checks below as Windows has a
59 // Debug bounds-check which prevents pushing an iterator beyond its end()
60 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
61 int bookmarks_left
= bookmarks
.end() - bookmarks
.begin();
62 for (std::vector
<ImportedBookmarkEntry
>::const_iterator it
=
63 bookmarks
.begin(); it
< bookmarks
.end();) {
64 std::vector
<ImportedBookmarkEntry
> bookmark_group
;
65 std::vector
<ImportedBookmarkEntry
>::const_iterator end_group
=
66 it
+ std::min(bookmarks_left
, kNumBookmarksToSend
);
67 bookmark_group
.assign(it
, end_group
);
69 Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportGroup(
71 bookmarks_left
-= end_group
- it
;
74 DCHECK_EQ(0, bookmarks_left
);
77 void ExternalProcessImporterBridge::AddHomePage(const GURL
& home_page
) {
78 Send(new ProfileImportProcessHostMsg_NotifyHomePageImportReady(home_page
));
82 void ExternalProcessImporterBridge::AddIE7PasswordInfo(
83 const importer::ImporterIE7PasswordInfo
& password_info
) {
84 Send(new ProfileImportProcessHostMsg_NotifyIE7PasswordInfo(password_info
));
88 void ExternalProcessImporterBridge::SetFavicons(
89 const std::vector
<ImportedFaviconUsage
>& favicons
) {
90 Send(new ProfileImportProcessHostMsg_NotifyFaviconsImportStart(
93 // |favicons_left| is required for the checks below as Windows has a
94 // Debug bounds-check which prevents pushing an iterator beyond its end()
95 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
96 int favicons_left
= favicons
.end() - favicons
.begin();
97 for (std::vector
<ImportedFaviconUsage
>::const_iterator it
=
98 favicons
.begin(); it
< favicons
.end();) {
99 std::vector
<ImportedFaviconUsage
> favicons_group
;
100 std::vector
<ImportedFaviconUsage
>::const_iterator end_group
=
101 it
+ std::min(favicons_left
, kNumFaviconsToSend
);
102 favicons_group
.assign(it
, end_group
);
104 Send(new ProfileImportProcessHostMsg_NotifyFaviconsImportGroup(
106 favicons_left
-= end_group
- it
;
109 DCHECK_EQ(0, favicons_left
);
112 void ExternalProcessImporterBridge::SetHistoryItems(
113 const std::vector
<ImporterURLRow
>& rows
,
114 importer::VisitSource visit_source
) {
115 Send(new ProfileImportProcessHostMsg_NotifyHistoryImportStart(rows
.size()));
117 // |rows_left| is required for the checks below as Windows has a
118 // Debug bounds-check which prevents pushing an iterator beyond its end()
119 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
120 int rows_left
= rows
.end() - rows
.begin();
121 for (std::vector
<ImporterURLRow
>::const_iterator it
= rows
.begin();
123 std::vector
<ImporterURLRow
> row_group
;
124 std::vector
<ImporterURLRow
>::const_iterator end_group
=
125 it
+ std::min(rows_left
, kNumHistoryRowsToSend
);
126 row_group
.assign(it
, end_group
);
128 Send(new ProfileImportProcessHostMsg_NotifyHistoryImportGroup(
129 row_group
, visit_source
));
130 rows_left
-= end_group
- it
;
133 DCHECK_EQ(0, rows_left
);
136 void ExternalProcessImporterBridge::SetKeywords(
137 const std::vector
<importer::URLKeywordInfo
>& url_keywords
,
138 bool unique_on_host_and_path
) {
139 Send(new ProfileImportProcessHostMsg_NotifyKeywordsReady(
140 url_keywords
, unique_on_host_and_path
));
143 void ExternalProcessImporterBridge::SetFirefoxSearchEnginesXMLData(
144 const std::vector
<std::string
>& search_engine_data
) {
145 Send(new ProfileImportProcessHostMsg_NotifyFirefoxSearchEngData(
146 search_engine_data
));
149 void ExternalProcessImporterBridge::SetPasswordForm(
150 const autofill::PasswordForm
& form
) {
151 CheckForEmptyUsernameAndPassword(form
);
152 Send(new ProfileImportProcessHostMsg_NotifyPasswordFormReady(form
));
155 void ExternalProcessImporterBridge::SetAutofillFormData(
156 const std::vector
<ImporterAutofillFormDataEntry
>& entries
) {
157 Send(new ProfileImportProcessHostMsg_AutofillFormDataImportStart(
160 // |autofill_form_data_entries_left| is required for the checks below as
161 // Windows has a Debug bounds-check which prevents pushing an iterator beyond
162 // its end() (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 ==
164 int autofill_form_data_entries_left
= entries
.end() - entries
.begin();
165 for (std::vector
<ImporterAutofillFormDataEntry
>::const_iterator it
=
167 it
< entries
.end();) {
168 std::vector
<ImporterAutofillFormDataEntry
> autofill_form_data_entry_group
;
169 std::vector
<ImporterAutofillFormDataEntry
>::const_iterator end_group
=
171 std::min(autofill_form_data_entries_left
, kNumAutofillFormDataToSend
);
172 autofill_form_data_entry_group
.assign(it
, end_group
);
174 Send(new ProfileImportProcessHostMsg_AutofillFormDataImportGroup(
175 autofill_form_data_entry_group
));
176 autofill_form_data_entries_left
-= end_group
- it
;
179 DCHECK_EQ(0, autofill_form_data_entries_left
);
182 void ExternalProcessImporterBridge::NotifyStarted() {
183 Send(new ProfileImportProcessHostMsg_Import_Started());
186 void ExternalProcessImporterBridge::NotifyItemStarted(
187 importer::ImportItem item
) {
188 Send(new ProfileImportProcessHostMsg_ImportItem_Started(item
));
191 void ExternalProcessImporterBridge::NotifyItemEnded(importer::ImportItem item
) {
192 Send(new ProfileImportProcessHostMsg_ImportItem_Finished(item
));
195 void ExternalProcessImporterBridge::NotifyEnded() {
196 // The internal process detects import end when all items have been received.
199 base::string16
ExternalProcessImporterBridge::GetLocalizedString(
201 base::string16 message
;
202 localized_strings_
->GetString(base::IntToString(message_id
), &message
);
206 ExternalProcessImporterBridge::~ExternalProcessImporterBridge() {}
208 void ExternalProcessImporterBridge::Send(IPC::Message
* message
) {
209 task_runner_
->PostTask(
211 base::Bind(&ExternalProcessImporterBridge::SendInternal
,
215 void ExternalProcessImporterBridge::SendInternal(IPC::Message
* message
) {
216 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
217 sender_
->Send(message
);