| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/utility/importer/external_process_importer_bridge.h" | 5 #include "chrome/utility/importer/external_process_importer_bridge.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/debug/dump_without_crashing.h" | 8 #include "base/debug/dump_without_crashing.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "chrome/common/importer/imported_bookmark_entry.h" | 15 #include "chrome/common/importer/imported_bookmark_entry.h" |
| 16 #include "chrome/common/importer/importer_autofill_form_data_entry.h" |
| 16 #include "chrome/common/importer/importer_data_types.h" | 17 #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" | 18 #include "components/autofill/core/common/password_form.h" |
| 19 #include "ipc/ipc_sender.h" | 19 |
| 20 using chrome::mojom::ProfileImportObserver; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // Rather than sending all import items over IPC at once we chunk them into | 24 // 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 // separate requests. This avoids the case of a large import causing |
| 25 // oversized IPC messages. | 26 // oversized IPC messages. |
| 26 const int kNumBookmarksToSend = 100; | 27 const int kNumBookmarksToSend = 100; |
| 27 const int kNumHistoryRowsToSend = 100; | 28 const int kNumHistoryRowsToSend = 100; |
| 28 const int kNumFaviconsToSend = 100; | 29 const int kNumFaviconsToSend = 100; |
| 29 const int kNumAutofillFormDataToSend = 100; | 30 const int kNumAutofillFormDataToSend = 100; |
| 30 | 31 |
| 31 } // namespace | 32 } // namespace |
| 32 | 33 |
| 33 ExternalProcessImporterBridge::ExternalProcessImporterBridge( | 34 ExternalProcessImporterBridge::ExternalProcessImporterBridge( |
| 34 const base::DictionaryValue& localized_strings, | 35 const base::DictionaryValue& localized_strings, |
| 35 IPC::Sender* sender, | 36 scoped_refptr<chrome::mojom::ThreadSafeProfileImportObserverPtr> observer) |
| 36 base::TaskRunner* task_runner) | 37 : observer_(std::move(observer)) { |
| 37 : sender_(sender), | |
| 38 task_runner_(task_runner) { | |
| 39 // Bridge needs to make its own copy because OS 10.6 autoreleases the | 38 // 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 ). | 39 // localized_strings value that is passed in (see http://crbug.com/46003 ). |
| 41 localized_strings_.reset(localized_strings.DeepCopy()); | 40 localized_strings_.reset(localized_strings.DeepCopy()); |
| 42 } | 41 } |
| 43 | 42 |
| 44 void ExternalProcessImporterBridge::AddBookmarks( | 43 void ExternalProcessImporterBridge::AddBookmarks( |
| 45 const std::vector<ImportedBookmarkEntry>& bookmarks, | 44 const std::vector<ImportedBookmarkEntry>& bookmarks, |
| 46 const base::string16& first_folder_name) { | 45 const base::string16& first_folder_name) { |
| 47 Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportStart( | 46 (*observer_)->OnBookmarksImportStart(first_folder_name, bookmarks.size()); |
| 48 first_folder_name, bookmarks.size())); | |
| 49 | 47 |
| 50 // |bookmarks_left| is required for the checks below as Windows has a | 48 // |bookmarks_left| is required for the checks below as Windows has a |
| 51 // Debug bounds-check which prevents pushing an iterator beyond its end() | 49 // 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()|). | 50 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|). |
| 53 int bookmarks_left = bookmarks.end() - bookmarks.begin(); | 51 int bookmarks_left = bookmarks.end() - bookmarks.begin(); |
| 54 for (std::vector<ImportedBookmarkEntry>::const_iterator it = | 52 for (std::vector<ImportedBookmarkEntry>::const_iterator it = |
| 55 bookmarks.begin(); it < bookmarks.end();) { | 53 bookmarks.begin(); it < bookmarks.end();) { |
| 56 std::vector<ImportedBookmarkEntry> bookmark_group; | 54 std::vector<ImportedBookmarkEntry> bookmark_group; |
| 57 std::vector<ImportedBookmarkEntry>::const_iterator end_group = | 55 std::vector<ImportedBookmarkEntry>::const_iterator end_group = |
| 58 it + std::min(bookmarks_left, kNumBookmarksToSend); | 56 it + std::min(bookmarks_left, kNumBookmarksToSend); |
| 59 bookmark_group.assign(it, end_group); | 57 bookmark_group.assign(it, end_group); |
| 60 | 58 |
| 61 Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportGroup( | 59 (*observer_)->OnBookmarksImportGroup(bookmark_group); |
| 62 bookmark_group)); | |
| 63 bookmarks_left -= end_group - it; | 60 bookmarks_left -= end_group - it; |
| 64 it = end_group; | 61 it = end_group; |
| 65 } | 62 } |
| 66 DCHECK_EQ(0, bookmarks_left); | 63 DCHECK_EQ(0, bookmarks_left); |
| 67 } | 64 } |
| 68 | 65 |
| 69 void ExternalProcessImporterBridge::AddHomePage(const GURL& home_page) { | 66 void ExternalProcessImporterBridge::AddHomePage(const GURL& home_page) { |
| 70 Send(new ProfileImportProcessHostMsg_NotifyHomePageImportReady(home_page)); | 67 (*observer_)->OnHomePageImportReady(home_page); |
| 71 } | 68 } |
| 72 | 69 |
| 73 #if defined(OS_WIN) | 70 #if defined(OS_WIN) |
| 74 void ExternalProcessImporterBridge::AddIE7PasswordInfo( | 71 void ExternalProcessImporterBridge::AddIE7PasswordInfo( |
| 75 const importer::ImporterIE7PasswordInfo& password_info) { | 72 const importer::ImporterIE7PasswordInfo& password_info) { |
| 76 Send(new ProfileImportProcessHostMsg_NotifyIE7PasswordInfo(password_info)); | 73 (*observer_)->OnIE7PasswordReceived(password_info); |
| 77 } | 74 } |
| 78 #endif | 75 #endif |
| 79 | 76 |
| 80 void ExternalProcessImporterBridge::SetFavicons( | 77 void ExternalProcessImporterBridge::SetFavicons( |
| 81 const favicon_base::FaviconUsageDataList& favicons) { | 78 const favicon_base::FaviconUsageDataList& favicons) { |
| 82 Send(new ProfileImportProcessHostMsg_NotifyFaviconsImportStart( | 79 (*observer_)->OnFaviconsImportStart(favicons.size()); |
| 83 favicons.size())); | |
| 84 | 80 |
| 85 // |favicons_left| is required for the checks below as Windows has a | 81 // |favicons_left| is required for the checks below as Windows has a |
| 86 // Debug bounds-check which prevents pushing an iterator beyond its end() | 82 // 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()|). | 83 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|). |
| 88 int favicons_left = favicons.end() - favicons.begin(); | 84 int favicons_left = favicons.end() - favicons.begin(); |
| 89 for (favicon_base::FaviconUsageDataList::const_iterator it = favicons.begin(); | 85 for (favicon_base::FaviconUsageDataList::const_iterator it = favicons.begin(); |
| 90 it < favicons.end();) { | 86 it < favicons.end();) { |
| 91 favicon_base::FaviconUsageDataList favicons_group; | 87 favicon_base::FaviconUsageDataList favicons_group; |
| 92 favicon_base::FaviconUsageDataList::const_iterator end_group = | 88 favicon_base::FaviconUsageDataList::const_iterator end_group = |
| 93 it + std::min(favicons_left, kNumFaviconsToSend); | 89 it + std::min(favicons_left, kNumFaviconsToSend); |
| 94 favicons_group.assign(it, end_group); | 90 favicons_group.assign(it, end_group); |
| 95 | 91 |
| 96 Send(new ProfileImportProcessHostMsg_NotifyFaviconsImportGroup( | 92 (*observer_)->OnFaviconsImportGroup(favicons_group); |
| 97 favicons_group)); | |
| 98 favicons_left -= end_group - it; | 93 favicons_left -= end_group - it; |
| 99 it = end_group; | 94 it = end_group; |
| 100 } | 95 } |
| 101 DCHECK_EQ(0, favicons_left); | 96 DCHECK_EQ(0, favicons_left); |
| 102 } | 97 } |
| 103 | 98 |
| 104 void ExternalProcessImporterBridge::SetHistoryItems( | 99 void ExternalProcessImporterBridge::SetHistoryItems( |
| 105 const std::vector<ImporterURLRow>& rows, | 100 const std::vector<ImporterURLRow>& rows, |
| 106 importer::VisitSource visit_source) { | 101 importer::VisitSource visit_source) { |
| 107 Send(new ProfileImportProcessHostMsg_NotifyHistoryImportStart(rows.size())); | 102 (*observer_)->OnHistoryImportStart(rows.size()); |
| 108 | 103 |
| 109 // |rows_left| is required for the checks below as Windows has a | 104 // |rows_left| is required for the checks below as Windows has a |
| 110 // Debug bounds-check which prevents pushing an iterator beyond its end() | 105 // 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()|). | 106 // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|). |
| 112 int rows_left = rows.end() - rows.begin(); | 107 int rows_left = rows.end() - rows.begin(); |
| 113 for (std::vector<ImporterURLRow>::const_iterator it = rows.begin(); | 108 for (std::vector<ImporterURLRow>::const_iterator it = rows.begin(); |
| 114 it < rows.end();) { | 109 it < rows.end();) { |
| 115 std::vector<ImporterURLRow> row_group; | 110 std::vector<ImporterURLRow> row_group; |
| 116 std::vector<ImporterURLRow>::const_iterator end_group = | 111 std::vector<ImporterURLRow>::const_iterator end_group = |
| 117 it + std::min(rows_left, kNumHistoryRowsToSend); | 112 it + std::min(rows_left, kNumHistoryRowsToSend); |
| 118 row_group.assign(it, end_group); | 113 row_group.assign(it, end_group); |
| 119 | 114 |
| 120 Send(new ProfileImportProcessHostMsg_NotifyHistoryImportGroup( | 115 (*observer_)->OnHistoryImportGroup(row_group, visit_source); |
| 121 row_group, visit_source)); | |
| 122 rows_left -= end_group - it; | 116 rows_left -= end_group - it; |
| 123 it = end_group; | 117 it = end_group; |
| 124 } | 118 } |
| 125 DCHECK_EQ(0, rows_left); | 119 DCHECK_EQ(0, rows_left); |
| 126 } | 120 } |
| 127 | 121 |
| 128 void ExternalProcessImporterBridge::SetKeywords( | 122 void ExternalProcessImporterBridge::SetKeywords( |
| 129 const std::vector<importer::SearchEngineInfo>& search_engines, | 123 const std::vector<importer::SearchEngineInfo>& search_engines, |
| 130 bool unique_on_host_and_path) { | 124 bool unique_on_host_and_path) { |
| 131 Send(new ProfileImportProcessHostMsg_NotifyKeywordsReady( | 125 (*observer_)->OnKeywordsImportReady(search_engines, unique_on_host_and_path); |
| 132 search_engines, unique_on_host_and_path)); | |
| 133 } | 126 } |
| 134 | 127 |
| 135 void ExternalProcessImporterBridge::SetFirefoxSearchEnginesXMLData( | 128 void ExternalProcessImporterBridge::SetFirefoxSearchEnginesXMLData( |
| 136 const std::vector<std::string>& search_engine_data) { | 129 const std::vector<std::string>& search_engine_data) { |
| 137 Send(new ProfileImportProcessHostMsg_NotifyFirefoxSearchEngData( | 130 (*observer_)->OnFirefoxSearchEngineDataReceived(search_engine_data); |
| 138 search_engine_data)); | |
| 139 } | 131 } |
| 140 | 132 |
| 141 void ExternalProcessImporterBridge::SetPasswordForm( | 133 void ExternalProcessImporterBridge::SetPasswordForm( |
| 142 const autofill::PasswordForm& form) { | 134 const autofill::PasswordForm& form) { |
| 143 Send(new ProfileImportProcessHostMsg_NotifyPasswordFormReady(form)); | 135 (*observer_)->OnPasswordFormImportReady(form); |
| 144 } | 136 } |
| 145 | 137 |
| 146 void ExternalProcessImporterBridge::SetAutofillFormData( | 138 void ExternalProcessImporterBridge::SetAutofillFormData( |
| 147 const std::vector<ImporterAutofillFormDataEntry>& entries) { | 139 const std::vector<ImporterAutofillFormDataEntry>& entries) { |
| 148 Send(new ProfileImportProcessHostMsg_AutofillFormDataImportStart( | 140 (*observer_)->OnAutofillFormDataImportStart(entries.size()); |
| 149 entries.size())); | |
| 150 | 141 |
| 151 // |autofill_form_data_entries_left| is required for the checks below as | 142 // |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 | 143 // 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 == | 144 // its end() (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == |
| 154 // s.end()|). | 145 // s.end()|). |
| 155 int autofill_form_data_entries_left = entries.end() - entries.begin(); | 146 int autofill_form_data_entries_left = entries.end() - entries.begin(); |
| 156 for (std::vector<ImporterAutofillFormDataEntry>::const_iterator it = | 147 for (std::vector<ImporterAutofillFormDataEntry>::const_iterator it = |
| 157 entries.begin(); | 148 entries.begin(); |
| 158 it < entries.end();) { | 149 it < entries.end();) { |
| 159 std::vector<ImporterAutofillFormDataEntry> autofill_form_data_entry_group; | 150 std::vector<ImporterAutofillFormDataEntry> autofill_form_data_entry_group; |
| 160 std::vector<ImporterAutofillFormDataEntry>::const_iterator end_group = | 151 std::vector<ImporterAutofillFormDataEntry>::const_iterator end_group = |
| 161 it + | 152 it + |
| 162 std::min(autofill_form_data_entries_left, kNumAutofillFormDataToSend); | 153 std::min(autofill_form_data_entries_left, kNumAutofillFormDataToSend); |
| 163 autofill_form_data_entry_group.assign(it, end_group); | 154 autofill_form_data_entry_group.assign(it, end_group); |
| 164 | 155 |
| 165 Send(new ProfileImportProcessHostMsg_AutofillFormDataImportGroup( | 156 (*observer_)->OnAutofillFormDataImportGroup(autofill_form_data_entry_group); |
| 166 autofill_form_data_entry_group)); | |
| 167 autofill_form_data_entries_left -= end_group - it; | 157 autofill_form_data_entries_left -= end_group - it; |
| 168 it = end_group; | 158 it = end_group; |
| 169 } | 159 } |
| 170 DCHECK_EQ(0, autofill_form_data_entries_left); | 160 DCHECK_EQ(0, autofill_form_data_entries_left); |
| 171 } | 161 } |
| 172 | 162 |
| 173 void ExternalProcessImporterBridge::NotifyStarted() { | 163 void ExternalProcessImporterBridge::NotifyStarted() { |
| 174 Send(new ProfileImportProcessHostMsg_Import_Started()); | 164 (*observer_)->OnImportStart(); |
| 175 } | 165 } |
| 176 | 166 |
| 177 void ExternalProcessImporterBridge::NotifyItemStarted( | 167 void ExternalProcessImporterBridge::NotifyItemStarted( |
| 178 importer::ImportItem item) { | 168 importer::ImportItem item) { |
| 179 Send(new ProfileImportProcessHostMsg_ImportItem_Started(item)); | 169 (*observer_)->OnImportItemStart(item); |
| 180 } | 170 } |
| 181 | 171 |
| 182 void ExternalProcessImporterBridge::NotifyItemEnded(importer::ImportItem item) { | 172 void ExternalProcessImporterBridge::NotifyItemEnded(importer::ImportItem item) { |
| 183 Send(new ProfileImportProcessHostMsg_ImportItem_Finished(item)); | 173 (*observer_)->OnImportItemFinished(item); |
| 184 } | 174 } |
| 185 | 175 |
| 186 void ExternalProcessImporterBridge::NotifyEnded() { | 176 void ExternalProcessImporterBridge::NotifyEnded() { |
| 187 // The internal process detects import end when all items have been received. | 177 (*observer_)->OnImportFinished(true, std::string()); |
| 188 } | 178 } |
| 189 | 179 |
| 190 base::string16 ExternalProcessImporterBridge::GetLocalizedString( | 180 base::string16 ExternalProcessImporterBridge::GetLocalizedString( |
| 191 int message_id) { | 181 int message_id) { |
| 192 base::string16 message; | 182 base::string16 message; |
| 193 localized_strings_->GetString(base::IntToString(message_id), &message); | 183 localized_strings_->GetString(base::IntToString(message_id), &message); |
| 194 return message; | 184 return message; |
| 195 } | 185 } |
| 196 | 186 |
| 197 ExternalProcessImporterBridge::~ExternalProcessImporterBridge() {} | 187 ExternalProcessImporterBridge::~ExternalProcessImporterBridge() {} |
| 198 | |
| 199 void ExternalProcessImporterBridge::Send(IPC::Message* message) { | |
| 200 task_runner_->PostTask( | |
| 201 FROM_HERE, | |
| 202 base::Bind(&ExternalProcessImporterBridge::SendInternal, | |
| 203 this, message)); | |
| 204 } | |
| 205 | |
| 206 void ExternalProcessImporterBridge::SendInternal(IPC::Message* message) { | |
| 207 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 208 sender_->Send(message); | |
| 209 } | |
| OLD | NEW |