From e46a9e38c55c7544fa3974de39663a63d168eb43 Mon Sep 17 00:00:00 2001 From: "tfarina@chromium.org" <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> Date: Wed, 9 Mar 2011 15:00:10 +0000 Subject: importer: Move chrome/browser/importer/importer.[cc,h] to importer_host.[cc,h]. This is the first change in preparation for splitting importer_host.[cc,h] into importer_host.[cc,h] and importer.[cc,h]. With this I'll be able to move the Importer class, in a followup CL, to importer.[cc,h]. Otherwise that will be harder and a mess. BUG=None TEST=None Review URL: http://codereview.chromium.org/6652015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77464 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/automation/automation_provider.cc | 18 +- .../automation/testing_automation_provider.cc | 2 +- .../extensions/extension_bookmarks_module.h | 2 +- chrome/browser/first_run/first_run.cc | 4 +- chrome/browser/first_run/first_run_win.cc | 2 +- chrome/browser/importer/firefox2_importer.h | 2 +- chrome/browser/importer/firefox3_importer.h | 2 +- chrome/browser/importer/ie_importer.h | 2 +- chrome/browser/importer/importer.cc | 589 --------------------- chrome/browser/importer/importer.h | 437 --------------- chrome/browser/importer/importer_bridge.cc | 6 +- chrome/browser/importer/importer_host.cc | 589 +++++++++++++++++++++ chrome/browser/importer/importer_host.h | 437 +++++++++++++++ chrome/browser/importer/importer_unittest.cc | 3 +- chrome/browser/importer/mork_reader.cc | 2 +- chrome/browser/importer/profile_writer.cc | 2 +- chrome/browser/importer/safari_importer.h | 2 +- chrome/browser/importer/toolbar_importer.h | 2 +- .../browser/importer/toolbar_importer_unittest.cc | 4 +- .../ui/cocoa/importer/import_dialog_cocoa.h | 2 +- .../cocoa/importer/import_dialog_cocoa_unittest.mm | 2 +- .../ui/cocoa/importer/import_lock_dialog_cocoa.mm | 2 +- .../cocoa/importer/import_progress_dialog_cocoa.mm | 2 +- .../browser/ui/gtk/importer/import_dialog_gtk.cc | 3 +- chrome/browser/ui/gtk/importer/import_dialog_gtk.h | 15 +- .../ui/gtk/importer/import_lock_dialog_gtk.cc | 2 +- .../ui/gtk/importer/import_progress_dialog_gtk.cc | 2 +- .../browser/ui/views/importer/import_dialog_view.h | 2 +- .../ui/views/importer/import_lock_dialog_view.cc | 2 +- .../views/importer/import_progress_dialog_view.cc | 2 +- .../ui/webui/options/import_data_handler.cc | 2 +- chrome/chrome_browser.gypi | 4 +- chrome/profile_import/profile_import_thread.cc | 2 +- 33 files changed, 1080 insertions(+), 1071 deletions(-) delete mode 100644 chrome/browser/importer/importer.cc delete mode 100644 chrome/browser/importer/importer.h create mode 100644 chrome/browser/importer/importer_host.cc create mode 100644 chrome/browser/importer/importer_host.h diff --git a/chrome/browser/automation/automation_provider.cc b/chrome/browser/automation/automation_provider.cc index 74d32e4..86fae50 100644 --- a/chrome/browser/automation/automation_provider.cc +++ b/chrome/browser/automation/automation_provider.cc @@ -16,14 +16,15 @@ #include "base/path_service.h" #include "base/process_util.h" #include "base/stl_util-inl.h" +#include "base/string_number_conversions.h" #include "base/string_util.h" +#include "base/synchronization/waitable_event.h" #include "base/task.h" #include "base/threading/thread.h" -#include "base/string_number_conversions.h" #include "base/utf_string_conversions.h" #include "base/values.h" -#include "base/synchronization/waitable_event.h" #include "chrome/app/chrome_command_ids.h" +#include "chrome/browser/autocomplete/autocomplete_edit.h" #include "chrome/browser/autofill/autofill_manager.h" #include "chrome/browser/automation/automation_autocomplete_edit_tracker.h" #include "chrome/browser/automation/automation_browser_tracker.h" @@ -34,7 +35,7 @@ #include "chrome/browser/automation/automation_tab_tracker.h" #include "chrome/browser/automation/automation_window_tracker.h" #include "chrome/browser/automation/extension_port_container.h" -#include "chrome/browser/autocomplete/autocomplete_edit.h" +#include "chrome/browser/automation/ui_controls.h" #include "chrome/browser/blocked_content_container.h" #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/bookmarks/bookmark_storage.h" @@ -44,8 +45,8 @@ #include "chrome/browser/browsing_data_remover.h" #include "chrome/browser/character_encoding.h" #include "chrome/browser/content_settings/host_content_settings_map.h" -#include "chrome/browser/dom_operation_notification_details.h" #include "chrome/browser/debugger/devtools_manager.h" +#include "chrome/browser/dom_operation_notification_details.h" #include "chrome/browser/download/download_item.h" #include "chrome/browser/download/download_shelf.h" #include "chrome/browser/download/save_package.h" @@ -54,20 +55,20 @@ #include "chrome/browser/extensions/extension_host.h" #include "chrome/browser/extensions/extension_install_ui.h" #include "chrome/browser/extensions/extension_message_service.h" +#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_tabs_module.h" #include "chrome/browser/extensions/extension_toolbar_model.h" -#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/user_script_master.h" -#include "chrome/browser/importer/importer.h" #include "chrome/browser/importer/importer_data_types.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/io_thread.h" #include "chrome/browser/net/url_request_mock_util.h" #include "chrome/browser/platform_util.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/printing/print_job.h" #include "chrome/browser/profiles/profile_manager.h" -#include "chrome/browser/ssl/ssl_manager.h" #include "chrome/browser/ssl/ssl_blocking_page.h" +#include "chrome/browser/ssl/ssl_manager.h" #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" #include "chrome/browser/ui/find_bar/find_bar.h" @@ -95,10 +96,9 @@ #include "content/browser/tab_contents/navigation_entry.h" #include "content/browser/tab_contents/tab_contents.h" #include "content/browser/tab_contents/tab_contents_view.h" -#include "net/proxy/proxy_service.h" #include "net/proxy/proxy_config_service_fixed.h" +#include "net/proxy/proxy_service.h" #include "net/url_request/url_request_context.h" -#include "chrome/browser/automation/ui_controls.h" #include "webkit/glue/password_form.h" #if defined(OS_WIN) diff --git a/chrome/browser/automation/testing_automation_provider.cc b/chrome/browser/automation/testing_automation_provider.cc index 10daa98..a9d212e 100644 --- a/chrome/browser/automation/testing_automation_provider.cc +++ b/chrome/browser/automation/testing_automation_provider.cc @@ -44,7 +44,7 @@ #include "chrome/browser/extensions/extension_host.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/history/top_sites.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/instant/instant_controller.h" #include "chrome/browser/notifications/balloon.h" #include "chrome/browser/notifications/balloon_collection.h" diff --git a/chrome/browser/extensions/extension_bookmarks_module.h b/chrome/browser/extensions/extension_bookmarks_module.h index a49a962..5d631e2 100644 --- a/chrome/browser/extensions/extension_bookmarks_module.h +++ b/chrome/browser/extensions/extension_bookmarks_module.h @@ -14,7 +14,7 @@ #include "base/singleton.h" #include "chrome/browser/bookmarks/bookmark_model_observer.h" #include "chrome/browser/extensions/extension_function.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/importer_list.h" #include "chrome/browser/ui/shell_dialogs.h" #include "content/common/notification_observer.h" diff --git a/chrome/browser/first_run/first_run.cc b/chrome/browser/first_run/first_run.cc index d9d7864..8791709 100644 --- a/chrome/browser/first_run/first_run.cc +++ b/chrome/browser/first_run/first_run.cc @@ -4,13 +4,13 @@ #include "chrome/browser/first_run/first_run.h" -#include "base/compiler_specific.h" #include "base/command_line.h" +#include "base/compiler_specific.h" #include "base/file_util.h" #include "base/path_service.h" #include "base/utf_string_conversions.h" #include "build/build_config.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/importer_progress_dialog.h" #include "chrome/browser/importer/importer_progress_observer.h" #include "chrome/browser/metrics/user_metrics.h" diff --git a/chrome/browser/first_run/first_run_win.cc b/chrome/browser/first_run/first_run_win.cc index 6b16485..0f6a0f5 100644 --- a/chrome/browser/first_run/first_run_win.cc +++ b/chrome/browser/first_run/first_run_win.cc @@ -24,7 +24,7 @@ #include "base/win/windows_version.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_updater.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/importer_progress_dialog.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/process_singleton.h" diff --git a/chrome/browser/importer/firefox2_importer.h b/chrome/browser/importer/firefox2_importer.h index 2c6412c..eb52f42 100644 --- a/chrome/browser/importer/firefox2_importer.h +++ b/chrome/browser/importer/firefox2_importer.h @@ -12,8 +12,8 @@ #include "base/compiler_specific.h" #include "base/file_path.h" #include "base/gtest_prod_util.h" -#include "chrome/browser/importer/importer.h" #include "chrome/browser/importer/importer_data_types.h" +#include "chrome/browser/importer/importer_host.h" class TemplateURL; diff --git a/chrome/browser/importer/firefox3_importer.h b/chrome/browser/importer/firefox3_importer.h index c5e3d8b..2202ce3 100644 --- a/chrome/browser/importer/firefox3_importer.h +++ b/chrome/browser/importer/firefox3_importer.h @@ -14,8 +14,8 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/file_path.h" -#include "chrome/browser/importer/importer.h" #include "chrome/browser/importer/importer_data_types.h" +#include "chrome/browser/importer/importer_host.h" #include "googleurl/src/gurl.h" struct sqlite3; diff --git a/chrome/browser/importer/ie_importer.h b/chrome/browser/importer/ie_importer.h index dc732c4..ce291b9 100644 --- a/chrome/browser/importer/ie_importer.h +++ b/chrome/browser/importer/ie_importer.h @@ -12,7 +12,7 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/gtest_prod_util.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" class IEImporter : public Importer { public: diff --git a/chrome/browser/importer/importer.cc b/chrome/browser/importer/importer.cc deleted file mode 100644 index 4851208..0000000 --- a/chrome/browser/importer/importer.cc +++ /dev/null @@ -1,589 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/importer/importer.h" - -#include "base/threading/thread.h" -#include "base/utf_string_conversions.h" -#include "base/values.h" -#include "chrome/browser/bookmarks/bookmark_model.h" -#include "chrome/browser/browser_list.h" -#include "chrome/browser/browser_process.h" -#include "chrome/browser/importer/firefox_profile_lock.h" -#include "chrome/browser/importer/importer_bridge.h" -#include "chrome/browser/importer/importer_progress_observer.h" -#include "chrome/browser/search_engines/template_url.h" -#include "chrome/browser/search_engines/template_url_model.h" -#include "chrome/browser/tabs/tab_strip_model.h" -#include "chrome/browser/ui/browser_dialogs.h" -#include "chrome/browser/ui/browser_navigator.h" -#include "chrome/browser/webdata/web_data_service.h" -#include "content/browser/browser_thread.h" -#include "content/browser/browsing_instance.h" -#include "content/browser/site_instance.h" -#include "content/common/notification_source.h" -#include "grit/generated_resources.h" -#include "skia/ext/image_operations.h" -#include "ui/base/l10n/l10n_util.h" -#include "ui/gfx/codec/png_codec.h" -#include "ui/gfx/favicon_size.h" -#include "webkit/glue/image_decoder.h" - -// TODO(port): Port these files. -#if defined(OS_WIN) -#include "ui/base/message_box_win.h" -#include "views/window/window.h" -#endif - -using webkit_glue::PasswordForm; - -// Importer. - -void Importer::Cancel() { cancelled_ = true; } - -Importer::Importer() - : cancelled_(false), - import_to_bookmark_bar_(false), - bookmark_bar_disabled_(false) { -} - -Importer::~Importer() { -} - -// static -bool Importer::ReencodeFavicon(const unsigned char* src_data, size_t src_len, - std::vector<unsigned char>* png_data) { - // Decode the favicon using WebKit's image decoder. - webkit_glue::ImageDecoder decoder(gfx::Size(kFavIconSize, kFavIconSize)); - SkBitmap decoded = decoder.Decode(src_data, src_len); - if (decoded.empty()) - return false; // Unable to decode. - - if (decoded.width() != kFavIconSize || decoded.height() != kFavIconSize) { - // The bitmap is not the correct size, re-sample. - int new_width = decoded.width(); - int new_height = decoded.height(); - calc_favicon_target_size(&new_width, &new_height); - decoded = skia::ImageOperations::Resize( - decoded, skia::ImageOperations::RESIZE_LANCZOS3, new_width, new_height); - } - - // Encode our bitmap as a PNG. - gfx::PNGCodec::EncodeBGRASkBitmap(decoded, false, png_data); - return true; -} - -// ImporterHost. - -ImporterHost::ImporterHost() - : profile_(NULL), - observer_(NULL), - task_(NULL), - importer_(NULL), - waiting_for_bookmarkbar_model_(false), - installed_bookmark_observer_(false), - is_source_readable_(true), - headless_(false), - parent_window_(NULL) { -} - -ImporterHost::~ImporterHost() { - if (NULL != importer_) - importer_->Release(); - - if (installed_bookmark_observer_) { - DCHECK(profile_); // Only way for waiting_for_bookmarkbar_model_ to be true - // is if we have a profile. - profile_->GetBookmarkModel()->RemoveObserver(this); - } -} - -void ImporterHost::ShowWarningDialog() { - if (headless_) { - OnLockViewEnd(false); - } else { - browser::ShowImportLockDialog(parent_window_, this); - } -} - -void ImporterHost::OnLockViewEnd(bool is_continue) { - if (is_continue) { - // User chose to continue, then we check the lock again to make - // sure that Firefox has been closed. Try to import the settings - // if successful. Otherwise, show a warning dialog. - firefox_lock_->Lock(); - if (firefox_lock_->HasAcquired()) { - is_source_readable_ = true; - InvokeTaskIfDone(); - } else { - ShowWarningDialog(); - } - } else { - // User chose to skip the import process. We should delete - // the task and notify the ImporterHost to finish. - delete task_; - task_ = NULL; - importer_ = NULL; - ImportEnded(); - } -} - -void ImporterHost::StartImportSettings( - const importer::ProfileInfo& profile_info, - Profile* target_profile, - uint16 items, - ProfileWriter* writer, - bool first_run) { - DCHECK(!profile_); // We really only support importing from one host at a - // time. - profile_ = target_profile; - // Preserves the observer and creates a task, since we do async import - // so that it doesn't block the UI. When the import is complete, observer - // will be notified. - writer_ = writer; - importer_ = ImporterList::CreateImporterByType(profile_info.browser_type); - // If we fail to create Importer, exit as we cannot do anything. - if (!importer_) { - ImportEnded(); - return; - } - - importer_->AddRef(); - - importer_->set_import_to_bookmark_bar(ShouldImportToBookmarkBar(first_run)); - importer_->set_bookmark_bar_disabled(first_run); - scoped_refptr<ImporterBridge> bridge( - new InProcessImporterBridge(writer_.get(), this)); - task_ = NewRunnableMethod(importer_, &Importer::StartImport, - profile_info, items, bridge); - - CheckForFirefoxLock(profile_info, items, first_run); - -#if defined(OS_WIN) - // For google toolbar import, we need the user to log in and store their GAIA - // credentials. - if (profile_info.browser_type == importer::GOOGLE_TOOLBAR5) { - if (!toolbar_importer_utils::IsGoogleGAIACookieInstalled()) { - ui::MessageBox( - NULL, - UTF16ToWide(l10n_util::GetStringUTF16( - IDS_IMPORTER_GOOGLE_LOGIN_TEXT)).c_str(), - L"", - MB_OK | MB_TOPMOST); - - GURL url("https://www.google.com/accounts/ServiceLogin"); - BrowserList::GetLastActive()->AddSelectedTabWithURL( - url, PageTransition::TYPED); - - MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( - this, &ImporterHost::OnLockViewEnd, false)); - - is_source_readable_ = false; - } - } -#endif - - CheckForLoadedModels(items); - AddRef(); - InvokeTaskIfDone(); -} - -void ImporterHost::Cancel() { - if (importer_) - importer_->Cancel(); -} - -void ImporterHost::SetObserver(importer::ImporterProgressObserver* observer) { - observer_ = observer; -} - -void ImporterHost::InvokeTaskIfDone() { - if (waiting_for_bookmarkbar_model_ || !registrar_.IsEmpty() || - !is_source_readable_) - return; - BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task_); -} - -void ImporterHost::Loaded(BookmarkModel* model) { - DCHECK(model->IsLoaded()); - model->RemoveObserver(this); - waiting_for_bookmarkbar_model_ = false; - installed_bookmark_observer_ = false; - - importer_->set_import_to_bookmark_bar(!model->HasBookmarks()); - InvokeTaskIfDone(); -} - -void ImporterHost::BookmarkModelBeingDeleted(BookmarkModel* model) { - installed_bookmark_observer_ = false; -} - -void ImporterHost::BookmarkModelChanged() { -} - -void ImporterHost::Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details) { - DCHECK(type == NotificationType::TEMPLATE_URL_MODEL_LOADED); - registrar_.RemoveAll(); - InvokeTaskIfDone(); -} - -void ImporterHost::ImportItemStarted(importer::ImportItem item) { - if (observer_) - observer_->ImportItemStarted(item); -} - -void ImporterHost::ImportItemEnded(importer::ImportItem item) { - if (observer_) - observer_->ImportItemEnded(item); -} - -void ImporterHost::ImportStarted() { - if (observer_) - observer_->ImportStarted(); -} - -void ImporterHost::ImportEnded() { - firefox_lock_.reset(); // Release the Firefox profile lock. - if (observer_) - observer_->ImportEnded(); - Release(); -} - -bool ImporterHost::ShouldImportToBookmarkBar(bool first_run) { - bool import_to_bookmark_bar = first_run; - if (profile_ && profile_->GetBookmarkModel()->IsLoaded()) { - import_to_bookmark_bar = (!profile_->GetBookmarkModel()->HasBookmarks()); - } - return import_to_bookmark_bar; -} - -void ImporterHost::CheckForFirefoxLock( - const importer::ProfileInfo& profile_info, uint16 items, bool first_run) { - if (profile_info.browser_type == importer::FIREFOX2 || - profile_info.browser_type == importer::FIREFOX3) { - DCHECK(!firefox_lock_.get()); - firefox_lock_.reset(new FirefoxProfileLock(profile_info.source_path)); - if (!firefox_lock_->HasAcquired()) { - // If fail to acquire the lock, we set the source unreadable and - // show a warning dialog, unless running without UI. - is_source_readable_ = false; - ShowWarningDialog(); - } - } -} - -void ImporterHost::CheckForLoadedModels(uint16 items) { - // BookmarkModel should be loaded before adding IE favorites. So we observe - // the BookmarkModel if needed, and start the task after it has been loaded. - if ((items & importer::FAVORITES) && !writer_->BookmarkModelIsLoaded()) { - profile_->GetBookmarkModel()->AddObserver(this); - waiting_for_bookmarkbar_model_ = true; - installed_bookmark_observer_ = true; - } - - // Observes the TemplateURLModel if needed to import search engines from the - // other browser. We also check to see if we're importing bookmarks because - // we can import bookmark keywords from Firefox as search engines. - if ((items & importer::SEARCH_ENGINES) || (items & importer::FAVORITES)) { - if (!writer_->TemplateURLModelIsLoaded()) { - TemplateURLModel* model = profile_->GetTemplateURLModel(); - registrar_.Add(this, NotificationType::TEMPLATE_URL_MODEL_LOADED, - Source<TemplateURLModel>(model)); - model->Load(); - } - } -} - -ExternalProcessImporterHost::ExternalProcessImporterHost() - : items_(0), - import_to_bookmark_bar_(false), - cancelled_(false), - import_process_launched_(false) { -} - -void ExternalProcessImporterHost::Loaded(BookmarkModel* model) { - DCHECK(model->IsLoaded()); - model->RemoveObserver(this); - waiting_for_bookmarkbar_model_ = false; - installed_bookmark_observer_ = false; - - // Because the import process is running externally, the decision whether - // to import to the bookmark bar must be stored here so that it can be - // passed to the importer when the import task is invoked. - import_to_bookmark_bar_ = (!model->HasBookmarks()); - InvokeTaskIfDone(); -} - -void ExternalProcessImporterHost::Cancel() { - cancelled_ = true; - if (import_process_launched_) - client_->Cancel(); - ImportEnded(); // Tells the observer that we're done, and releases us. -} - -void ExternalProcessImporterHost::StartImportSettings( - const importer::ProfileInfo& profile_info, - Profile* target_profile, - uint16 items, - ProfileWriter* writer, - bool first_run) { - DCHECK(!profile_); - profile_ = target_profile; - writer_ = writer; - profile_info_ = &profile_info; - items_ = items; - - ImporterHost::AddRef(); // Balanced in ImporterHost::ImportEnded. - - import_to_bookmark_bar_ = ShouldImportToBookmarkBar(first_run); - CheckForFirefoxLock(profile_info, items, first_run); - CheckForLoadedModels(items); - - InvokeTaskIfDone(); -} - -void ExternalProcessImporterHost::InvokeTaskIfDone() { - if (waiting_for_bookmarkbar_model_ || !registrar_.IsEmpty() || - !is_source_readable_ || cancelled_) - return; - - // The in-process half of the bridge which catches data from the IPC pipe - // and feeds it to the ProfileWriter. The external process half of the - // bridge lives in the external process -- see ProfileImportThread. - // The ExternalProcessImporterClient created in the next line owns this - // bridge, and will delete it. - InProcessImporterBridge* bridge = - new InProcessImporterBridge(writer_.get(), this); - client_ = new ExternalProcessImporterClient(this, *profile_info_, items_, - bridge, import_to_bookmark_bar_); - import_process_launched_ = true; - client_->Start(); -} - -ExternalProcessImporterClient::ExternalProcessImporterClient( - ExternalProcessImporterHost* importer_host, - const importer::ProfileInfo& profile_info, - int items, - InProcessImporterBridge* bridge, - bool import_to_bookmark_bar) - : bookmarks_options_(0), - total_bookmarks_count_(0), - total_history_rows_count_(0), - total_fav_icons_count_(0), - process_importer_host_(importer_host), - profile_import_process_host_(NULL), - profile_info_(profile_info), - items_(items), - import_to_bookmark_bar_(import_to_bookmark_bar), - bridge_(bridge), - cancelled_(false) { - bridge_->AddRef(); - process_importer_host_->ImportStarted(); -} - -ExternalProcessImporterClient::~ExternalProcessImporterClient() { - bridge_->Release(); -} - -void ExternalProcessImporterClient::Start() { - AddRef(); // balanced in Cleanup. - BrowserThread::ID thread_id; - CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableMethod(this, - &ExternalProcessImporterClient::StartProcessOnIOThread, - g_browser_process->resource_dispatcher_host(), thread_id)); -} - -void ExternalProcessImporterClient::StartProcessOnIOThread( - ResourceDispatcherHost* rdh, - BrowserThread::ID thread_id) { - profile_import_process_host_ = - new ProfileImportProcessHost(rdh, this, thread_id); - profile_import_process_host_->StartProfileImportProcess(profile_info_, - items_, import_to_bookmark_bar_); -} - -void ExternalProcessImporterClient::Cancel() { - if (cancelled_) - return; - - cancelled_ = true; - if (profile_import_process_host_) { - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableMethod(this, - &ExternalProcessImporterClient::CancelImportProcessOnIOThread)); - } - Release(); -} - -void ExternalProcessImporterClient::CancelImportProcessOnIOThread() { - profile_import_process_host_->CancelProfileImportProcess(); -} - -void ExternalProcessImporterClient::NotifyItemFinishedOnIOThread( - importer::ImportItem import_item) { - profile_import_process_host_->ReportImportItemFinished(import_item); -} - -void ExternalProcessImporterClient::OnProcessCrashed(int exit_code) { - if (cancelled_) - return; - - process_importer_host_->Cancel(); -} - -void ExternalProcessImporterClient::Cleanup() { - if (cancelled_) - return; - - if (process_importer_host_) - process_importer_host_->ImportEnded(); - Release(); -} - -void ExternalProcessImporterClient::OnImportStart() { - if (cancelled_) - return; - - bridge_->NotifyStarted(); -} - -void ExternalProcessImporterClient::OnImportFinished(bool succeeded, - std::string error_msg) { - if (cancelled_) - return; - - if (!succeeded) - LOG(WARNING) << "Import failed. Error: " << error_msg; - Cleanup(); -} - -void ExternalProcessImporterClient::OnImportItemStart(int item_data) { - if (cancelled_) - return; - - bridge_->NotifyItemStarted(static_cast<importer::ImportItem>(item_data)); -} - -void ExternalProcessImporterClient::OnImportItemFinished(int item_data) { - if (cancelled_) - return; - - importer::ImportItem import_item = - static_cast<importer::ImportItem>(item_data); - bridge_->NotifyItemEnded(import_item); - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableMethod(this, - &ExternalProcessImporterClient::NotifyItemFinishedOnIOThread, - import_item)); -} - -void ExternalProcessImporterClient::OnHistoryImportStart( - size_t total_history_rows_count) { - if (cancelled_) - return; - - total_history_rows_count_ = total_history_rows_count; - history_rows_.reserve(total_history_rows_count); -} - -void ExternalProcessImporterClient::OnHistoryImportGroup( - const std::vector<history::URLRow>& history_rows_group, - int visit_source) { - if (cancelled_) - return; - - history_rows_.insert(history_rows_.end(), history_rows_group.begin(), - history_rows_group.end()); - if (history_rows_.size() == total_history_rows_count_) - bridge_->SetHistoryItems(history_rows_, - static_cast<history::VisitSource>(visit_source)); -} - -void ExternalProcessImporterClient::OnHomePageImportReady( - const GURL& home_page) { - if (cancelled_) - return; - - bridge_->AddHomePage(home_page); -} - -void ExternalProcessImporterClient::OnBookmarksImportStart( - const std::wstring first_folder_name, - int options, size_t total_bookmarks_count) { - if (cancelled_) - return; - - bookmarks_first_folder_name_ = first_folder_name; - bookmarks_options_ = options; - total_bookmarks_count_ = total_bookmarks_count; - bookmarks_.reserve(total_bookmarks_count); -} - -void ExternalProcessImporterClient::OnBookmarksImportGroup( - const std::vector<ProfileWriter::BookmarkEntry>& bookmarks_group) { - if (cancelled_) - return; - - // Collect sets of bookmarks from importer process until we have reached - // total_bookmarks_count_: - bookmarks_.insert(bookmarks_.end(), bookmarks_group.begin(), - bookmarks_group.end()); - if (bookmarks_.size() == total_bookmarks_count_) { - bridge_->AddBookmarkEntries(bookmarks_, bookmarks_first_folder_name_, - bookmarks_options_); - } -} - -void ExternalProcessImporterClient::OnFavIconsImportStart( - size_t total_fav_icons_count) { - if (cancelled_) - return; - - total_fav_icons_count_ = total_fav_icons_count; - fav_icons_.reserve(total_fav_icons_count); -} - -void ExternalProcessImporterClient::OnFavIconsImportGroup( - const std::vector<history::ImportedFavIconUsage>& fav_icons_group) { - if (cancelled_) - return; - - fav_icons_.insert(fav_icons_.end(), fav_icons_group.begin(), - fav_icons_group.end()); - if (fav_icons_.size() == total_fav_icons_count_) - bridge_->SetFavIcons(fav_icons_); -} - -void ExternalProcessImporterClient::OnPasswordFormImportReady( - const webkit_glue::PasswordForm& form) { - if (cancelled_) - return; - - bridge_->SetPasswordForm(form); -} - -void ExternalProcessImporterClient::OnKeywordsImportReady( - const std::vector<TemplateURL>& template_urls, - int default_keyword_index, bool unique_on_host_and_path) { - if (cancelled_) - return; - - std::vector<TemplateURL*> template_url_vec; - template_url_vec.reserve(template_urls.size()); - std::vector<TemplateURL>::const_iterator iter; - for (iter = template_urls.begin(); - iter != template_urls.end(); - ++iter) { - template_url_vec.push_back(new TemplateURL(*iter)); - } - bridge_->SetKeywords(template_url_vec, default_keyword_index, - unique_on_host_and_path); -} diff --git a/chrome/browser/importer/importer.h b/chrome/browser/importer/importer.h deleted file mode 100644 index dfae6d8..0000000 --- a/chrome/browser/importer/importer.h +++ /dev/null @@ -1,437 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_IMPORTER_IMPORTER_H_ -#define CHROME_BROWSER_IMPORTER_IMPORTER_H_ -#pragma once - -#include <string> -#include <vector> - -#include "base/basictypes.h" -#include "base/compiler_specific.h" -#include "base/ref_counted.h" -#include "base/scoped_ptr.h" -#include "build/build_config.h" -#include "chrome/browser/bookmarks/base_bookmark_model_observer.h" -#include "chrome/browser/importer/importer_data_types.h" -#include "chrome/browser/importer/importer_list.h" -#include "chrome/browser/importer/profile_writer.h" -#include "chrome/browser/profile_import_process_host.h" -#include "content/common/notification_observer.h" -#include "content/common/notification_registrar.h" -#include "ui/gfx/native_widget_types.h" - -class ExternalProcessImporterClient; -class FirefoxProfileLock; -class GURL; -class Importer; -class ImporterBridge; -class InProcessImporterBridge; -class Profile; -class Task; -class TemplateURL; -struct IE7PasswordInfo; - -namespace history { -class URLRow; -struct ImportedFavIconUsage; -} - -namespace importer { -class ImporterProgressObserver; -} - -namespace webkit_glue { -struct PasswordForm; -} - -// This class hosts the importers. It enumerates profiles from other -// browsers dynamically, and controls the process of importing. When -// the import process is done, ImporterHost deletes itself. -class ImporterHost : public base::RefCountedThreadSafe<ImporterHost>, - public BaseBookmarkModelObserver, - public NotificationObserver { - public: - ImporterHost(); - - // ShowWarningDialog() asks user to close the application that is owning the - // lock. They can retry or skip the importing process. - void ShowWarningDialog(); - - // OnLockViewEnd() is called when user end the dialog by clicking a push - // button. |is_continue| is true when user clicked the "Continue" button. - void OnLockViewEnd(bool is_continue); - - // Starts the process of importing the settings and data depending on what - // the user selected. - // |profile_info| -- browser profile to import. - // |target_profile| -- profile to import into. - // |items| -- specifies which data to import (mask of ImportItems). - // |writer| -- called to actually write data back to the profile. - // |first_run| -- true if this method is being called during first run. - virtual void StartImportSettings(const importer::ProfileInfo& profile_info, - Profile* target_profile, - uint16 items, - ProfileWriter* writer, - bool first_run); - - // Cancel import. - virtual void Cancel(); - - // When in headless mode, the importer will not show the warning dialog and - // the outcome is as if the user had canceled the import operation. - void set_headless() { - headless_ = true; - } - - bool is_headless() const { - return headless_; - } - - void set_parent_window(gfx::NativeWindow parent_window) { - parent_window_ = parent_window; - } - - void SetObserver(importer::ImporterProgressObserver* observer); - - // A series of functions invoked at the start, during and end of the end - // of the import process. The middle functions are notifications that the - // harvesting of a particular source of data (specified by |item|) is under - // way. - virtual void ImportStarted(); - virtual void ImportItemStarted(importer::ImportItem item); - virtual void ImportItemEnded(importer::ImportItem item); - virtual void ImportEnded(); - - protected: - friend class base::RefCountedThreadSafe<ImporterHost>; - - ~ImporterHost(); - - // Returns true if importer should import to bookmark bar. - bool ShouldImportToBookmarkBar(bool first_run); - - // Make sure that Firefox isn't running, if import browser is Firefox. Show - // the user a dialog to notify that they need to close FF to continue. - // |profile_info| holds the browser type and source path. - // |items| is a mask of all ImportItems that are to be imported. - // |first_run| is true if this method is being called during first run. - void CheckForFirefoxLock(const importer::ProfileInfo& profile_info, - uint16 items, bool first_run); - - // Make sure BookmarkModel and TemplateURLModel are loaded before import - // process starts, if bookmarks and / or search engines are among the items - // which are to be imported. - void CheckForLoadedModels(uint16 items); - - // Profile we're importing from. - Profile* profile_; - - importer::ImporterProgressObserver* observer_; - - // TODO(mirandac): task_ and importer_ should be private. Can't just put - // them there without changing the order of construct/destruct, so do this - // after main CL has been committed. - // The task is the process of importing settings from other browsers. - Task* task_; - - // The importer used in the task; - Importer* importer_; - - // Writes data from the importer back to the profile. - scoped_refptr<ProfileWriter> writer_; - - // True if we're waiting for the model to finish loading. - bool waiting_for_bookmarkbar_model_; - - // Have we installed a listener on the bookmark model? - bool installed_bookmark_observer_; - - // True if source profile is readable. - bool is_source_readable_; - - // True if UI is not to be shown. - bool headless_; - - // Receives notification when the TemplateURLModel has loaded. - NotificationRegistrar registrar_; - - // Parent Window to use when showing any modal dialog boxes. - gfx::NativeWindow parent_window_; - - // Firefox profile lock. - scoped_ptr<FirefoxProfileLock> firefox_lock_; - - private: - // Launches the thread that starts the import task, unless bookmark or - // template model are not yet loaded. If load is not detected, this method - // will be called when the loading observer sees that model loading is - // complete. - virtual void InvokeTaskIfDone(); - - // BaseBookmarkModelObserver: - virtual void Loaded(BookmarkModel* model) OVERRIDE; - virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; - virtual void BookmarkModelChanged() OVERRIDE; - - // NotificationObserver: - // Called when TemplateURLModel has been loaded. - virtual void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details) OVERRIDE; - - DISALLOW_COPY_AND_ASSIGN(ImporterHost); -}; - -// This class manages the import process. It creates the in-process half of the -// importer bridge and the external process importer client. -class ExternalProcessImporterHost : public ImporterHost { - public: - ExternalProcessImporterHost(); - - // Called when the BookmarkModel has finished loading. Calls InvokeTaskIfDone - // to start importing. - virtual void Loaded(BookmarkModel* model); - - // Methods inherited from ImporterHost. - virtual void StartImportSettings(const importer::ProfileInfo& profile_info, - Profile* target_profile, - uint16 items, - ProfileWriter* writer, - bool first_run); - - virtual void Cancel(); - - protected: - // Launches the ExternalProcessImporterClient unless bookmark or template - // model are not yet loaded. If load is not detected, this method will be - // called when the loading observer sees that model loading is complete. - virtual void InvokeTaskIfDone(); - - private: - // Used to pass notifications from the browser side to the external process. - ExternalProcessImporterClient* client_; - - // Data for the external importer: ------------------------------------------ - // Information about a profile needed for importing. - const importer::ProfileInfo* profile_info_; - - // Mask of items to be imported (see importer::ImportItem). - uint16 items_; - - // Whether to import bookmarks to the bookmark bar. - bool import_to_bookmark_bar_; - - // True if the import process has been cancelled. - bool cancelled_; - - // True if the import process has been launched. This prevents race - // conditions on import cancel. - bool import_process_launched_; - - // End of external importer data -------------------------------------------- - - DISALLOW_COPY_AND_ASSIGN(ExternalProcessImporterHost); -}; - -// This class is the client for the ProfileImportProcessHost. It collects -// notifications from this process host and feeds data back to the importer -// host, who actually does the writing. -class ExternalProcessImporterClient - : public ProfileImportProcessHost::ImportProcessClient { - public: - ExternalProcessImporterClient(ExternalProcessImporterHost* importer_host, - const importer::ProfileInfo& profile_info, - int items, - InProcessImporterBridge* bridge, - bool import_to_bookmark_bar); - - ~ExternalProcessImporterClient(); - - // Launches the task to start the external process. - virtual void Start(); - - // Creates a new ProfileImportProcessHost, which launches the import process. - virtual void StartProcessOnIOThread(ResourceDispatcherHost* rdh, - BrowserThread::ID thread_id); - - // Called by the ExternalProcessImporterHost on import cancel. - virtual void Cancel(); - - // Cancel import process on IO thread. - void CancelImportProcessOnIOThread(); - - // Report item completely downloaded on IO thread. - void NotifyItemFinishedOnIOThread(importer::ImportItem import_item); - - // Cancel import on process crash. - virtual void OnProcessCrashed(int exit_code); - - // Notifies the importerhost that import has finished, and calls Release(). - void Cleanup(); - - // ProfileImportProcessHost messages ---------------------------------------- - // The following methods are called by ProfileImportProcessHost when the - // corresponding message has been received from the import process. - virtual void OnImportStart(); - virtual void OnImportFinished(bool succeeded, std::string error_msg); - virtual void OnImportItemStart(int item_data); - virtual void OnImportItemFinished(int item_data); - - // Called on first message received when importing history; gives total - // number of rows to be imported. - virtual void OnHistoryImportStart(size_t total_history_rows_count); - - // Called when a group of URLRows has been received. - // The source is passed with history::VisitSource type. - virtual void OnHistoryImportGroup( - const std::vector<history::URLRow> &history_rows_group, - int visit_source); - - // Called when the home page has been received. - virtual void OnHomePageImportReady(const GURL& home_page); - - // First message received when importing bookmarks. - // |first_folder_name| can be NULL. - // |options| is described in ProfileWriter::BookmarkOptions. - // |total_bookmarks_count| is the total number of bookmarks to be imported. - virtual void OnBookmarksImportStart( - const std::wstring first_folder_name, - int options, size_t total_bookmarks_count); - - // Called when a group of bookmarks has been received. - virtual void OnBookmarksImportGroup( - const std::vector<ProfileWriter::BookmarkEntry>& bookmarks_group); - - // First message received when importing favicons. |total_fav_icons_size| - // gives the total number of fav icons to be imported. - virtual void OnFavIconsImportStart(size_t total_fav_icons_count); - - // Called when a group of favicons has been received. - virtual void OnFavIconsImportGroup( - const std::vector<history::ImportedFavIconUsage>& fav_icons_group); - - // Called when the passwordform has been received. - virtual void OnPasswordFormImportReady( - const webkit_glue::PasswordForm& form); - - // Called when search engines have been received. - virtual void OnKeywordsImportReady( - const std::vector<TemplateURL>& template_urls, - int default_keyword_index, bool unique_on_host_and_path); - - // End ProfileImportProcessHost messages ------------------------------------ - - private: - // These variables store data being collected from the importer until the - // entire group has been collected and is ready to be written to the profile. - std::vector<history::URLRow> history_rows_; - std::vector<ProfileWriter::BookmarkEntry> bookmarks_; - std::vector<history::ImportedFavIconUsage> fav_icons_; - - // Usually some variation on IDS_BOOKMARK_GROUP_...; the name of the folder - // under which imported bookmarks will be placed. - std::wstring bookmarks_first_folder_name_; - - // Determines how bookmarks should be added (ProfileWriter::BookmarkOptions). - int bookmarks_options_; - - // Total number of bookmarks to import. - size_t total_bookmarks_count_; - - // Total number of history items to import. - size_t total_history_rows_count_; - - // Total number of fav icons to import. - size_t total_fav_icons_count_; - - // Notifications received from the ProfileImportProcessHost are passed back - // to process_importer_host_, which calls the ProfileWriter to record the - // import data. When the import process is done, process_importer_host_ - // deletes itself. - ExternalProcessImporterHost* process_importer_host_; - - // Handles sending messages to the external process. Deletes itself when - // the external process dies (see ChildProcessHost::OnChildDied). - ProfileImportProcessHost* profile_import_process_host_; - - // Data to be passed from the importer host to the external importer. - const importer::ProfileInfo& profile_info_; - int items_; - bool import_to_bookmark_bar_; - - // Takes import data coming over IPC and delivers it to be written by the - // ProfileWriter. Released by ExternalProcessImporterClient in its - // destructor. - InProcessImporterBridge* bridge_; - - // True if import process has been cancelled. - bool cancelled_; - - DISALLOW_COPY_AND_ASSIGN(ExternalProcessImporterClient); -}; - -// The base class of all importers. -class Importer : public base::RefCountedThreadSafe<Importer> { - public: - // All importers should implement this method by adding their - // import logic. And it will be run in file thread by ImporterHost. - // - // Since we do async import, the importer should invoke - // ImporterHost::Finished() to notify its host that import - // stuff have been finished. - virtual void StartImport(const importer::ProfileInfo& profile_info, - uint16 items, - ImporterBridge* bridge) = 0; - - // Cancels the import process. - virtual void Cancel(); - - void set_import_to_bookmark_bar(bool import_to_bookmark_bar) { - import_to_bookmark_bar_ = import_to_bookmark_bar; - } - - void set_bookmark_bar_disabled(bool bookmark_bar_disabled) { - bookmark_bar_disabled_ = bookmark_bar_disabled; - } - - bool bookmark_bar_disabled() { - return bookmark_bar_disabled_; - } - - bool cancelled() const { return cancelled_; } - - protected: - friend class base::RefCountedThreadSafe<Importer>; - - Importer(); - virtual ~Importer(); - - // Given raw image data, decodes the icon, re-sampling to the correct size as - // necessary, and re-encodes as PNG data in the given output vector. Returns - // true on success. - static bool ReencodeFavicon(const unsigned char* src_data, size_t src_len, - std::vector<unsigned char>* png_data); - - bool import_to_bookmark_bar() const { return import_to_bookmark_bar_; } - - scoped_refptr<ImporterBridge> bridge_; - - private: - // True if the caller cancels the import process. - bool cancelled_; - - // True if the importer is created in the first run UI. - bool import_to_bookmark_bar_; - - // Whether bookmark bar is disabled (not shown) for importer. This is set - // true during first run to prevent out of process bookmark importer from - // updating bookmark bar settings. - bool bookmark_bar_disabled_; - - DISALLOW_COPY_AND_ASSIGN(Importer); -}; - -#endif // CHROME_BROWSER_IMPORTER_IMPORTER_H_ diff --git a/chrome/browser/importer/importer_bridge.cc b/chrome/browser/importer/importer_bridge.cc index e1ddf2a1..03feb7c 100644 --- a/chrome/browser/importer/importer_bridge.cc +++ b/chrome/browser/importer/importer_bridge.cc @@ -1,16 +1,16 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/browser/importer/importer_bridge.h" #include "base/scoped_ptr.h" -#include "base/string_number_conversions.h" #include "base/string16.h" +#include "base/string_number_conversions.h" #include "base/utf_string_conversions.h" #include "base/values.h" #include "chrome/browser/history/history_types.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #if defined(OS_WIN) #include "chrome/browser/password_manager/ie7_password.h" #endif diff --git a/chrome/browser/importer/importer_host.cc b/chrome/browser/importer/importer_host.cc new file mode 100644 index 0000000..9c04299 --- /dev/null +++ b/chrome/browser/importer/importer_host.cc @@ -0,0 +1,589 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/importer/importer_host.h" + +#include "base/threading/thread.h" +#include "base/utf_string_conversions.h" +#include "base/values.h" +#include "chrome/browser/bookmarks/bookmark_model.h" +#include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/importer/firefox_profile_lock.h" +#include "chrome/browser/importer/importer_bridge.h" +#include "chrome/browser/importer/importer_progress_observer.h" +#include "chrome/browser/search_engines/template_url.h" +#include "chrome/browser/search_engines/template_url_model.h" +#include "chrome/browser/tabs/tab_strip_model.h" +#include "chrome/browser/ui/browser_dialogs.h" +#include "chrome/browser/ui/browser_navigator.h" +#include "chrome/browser/webdata/web_data_service.h" +#include "content/browser/browser_thread.h" +#include "content/browser/browsing_instance.h" +#include "content/browser/site_instance.h" +#include "content/common/notification_source.h" +#include "grit/generated_resources.h" +#include "skia/ext/image_operations.h" +#include "ui/base/l10n/l10n_util.h" +#include "ui/gfx/codec/png_codec.h" +#include "ui/gfx/favicon_size.h" +#include "webkit/glue/image_decoder.h" + +// TODO(port): Port these files. +#if defined(OS_WIN) +#include "ui/base/message_box_win.h" +#include "views/window/window.h" +#endif + +using webkit_glue::PasswordForm; + +// Importer. + +void Importer::Cancel() { cancelled_ = true; } + +Importer::Importer() + : cancelled_(false), + import_to_bookmark_bar_(false), + bookmark_bar_disabled_(false) { +} + +Importer::~Importer() { +} + +// static +bool Importer::ReencodeFavicon(const unsigned char* src_data, size_t src_len, + std::vector<unsigned char>* png_data) { + // Decode the favicon using WebKit's image decoder. + webkit_glue::ImageDecoder decoder(gfx::Size(kFavIconSize, kFavIconSize)); + SkBitmap decoded = decoder.Decode(src_data, src_len); + if (decoded.empty()) + return false; // Unable to decode. + + if (decoded.width() != kFavIconSize || decoded.height() != kFavIconSize) { + // The bitmap is not the correct size, re-sample. + int new_width = decoded.width(); + int new_height = decoded.height(); + calc_favicon_target_size(&new_width, &new_height); + decoded = skia::ImageOperations::Resize( + decoded, skia::ImageOperations::RESIZE_LANCZOS3, new_width, new_height); + } + + // Encode our bitmap as a PNG. + gfx::PNGCodec::EncodeBGRASkBitmap(decoded, false, png_data); + return true; +} + +// ImporterHost. + +ImporterHost::ImporterHost() + : profile_(NULL), + observer_(NULL), + task_(NULL), + importer_(NULL), + waiting_for_bookmarkbar_model_(false), + installed_bookmark_observer_(false), + is_source_readable_(true), + headless_(false), + parent_window_(NULL) { +} + +ImporterHost::~ImporterHost() { + if (NULL != importer_) + importer_->Release(); + + if (installed_bookmark_observer_) { + DCHECK(profile_); // Only way for waiting_for_bookmarkbar_model_ to be true + // is if we have a profile. + profile_->GetBookmarkModel()->RemoveObserver(this); + } +} + +void ImporterHost::ShowWarningDialog() { + if (headless_) { + OnLockViewEnd(false); + } else { + browser::ShowImportLockDialog(parent_window_, this); + } +} + +void ImporterHost::OnLockViewEnd(bool is_continue) { + if (is_continue) { + // User chose to continue, then we check the lock again to make + // sure that Firefox has been closed. Try to import the settings + // if successful. Otherwise, show a warning dialog. + firefox_lock_->Lock(); + if (firefox_lock_->HasAcquired()) { + is_source_readable_ = true; + InvokeTaskIfDone(); + } else { + ShowWarningDialog(); + } + } else { + // User chose to skip the import process. We should delete + // the task and notify the ImporterHost to finish. + delete task_; + task_ = NULL; + importer_ = NULL; + ImportEnded(); + } +} + +void ImporterHost::StartImportSettings( + const importer::ProfileInfo& profile_info, + Profile* target_profile, + uint16 items, + ProfileWriter* writer, + bool first_run) { + DCHECK(!profile_); // We really only support importing from one host at a + // time. + profile_ = target_profile; + // Preserves the observer and creates a task, since we do async import + // so that it doesn't block the UI. When the import is complete, observer + // will be notified. + writer_ = writer; + importer_ = ImporterList::CreateImporterByType(profile_info.browser_type); + // If we fail to create Importer, exit as we cannot do anything. + if (!importer_) { + ImportEnded(); + return; + } + + importer_->AddRef(); + + importer_->set_import_to_bookmark_bar(ShouldImportToBookmarkBar(first_run)); + importer_->set_bookmark_bar_disabled(first_run); + scoped_refptr<ImporterBridge> bridge( + new InProcessImporterBridge(writer_.get(), this)); + task_ = NewRunnableMethod(importer_, &Importer::StartImport, + profile_info, items, bridge); + + CheckForFirefoxLock(profile_info, items, first_run); + +#if defined(OS_WIN) + // For google toolbar import, we need the user to log in and store their GAIA + // credentials. + if (profile_info.browser_type == importer::GOOGLE_TOOLBAR5) { + if (!toolbar_importer_utils::IsGoogleGAIACookieInstalled()) { + ui::MessageBox( + NULL, + UTF16ToWide(l10n_util::GetStringUTF16( + IDS_IMPORTER_GOOGLE_LOGIN_TEXT)).c_str(), + L"", + MB_OK | MB_TOPMOST); + + GURL url("https://www.google.com/accounts/ServiceLogin"); + BrowserList::GetLastActive()->AddSelectedTabWithURL( + url, PageTransition::TYPED); + + MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( + this, &ImporterHost::OnLockViewEnd, false)); + + is_source_readable_ = false; + } + } +#endif + + CheckForLoadedModels(items); + AddRef(); + InvokeTaskIfDone(); +} + +void ImporterHost::Cancel() { + if (importer_) + importer_->Cancel(); +} + +void ImporterHost::SetObserver(importer::ImporterProgressObserver* observer) { + observer_ = observer; +} + +void ImporterHost::InvokeTaskIfDone() { + if (waiting_for_bookmarkbar_model_ || !registrar_.IsEmpty() || + !is_source_readable_) + return; + BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task_); +} + +void ImporterHost::Loaded(BookmarkModel* model) { + DCHECK(model->IsLoaded()); + model->RemoveObserver(this); + waiting_for_bookmarkbar_model_ = false; + installed_bookmark_observer_ = false; + + importer_->set_import_to_bookmark_bar(!model->HasBookmarks()); + InvokeTaskIfDone(); +} + +void ImporterHost::BookmarkModelBeingDeleted(BookmarkModel* model) { + installed_bookmark_observer_ = false; +} + +void ImporterHost::BookmarkModelChanged() { +} + +void ImporterHost::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + DCHECK(type == NotificationType::TEMPLATE_URL_MODEL_LOADED); + registrar_.RemoveAll(); + InvokeTaskIfDone(); +} + +void ImporterHost::ImportItemStarted(importer::ImportItem item) { + if (observer_) + observer_->ImportItemStarted(item); +} + +void ImporterHost::ImportItemEnded(importer::ImportItem item) { + if (observer_) + observer_->ImportItemEnded(item); +} + +void ImporterHost::ImportStarted() { + if (observer_) + observer_->ImportStarted(); +} + +void ImporterHost::ImportEnded() { + firefox_lock_.reset(); // Release the Firefox profile lock. + if (observer_) + observer_->ImportEnded(); + Release(); +} + +bool ImporterHost::ShouldImportToBookmarkBar(bool first_run) { + bool import_to_bookmark_bar = first_run; + if (profile_ && profile_->GetBookmarkModel()->IsLoaded()) { + import_to_bookmark_bar = (!profile_->GetBookmarkModel()->HasBookmarks()); + } + return import_to_bookmark_bar; +} + +void ImporterHost::CheckForFirefoxLock( + const importer::ProfileInfo& profile_info, uint16 items, bool first_run) { + if (profile_info.browser_type == importer::FIREFOX2 || + profile_info.browser_type == importer::FIREFOX3) { + DCHECK(!firefox_lock_.get()); + firefox_lock_.reset(new FirefoxProfileLock(profile_info.source_path)); + if (!firefox_lock_->HasAcquired()) { + // If fail to acquire the lock, we set the source unreadable and + // show a warning dialog, unless running without UI. + is_source_readable_ = false; + ShowWarningDialog(); + } + } +} + +void ImporterHost::CheckForLoadedModels(uint16 items) { + // BookmarkModel should be loaded before adding IE favorites. So we observe + // the BookmarkModel if needed, and start the task after it has been loaded. + if ((items & importer::FAVORITES) && !writer_->BookmarkModelIsLoaded()) { + profile_->GetBookmarkModel()->AddObserver(this); + waiting_for_bookmarkbar_model_ = true; + installed_bookmark_observer_ = true; + } + + // Observes the TemplateURLModel if needed to import search engines from the + // other browser. We also check to see if we're importing bookmarks because + // we can import bookmark keywords from Firefox as search engines. + if ((items & importer::SEARCH_ENGINES) || (items & importer::FAVORITES)) { + if (!writer_->TemplateURLModelIsLoaded()) { + TemplateURLModel* model = profile_->GetTemplateURLModel(); + registrar_.Add(this, NotificationType::TEMPLATE_URL_MODEL_LOADED, + Source<TemplateURLModel>(model)); + model->Load(); + } + } +} + +ExternalProcessImporterHost::ExternalProcessImporterHost() + : items_(0), + import_to_bookmark_bar_(false), + cancelled_(false), + import_process_launched_(false) { +} + +void ExternalProcessImporterHost::Loaded(BookmarkModel* model) { + DCHECK(model->IsLoaded()); + model->RemoveObserver(this); + waiting_for_bookmarkbar_model_ = false; + installed_bookmark_observer_ = false; + + // Because the import process is running externally, the decision whether + // to import to the bookmark bar must be stored here so that it can be + // passed to the importer when the import task is invoked. + import_to_bookmark_bar_ = (!model->HasBookmarks()); + InvokeTaskIfDone(); +} + +void ExternalProcessImporterHost::Cancel() { + cancelled_ = true; + if (import_process_launched_) + client_->Cancel(); + ImportEnded(); // Tells the observer that we're done, and releases us. +} + +void ExternalProcessImporterHost::StartImportSettings( + const importer::ProfileInfo& profile_info, + Profile* target_profile, + uint16 items, + ProfileWriter* writer, + bool first_run) { + DCHECK(!profile_); + profile_ = target_profile; + writer_ = writer; + profile_info_ = &profile_info; + items_ = items; + + ImporterHost::AddRef(); // Balanced in ImporterHost::ImportEnded. + + import_to_bookmark_bar_ = ShouldImportToBookmarkBar(first_run); + CheckForFirefoxLock(profile_info, items, first_run); + CheckForLoadedModels(items); + + InvokeTaskIfDone(); +} + +void ExternalProcessImporterHost::InvokeTaskIfDone() { + if (waiting_for_bookmarkbar_model_ || !registrar_.IsEmpty() || + !is_source_readable_ || cancelled_) + return; + + // The in-process half of the bridge which catches data from the IPC pipe + // and feeds it to the ProfileWriter. The external process half of the + // bridge lives in the external process -- see ProfileImportThread. + // The ExternalProcessImporterClient created in the next line owns this + // bridge, and will delete it. + InProcessImporterBridge* bridge = + new InProcessImporterBridge(writer_.get(), this); + client_ = new ExternalProcessImporterClient(this, *profile_info_, items_, + bridge, import_to_bookmark_bar_); + import_process_launched_ = true; + client_->Start(); +} + +ExternalProcessImporterClient::ExternalProcessImporterClient( + ExternalProcessImporterHost* importer_host, + const importer::ProfileInfo& profile_info, + int items, + InProcessImporterBridge* bridge, + bool import_to_bookmark_bar) + : bookmarks_options_(0), + total_bookmarks_count_(0), + total_history_rows_count_(0), + total_fav_icons_count_(0), + process_importer_host_(importer_host), + profile_import_process_host_(NULL), + profile_info_(profile_info), + items_(items), + import_to_bookmark_bar_(import_to_bookmark_bar), + bridge_(bridge), + cancelled_(false) { + bridge_->AddRef(); + process_importer_host_->ImportStarted(); +} + +ExternalProcessImporterClient::~ExternalProcessImporterClient() { + bridge_->Release(); +} + +void ExternalProcessImporterClient::Start() { + AddRef(); // balanced in Cleanup. + BrowserThread::ID thread_id; + CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_id)); + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + NewRunnableMethod(this, + &ExternalProcessImporterClient::StartProcessOnIOThread, + g_browser_process->resource_dispatcher_host(), thread_id)); +} + +void ExternalProcessImporterClient::StartProcessOnIOThread( + ResourceDispatcherHost* rdh, + BrowserThread::ID thread_id) { + profile_import_process_host_ = + new ProfileImportProcessHost(rdh, this, thread_id); + profile_import_process_host_->StartProfileImportProcess(profile_info_, + items_, import_to_bookmark_bar_); +} + +void ExternalProcessImporterClient::Cancel() { + if (cancelled_) + return; + + cancelled_ = true; + if (profile_import_process_host_) { + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + NewRunnableMethod(this, + &ExternalProcessImporterClient::CancelImportProcessOnIOThread)); + } + Release(); +} + +void ExternalProcessImporterClient::CancelImportProcessOnIOThread() { + profile_import_process_host_->CancelProfileImportProcess(); +} + +void ExternalProcessImporterClient::NotifyItemFinishedOnIOThread( + importer::ImportItem import_item) { + profile_import_process_host_->ReportImportItemFinished(import_item); +} + +void ExternalProcessImporterClient::OnProcessCrashed(int exit_code) { + if (cancelled_) + return; + + process_importer_host_->Cancel(); +} + +void ExternalProcessImporterClient::Cleanup() { + if (cancelled_) + return; + + if (process_importer_host_) + process_importer_host_->ImportEnded(); + Release(); +} + +void ExternalProcessImporterClient::OnImportStart() { + if (cancelled_) + return; + + bridge_->NotifyStarted(); +} + +void ExternalProcessImporterClient::OnImportFinished(bool succeeded, + std::string error_msg) { + if (cancelled_) + return; + + if (!succeeded) + LOG(WARNING) << "Import failed. Error: " << error_msg; + Cleanup(); +} + +void ExternalProcessImporterClient::OnImportItemStart(int item_data) { + if (cancelled_) + return; + + bridge_->NotifyItemStarted(static_cast<importer::ImportItem>(item_data)); +} + +void ExternalProcessImporterClient::OnImportItemFinished(int item_data) { + if (cancelled_) + return; + + importer::ImportItem import_item = + static_cast<importer::ImportItem>(item_data); + bridge_->NotifyItemEnded(import_item); + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + NewRunnableMethod(this, + &ExternalProcessImporterClient::NotifyItemFinishedOnIOThread, + import_item)); +} + +void ExternalProcessImporterClient::OnHistoryImportStart( + size_t total_history_rows_count) { + if (cancelled_) + return; + + total_history_rows_count_ = total_history_rows_count; + history_rows_.reserve(total_history_rows_count); +} + +void ExternalProcessImporterClient::OnHistoryImportGroup( + const std::vector<history::URLRow>& history_rows_group, + int visit_source) { + if (cancelled_) + return; + + history_rows_.insert(history_rows_.end(), history_rows_group.begin(), + history_rows_group.end()); + if (history_rows_.size() == total_history_rows_count_) + bridge_->SetHistoryItems(history_rows_, + static_cast<history::VisitSource>(visit_source)); +} + +void ExternalProcessImporterClient::OnHomePageImportReady( + const GURL& home_page) { + if (cancelled_) + return; + + bridge_->AddHomePage(home_page); +} + +void ExternalProcessImporterClient::OnBookmarksImportStart( + const std::wstring first_folder_name, + int options, size_t total_bookmarks_count) { + if (cancelled_) + return; + + bookmarks_first_folder_name_ = first_folder_name; + bookmarks_options_ = options; + total_bookmarks_count_ = total_bookmarks_count; + bookmarks_.reserve(total_bookmarks_count); +} + +void ExternalProcessImporterClient::OnBookmarksImportGroup( + const std::vector<ProfileWriter::BookmarkEntry>& bookmarks_group) { + if (cancelled_) + return; + + // Collect sets of bookmarks from importer process until we have reached + // total_bookmarks_count_: + bookmarks_.insert(bookmarks_.end(), bookmarks_group.begin(), + bookmarks_group.end()); + if (bookmarks_.size() == total_bookmarks_count_) { + bridge_->AddBookmarkEntries(bookmarks_, bookmarks_first_folder_name_, + bookmarks_options_); + } +} + +void ExternalProcessImporterClient::OnFavIconsImportStart( + size_t total_fav_icons_count) { + if (cancelled_) + return; + + total_fav_icons_count_ = total_fav_icons_count; + fav_icons_.reserve(total_fav_icons_count); +} + +void ExternalProcessImporterClient::OnFavIconsImportGroup( + const std::vector<history::ImportedFavIconUsage>& fav_icons_group) { + if (cancelled_) + return; + + fav_icons_.insert(fav_icons_.end(), fav_icons_group.begin(), + fav_icons_group.end()); + if (fav_icons_.size() == total_fav_icons_count_) + bridge_->SetFavIcons(fav_icons_); +} + +void ExternalProcessImporterClient::OnPasswordFormImportReady( + const webkit_glue::PasswordForm& form) { + if (cancelled_) + return; + + bridge_->SetPasswordForm(form); +} + +void ExternalProcessImporterClient::OnKeywordsImportReady( + const std::vector<TemplateURL>& template_urls, + int default_keyword_index, bool unique_on_host_and_path) { + if (cancelled_) + return; + + std::vector<TemplateURL*> template_url_vec; + template_url_vec.reserve(template_urls.size()); + std::vector<TemplateURL>::const_iterator iter; + for (iter = template_urls.begin(); + iter != template_urls.end(); + ++iter) { + template_url_vec.push_back(new TemplateURL(*iter)); + } + bridge_->SetKeywords(template_url_vec, default_keyword_index, + unique_on_host_and_path); +} diff --git a/chrome/browser/importer/importer_host.h b/chrome/browser/importer/importer_host.h new file mode 100644 index 0000000..0736514 --- /dev/null +++ b/chrome/browser/importer/importer_host.h @@ -0,0 +1,437 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_IMPORTER_IMPORTER_HOST_H_ +#define CHROME_BROWSER_IMPORTER_IMPORTER_HOST_H_ +#pragma once + +#include <string> +#include <vector> + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "base/ref_counted.h" +#include "base/scoped_ptr.h" +#include "build/build_config.h" +#include "chrome/browser/bookmarks/base_bookmark_model_observer.h" +#include "chrome/browser/importer/importer_data_types.h" +#include "chrome/browser/importer/importer_list.h" +#include "chrome/browser/importer/profile_writer.h" +#include "chrome/browser/profile_import_process_host.h" +#include "content/common/notification_observer.h" +#include "content/common/notification_registrar.h" +#include "ui/gfx/native_widget_types.h" + +class ExternalProcessImporterClient; +class FirefoxProfileLock; +class GURL; +class Importer; +class ImporterBridge; +class InProcessImporterBridge; +class Profile; +class Task; +class TemplateURL; +struct IE7PasswordInfo; + +namespace history { +class URLRow; +struct ImportedFavIconUsage; +} + +namespace importer { +class ImporterProgressObserver; +} + +namespace webkit_glue { +struct PasswordForm; +} + +// This class hosts the importers. It enumerates profiles from other +// browsers dynamically, and controls the process of importing. When +// the import process is done, ImporterHost deletes itself. +class ImporterHost : public base::RefCountedThreadSafe<ImporterHost>, + public BaseBookmarkModelObserver, + public NotificationObserver { + public: + ImporterHost(); + + // ShowWarningDialog() asks user to close the application that is owning the + // lock. They can retry or skip the importing process. + void ShowWarningDialog(); + + // OnLockViewEnd() is called when user end the dialog by clicking a push + // button. |is_continue| is true when user clicked the "Continue" button. + void OnLockViewEnd(bool is_continue); + + // Starts the process of importing the settings and data depending on what + // the user selected. + // |profile_info| -- browser profile to import. + // |target_profile| -- profile to import into. + // |items| -- specifies which data to import (mask of ImportItems). + // |writer| -- called to actually write data back to the profile. + // |first_run| -- true if this method is being called during first run. + virtual void StartImportSettings(const importer::ProfileInfo& profile_info, + Profile* target_profile, + uint16 items, + ProfileWriter* writer, + bool first_run); + + // Cancel import. + virtual void Cancel(); + + // When in headless mode, the importer will not show the warning dialog and + // the outcome is as if the user had canceled the import operation. + void set_headless() { + headless_ = true; + } + + bool is_headless() const { + return headless_; + } + + void set_parent_window(gfx::NativeWindow parent_window) { + parent_window_ = parent_window; + } + + void SetObserver(importer::ImporterProgressObserver* observer); + + // A series of functions invoked at the start, during and end of the end + // of the import process. The middle functions are notifications that the + // harvesting of a particular source of data (specified by |item|) is under + // way. + virtual void ImportStarted(); + virtual void ImportItemStarted(importer::ImportItem item); + virtual void ImportItemEnded(importer::ImportItem item); + virtual void ImportEnded(); + + protected: + friend class base::RefCountedThreadSafe<ImporterHost>; + + ~ImporterHost(); + + // Returns true if importer should import to bookmark bar. + bool ShouldImportToBookmarkBar(bool first_run); + + // Make sure that Firefox isn't running, if import browser is Firefox. Show + // the user a dialog to notify that they need to close FF to continue. + // |profile_info| holds the browser type and source path. + // |items| is a mask of all ImportItems that are to be imported. + // |first_run| is true if this method is being called during first run. + void CheckForFirefoxLock(const importer::ProfileInfo& profile_info, + uint16 items, bool first_run); + + // Make sure BookmarkModel and TemplateURLModel are loaded before import + // process starts, if bookmarks and / or search engines are among the items + // which are to be imported. + void CheckForLoadedModels(uint16 items); + + // Profile we're importing from. + Profile* profile_; + + importer::ImporterProgressObserver* observer_; + + // TODO(mirandac): task_ and importer_ should be private. Can't just put + // them there without changing the order of construct/destruct, so do this + // after main CL has been committed. + // The task is the process of importing settings from other browsers. + Task* task_; + + // The importer used in the task; + Importer* importer_; + + // Writes data from the importer back to the profile. + scoped_refptr<ProfileWriter> writer_; + + // True if we're waiting for the model to finish loading. + bool waiting_for_bookmarkbar_model_; + + // Have we installed a listener on the bookmark model? + bool installed_bookmark_observer_; + + // True if source profile is readable. + bool is_source_readable_; + + // True if UI is not to be shown. + bool headless_; + + // Receives notification when the TemplateURLModel has loaded. + NotificationRegistrar registrar_; + + // Parent Window to use when showing any modal dialog boxes. + gfx::NativeWindow parent_window_; + + // Firefox profile lock. + scoped_ptr<FirefoxProfileLock> firefox_lock_; + + private: + // Launches the thread that starts the import task, unless bookmark or + // template model are not yet loaded. If load is not detected, this method + // will be called when the loading observer sees that model loading is + // complete. + virtual void InvokeTaskIfDone(); + + // BaseBookmarkModelObserver: + virtual void Loaded(BookmarkModel* model) OVERRIDE; + virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; + virtual void BookmarkModelChanged() OVERRIDE; + + // NotificationObserver: + // Called when TemplateURLModel has been loaded. + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) OVERRIDE; + + DISALLOW_COPY_AND_ASSIGN(ImporterHost); +}; + +// This class manages the import process. It creates the in-process half of the +// importer bridge and the external process importer client. +class ExternalProcessImporterHost : public ImporterHost { + public: + ExternalProcessImporterHost(); + + // Called when the BookmarkModel has finished loading. Calls InvokeTaskIfDone + // to start importing. + virtual void Loaded(BookmarkModel* model); + + // Methods inherited from ImporterHost. + virtual void StartImportSettings(const importer::ProfileInfo& profile_info, + Profile* target_profile, + uint16 items, + ProfileWriter* writer, + bool first_run); + + virtual void Cancel(); + + protected: + // Launches the ExternalProcessImporterClient unless bookmark or template + // model are not yet loaded. If load is not detected, this method will be + // called when the loading observer sees that model loading is complete. + virtual void InvokeTaskIfDone(); + + private: + // Used to pass notifications from the browser side to the external process. + ExternalProcessImporterClient* client_; + + // Data for the external importer: ------------------------------------------ + // Information about a profile needed for importing. + const importer::ProfileInfo* profile_info_; + + // Mask of items to be imported (see importer::ImportItem). + uint16 items_; + + // Whether to import bookmarks to the bookmark bar. + bool import_to_bookmark_bar_; + + // True if the import process has been cancelled. + bool cancelled_; + + // True if the import process has been launched. This prevents race + // conditions on import cancel. + bool import_process_launched_; + + // End of external importer data -------------------------------------------- + + DISALLOW_COPY_AND_ASSIGN(ExternalProcessImporterHost); +}; + +// This class is the client for the ProfileImportProcessHost. It collects +// notifications from this process host and feeds data back to the importer +// host, who actually does the writing. +class ExternalProcessImporterClient + : public ProfileImportProcessHost::ImportProcessClient { + public: + ExternalProcessImporterClient(ExternalProcessImporterHost* importer_host, + const importer::ProfileInfo& profile_info, + int items, + InProcessImporterBridge* bridge, + bool import_to_bookmark_bar); + + ~ExternalProcessImporterClient(); + + // Launches the task to start the external process. + virtual void Start(); + + // Creates a new ProfileImportProcessHost, which launches the import process. + virtual void StartProcessOnIOThread(ResourceDispatcherHost* rdh, + BrowserThread::ID thread_id); + + // Called by the ExternalProcessImporterHost on import cancel. + virtual void Cancel(); + + // Cancel import process on IO thread. + void CancelImportProcessOnIOThread(); + + // Report item completely downloaded on IO thread. + void NotifyItemFinishedOnIOThread(importer::ImportItem import_item); + + // Cancel import on process crash. + virtual void OnProcessCrashed(int exit_code); + + // Notifies the importerhost that import has finished, and calls Release(). + void Cleanup(); + + // ProfileImportProcessHost messages ---------------------------------------- + // The following methods are called by ProfileImportProcessHost when the + // corresponding message has been received from the import process. + virtual void OnImportStart(); + virtual void OnImportFinished(bool succeeded, std::string error_msg); + virtual void OnImportItemStart(int item_data); + virtual void OnImportItemFinished(int item_data); + + // Called on first message received when importing history; gives total + // number of rows to be imported. + virtual void OnHistoryImportStart(size_t total_history_rows_count); + + // Called when a group of URLRows has been received. + // The source is passed with history::VisitSource type. + virtual void OnHistoryImportGroup( + const std::vector<history::URLRow> &history_rows_group, + int visit_source); + + // Called when the home page has been received. + virtual void OnHomePageImportReady(const GURL& home_page); + + // First message received when importing bookmarks. + // |first_folder_name| can be NULL. + // |options| is described in ProfileWriter::BookmarkOptions. + // |total_bookmarks_count| is the total number of bookmarks to be imported. + virtual void OnBookmarksImportStart( + const std::wstring first_folder_name, + int options, size_t total_bookmarks_count); + + // Called when a group of bookmarks has been received. + virtual void OnBookmarksImportGroup( + const std::vector<ProfileWriter::BookmarkEntry>& bookmarks_group); + + // First message received when importing favicons. |total_fav_icons_size| + // gives the total number of fav icons to be imported. + virtual void OnFavIconsImportStart(size_t total_fav_icons_count); + + // Called when a group of favicons has been received. + virtual void OnFavIconsImportGroup( + const std::vector<history::ImportedFavIconUsage>& fav_icons_group); + + // Called when the passwordform has been received. + virtual void OnPasswordFormImportReady( + const webkit_glue::PasswordForm& form); + + // Called when search engines have been received. + virtual void OnKeywordsImportReady( + const std::vector<TemplateURL>& template_urls, + int default_keyword_index, bool unique_on_host_and_path); + + // End ProfileImportProcessHost messages ------------------------------------ + + private: + // These variables store data being collected from the importer until the + // entire group has been collected and is ready to be written to the profile. + std::vector<history::URLRow> history_rows_; + std::vector<ProfileWriter::BookmarkEntry> bookmarks_; + std::vector<history::ImportedFavIconUsage> fav_icons_; + + // Usually some variation on IDS_BOOKMARK_GROUP_...; the name of the folder + // under which imported bookmarks will be placed. + std::wstring bookmarks_first_folder_name_; + + // Determines how bookmarks should be added (ProfileWriter::BookmarkOptions). + int bookmarks_options_; + + // Total number of bookmarks to import. + size_t total_bookmarks_count_; + + // Total number of history items to import. + size_t total_history_rows_count_; + + // Total number of fav icons to import. + size_t total_fav_icons_count_; + + // Notifications received from the ProfileImportProcessHost are passed back + // to process_importer_host_, which calls the ProfileWriter to record the + // import data. When the import process is done, process_importer_host_ + // deletes itself. + ExternalProcessImporterHost* process_importer_host_; + + // Handles sending messages to the external process. Deletes itself when + // the external process dies (see ChildProcessHost::OnChildDied). + ProfileImportProcessHost* profile_import_process_host_; + + // Data to be passed from the importer host to the external importer. + const importer::ProfileInfo& profile_info_; + int items_; + bool import_to_bookmark_bar_; + + // Takes import data coming over IPC and delivers it to be written by the + // ProfileWriter. Released by ExternalProcessImporterClient in its + // destructor. + InProcessImporterBridge* bridge_; + + // True if import process has been cancelled. + bool cancelled_; + + DISALLOW_COPY_AND_ASSIGN(ExternalProcessImporterClient); +}; + +// The base class of all importers. +class Importer : public base::RefCountedThreadSafe<Importer> { + public: + // All importers should implement this method by adding their + // import logic. And it will be run in file thread by ImporterHost. + // + // Since we do async import, the importer should invoke + // ImporterHost::Finished() to notify its host that import + // stuff have been finished. + virtual void StartImport(const importer::ProfileInfo& profile_info, + uint16 items, + ImporterBridge* bridge) = 0; + + // Cancels the import process. + virtual void Cancel(); + + void set_import_to_bookmark_bar(bool import_to_bookmark_bar) { + import_to_bookmark_bar_ = import_to_bookmark_bar; + } + + void set_bookmark_bar_disabled(bool bookmark_bar_disabled) { + bookmark_bar_disabled_ = bookmark_bar_disabled; + } + + bool bookmark_bar_disabled() { + return bookmark_bar_disabled_; + } + + bool cancelled() const { return cancelled_; } + + protected: + friend class base::RefCountedThreadSafe<Importer>; + + Importer(); + virtual ~Importer(); + + // Given raw image data, decodes the icon, re-sampling to the correct size as + // necessary, and re-encodes as PNG data in the given output vector. Returns + // true on success. + static bool ReencodeFavicon(const unsigned char* src_data, size_t src_len, + std::vector<unsigned char>* png_data); + + bool import_to_bookmark_bar() const { return import_to_bookmark_bar_; } + + scoped_refptr<ImporterBridge> bridge_; + + private: + // True if the caller cancels the import process. + bool cancelled_; + + // True if the importer is created in the first run UI. + bool import_to_bookmark_bar_; + + // Whether bookmark bar is disabled (not shown) for importer. This is set + // true during first run to prevent out of process bookmark importer from + // updating bookmark bar settings. + bool bookmark_bar_disabled_; + + DISALLOW_COPY_AND_ASSIGN(Importer); +}; + +#endif // CHROME_BROWSER_IMPORTER_IMPORTER_HOST_H_ diff --git a/chrome/browser/importer/importer_unittest.cc b/chrome/browser/importer/importer_unittest.cc index fe38155..c66f5e2 100644 --- a/chrome/browser/importer/importer_unittest.cc +++ b/chrome/browser/importer/importer_unittest.cc @@ -5,6 +5,7 @@ #include "build/build_config.h" #if defined(OS_WIN) +// The order of these includes is important. #include <windows.h> #include <unknwn.h> #include <intshcut.h> @@ -25,9 +26,9 @@ #include "base/utf_string_conversions.h" #include "base/values.h" #include "chrome/browser/history/history_types.h" -#include "chrome/browser/importer/importer.h" #include "chrome/browser/importer/importer_bridge.h" #include "chrome/browser/importer/importer_data_types.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/importer_progress_observer.h" #include "chrome/browser/search_engines/template_url.h" #include "chrome/common/chrome_paths.h" diff --git a/chrome/browser/importer/mork_reader.cc b/chrome/browser/importer/mork_reader.cc index ce1ffe8..916c06c 100644 --- a/chrome/browser/importer/mork_reader.cc +++ b/chrome/browser/importer/mork_reader.cc @@ -53,8 +53,8 @@ #include "base/values.h" #include "chrome/browser/history/history_types.h" #include "chrome/browser/importer/firefox_importer_utils.h" -#include "chrome/browser/importer/importer.h" #include "chrome/browser/importer/importer_bridge.h" +#include "chrome/browser/importer/importer_host.h" using base::Time; diff --git a/chrome/browser/importer/profile_writer.cc b/chrome/browser/importer/profile_writer.cc index 4c7bed8..99d9d49 100644 --- a/chrome/browser/importer/profile_writer.cc +++ b/chrome/browser/importer/profile_writer.cc @@ -8,7 +8,7 @@ #include "base/threading/thread.h" #include "base/utf_string_conversions.h" #include "chrome/browser/bookmarks/bookmark_model.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/password_manager/password_store.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" diff --git a/chrome/browser/importer/safari_importer.h b/chrome/browser/importer/safari_importer.h index a5150ae..4bbc1cc 100644 --- a/chrome/browser/importer/safari_importer.h +++ b/chrome/browser/importer/safari_importer.h @@ -14,8 +14,8 @@ #include "base/compiler_specific.h" #include "base/file_path.h" #include "base/gtest_prod_util.h" -#include "chrome/browser/importer/importer.h" #include "chrome/browser/importer/importer_data_types.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/common/sqlite_utils.h" #if __OBJC__ diff --git a/chrome/browser/importer/toolbar_importer.h b/chrome/browser/importer/toolbar_importer.h index 8e5e322..608fc12 100644 --- a/chrome/browser/importer/toolbar_importer.h +++ b/chrome/browser/importer/toolbar_importer.h @@ -16,8 +16,8 @@ #include "base/compiler_specific.h" #include "base/gtest_prod_util.h" #include "base/string16.h" -#include "chrome/browser/importer/importer.h" #include "chrome/browser/importer/importer_data_types.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/common/net/url_fetcher.h" class ImporterBridge; diff --git a/chrome/browser/importer/toolbar_importer_unittest.cc b/chrome/browser/importer/toolbar_importer_unittest.cc index 6b042de..d9be3fa 100644 --- a/chrome/browser/importer/toolbar_importer_unittest.cc +++ b/chrome/browser/importer/toolbar_importer_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -11,7 +11,7 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/browser/first_run/first_run.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/toolbar_importer.h" #include "chrome/common/libxml_utils.h" #include "googleurl/src/gurl.h" diff --git a/chrome/browser/ui/cocoa/importer/import_dialog_cocoa.h b/chrome/browser/ui/cocoa/importer/import_dialog_cocoa.h index 74564f0..2d20602c 100644 --- a/chrome/browser/ui/cocoa/importer/import_dialog_cocoa.h +++ b/chrome/browser/ui/cocoa/importer/import_dialog_cocoa.h @@ -9,7 +9,7 @@ #import <Cocoa/Cocoa.h> #include "base/scoped_nsobject.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" class Profile; class ImporterListObserverBridge; diff --git a/chrome/browser/ui/cocoa/importer/import_dialog_cocoa_unittest.mm b/chrome/browser/ui/cocoa/importer/import_dialog_cocoa_unittest.mm index 2a76dfb..e9cf7aa 100644 --- a/chrome/browser/ui/cocoa/importer/import_dialog_cocoa_unittest.mm +++ b/chrome/browser/ui/cocoa/importer/import_dialog_cocoa_unittest.mm @@ -5,7 +5,7 @@ #import <Cocoa/Cocoa.h> #include "base/scoped_nsobject.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #import "chrome/browser/ui/cocoa/browser_test_helper.h" #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" #import "chrome/browser/ui/cocoa/importer/import_dialog_cocoa.h" diff --git a/chrome/browser/ui/cocoa/importer/import_lock_dialog_cocoa.mm b/chrome/browser/ui/cocoa/importer/import_lock_dialog_cocoa.mm index ab53984..29b7ad0 100644 --- a/chrome/browser/ui/cocoa/importer/import_lock_dialog_cocoa.mm +++ b/chrome/browser/ui/cocoa/importer/import_lock_dialog_cocoa.mm @@ -6,7 +6,7 @@ #include "base/message_loop.h" #include "base/scoped_nsobject.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/ui/browser_dialogs.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" diff --git a/chrome/browser/ui/cocoa/importer/import_progress_dialog_cocoa.mm b/chrome/browser/ui/cocoa/importer/import_progress_dialog_cocoa.mm index 2dbc683f..2dda0f4 100644 --- a/chrome/browser/ui/cocoa/importer/import_progress_dialog_cocoa.mm +++ b/chrome/browser/ui/cocoa/importer/import_progress_dialog_cocoa.mm @@ -10,7 +10,7 @@ #import "base/scoped_nsobject.h" #import "base/sys_string_conversions.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/importer_observer.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" diff --git a/chrome/browser/ui/gtk/importer/import_dialog_gtk.cc b/chrome/browser/ui/gtk/importer/import_dialog_gtk.cc index 52feb82..afd46ba 100644 --- a/chrome/browser/ui/gtk/importer/import_dialog_gtk.cc +++ b/chrome/browser/ui/gtk/importer/import_dialog_gtk.cc @@ -8,6 +8,7 @@ #include "base/utf_string_conversions.h" #include "chrome/browser/importer/importer_data_types.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/importer_progress_dialog.h" #include "chrome/browser/ui/gtk/gtk_util.h" #include "grit/generated_resources.h" @@ -21,7 +22,7 @@ gboolean IsChecked(GtkWidget* widget) { return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); } -} // namespace +} // namespace // static void ImportDialogGtk::Show(GtkWindow* parent, Profile* profile, diff --git a/chrome/browser/ui/gtk/importer/import_dialog_gtk.h b/chrome/browser/ui/gtk/importer/import_dialog_gtk.h index e2e37f1..136b74e 100644 --- a/chrome/browser/ui/gtk/importer/import_dialog_gtk.h +++ b/chrome/browser/ui/gtk/importer/import_dialog_gtk.h @@ -8,18 +8,25 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" -#include "chrome/browser/importer/importer.h" +#include "base/ref_counted.h" +#include "chrome/browser/importer/importer_list.h" #include "chrome/browser/importer/importer_observer.h" #include "ui/base/gtk/gtk_signal.h" +class ImporterHost; class Profile; +typedef struct _GtkWidget GtkWidget; +typedef struct _GtkWindow GtkWindow; + +// ImportDialogGtk presents the dialog that allows the user to select what to +// import from other browsers. class ImportDialogGtk : public ImporterList::Observer, public ImporterObserver { public: - // Displays the import box to import data from another browser into |profile| - // |initial_state| is a bitmask of ImportItems. Each checkbox for the bits in - // is checked. + // Displays the import box to import data from another browser into |profile|. + // |initial_state| is a bitmask of importer::ImportItem. + // Each checkbox for the bits in |initial_state| is checked. static void Show(GtkWindow* parent, Profile* profile, uint16 initial_state); private: diff --git a/chrome/browser/ui/gtk/importer/import_lock_dialog_gtk.cc b/chrome/browser/ui/gtk/importer/import_lock_dialog_gtk.cc index ff607b0..005ca04 100644 --- a/chrome/browser/ui/gtk/importer/import_lock_dialog_gtk.cc +++ b/chrome/browser/ui/gtk/importer/import_lock_dialog_gtk.cc @@ -7,7 +7,7 @@ #include <gtk/gtk.h> #include "base/message_loop.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/ui/browser_dialogs.h" #include "chrome/browser/ui/gtk/gtk_util.h" #include "grit/chromium_strings.h" diff --git a/chrome/browser/ui/gtk/importer/import_progress_dialog_gtk.cc b/chrome/browser/ui/gtk/importer/import_progress_dialog_gtk.cc index f160f3a..5043f5f 100644 --- a/chrome/browser/ui/gtk/importer/import_progress_dialog_gtk.cc +++ b/chrome/browser/ui/gtk/importer/import_progress_dialog_gtk.cc @@ -9,7 +9,7 @@ #include <string> #include "base/utf_string_conversions.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/importer_observer.h" #include "chrome/browser/ui/gtk/gtk_util.h" #include "grit/chromium_strings.h" diff --git a/chrome/browser/ui/views/importer/import_dialog_view.h b/chrome/browser/ui/views/importer/import_dialog_view.h index 1fa678c..1a5ec93 100644 --- a/chrome/browser/ui/views/importer/import_dialog_view.h +++ b/chrome/browser/ui/views/importer/import_dialog_view.h @@ -12,7 +12,7 @@ #include "base/compiler_specific.h" #include "base/ref_counted.h" #include "base/string16.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/importer_observer.h" #include "ui/base/models/combobox_model.h" #include "views/controls/button/native_button.h" diff --git a/chrome/browser/ui/views/importer/import_lock_dialog_view.cc b/chrome/browser/ui/views/importer/import_lock_dialog_view.cc index 99195ba..99742b3 100644 --- a/chrome/browser/ui/views/importer/import_lock_dialog_view.cc +++ b/chrome/browser/ui/views/importer/import_lock_dialog_view.cc @@ -6,7 +6,7 @@ #include "base/message_loop.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "grit/locale_settings.h" diff --git a/chrome/browser/ui/views/importer/import_progress_dialog_view.cc b/chrome/browser/ui/views/importer/import_progress_dialog_view.cc index 5027ce4..36657e2 100644 --- a/chrome/browser/ui/views/importer/import_progress_dialog_view.cc +++ b/chrome/browser/ui/views/importer/import_progress_dialog_view.cc @@ -5,7 +5,7 @@ #include "chrome/browser/ui/views/importer/import_progress_dialog_view.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/importer_observer.h" #include "chrome/browser/importer/importer_progress_dialog.h" #include "grit/chromium_strings.h" diff --git a/chrome/browser/ui/webui/options/import_data_handler.cc b/chrome/browser/ui/webui/options/import_data_handler.cc index 139d25a..ba5f8d8 100644 --- a/chrome/browser/ui/webui/options/import_data_handler.cc +++ b/chrome/browser/ui/webui/options/import_data_handler.cc @@ -15,7 +15,7 @@ #include "base/threading/thread_restrictions.h" #include "base/utf_string_conversions.h" #include "base/values.h" -#include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/profiles/profile.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 0292994..7662e1d 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1221,12 +1221,12 @@ 'browser/importer/firefox_proxy_settings.h', 'browser/importer/ie_importer.cc', 'browser/importer/ie_importer.h', - 'browser/importer/importer.cc', - 'browser/importer/importer.h', 'browser/importer/importer_bridge.cc', 'browser/importer/importer_bridge.h', 'browser/importer/importer_data_types.cc', 'browser/importer/importer_data_types.h', + 'browser/importer/importer_host.cc', + 'browser/importer/importer_host.h', 'browser/importer/importer_list.cc', 'browser/importer/importer_list.h', 'browser/importer/importer_messages.cc', diff --git a/chrome/profile_import/profile_import_thread.cc b/chrome/profile_import/profile_import_thread.cc index 2c44614..14c66f6 100644 --- a/chrome/profile_import/profile_import_thread.cc +++ b/chrome/profile_import/profile_import_thread.cc @@ -7,9 +7,9 @@ #include <algorithm> #include "base/values.h" -#include "chrome/browser/importer/importer.h" #include "chrome/browser/importer/importer_bridge.h" #include "chrome/browser/importer/importer_data_types.h" +#include "chrome/browser/importer/importer_host.h" #include "chrome/browser/importer/importer_list.h" #include "chrome/browser/importer/importer_messages.h" #include "chrome/browser/search_engines/template_url.h" -- cgit v1.1