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