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/importer_data_types.h"
16 #include "chrome/common/importer/profile_import_process_messages.h"
17 #include "components/autofill/core/common/password_form.h"
18 #include "ipc/ipc_sender.h"
22 // Rather than sending all import items over IPC at once we chunk them into
23 // separate requests. This avoids the case of a large import causing
24 // oversized IPC messages.
25 const int kNumBookmarksToSend
= 100;
26 const int kNumHistoryRowsToSend
= 100;
27 const int kNumFaviconsToSend
= 100;
28 const int kNumAutofillFormDataToSend
= 100;
32 ExternalProcessImporterBridge::ExternalProcessImporterBridge(
33 const base::DictionaryValue
& localized_strings
,
35 base::TaskRunner
* task_runner
)
37 task_runner_(task_runner
) {
38 // Bridge needs to make its own copy because OS 10.6 autoreleases the
39 // localized_strings value that is passed in (see http://crbug.com/46003 ).
40 localized_strings_
.reset(localized_strings
.DeepCopy());
43 void ExternalProcessImporterBridge::AddBookmarks(
44 const std::vector
<ImportedBookmarkEntry
>& bookmarks
,
45 const base::string16
& first_folder_name
) {
46 Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportStart(
47 first_folder_name
, bookmarks
.size()));
49 // |bookmarks_left| is required for the checks below as Windows has a
50 // Debug bounds-check which prevents pushing an iterator beyond its end()
51 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
52 int bookmarks_left
= bookmarks
.end() - bookmarks
.begin();
53 for (std::vector
<ImportedBookmarkEntry
>::const_iterator it
=
54 bookmarks
.begin(); it
< bookmarks
.end();) {
55 std::vector
<ImportedBookmarkEntry
> bookmark_group
;
56 std::vector
<ImportedBookmarkEntry
>::const_iterator end_group
=
57 it
+ std::min(bookmarks_left
, kNumBookmarksToSend
);
58 bookmark_group
.assign(it
, end_group
);
60 Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportGroup(
62 bookmarks_left
-= end_group
- it
;
65 DCHECK_EQ(0, bookmarks_left
);
68 void ExternalProcessImporterBridge::AddHomePage(const GURL
& home_page
) {
69 Send(new ProfileImportProcessHostMsg_NotifyHomePageImportReady(home_page
));
73 void ExternalProcessImporterBridge::AddIE7PasswordInfo(
74 const importer::ImporterIE7PasswordInfo
& password_info
) {
75 Send(new ProfileImportProcessHostMsg_NotifyIE7PasswordInfo(password_info
));
79 void ExternalProcessImporterBridge::SetFavicons(
80 const favicon_base::FaviconUsageDataList
& favicons
) {
81 Send(new ProfileImportProcessHostMsg_NotifyFaviconsImportStart(
84 // |favicons_left| is required for the checks below as Windows has a
85 // Debug bounds-check which prevents pushing an iterator beyond its end()
86 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
87 int favicons_left
= favicons
.end() - favicons
.begin();
88 for (favicon_base::FaviconUsageDataList::const_iterator it
= favicons
.begin();
89 it
< favicons
.end();) {
90 favicon_base::FaviconUsageDataList favicons_group
;
91 favicon_base::FaviconUsageDataList::const_iterator end_group
=
92 it
+ std::min(favicons_left
, kNumFaviconsToSend
);
93 favicons_group
.assign(it
, end_group
);
95 Send(new ProfileImportProcessHostMsg_NotifyFaviconsImportGroup(
97 favicons_left
-= end_group
- it
;
100 DCHECK_EQ(0, favicons_left
);
103 void ExternalProcessImporterBridge::SetHistoryItems(
104 const std::vector
<ImporterURLRow
>& rows
,
105 importer::VisitSource visit_source
) {
106 Send(new ProfileImportProcessHostMsg_NotifyHistoryImportStart(rows
.size()));
108 // |rows_left| is required for the checks below as Windows has a
109 // Debug bounds-check which prevents pushing an iterator beyond its end()
110 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
111 int rows_left
= rows
.end() - rows
.begin();
112 for (std::vector
<ImporterURLRow
>::const_iterator it
= rows
.begin();
114 std::vector
<ImporterURLRow
> row_group
;
115 std::vector
<ImporterURLRow
>::const_iterator end_group
=
116 it
+ std::min(rows_left
, kNumHistoryRowsToSend
);
117 row_group
.assign(it
, end_group
);
119 Send(new ProfileImportProcessHostMsg_NotifyHistoryImportGroup(
120 row_group
, visit_source
));
121 rows_left
-= end_group
- it
;
124 DCHECK_EQ(0, rows_left
);
127 void ExternalProcessImporterBridge::SetKeywords(
128 const std::vector
<importer::SearchEngineInfo
>& search_engines
,
129 bool unique_on_host_and_path
) {
130 Send(new ProfileImportProcessHostMsg_NotifyKeywordsReady(
131 search_engines
, unique_on_host_and_path
));
134 void ExternalProcessImporterBridge::SetFirefoxSearchEnginesXMLData(
135 const std::vector
<std::string
>& search_engine_data
) {
136 Send(new ProfileImportProcessHostMsg_NotifyFirefoxSearchEngData(
137 search_engine_data
));
140 void ExternalProcessImporterBridge::SetPasswordForm(
141 const autofill::PasswordForm
& form
) {
142 Send(new ProfileImportProcessHostMsg_NotifyPasswordFormReady(form
));
145 void ExternalProcessImporterBridge::SetAutofillFormData(
146 const std::vector
<ImporterAutofillFormDataEntry
>& entries
) {
147 Send(new ProfileImportProcessHostMsg_AutofillFormDataImportStart(
150 // |autofill_form_data_entries_left| is required for the checks below as
151 // Windows has a Debug bounds-check which prevents pushing an iterator beyond
152 // its end() (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 ==
154 int autofill_form_data_entries_left
= entries
.end() - entries
.begin();
155 for (std::vector
<ImporterAutofillFormDataEntry
>::const_iterator it
=
157 it
< entries
.end();) {
158 std::vector
<ImporterAutofillFormDataEntry
> autofill_form_data_entry_group
;
159 std::vector
<ImporterAutofillFormDataEntry
>::const_iterator end_group
=
161 std::min(autofill_form_data_entries_left
, kNumAutofillFormDataToSend
);
162 autofill_form_data_entry_group
.assign(it
, end_group
);
164 Send(new ProfileImportProcessHostMsg_AutofillFormDataImportGroup(
165 autofill_form_data_entry_group
));
166 autofill_form_data_entries_left
-= end_group
- it
;
169 DCHECK_EQ(0, autofill_form_data_entries_left
);
172 void ExternalProcessImporterBridge::NotifyStarted() {
173 Send(new ProfileImportProcessHostMsg_Import_Started());
176 void ExternalProcessImporterBridge::NotifyItemStarted(
177 importer::ImportItem item
) {
178 Send(new ProfileImportProcessHostMsg_ImportItem_Started(item
));
181 void ExternalProcessImporterBridge::NotifyItemEnded(importer::ImportItem item
) {
182 Send(new ProfileImportProcessHostMsg_ImportItem_Finished(item
));
185 void ExternalProcessImporterBridge::NotifyEnded() {
186 // The internal process detects import end when all items have been received.
189 base::string16
ExternalProcessImporterBridge::GetLocalizedString(
191 base::string16 message
;
192 localized_strings_
->GetString(base::IntToString(message_id
), &message
);
196 ExternalProcessImporterBridge::~ExternalProcessImporterBridge() {}
198 void ExternalProcessImporterBridge::Send(IPC::Message
* message
) {
199 task_runner_
->PostTask(
201 base::Bind(&ExternalProcessImporterBridge::SendInternal
,
205 void ExternalProcessImporterBridge::SendInternal(IPC::Message
* message
) {
206 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
207 sender_
->Send(message
);