OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "chrome/browser/importer/external_process_importer_client.h" |
| 6 |
| 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/importer/importer_bridge.h" |
| 9 #include "chrome/browser/importer/importer_host.h" |
| 10 #include "chrome/browser/search_engines/template_url.h" |
| 11 #include "chrome/browser/search_engines/template_url_model.h" |
| 12 #include "content/browser/browser_thread.h" |
| 13 |
| 14 ExternalProcessImporterClient::ExternalProcessImporterClient( |
| 15 ExternalProcessImporterHost* importer_host, |
| 16 const importer::ProfileInfo& profile_info, |
| 17 uint16 items, |
| 18 InProcessImporterBridge* bridge, |
| 19 bool import_to_bookmark_bar) |
| 20 : bookmarks_options_(0), |
| 21 total_bookmarks_count_(0), |
| 22 total_history_rows_count_(0), |
| 23 total_fav_icons_count_(0), |
| 24 process_importer_host_(importer_host), |
| 25 profile_import_process_host_(NULL), |
| 26 profile_info_(profile_info), |
| 27 items_(items), |
| 28 import_to_bookmark_bar_(import_to_bookmark_bar), |
| 29 bridge_(bridge), |
| 30 cancelled_(false) { |
| 31 bridge_->AddRef(); |
| 32 process_importer_host_->NotifyImportStarted(); |
| 33 } |
| 34 |
| 35 ExternalProcessImporterClient::~ExternalProcessImporterClient() { |
| 36 bridge_->Release(); |
| 37 } |
| 38 |
| 39 void ExternalProcessImporterClient::CancelImportProcessOnIOThread() { |
| 40 profile_import_process_host_->CancelProfileImportProcess(); |
| 41 } |
| 42 |
| 43 void ExternalProcessImporterClient::NotifyItemFinishedOnIOThread( |
| 44 importer::ImportItem import_item) { |
| 45 profile_import_process_host_->ReportImportItemFinished(import_item); |
| 46 } |
| 47 |
| 48 void ExternalProcessImporterClient::Cleanup() { |
| 49 if (cancelled_) |
| 50 return; |
| 51 |
| 52 if (process_importer_host_) |
| 53 process_importer_host_->NotifyImportEnded(); |
| 54 Release(); |
| 55 } |
| 56 |
| 57 void ExternalProcessImporterClient::Start() { |
| 58 AddRef(); // balanced in Cleanup. |
| 59 BrowserThread::ID thread_id; |
| 60 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); |
| 61 BrowserThread::PostTask( |
| 62 BrowserThread::IO, FROM_HERE, |
| 63 NewRunnableMethod(this, |
| 64 &ExternalProcessImporterClient::StartProcessOnIOThread, |
| 65 g_browser_process->resource_dispatcher_host(), thread_id)); |
| 66 } |
| 67 |
| 68 void ExternalProcessImporterClient::StartProcessOnIOThread( |
| 69 ResourceDispatcherHost* rdh, |
| 70 BrowserThread::ID thread_id) { |
| 71 profile_import_process_host_ = |
| 72 new ProfileImportProcessHost(rdh, this, thread_id); |
| 73 profile_import_process_host_->StartProfileImportProcess(profile_info_, |
| 74 items_, import_to_bookmark_bar_); |
| 75 } |
| 76 |
| 77 void ExternalProcessImporterClient::Cancel() { |
| 78 if (cancelled_) |
| 79 return; |
| 80 |
| 81 cancelled_ = true; |
| 82 if (profile_import_process_host_) { |
| 83 BrowserThread::PostTask( |
| 84 BrowserThread::IO, FROM_HERE, |
| 85 NewRunnableMethod(this, |
| 86 &ExternalProcessImporterClient::CancelImportProcessOnIOThread)); |
| 87 } |
| 88 Release(); |
| 89 } |
| 90 |
| 91 void ExternalProcessImporterClient::OnProcessCrashed(int exit_code) { |
| 92 if (cancelled_) |
| 93 return; |
| 94 |
| 95 process_importer_host_->Cancel(); |
| 96 } |
| 97 |
| 98 void ExternalProcessImporterClient::OnImportStart() { |
| 99 if (cancelled_) |
| 100 return; |
| 101 |
| 102 bridge_->NotifyStarted(); |
| 103 } |
| 104 |
| 105 void ExternalProcessImporterClient::OnImportFinished(bool succeeded, |
| 106 std::string error_msg) { |
| 107 if (cancelled_) |
| 108 return; |
| 109 |
| 110 if (!succeeded) |
| 111 LOG(WARNING) << "Import failed. Error: " << error_msg; |
| 112 Cleanup(); |
| 113 } |
| 114 |
| 115 void ExternalProcessImporterClient::OnImportItemStart(int item_data) { |
| 116 if (cancelled_) |
| 117 return; |
| 118 |
| 119 bridge_->NotifyItemStarted(static_cast<importer::ImportItem>(item_data)); |
| 120 } |
| 121 |
| 122 void ExternalProcessImporterClient::OnImportItemFinished(int item_data) { |
| 123 if (cancelled_) |
| 124 return; |
| 125 |
| 126 importer::ImportItem import_item = |
| 127 static_cast<importer::ImportItem>(item_data); |
| 128 bridge_->NotifyItemEnded(import_item); |
| 129 BrowserThread::PostTask( |
| 130 BrowserThread::IO, FROM_HERE, |
| 131 NewRunnableMethod(this, |
| 132 &ExternalProcessImporterClient::NotifyItemFinishedOnIOThread, |
| 133 import_item)); |
| 134 } |
| 135 |
| 136 void ExternalProcessImporterClient::OnHistoryImportStart( |
| 137 size_t total_history_rows_count) { |
| 138 if (cancelled_) |
| 139 return; |
| 140 |
| 141 total_history_rows_count_ = total_history_rows_count; |
| 142 history_rows_.reserve(total_history_rows_count); |
| 143 } |
| 144 |
| 145 void ExternalProcessImporterClient::OnHistoryImportGroup( |
| 146 const std::vector<history::URLRow>& history_rows_group, |
| 147 int visit_source) { |
| 148 if (cancelled_) |
| 149 return; |
| 150 |
| 151 history_rows_.insert(history_rows_.end(), history_rows_group.begin(), |
| 152 history_rows_group.end()); |
| 153 if (history_rows_.size() == total_history_rows_count_) |
| 154 bridge_->SetHistoryItems(history_rows_, |
| 155 static_cast<history::VisitSource>(visit_source)); |
| 156 } |
| 157 |
| 158 void ExternalProcessImporterClient::OnHomePageImportReady( |
| 159 const GURL& home_page) { |
| 160 if (cancelled_) |
| 161 return; |
| 162 |
| 163 bridge_->AddHomePage(home_page); |
| 164 } |
| 165 |
| 166 void ExternalProcessImporterClient::OnBookmarksImportStart( |
| 167 const std::wstring first_folder_name, |
| 168 int options, size_t total_bookmarks_count) { |
| 169 if (cancelled_) |
| 170 return; |
| 171 |
| 172 bookmarks_first_folder_name_ = first_folder_name; |
| 173 bookmarks_options_ = options; |
| 174 total_bookmarks_count_ = total_bookmarks_count; |
| 175 bookmarks_.reserve(total_bookmarks_count); |
| 176 } |
| 177 |
| 178 void ExternalProcessImporterClient::OnBookmarksImportGroup( |
| 179 const std::vector<ProfileWriter::BookmarkEntry>& bookmarks_group) { |
| 180 if (cancelled_) |
| 181 return; |
| 182 |
| 183 // Collect sets of bookmarks from importer process until we have reached |
| 184 // total_bookmarks_count_: |
| 185 bookmarks_.insert(bookmarks_.end(), bookmarks_group.begin(), |
| 186 bookmarks_group.end()); |
| 187 if (bookmarks_.size() == total_bookmarks_count_) { |
| 188 bridge_->AddBookmarkEntries(bookmarks_, bookmarks_first_folder_name_, |
| 189 bookmarks_options_); |
| 190 } |
| 191 } |
| 192 |
| 193 void ExternalProcessImporterClient::OnFavIconsImportStart( |
| 194 size_t total_fav_icons_count) { |
| 195 if (cancelled_) |
| 196 return; |
| 197 |
| 198 total_fav_icons_count_ = total_fav_icons_count; |
| 199 fav_icons_.reserve(total_fav_icons_count); |
| 200 } |
| 201 |
| 202 void ExternalProcessImporterClient::OnFavIconsImportGroup( |
| 203 const std::vector<history::ImportedFavIconUsage>& fav_icons_group) { |
| 204 if (cancelled_) |
| 205 return; |
| 206 |
| 207 fav_icons_.insert(fav_icons_.end(), fav_icons_group.begin(), |
| 208 fav_icons_group.end()); |
| 209 if (fav_icons_.size() == total_fav_icons_count_) |
| 210 bridge_->SetFavIcons(fav_icons_); |
| 211 } |
| 212 |
| 213 void ExternalProcessImporterClient::OnPasswordFormImportReady( |
| 214 const webkit_glue::PasswordForm& form) { |
| 215 if (cancelled_) |
| 216 return; |
| 217 |
| 218 bridge_->SetPasswordForm(form); |
| 219 } |
| 220 |
| 221 void ExternalProcessImporterClient::OnKeywordsImportReady( |
| 222 const std::vector<TemplateURL>& template_urls, |
| 223 int default_keyword_index, bool unique_on_host_and_path) { |
| 224 if (cancelled_) |
| 225 return; |
| 226 |
| 227 std::vector<TemplateURL*> template_url_vec; |
| 228 template_url_vec.reserve(template_urls.size()); |
| 229 std::vector<TemplateURL>::const_iterator iter; |
| 230 for (iter = template_urls.begin(); |
| 231 iter != template_urls.end(); |
| 232 ++iter) { |
| 233 template_url_vec.push_back(new TemplateURL(*iter)); |
| 234 } |
| 235 bridge_->SetKeywords(template_url_vec, default_keyword_index, |
| 236 unique_on_host_and_path); |
| 237 } |
OLD | NEW |