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;
33 ExternalProcessImporterBridge::ExternalProcessImporterBridge(
34 const base::DictionaryValue
& localized_strings
,
36 base::TaskRunner
* task_runner
)
38 task_runner_(task_runner
) {
39 // Bridge needs to make its own copy because OS 10.6 autoreleases the
40 // localized_strings value that is passed in (see http://crbug.com/46003 ).
41 localized_strings_
.reset(localized_strings
.DeepCopy());
44 void ExternalProcessImporterBridge::AddBookmarks(
45 const std::vector
<ImportedBookmarkEntry
>& bookmarks
,
46 const base::string16
& first_folder_name
) {
47 Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportStart(
48 first_folder_name
, bookmarks
.size()));
50 // |bookmarks_left| is required for the checks below as Windows has a
51 // Debug bounds-check which prevents pushing an iterator beyond its end()
52 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
53 int bookmarks_left
= bookmarks
.end() - bookmarks
.begin();
54 for (std::vector
<ImportedBookmarkEntry
>::const_iterator it
=
55 bookmarks
.begin(); it
< bookmarks
.end();) {
56 std::vector
<ImportedBookmarkEntry
> bookmark_group
;
57 std::vector
<ImportedBookmarkEntry
>::const_iterator end_group
=
58 it
+ std::min(bookmarks_left
, kNumBookmarksToSend
);
59 bookmark_group
.assign(it
, end_group
);
61 Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportGroup(
63 bookmarks_left
-= end_group
- it
;
66 DCHECK_EQ(0, bookmarks_left
);
69 void ExternalProcessImporterBridge::AddHomePage(const GURL
& home_page
) {
70 Send(new ProfileImportProcessHostMsg_NotifyHomePageImportReady(home_page
));
74 void ExternalProcessImporterBridge::AddIE7PasswordInfo(
75 const importer::ImporterIE7PasswordInfo
& password_info
) {
76 Send(new ProfileImportProcessHostMsg_NotifyIE7PasswordInfo(password_info
));
80 void ExternalProcessImporterBridge::SetFavicons(
81 const std::vector
<ImportedFaviconUsage
>& favicons
) {
82 Send(new ProfileImportProcessHostMsg_NotifyFaviconsImportStart(
85 // |favicons_left| is required for the checks below as Windows has a
86 // Debug bounds-check which prevents pushing an iterator beyond its end()
87 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
88 int favicons_left
= favicons
.end() - favicons
.begin();
89 for (std::vector
<ImportedFaviconUsage
>::const_iterator it
=
90 favicons
.begin(); it
< favicons
.end();) {
91 std::vector
<ImportedFaviconUsage
> favicons_group
;
92 std::vector
<ImportedFaviconUsage
>::const_iterator end_group
=
93 it
+ std::min(favicons_left
, kNumFaviconsToSend
);
94 favicons_group
.assign(it
, end_group
);
96 Send(new ProfileImportProcessHostMsg_NotifyFaviconsImportGroup(
98 favicons_left
-= end_group
- it
;
101 DCHECK_EQ(0, favicons_left
);
104 void ExternalProcessImporterBridge::SetHistoryItems(
105 const std::vector
<ImporterURLRow
>& rows
,
106 importer::VisitSource visit_source
) {
107 Send(new ProfileImportProcessHostMsg_NotifyHistoryImportStart(rows
.size()));
109 // |rows_left| is required for the checks below as Windows has a
110 // Debug bounds-check which prevents pushing an iterator beyond its end()
111 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
112 int rows_left
= rows
.end() - rows
.begin();
113 for (std::vector
<ImporterURLRow
>::const_iterator it
= rows
.begin();
115 std::vector
<ImporterURLRow
> row_group
;
116 std::vector
<ImporterURLRow
>::const_iterator end_group
=
117 it
+ std::min(rows_left
, kNumHistoryRowsToSend
);
118 row_group
.assign(it
, end_group
);
120 Send(new ProfileImportProcessHostMsg_NotifyHistoryImportGroup(
121 row_group
, visit_source
));
122 rows_left
-= end_group
- it
;
125 DCHECK_EQ(0, rows_left
);
128 void ExternalProcessImporterBridge::SetKeywords(
129 const std::vector
<importer::SearchEngineInfo
>& search_engines
,
130 bool unique_on_host_and_path
) {
131 Send(new ProfileImportProcessHostMsg_NotifyKeywordsReady(
132 search_engines
, unique_on_host_and_path
));
135 void ExternalProcessImporterBridge::SetFirefoxSearchEnginesXMLData(
136 const std::vector
<std::string
>& search_engine_data
) {
137 Send(new ProfileImportProcessHostMsg_NotifyFirefoxSearchEngData(
138 search_engine_data
));
141 void ExternalProcessImporterBridge::SetPasswordForm(
142 const autofill::PasswordForm
& form
) {
143 Send(new ProfileImportProcessHostMsg_NotifyPasswordFormReady(form
));
146 void ExternalProcessImporterBridge::SetAutofillFormData(
147 const std::vector
<ImporterAutofillFormDataEntry
>& entries
) {
148 Send(new ProfileImportProcessHostMsg_AutofillFormDataImportStart(
151 // |autofill_form_data_entries_left| is required for the checks below as
152 // Windows has a Debug bounds-check which prevents pushing an iterator beyond
153 // its end() (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 ==
155 int autofill_form_data_entries_left
= entries
.end() - entries
.begin();
156 for (std::vector
<ImporterAutofillFormDataEntry
>::const_iterator it
=
158 it
< entries
.end();) {
159 std::vector
<ImporterAutofillFormDataEntry
> autofill_form_data_entry_group
;
160 std::vector
<ImporterAutofillFormDataEntry
>::const_iterator end_group
=
162 std::min(autofill_form_data_entries_left
, kNumAutofillFormDataToSend
);
163 autofill_form_data_entry_group
.assign(it
, end_group
);
165 Send(new ProfileImportProcessHostMsg_AutofillFormDataImportGroup(
166 autofill_form_data_entry_group
));
167 autofill_form_data_entries_left
-= end_group
- it
;
170 DCHECK_EQ(0, autofill_form_data_entries_left
);
173 void ExternalProcessImporterBridge::NotifyStarted() {
174 Send(new ProfileImportProcessHostMsg_Import_Started());
177 void ExternalProcessImporterBridge::NotifyItemStarted(
178 importer::ImportItem item
) {
179 Send(new ProfileImportProcessHostMsg_ImportItem_Started(item
));
182 void ExternalProcessImporterBridge::NotifyItemEnded(importer::ImportItem item
) {
183 Send(new ProfileImportProcessHostMsg_ImportItem_Finished(item
));
186 void ExternalProcessImporterBridge::NotifyEnded() {
187 // The internal process detects import end when all items have been received.
190 base::string16
ExternalProcessImporterBridge::GetLocalizedString(
192 base::string16 message
;
193 localized_strings_
->GetString(base::IntToString(message_id
), &message
);
197 ExternalProcessImporterBridge::~ExternalProcessImporterBridge() {}
199 void ExternalProcessImporterBridge::Send(IPC::Message
* message
) {
200 task_runner_
->PostTask(
202 base::Bind(&ExternalProcessImporterBridge::SendInternal
,
206 void ExternalProcessImporterBridge::SendInternal(IPC::Message
* message
) {
207 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
208 sender_
->Send(message
);