OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/browser/importer/importer_host.h" | 5 #include "chrome/browser/importer/importer_host.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/bookmarks/bookmark_model.h" | 8 #include "chrome/browser/bookmarks/bookmark_model.h" |
9 #include "chrome/browser/browser_list.h" | 9 #include "chrome/browser/browser_list.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/importer/external_process_importer_client.h" |
11 #include "chrome/browser/importer/firefox_profile_lock.h" | 12 #include "chrome/browser/importer/firefox_profile_lock.h" |
12 #include "chrome/browser/importer/importer_bridge.h" | 13 #include "chrome/browser/importer/importer_bridge.h" |
13 #include "chrome/browser/importer/importer_lock_dialog.h" | 14 #include "chrome/browser/importer/importer_lock_dialog.h" |
14 #include "chrome/browser/importer/importer_progress_observer.h" | 15 #include "chrome/browser/importer/importer_progress_observer.h" |
15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/search_engines/template_url.h" | 17 #include "chrome/browser/search_engines/template_url.h" |
17 #include "chrome/browser/search_engines/template_url_model.h" | 18 #include "chrome/browser/search_engines/template_url_model.h" |
18 #include "content/browser/browser_thread.h" | 19 #include "content/browser/browser_thread.h" |
19 #include "content/common/notification_source.h" | 20 #include "content/common/notification_source.h" |
20 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 model->RemoveObserver(this); | 309 model->RemoveObserver(this); |
309 waiting_for_bookmarkbar_model_ = false; | 310 waiting_for_bookmarkbar_model_ = false; |
310 installed_bookmark_observer_ = false; | 311 installed_bookmark_observer_ = false; |
311 | 312 |
312 // Because the import process is running externally, the decision whether | 313 // Because the import process is running externally, the decision whether |
313 // to import to the bookmark bar must be stored here so that it can be | 314 // to import to the bookmark bar must be stored here so that it can be |
314 // passed to the importer when the import task is invoked. | 315 // passed to the importer when the import task is invoked. |
315 import_to_bookmark_bar_ = (!model->HasBookmarks()); | 316 import_to_bookmark_bar_ = (!model->HasBookmarks()); |
316 InvokeTaskIfDone(); | 317 InvokeTaskIfDone(); |
317 } | 318 } |
318 | |
319 // ExternalProcessImporterClient ----------------------------------------------- | |
320 | |
321 ExternalProcessImporterClient::ExternalProcessImporterClient( | |
322 ExternalProcessImporterHost* importer_host, | |
323 const importer::ProfileInfo& profile_info, | |
324 int items, | |
325 InProcessImporterBridge* bridge, | |
326 bool import_to_bookmark_bar) | |
327 : bookmarks_options_(0), | |
328 total_bookmarks_count_(0), | |
329 total_history_rows_count_(0), | |
330 total_fav_icons_count_(0), | |
331 process_importer_host_(importer_host), | |
332 profile_import_process_host_(NULL), | |
333 profile_info_(profile_info), | |
334 items_(items), | |
335 import_to_bookmark_bar_(import_to_bookmark_bar), | |
336 bridge_(bridge), | |
337 cancelled_(false) { | |
338 bridge_->AddRef(); | |
339 process_importer_host_->NotifyImportStarted(); | |
340 } | |
341 | |
342 ExternalProcessImporterClient::~ExternalProcessImporterClient() { | |
343 bridge_->Release(); | |
344 } | |
345 | |
346 void ExternalProcessImporterClient::Start() { | |
347 AddRef(); // balanced in Cleanup. | |
348 BrowserThread::ID thread_id; | |
349 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); | |
350 BrowserThread::PostTask( | |
351 BrowserThread::IO, FROM_HERE, | |
352 NewRunnableMethod(this, | |
353 &ExternalProcessImporterClient::StartProcessOnIOThread, | |
354 g_browser_process->resource_dispatcher_host(), thread_id)); | |
355 } | |
356 | |
357 void ExternalProcessImporterClient::StartProcessOnIOThread( | |
358 ResourceDispatcherHost* rdh, | |
359 BrowserThread::ID thread_id) { | |
360 profile_import_process_host_ = | |
361 new ProfileImportProcessHost(rdh, this, thread_id); | |
362 profile_import_process_host_->StartProfileImportProcess(profile_info_, | |
363 items_, import_to_bookmark_bar_); | |
364 } | |
365 | |
366 void ExternalProcessImporterClient::Cancel() { | |
367 if (cancelled_) | |
368 return; | |
369 | |
370 cancelled_ = true; | |
371 if (profile_import_process_host_) { | |
372 BrowserThread::PostTask( | |
373 BrowserThread::IO, FROM_HERE, | |
374 NewRunnableMethod(this, | |
375 &ExternalProcessImporterClient::CancelImportProcessOnIOThread)); | |
376 } | |
377 Release(); | |
378 } | |
379 | |
380 void ExternalProcessImporterClient::CancelImportProcessOnIOThread() { | |
381 profile_import_process_host_->CancelProfileImportProcess(); | |
382 } | |
383 | |
384 void ExternalProcessImporterClient::NotifyItemFinishedOnIOThread( | |
385 importer::ImportItem import_item) { | |
386 profile_import_process_host_->ReportImportItemFinished(import_item); | |
387 } | |
388 | |
389 void ExternalProcessImporterClient::OnProcessCrashed(int exit_code) { | |
390 if (cancelled_) | |
391 return; | |
392 | |
393 process_importer_host_->Cancel(); | |
394 } | |
395 | |
396 void ExternalProcessImporterClient::Cleanup() { | |
397 if (cancelled_) | |
398 return; | |
399 | |
400 if (process_importer_host_) | |
401 process_importer_host_->NotifyImportEnded(); | |
402 Release(); | |
403 } | |
404 | |
405 void ExternalProcessImporterClient::OnImportStart() { | |
406 if (cancelled_) | |
407 return; | |
408 | |
409 bridge_->NotifyStarted(); | |
410 } | |
411 | |
412 void ExternalProcessImporterClient::OnImportFinished(bool succeeded, | |
413 std::string error_msg) { | |
414 if (cancelled_) | |
415 return; | |
416 | |
417 if (!succeeded) | |
418 LOG(WARNING) << "Import failed. Error: " << error_msg; | |
419 Cleanup(); | |
420 } | |
421 | |
422 void ExternalProcessImporterClient::OnImportItemStart(int item_data) { | |
423 if (cancelled_) | |
424 return; | |
425 | |
426 bridge_->NotifyItemStarted(static_cast<importer::ImportItem>(item_data)); | |
427 } | |
428 | |
429 void ExternalProcessImporterClient::OnImportItemFinished(int item_data) { | |
430 if (cancelled_) | |
431 return; | |
432 | |
433 importer::ImportItem import_item = | |
434 static_cast<importer::ImportItem>(item_data); | |
435 bridge_->NotifyItemEnded(import_item); | |
436 BrowserThread::PostTask( | |
437 BrowserThread::IO, FROM_HERE, | |
438 NewRunnableMethod(this, | |
439 &ExternalProcessImporterClient::NotifyItemFinishedOnIOThread, | |
440 import_item)); | |
441 } | |
442 | |
443 void ExternalProcessImporterClient::OnHistoryImportStart( | |
444 size_t total_history_rows_count) { | |
445 if (cancelled_) | |
446 return; | |
447 | |
448 total_history_rows_count_ = total_history_rows_count; | |
449 history_rows_.reserve(total_history_rows_count); | |
450 } | |
451 | |
452 void ExternalProcessImporterClient::OnHistoryImportGroup( | |
453 const std::vector<history::URLRow>& history_rows_group, | |
454 int visit_source) { | |
455 if (cancelled_) | |
456 return; | |
457 | |
458 history_rows_.insert(history_rows_.end(), history_rows_group.begin(), | |
459 history_rows_group.end()); | |
460 if (history_rows_.size() == total_history_rows_count_) | |
461 bridge_->SetHistoryItems(history_rows_, | |
462 static_cast<history::VisitSource>(visit_source)); | |
463 } | |
464 | |
465 void ExternalProcessImporterClient::OnHomePageImportReady( | |
466 const GURL& home_page) { | |
467 if (cancelled_) | |
468 return; | |
469 | |
470 bridge_->AddHomePage(home_page); | |
471 } | |
472 | |
473 void ExternalProcessImporterClient::OnBookmarksImportStart( | |
474 const std::wstring first_folder_name, | |
475 int options, size_t total_bookmarks_count) { | |
476 if (cancelled_) | |
477 return; | |
478 | |
479 bookmarks_first_folder_name_ = first_folder_name; | |
480 bookmarks_options_ = options; | |
481 total_bookmarks_count_ = total_bookmarks_count; | |
482 bookmarks_.reserve(total_bookmarks_count); | |
483 } | |
484 | |
485 void ExternalProcessImporterClient::OnBookmarksImportGroup( | |
486 const std::vector<ProfileWriter::BookmarkEntry>& bookmarks_group) { | |
487 if (cancelled_) | |
488 return; | |
489 | |
490 // Collect sets of bookmarks from importer process until we have reached | |
491 // total_bookmarks_count_: | |
492 bookmarks_.insert(bookmarks_.end(), bookmarks_group.begin(), | |
493 bookmarks_group.end()); | |
494 if (bookmarks_.size() == total_bookmarks_count_) { | |
495 bridge_->AddBookmarkEntries(bookmarks_, bookmarks_first_folder_name_, | |
496 bookmarks_options_); | |
497 } | |
498 } | |
499 | |
500 void ExternalProcessImporterClient::OnFavIconsImportStart( | |
501 size_t total_fav_icons_count) { | |
502 if (cancelled_) | |
503 return; | |
504 | |
505 total_fav_icons_count_ = total_fav_icons_count; | |
506 fav_icons_.reserve(total_fav_icons_count); | |
507 } | |
508 | |
509 void ExternalProcessImporterClient::OnFavIconsImportGroup( | |
510 const std::vector<history::ImportedFavIconUsage>& fav_icons_group) { | |
511 if (cancelled_) | |
512 return; | |
513 | |
514 fav_icons_.insert(fav_icons_.end(), fav_icons_group.begin(), | |
515 fav_icons_group.end()); | |
516 if (fav_icons_.size() == total_fav_icons_count_) | |
517 bridge_->SetFavIcons(fav_icons_); | |
518 } | |
519 | |
520 void ExternalProcessImporterClient::OnPasswordFormImportReady( | |
521 const webkit_glue::PasswordForm& form) { | |
522 if (cancelled_) | |
523 return; | |
524 | |
525 bridge_->SetPasswordForm(form); | |
526 } | |
527 | |
528 void ExternalProcessImporterClient::OnKeywordsImportReady( | |
529 const std::vector<TemplateURL>& template_urls, | |
530 int default_keyword_index, bool unique_on_host_and_path) { | |
531 if (cancelled_) | |
532 return; | |
533 | |
534 std::vector<TemplateURL*> template_url_vec; | |
535 template_url_vec.reserve(template_urls.size()); | |
536 std::vector<TemplateURL>::const_iterator iter; | |
537 for (iter = template_urls.begin(); | |
538 iter != template_urls.end(); | |
539 ++iter) { | |
540 template_url_vec.push_back(new TemplateURL(*iter)); | |
541 } | |
542 bridge_->SetKeywords(template_url_vec, default_keyword_index, | |
543 unique_on_host_and_path); | |
544 } | |
OLD | NEW |