diff options
Diffstat (limited to 'chrome/browser')
20 files changed, 673 insertions, 478 deletions
diff --git a/chrome/browser/importer/firefox2_importer.cc b/chrome/browser/importer/firefox2_importer.cc index 3a38b2f..943cb9e 100644 --- a/chrome/browser/importer/firefox2_importer.cc +++ b/chrome/browser/importer/firefox2_importer.cc @@ -13,6 +13,7 @@ #include "base/string_util.h" #include "base/values.h" #include "chrome/browser/importer/firefox_importer_utils.h" +#include "chrome/browser/importer/importer_bridge.h" #include "chrome/browser/importer/mork_reader.h" #include "chrome/browser/importer/nss_decryptor.h" #include "chrome/browser/search_engines/template_url.h" @@ -35,18 +36,16 @@ Firefox2Importer::~Firefox2Importer() { } void Firefox2Importer::StartImport(ProfileInfo profile_info, - uint16 items, ProfileWriter* writer, - MessageLoop* delagate_loop, - ImporterHost* host) { - writer_ = writer; + uint16 items, + ImporterBridge* bridge) { + bridge_ = bridge; source_path_ = profile_info.source_path; app_path_ = profile_info.app_path; - importer_host_ = host; parsing_bookmarks_html_file_ = (profile_info.browser_type == BOOKMARKS_HTML); // The order here is important! - NotifyStarted(); + bridge_->NotifyStarted(); if ((items & HOME_PAGE) && !cancelled()) ImportHomepage(); // Doesn't have a UI item. @@ -54,27 +53,27 @@ void Firefox2Importer::StartImport(ProfileInfo profile_info, // will also import favicons and we store favicon for a URL only if the URL // exist in history or bookmarks. if ((items & HISTORY) && !cancelled()) { - NotifyItemStarted(HISTORY); + bridge_->NotifyItemStarted(HISTORY); ImportHistory(); - NotifyItemEnded(HISTORY); + bridge_->NotifyItemEnded(HISTORY); } if ((items & FAVORITES) && !cancelled()) { - NotifyItemStarted(FAVORITES); + bridge_->NotifyItemStarted(FAVORITES); ImportBookmarks(); - NotifyItemEnded(FAVORITES); + bridge_->NotifyItemEnded(FAVORITES); } if ((items & SEARCH_ENGINES) && !cancelled()) { - NotifyItemStarted(SEARCH_ENGINES); + bridge_->NotifyItemStarted(SEARCH_ENGINES); ImportSearchEngines(); - NotifyItemEnded(SEARCH_ENGINES); + bridge_->NotifyItemEnded(SEARCH_ENGINES); } if ((items & PASSWORDS) && !cancelled()) { - NotifyItemStarted(PASSWORDS); + bridge_->NotifyItemStarted(PASSWORDS); ImportPasswords(); - NotifyItemEnded(PASSWORDS); + bridge_->NotifyItemEnded(PASSWORDS); } - NotifyEnded(); + bridge_->NotifyEnded(); } // static @@ -264,21 +263,19 @@ void Firefox2Importer::ImportBookmarks() { // Write data into profile. if (!bookmarks.empty() && !cancelled()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddBookmarkEntry, bookmarks, - first_folder_name, - import_to_bookmark_bar() ? ProfileWriter::IMPORT_TO_BOOKMARK_BAR : 0)); + int options = 0; + if (import_to_bookmark_bar()) + options = ProfileWriter::IMPORT_TO_BOOKMARK_BAR; + bridge_->AddBookmarkEntries(bookmarks, first_folder_name, options); } if (!parsing_bookmarks_html_file_ && !template_urls.empty() && !cancelled()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddKeywords, template_urls, -1, false)); + bridge_->SetKeywords(template_urls, -1, false); } else { STLDeleteContainerPointers(template_urls.begin(), template_urls.end()); } if (!favicons.empty()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddFavicons, favicons)); + bridge_->SetFavIcons(favicons); } } @@ -305,8 +302,7 @@ void Firefox2Importer::ImportPasswords() { if (!cancelled()) { for (size_t i = 0; i < forms.size(); ++i) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddPasswordForm, forms[i])); + bridge_->SetPasswordForm(forms[i]); } } } @@ -314,7 +310,7 @@ void Firefox2Importer::ImportPasswords() { void Firefox2Importer::ImportHistory() { std::wstring file = source_path_; file_util::AppendToPath(&file, L"history.dat"); - ImportHistoryFromFirefox2(file, main_loop_, writer_); + ImportHistoryFromFirefox2(file, bridge_); } void Firefox2Importer::ImportSearchEngines() { @@ -323,17 +319,16 @@ void Firefox2Importer::ImportSearchEngines() { std::vector<TemplateURL*> search_engines; ParseSearchEnginesFromXMLFiles(files, &search_engines); - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddKeywords, search_engines, - GetFirefoxDefaultSearchEngineIndex(search_engines, source_path_), - true)); + + int default_index = + GetFirefoxDefaultSearchEngineIndex(search_engines, source_path_); + bridge_->SetKeywords(search_engines, default_index, true); } void Firefox2Importer::ImportHomepage() { - GURL homepage = GetHomepage(source_path_); - if (homepage.is_valid() && !IsDefaultHomepage(homepage, app_path_)) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddHomepage, homepage)); + GURL home_page = GetHomepage(source_path_); + if (home_page.is_valid() && !IsDefaultHomepage(home_page, app_path_)) { + bridge_->AddHomePage(home_page); } } diff --git a/chrome/browser/importer/firefox2_importer.h b/chrome/browser/importer/firefox2_importer.h index 8078c66..d9aa263 100644 --- a/chrome/browser/importer/firefox2_importer.h +++ b/chrome/browser/importer/firefox2_importer.h @@ -18,9 +18,7 @@ class Firefox2Importer : public Importer { // Importer methods. virtual void StartImport(ProfileInfo profile_info, uint16 items, - ProfileWriter* writer, - MessageLoop* delagate_loop, - ImporterHost* host); + ImporterBridge* bridge); // Loads the default bookmarks in the Firefox installed at |firefox_app_path|, // and stores their locations in |urls|. diff --git a/chrome/browser/importer/firefox3_importer.cc b/chrome/browser/importer/firefox3_importer.cc index a509f55..10d2855 100644 --- a/chrome/browser/importer/firefox3_importer.cc +++ b/chrome/browser/importer/firefox3_importer.cc @@ -14,6 +14,7 @@ #include "base/string_util.h" #include "chrome/browser/importer/firefox2_importer.h" #include "chrome/browser/importer/firefox_importer_utils.h" +#include "chrome/browser/importer/importer_bridge.h" #include "chrome/browser/importer/nss_decryptor.h" #include "chrome/browser/search_engines/template_url.h" #include "chrome/common/time_format.h" @@ -25,17 +26,15 @@ using base::Time; using webkit_glue::PasswordForm; void Firefox3Importer::StartImport(ProfileInfo profile_info, - uint16 items, ProfileWriter* writer, - MessageLoop* delagate_loop, - ImporterHost* host) { - writer_ = writer; + uint16 items, + ImporterBridge* bridge) { + bridge_ = bridge; source_path_ = profile_info.source_path; app_path_ = profile_info.app_path; - importer_host_ = host; // The order here is important! - NotifyStarted(); + bridge_->NotifyStarted(); if ((items & HOME_PAGE) && !cancelled()) ImportHomepage(); // Doesn't have a UI item. @@ -43,27 +42,27 @@ void Firefox3Importer::StartImport(ProfileInfo profile_info, // will also import favicons and we store favicon for a URL only if the URL // exist in history or bookmarks. if ((items & HISTORY) && !cancelled()) { - NotifyItemStarted(HISTORY); + bridge_->NotifyItemStarted(HISTORY); ImportHistory(); - NotifyItemEnded(HISTORY); + bridge_->NotifyItemEnded(HISTORY); } if ((items & FAVORITES) && !cancelled()) { - NotifyItemStarted(FAVORITES); + bridge_->NotifyItemStarted(FAVORITES); ImportBookmarks(); - NotifyItemEnded(FAVORITES); + bridge_->NotifyItemEnded(FAVORITES); } if ((items & SEARCH_ENGINES) && !cancelled()) { - NotifyItemStarted(SEARCH_ENGINES); + bridge_->NotifyItemStarted(SEARCH_ENGINES); ImportSearchEngines(); - NotifyItemEnded(SEARCH_ENGINES); + bridge_->NotifyItemEnded(SEARCH_ENGINES); } if ((items & PASSWORDS) && !cancelled()) { - NotifyItemStarted(PASSWORDS); + bridge_->NotifyItemStarted(PASSWORDS); ImportPasswords(); - NotifyItemEnded(PASSWORDS); + bridge_->NotifyItemEnded(PASSWORDS); } - NotifyEnded(); + bridge_->NotifyEnded(); } void Firefox3Importer::ImportHistory() { @@ -110,8 +109,7 @@ void Firefox3Importer::ImportHistory() { rows.push_back(row); } if (!rows.empty() && !cancelled()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddHistoryPage, rows)); + bridge_->SetHistoryItems(rows); } } @@ -244,22 +242,22 @@ void Firefox3Importer::ImportBookmarks() { // Write into profile. if (!bookmarks.empty() && !cancelled()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddBookmarkEntry, bookmarks, - l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_FIREFOX), - import_to_bookmark_bar() ? ProfileWriter::IMPORT_TO_BOOKMARK_BAR : 0)); + const std::wstring& first_folder_name = + l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_FIREFOX); + int options = 0; + if (import_to_bookmark_bar()) + options = ProfileWriter::IMPORT_TO_BOOKMARK_BAR; + bridge_->AddBookmarkEntries(bookmarks, first_folder_name, options); } if (!template_urls.empty() && !cancelled()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddKeywords, template_urls, -1, false)); + bridge_->SetKeywords(template_urls, -1, false); } else { STLDeleteContainerPointers(template_urls.begin(), template_urls.end()); } if (!favicon_map.empty() && !cancelled()) { std::vector<history::ImportedFavIconUsage> favicons; LoadFavicons(db.get(), favicon_map, &favicons); - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddFavicons, favicons)); + bridge_->SetFavIcons(favicons); } } @@ -289,8 +287,7 @@ void Firefox3Importer::ImportPasswords() { if (!cancelled()) { for (size_t i = 0; i < forms.size(); ++i) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddPasswordForm, forms[i])); + bridge_->SetPasswordForm(forms[i]); } } } @@ -301,16 +298,15 @@ void Firefox3Importer::ImportSearchEngines() { std::vector<TemplateURL*> search_engines; ParseSearchEnginesFromXMLFiles(files, &search_engines); - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddKeywords, search_engines, - GetFirefoxDefaultSearchEngineIndex(search_engines, source_path_), true)); + int default_index = + GetFirefoxDefaultSearchEngineIndex(search_engines, source_path_); + bridge_->SetKeywords(search_engines, default_index, true); } void Firefox3Importer::ImportHomepage() { - GURL homepage = GetHomepage(source_path_); - if (homepage.is_valid() && !IsDefaultHomepage(homepage, app_path_)) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddHomepage, homepage)); + GURL home_page = GetHomepage(source_path_); + if (home_page.is_valid() && !IsDefaultHomepage(home_page, app_path_)) { + bridge_->AddHomePage(home_page); } } diff --git a/chrome/browser/importer/firefox3_importer.h b/chrome/browser/importer/firefox3_importer.h index 9daa6b53a..926272b 100644 --- a/chrome/browser/importer/firefox3_importer.h +++ b/chrome/browser/importer/firefox3_importer.h @@ -26,9 +26,7 @@ class Firefox3Importer : public Importer { // Importer methods. virtual void StartImport(ProfileInfo profile_info, uint16 items, - ProfileWriter* writer, - MessageLoop* delagate_loop_, - ImporterHost* host); + ImporterBridge* bridge); private: typedef std::map<int64, std::set<GURL> > FaviconMap; @@ -76,7 +74,6 @@ class Firefox3Importer : public Importer { const FaviconMap& favicon_map, std::vector<history::ImportedFavIconUsage>* favicons); - ProfileWriter* writer_; std::wstring source_path_; std::wstring app_path_; diff --git a/chrome/browser/importer/ie_importer.cc b/chrome/browser/importer/ie_importer.cc index 9a2606b..8c11064 100644 --- a/chrome/browser/importer/ie_importer.cc +++ b/chrome/browser/importer/ie_importer.cc @@ -25,6 +25,7 @@ #include "base/time.h" #include "base/win_util.h" #include "chrome/browser/bookmarks/bookmark_model.h" +#include "chrome/browser/importer/importer_bridge.h" #include "chrome/browser/password_manager/ie7_password.h" #include "chrome/browser/search_engines/template_url_model.h" #include "chrome/common/time_format.h" @@ -64,14 +65,11 @@ const GUID IEImporter::kUnittestGUID = { 0xa79029d6, 0x753e, 0x4e27, void IEImporter::StartImport(ProfileInfo profile_info, uint16 items, - ProfileWriter* writer, - MessageLoop* delagate_loop, - ImporterHost* host) { - writer_ = writer; + ImporterBridge* bridge) { + bridge_ = bridge; source_path_ = profile_info.source_path; - importer_host_ = host; - NotifyStarted(); + bridge_->NotifyStarted(); // Some IE settings (such as Protected Storage) are obtained via COM APIs. win_util::ScopedCOMInitializer com_initializer; @@ -80,30 +78,30 @@ void IEImporter::StartImport(ProfileInfo profile_info, ImportHomepage(); // Doesn't have a UI item. // The order here is important! if ((items & HISTORY) && !cancelled()) { - NotifyItemStarted(HISTORY); + bridge_->NotifyItemStarted(HISTORY); ImportHistory(); - NotifyItemEnded(HISTORY); + bridge_->NotifyItemEnded(HISTORY); } if ((items & FAVORITES) && !cancelled()) { - NotifyItemStarted(FAVORITES); + bridge_->NotifyItemStarted(FAVORITES); ImportFavorites(); - NotifyItemEnded(FAVORITES); + bridge_->NotifyItemEnded(FAVORITES); } if ((items & SEARCH_ENGINES) && !cancelled()) { - NotifyItemStarted(SEARCH_ENGINES); + bridge_->NotifyItemStarted(SEARCH_ENGINES); ImportSearchEngines(); - NotifyItemEnded(SEARCH_ENGINES); + bridge_->NotifyItemEnded(SEARCH_ENGINES); } if ((items & PASSWORDS) && !cancelled()) { - NotifyItemStarted(PASSWORDS); + bridge_->NotifyItemStarted(PASSWORDS); // Always import IE6 passwords. ImportPasswordsIE6(); if (CurrentIEVersion() >= 7) ImportPasswordsIE7(); - NotifyItemEnded(PASSWORDS); + bridge_->NotifyItemEnded(PASSWORDS); } - NotifyEnded(); + bridge_->NotifyEnded(); } void IEImporter::ImportFavorites() { @@ -117,10 +115,12 @@ void IEImporter::ImportFavorites() { ParseFavoritesFolder(info, &bookmarks); if (!bookmarks.empty() && !cancelled()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddBookmarkEntry, bookmarks, - l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_IE), - import_to_bookmark_bar() ? ProfileWriter::IMPORT_TO_BOOKMARK_BAR : 0)); + const std::wstring& first_folder_name = + l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_IE); + int options = 0; + if (import_to_bookmark_bar()) + options = ProfileWriter::IMPORT_TO_BOOKMARK_BAR; + bridge_->AddBookmarkEntries(bookmarks, first_folder_name, options); } } @@ -239,8 +239,7 @@ void IEImporter::ImportPasswordsIE6() { } } - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddPasswordForm, form)); + bridge_->SetPasswordForm(form); } } @@ -267,10 +266,8 @@ void IEImporter::ImportPasswordsIE7() { password_info.url_hash = reg_iterator.Name(); password_info.encrypted_data = value; password_info.date_created = Time::Now(); - main_loop_->PostTask(FROM_HERE, - NewRunnableMethod(writer_, - &ProfileWriter::AddIE7PasswordInfo, - password_info)); + + bridge_->AddIE7PasswordInfo(password_info); } } @@ -331,8 +328,7 @@ void IEImporter::ImportHistory() { } if (!rows.empty() && !cancelled()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddHistoryPage, rows)); + bridge_->SetHistoryItems(rows); } } } @@ -409,9 +405,7 @@ void IEImporter::ImportSearchEngines() { static_cast<int>(search_engines.size()) - 1; } } - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddKeywords, search_engines, default_search_engine_index, - true)); + bridge_->SetKeywords(search_engines, default_search_engine_index, true); } void IEImporter::ImportHomepage() { @@ -438,8 +432,7 @@ void IEImporter::ImportHomepage() { return; } - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddHomepage, homepage)); + bridge_->AddHomePage(homepage); } bool IEImporter::GetFavoritesInfo(IEImporter::FavoritesInfo *info) { diff --git a/chrome/browser/importer/ie_importer.h b/chrome/browser/importer/ie_importer.h index 26e133c..a43b660 100644 --- a/chrome/browser/importer/ie_importer.h +++ b/chrome/browser/importer/ie_importer.h @@ -15,9 +15,7 @@ class IEImporter : public Importer { // Importer methods. virtual void StartImport(ProfileInfo browser_info, uint16 items, - ProfileWriter* writer, - MessageLoop* delagate_loop, - ImporterHost* host); + ImporterBridge* bridge); private: FRIEND_TEST(ImporterTest, IEImporter); @@ -81,7 +79,7 @@ class IEImporter : public Importer { // providing a fake source. std::wstring source_path_; - DISALLOW_EVIL_CONSTRUCTORS(IEImporter); + DISALLOW_COPY_AND_ASSIGN(IEImporter); }; #endif // CHROME_BROWSER_IMPORTER_IE_IMPORTER_H_ diff --git a/chrome/browser/importer/importer.cc b/chrome/browser/importer/importer.cc index 29911f6..6f36d19 100644 --- a/chrome/browser/importer/importer.cc +++ b/chrome/browser/importer/importer.cc @@ -19,11 +19,8 @@ #include "chrome/browser/browser_process.h" #include "chrome/browser/favicon_service.h" #include "chrome/browser/first_run.h" -#include "chrome/browser/importer/firefox2_importer.h" -#include "chrome/browser/importer/firefox3_importer.h" -#include "chrome/browser/importer/firefox_importer_utils.h" #include "chrome/browser/importer/firefox_profile_lock.h" -#include "chrome/browser/importer/toolbar_importer.h" +#include "chrome/browser/importer/importer_bridge.h" #include "chrome/browser/renderer_host/site_instance.h" #include "chrome/browser/search_engines/template_url_model.h" #include "chrome/browser/shell_integration.h" @@ -38,22 +35,13 @@ // TODO(port): Port these files. #if defined(OS_WIN) +#include "app/win_util.h" #include "chrome/browser/views/importer_lock_view.h" #include "views/window/window.h" #elif defined(OS_LINUX) #include "chrome/browser/gtk/import_lock_dialog_gtk.h" #endif -#if defined(OS_WIN) -#include "app/win_util.h" -#include "chrome/browser/importer/ie_importer.h" -#include "chrome/browser/password_manager/ie7_password.h" -#endif -#if defined(OS_MACOSX) -#include "base/mac_util.h" -#include "chrome/browser/importer/safari_importer.h" -#endif - using webkit_glue::PasswordForm; // ProfileWriter. @@ -379,33 +367,10 @@ bool ProfileWriter::DoesBookmarkExist( // Importer. Importer::Importer() - : main_loop_(MessageLoop::current()), - delagate_loop_(NULL), - importer_host_(NULL), - cancelled_(false), + : cancelled_(false), import_to_bookmark_bar_(false) { } -void Importer::NotifyItemStarted(ImportItem item) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(importer_host_, - &ImporterHost::ImportItemStarted, item)); -} - -void Importer::NotifyItemEnded(ImportItem item) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(importer_host_, - &ImporterHost::ImportItemEnded, item)); -} - -void Importer::NotifyStarted() { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(importer_host_, - &ImporterHost::ImportStarted)); -} - -void Importer::NotifyEnded() { - main_loop_->PostTask(FROM_HERE, - NewRunnableMethod(importer_host_, &ImporterHost::ImportEnded)); -} - // static bool Importer::ReencodeFavicon(const unsigned char* src_data, size_t src_len, std::vector<unsigned char>* png_data) { @@ -440,7 +405,7 @@ ImporterHost::ImporterHost() is_source_readable_(true), headless_(false), parent_window_(NULL) { - DetectSourceProfiles(); + importer_list_.DetectSourceProfiles(); } ImporterHost::ImporterHost(MessageLoop* file_loop) @@ -452,11 +417,10 @@ ImporterHost::ImporterHost(MessageLoop* file_loop) is_source_readable_(true), headless_(false), parent_window_(NULL) { - DetectSourceProfiles(); + importer_list_.DetectSourceProfiles(); } ImporterHost::~ImporterHost() { - STLDeleteContainerPointers(source_profiles_.begin(), source_profiles_.end()); if (NULL != importer_) importer_->Release(); } @@ -527,7 +491,7 @@ void ImporterHost::StartImportSettings(const ProfileInfo& profile_info, // so that it doesn't block the UI. When the import is complete, observer // will be notified. writer_ = writer; - importer_ = CreateImporterByType(profile_info.browser_type); + importer_ = importer_list_.CreateImporterByType(profile_info.browser_type); // If we fail to create Importer, exit as we can not do anything. if (!importer_) { ImportEnded(); @@ -543,8 +507,10 @@ void ImporterHost::StartImportSettings(const ProfileInfo& profile_info, import_to_bookmark_bar = (starred_urls.size() == 0); } importer_->set_import_to_bookmark_bar(import_to_bookmark_bar); + scoped_refptr<ImporterBridge> bridge( + new InProcessImporterBridge(writer_.get(), file_loop_, this)); task_ = NewRunnableMethod(importer_, &Importer::StartImport, - profile_info, items, writer_.get(), file_loop_, this); + profile_info, items, bridge); // We should lock the Firefox profile directory to prevent corruption. if (profile_info.browser_type == FIREFOX2 || @@ -653,196 +619,3 @@ void ImporterHost::ImportEnded() { observer_->ImportEnded(); Release(); } - -Importer* ImporterHost::CreateImporterByType(ProfileType type) { - switch (type) { -#if defined(OS_WIN) - case MS_IE: - return new IEImporter(); -#endif - case BOOKMARKS_HTML: - case FIREFOX2: - return new Firefox2Importer(); - case FIREFOX3: - return new Firefox3Importer(); - case GOOGLE_TOOLBAR5: - return new Toolbar5Importer(); -#if defined(OS_MACOSX) - case SAFARI: - return new SafariImporter(mac_util::GetUserLibraryPath()); -#endif // OS_MACOSX - } - NOTREACHED(); - return NULL; -} - -int ImporterHost::GetAvailableProfileCount() { - return static_cast<int>(source_profiles_.size()); -} - -std::wstring ImporterHost::GetSourceProfileNameAt(int index) const { - DCHECK(index < static_cast<int>(source_profiles_.size())); - return source_profiles_[index]->description; -} - -const ProfileInfo& ImporterHost::GetSourceProfileInfoAt(int index) const { - DCHECK(index < static_cast<int>(source_profiles_.size())); - return *source_profiles_[index]; -} - -const ProfileInfo& ImporterHost::GetSourceProfileInfoForBrowserType( - int browser_type) const { - int size = source_profiles_.size(); - for (int i = 0; i < size; i++) { - if (source_profiles_[i]->browser_type == browser_type) - return *source_profiles_[i]; - } - NOTREACHED(); - return *(new ProfileInfo()); -} - -void ImporterHost::DetectSourceProfiles() { -#if defined(OS_WIN) - // The order in which detect is called determines the order - // in which the options appear in the dropdown combo-box - if (ShellIntegration::IsFirefoxDefaultBrowser()) { - DetectFirefoxProfiles(); - DetectIEProfiles(); - } else { - DetectIEProfiles(); - DetectFirefoxProfiles(); - } - // TODO(brg) : Current UI requires win_util. - DetectGoogleToolbarProfiles(); -#else -#if defined(OS_MACOSX) - DetectSafariProfiles(); -#endif - DetectFirefoxProfiles(); -#endif -} - - -#if defined(OS_WIN) -void ImporterHost::DetectIEProfiles() { - // IE always exists and don't have multiple profiles. - ProfileInfo* ie = new ProfileInfo(); - ie->description = l10n_util::GetString(IDS_IMPORT_FROM_IE); - ie->browser_type = MS_IE; - ie->source_path.clear(); - ie->app_path.clear(); - ie->services_supported = HISTORY | FAVORITES | COOKIES | PASSWORDS | - SEARCH_ENGINES; - source_profiles_.push_back(ie); -} -#endif - -void ImporterHost::DetectFirefoxProfiles() { - DictionaryValue root; - std::wstring ini_file = GetProfilesINI().ToWStringHack(); - ParseProfileINI(ini_file, &root); - - std::wstring source_path; - for (int i = 0; ; ++i) { - std::wstring current_profile = L"Profile" + IntToWString(i); - if (!root.HasKey(current_profile)) { - // Profiles are continuously numbered. So we exit when we can't - // find the i-th one. - break; - } - std::wstring is_relative, path, profile_path; - if (root.GetString(current_profile + L".IsRelative", &is_relative) && - root.GetString(current_profile + L".Path", &path)) { -#if defined(OS_WIN) - string16 path16 = WideToUTF16Hack(path); - ReplaceSubstringsAfterOffset( - &path16, 0, ASCIIToUTF16("/"), ASCIIToUTF16("\\")); - path.assign(UTF16ToWideHack(path16)); -#endif - - // IsRelative=1 means the folder path would be relative to the - // path of profiles.ini. IsRelative=0 refers to a custom profile - // location. - if (is_relative == L"1") { - profile_path = file_util::GetDirectoryFromPath(ini_file); - file_util::AppendToPath(&profile_path, path); - } else { - profile_path = path; - } - - // We only import the default profile when multiple profiles exist, - // since the other profiles are used mostly by developers for testing. - // Otherwise, Profile0 will be imported. - std::wstring is_default; - if ((root.GetString(current_profile + L".Default", &is_default) && - is_default == L"1") || i == 0) { - source_path = profile_path; - // We break out of the loop when we have found the default profile. - if (is_default == L"1") - break; - } - } - } - - // Detects which version of Firefox is installed. - ProfileType firefox_type; - std::wstring app_path; - int version = 0; -#if defined(OS_WIN) - version = GetCurrentFirefoxMajorVersionFromRegistry(); -#endif - if (version != 2 && version != 3) - GetFirefoxVersionAndPathFromProfile(source_path, &version, &app_path); - - if (version == 2) { - firefox_type = FIREFOX2; - } else if (version == 3) { - firefox_type = FIREFOX3; - } else { - // Ignores other versions of firefox. - return; - } - - if (!source_path.empty()) { - ProfileInfo* firefox = new ProfileInfo(); - firefox->description = l10n_util::GetString(IDS_IMPORT_FROM_FIREFOX); - firefox->browser_type = firefox_type; - firefox->source_path = source_path; -#if defined(OS_WIN) - firefox->app_path = GetFirefoxInstallPathFromRegistry(); -#endif - if (firefox->app_path.empty()) - firefox->app_path = app_path; - firefox->services_supported = HISTORY | FAVORITES | COOKIES | PASSWORDS | - SEARCH_ENGINES; - source_profiles_.push_back(firefox); - } -} - -void ImporterHost::DetectGoogleToolbarProfiles() { - if (!FirstRun::IsChromeFirstRun()) { - ProfileInfo* google_toolbar = new ProfileInfo(); - google_toolbar->browser_type = GOOGLE_TOOLBAR5; - google_toolbar->description = l10n_util::GetString( - IDS_IMPORT_FROM_GOOGLE_TOOLBAR); - google_toolbar->source_path.clear(); - google_toolbar->app_path.clear(); - google_toolbar->services_supported = FAVORITES; - source_profiles_.push_back(google_toolbar); - } -} - -#if defined(OS_MACOSX) -void ImporterHost::DetectSafariProfiles() { - uint16 items = NONE; - if (SafariImporter::CanImport(mac_util::GetUserLibraryPath(), &items)) { - ProfileInfo* safari = new ProfileInfo(); - safari->browser_type = SAFARI; - safari->description = l10n_util::GetString(IDS_IMPORT_FROM_SAFARI); - safari->source_path.clear(); - safari->app_path.clear(); - safari->services_supported = items; - source_profiles_.push_back(safari); - } -} -#endif // OS_MACOSX diff --git a/chrome/browser/importer/importer.h b/chrome/browser/importer/importer.h index 1b8499e..4b8739e 100644 --- a/chrome/browser/importer/importer.h +++ b/chrome/browser/importer/importer.h @@ -15,10 +15,12 @@ #include "base/ref_counted.h" #include "chrome/browser/bookmarks/bookmark_model_observer.h" #include "chrome/browser/history/history_types.h" +#include "chrome/browser/importer/importer_list.h" #include "chrome/browser/profile.h" #include "chrome/common/notification_registrar.h" #include "googleurl/src/gurl.h" +class ImporterBridge; class MessageLoop; class TemplateURL; @@ -28,22 +30,6 @@ namespace webkit_glue { struct PasswordForm; } -// An enumeration of the type of browsers that we support to import -// settings and data from them. -enum ProfileType { -#if defined(OS_WIN) - MS_IE, -#endif - FIREFOX2, - FIREFOX3, -#if defined(OS_MACOSX) - SAFARI, -#endif - GOOGLE_TOOLBAR5, - // Identifies a 'bookmarks.html' file. - BOOKMARKS_HTML -}; - // An enumeration of the type of data we want to import. enum ImportItem { NONE = 0x0000, @@ -56,20 +42,20 @@ enum ImportItem { ALL = 0x003f }; -typedef struct { +struct ProfileInfo { std::wstring description; ProfileType browser_type; std::wstring source_path; std::wstring app_path; uint16 services_supported; // bitmap of ImportItem -} ProfileInfo; +}; class FirefoxProfileLock; class Importer; // ProfileWriter encapsulates profile for writing entries into it. // This object must be invoked on UI thread. -class ProfileWriter : public base::RefCounted<ProfileWriter> { +class ProfileWriter : public base::RefCountedThreadSafe<ProfileWriter> { public: // Used to identify how the bookmarks are added. enum BookmarkOptions { @@ -165,7 +151,7 @@ class ProfileWriter : public base::RefCounted<ProfileWriter> { // 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::RefCounted<ImporterHost>, +class ImporterHost : public base::RefCountedThreadSafe<ImporterHost>, public BookmarkModelObserver, public NotificationObserver { public: @@ -264,45 +250,35 @@ class ImporterHost : public base::RefCounted<ImporterHost>, void ImportItemEnded(ImportItem item); void ImportEnded(); - Importer* CreateImporterByType(ProfileType type); - - // Returns the number of different browser profiles you can import from. - int GetAvailableProfileCount(); + int GetAvailableProfileCount() { + return importer_list_.GetAvailableProfileCount(); + } - // Returns the name of the profile at the 'index' slot. The profiles are + // Returns the name of the profile at the 'index' slot. The profiles are // ordered such that the profile at index 0 is the likely default browser. - std::wstring GetSourceProfileNameAt(int index) const; + std::wstring GetSourceProfileNameAt(int index) const { + return importer_list_.GetSourceProfileNameAt(index); + } // Returns the ProfileInfo at the specified index. The ProfileInfo should be // passed to StartImportSettings(). - const ProfileInfo& GetSourceProfileInfoAt(int index) const; + const ProfileInfo& GetSourceProfileInfoAt(int index) const { + return importer_list_.GetSourceProfileInfoAt(index); + } + + // Returns the ProfileInfo with the given browser type. + const ProfileInfo& GetSourceProfileInfoForBrowserType(int browser_type) + const { + return importer_list_.GetSourceProfileInfoForBrowserType(browser_type); + } - // Returns the ProfileInfo with the given browser type - const ProfileInfo& GetSourceProfileInfoForBrowserType(int browser_type) const; private: // If we're not waiting on any model to finish loading, invokes the task_. void InvokeTaskIfDone(); - // Detects the installed browsers and their associated profiles, then - // stores their information in a list. It returns the list of description - // of all profiles. - void DetectSourceProfiles(); - - // Helper methods for detecting available profiles. -#if defined(OS_WIN) - void DetectIEProfiles(); -#endif - void DetectFirefoxProfiles(); - void DetectGoogleToolbarProfiles(); -#if defined(OS_MACOSX) - void DetectSafariProfiles(); -#endif - NotificationRegistrar registrar_; - - // The list of profiles with the default one first. - std::vector<ProfileInfo*> source_profiles_; + ImporterList importer_list_; Observer* observer_; scoped_refptr<ProfileWriter> writer_; @@ -335,10 +311,8 @@ class ImporterHost : public base::RefCounted<ImporterHost>, }; // The base class of all importers. -class Importer : public base::RefCounted<Importer> { +class Importer : public base::RefCountedThreadSafe<Importer> { public: - virtual ~Importer() { } - // All importers should implement this method by adding their // import logic. And it will be run in file thread by ImporterHost. // @@ -347,9 +321,7 @@ class Importer : public base::RefCounted<Importer> { // stuff have been finished. virtual void StartImport(ProfileInfo profile_info, uint16 items, - ProfileWriter* writer, - MessageLoop* delegate_loop, - ImporterHost* host) = 0; + ImporterBridge* bridge) = 0; // Cancels the import process. virtual void Cancel() { cancelled_ = true; } @@ -363,20 +335,6 @@ class Importer : public base::RefCounted<Importer> { protected: Importer(); - // Notifies the coordinator that the collection of data for the specified - // item has begun. - void NotifyItemStarted(ImportItem item); - - // Notifies the coordinator that the collection of data for the specified - // item has completed. - void NotifyItemEnded(ImportItem item); - - // Notifies the coordinator that the import operation has begun. - void NotifyStarted(); - - // Notifies the coordinator that the entire import operation has completed. - void NotifyEnded(); - // 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. @@ -385,15 +343,7 @@ class Importer : public base::RefCounted<Importer> { bool import_to_bookmark_bar() const { return import_to_bookmark_bar_; } - // The importer should know the main thread so that ProfileWriter - // will be invoked in thread instead. - MessageLoop* main_loop_; - - // The message loop in which the importer operates. - MessageLoop* delagate_loop_; - - // The coordinator host for this importer. - ImporterHost* importer_host_; + scoped_refptr<ImporterBridge> bridge_; private: // True if the caller cancels the import process. diff --git a/chrome/browser/importer/importer_bridge.cc b/chrome/browser/importer/importer_bridge.cc new file mode 100644 index 0000000..dfe98fd --- /dev/null +++ b/chrome/browser/importer/importer_bridge.cc @@ -0,0 +1,89 @@ +// Copyright (c) 2009 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/message_loop.h" +#include "chrome/browser/importer/importer.h" +#if defined(OS_WIN) +#include "chrome/browser/password_manager/ie7_password.h" +#endif +#include "webkit/glue/password_form.h" + +InProcessImporterBridge::InProcessImporterBridge(ProfileWriter* writer, + MessageLoop* delegate_loop, + ImporterHost* host) + : ImporterBridge(writer, delegate_loop, host), + main_loop_(MessageLoop::current()), + delegate_loop_(NULL) { +} + +void InProcessImporterBridge::AddBookmarkEntries( + const std::vector<ProfileWriter::BookmarkEntry>& bookmarks, + const std::wstring& first_folder_name, + int options) { + main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, + &ProfileWriter::AddBookmarkEntry, bookmarks, first_folder_name, + options)); +} + +void InProcessImporterBridge::AddHomePage(const GURL &home_page) { + main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, + &ProfileWriter::AddHomepage, home_page)); +} + +#if defined(OS_WIN) +void InProcessImporterBridge::AddIE7PasswordInfo( + const IE7PasswordInfo password_info) { + main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, + &ProfileWriter::AddIE7PasswordInfo, password_info)); +} +#endif // OS_WIN + +void InProcessImporterBridge::SetFavIcons( + const std::vector<history::ImportedFavIconUsage>& fav_icons) { + main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, + &ProfileWriter::AddFavicons, fav_icons)); +} + +void InProcessImporterBridge::SetHistoryItems( + const std::vector<history::URLRow> &rows) { + main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, + &ProfileWriter::AddHistoryPage, rows)); +} + +void InProcessImporterBridge::SetKeywords( + const std::vector<TemplateURL*>& template_urls, + int default_keyword_index, + bool unique_on_host_and_path) { + main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, + &ProfileWriter::AddKeywords, template_urls, default_keyword_index, + unique_on_host_and_path)); +} + +void InProcessImporterBridge::SetPasswordForm( + const webkit_glue::PasswordForm& form) { + main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, + &ProfileWriter::AddPasswordForm, form)); +} + +void InProcessImporterBridge::NotifyItemStarted(ImportItem item) { + main_loop_->PostTask(FROM_HERE, NewRunnableMethod(host_, + &ImporterHost::ImportItemStarted, item)); +} + +void InProcessImporterBridge::NotifyItemEnded(ImportItem item) { + main_loop_->PostTask(FROM_HERE, NewRunnableMethod(host_, + &ImporterHost::ImportItemEnded, item)); +} + +void InProcessImporterBridge::NotifyStarted() { + main_loop_->PostTask(FROM_HERE, NewRunnableMethod(host_, + &ImporterHost::ImportStarted)); +} + +void InProcessImporterBridge::NotifyEnded() { + main_loop_->PostTask(FROM_HERE, + NewRunnableMethod(host_, &ImporterHost::ImportEnded)); +} diff --git a/chrome/browser/importer/importer_bridge.h b/chrome/browser/importer/importer_bridge.h new file mode 100644 index 0000000..02a8d9a --- /dev/null +++ b/chrome/browser/importer/importer_bridge.h @@ -0,0 +1,112 @@ +// Copyright (c) 2009 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_BRIDGE_H_ +#define CHROME_BROWSER_IMPORTER_IMPORTER_BRIDGE_H_ + +#include "build/build_config.h" + +#include <vector> + +#include "base/basictypes.h" +#include "base/string16.h" + +#include "chrome/browser/importer/importer.h" +// TODO: remove this, see friend declaration in ImporterBridge. +#include "chrome/browser/importer/toolbar_importer.h" + +class ImporterBridge : public base::RefCountedThreadSafe<ImporterBridge> { + public: + ImporterBridge(ProfileWriter* writer, + MessageLoop* delegate_loop, + ImporterHost* host) + : writer_(writer), + delegate_loop_(delegate_loop), + host_(host) { + } + virtual ~ImporterBridge() {} + + virtual void AddBookmarkEntries( + const std::vector<ProfileWriter::BookmarkEntry>& bookmarks, + const std::wstring& first_folder_name, + int options) = 0; + virtual void AddHomePage(const GURL &home_page) = 0; + +#if defined(OS_WIN) + virtual void AddIE7PasswordInfo(const IE7PasswordInfo password_info) = 0; +#endif + + virtual void SetFavIcons( + const std::vector<history::ImportedFavIconUsage>& fav_icons) = 0; + virtual void SetHistoryItems(const std::vector<history::URLRow> &rows) = 0; + virtual void SetKeywords(const std::vector<TemplateURL*> &template_urls, + int default_keyword_index, + bool unique_on_host_and_path) = 0; + virtual void SetPasswordForm(const webkit_glue::PasswordForm& form) = 0; + + // Notifies the coordinator that the collection of data for the specified + // item has begun. + virtual void NotifyItemStarted(ImportItem item) = 0; + + // Notifies the coordinator that the collection of data for the specified + // item has completed. + virtual void NotifyItemEnded(ImportItem item) = 0; + + // Notifies the coordinator that the import operation has begun. + virtual void NotifyStarted() = 0; + + // Notifies the coordinator that the entire import operation has completed. + virtual void NotifyEnded() = 0; + + protected: + + // TODO: In order to run Toolbar5Importer OOP we need to cut this + // connection, but as an interim step we allow Toolbar5Import to break + // the abstraction here and assume import is in-process. + friend class Toolbar5Importer; + + ProfileWriter* writer_; + MessageLoop* delegate_loop_; + ImporterHost* host_; + + DISALLOW_COPY_AND_ASSIGN(ImporterBridge); +}; + +class InProcessImporterBridge : public ImporterBridge { + public: + InProcessImporterBridge(ProfileWriter* writer, + MessageLoop* delegate_loop, + ImporterHost* host); + + // Methods inherited from ImporterBridge. + virtual void AddBookmarkEntries( + const std::vector<ProfileWriter::BookmarkEntry>& bookmarks, + const std::wstring& first_folder_name, + int options); + virtual void AddHomePage(const GURL &home_page); + +#if defined(OS_WIN) + virtual void AddIE7PasswordInfo(const IE7PasswordInfo password_info); +#endif + + virtual void SetFavIcons( + const std::vector<history::ImportedFavIconUsage>& fav_icons); + virtual void SetHistoryItems(const std::vector<history::URLRow> &rows); + virtual void SetKeywords(const std::vector<TemplateURL*>& template_urls, + int default_keyword_index, + bool unique_on_host_and_path); + virtual void SetPasswordForm(const webkit_glue::PasswordForm& form); + + virtual void NotifyItemStarted(ImportItem item); + virtual void NotifyItemEnded(ImportItem item); + virtual void NotifyStarted(); + virtual void NotifyEnded(); + + private: + MessageLoop* main_loop_; + MessageLoop* delegate_loop_; + DISALLOW_COPY_AND_ASSIGN(InProcessImporterBridge); +}; + +#endif // CHROME_BROWSER_IMPORTER_IMPORTER_BRIDGE_H_ diff --git a/chrome/browser/importer/importer_list.cc b/chrome/browser/importer/importer_list.cc new file mode 100644 index 0000000..4f3c815 --- /dev/null +++ b/chrome/browser/importer/importer_list.cc @@ -0,0 +1,226 @@ +// Copyright (c) 2009 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_list.h" + +#include "app/l10n_util.h" +#include "base/file_util.h" +#include "base/stl_util-inl.h" +#include "chrome/browser/first_run.h" +#include "chrome/browser/importer/firefox2_importer.h" +#include "chrome/browser/importer/firefox3_importer.h" +#include "chrome/browser/importer/firefox_importer_utils.h" +#include "chrome/browser/importer/importer_bridge.h" +#include "chrome/browser/importer/toolbar_importer.h" +#include "chrome/browser/shell_integration.h" +#include "grit/generated_resources.h" + +#if defined(OS_WIN) +#include "app/win_util.h" +#include "chrome/browser/importer/ie_importer.h" +#include "chrome/browser/password_manager/ie7_password.h" +#endif +#if defined(OS_MACOSX) +#include "base/mac_util.h" +#include "chrome/browser/importer/safari_importer.h" +#endif + +ImporterList::ImporterList() { +} + +ImporterList::~ImporterList() { + STLDeleteContainerPointers(source_profiles_.begin(), source_profiles_.end()); +} + +void ImporterList::DetectSourceProfiles() { +#if defined(OS_WIN) + // The order in which detect is called determines the order + // in which the options appear in the dropdown combo-box + if (ShellIntegration::IsFirefoxDefaultBrowser()) { + DetectFirefoxProfiles(); + DetectIEProfiles(); + } else { + DetectIEProfiles(); + DetectFirefoxProfiles(); + } + // TODO(brg) : Current UI requires win_util. + DetectGoogleToolbarProfiles(); +#else +#if defined(OS_MACOSX) + DetectSafariProfiles(); +#endif + DetectFirefoxProfiles(); +#endif +} + +Importer* ImporterList::CreateImporterByType(ProfileType type) { + switch (type) { +#if defined(OS_WIN) + case MS_IE: + return new IEImporter(); +#endif + case BOOKMARKS_HTML: + case FIREFOX2: + return new Firefox2Importer(); + case FIREFOX3: + return new Firefox3Importer(); + case GOOGLE_TOOLBAR5: + return new Toolbar5Importer(); +#if defined(OS_MACOSX) + case SAFARI: + return new SafariImporter(mac_util::GetUserLibraryPath()); +#endif // OS_MACOSX + } + NOTREACHED(); + return NULL; +} + +int ImporterList::GetAvailableProfileCount() { + return static_cast<int>(source_profiles_.size()); +} + +std::wstring ImporterList::GetSourceProfileNameAt(int index) const { + DCHECK(index < static_cast<int>(source_profiles_.size())); + return source_profiles_[index]->description; +} + +const ProfileInfo& ImporterList::GetSourceProfileInfoAt(int index) const { + DCHECK(index < static_cast<int>(source_profiles_.size())); + return *source_profiles_[index]; +} + +const ProfileInfo& ImporterList::GetSourceProfileInfoForBrowserType( + int browser_type) const { + int size = source_profiles_.size(); + for (int i = 0; i < size; i++) { + if (source_profiles_[i]->browser_type == browser_type) + return *source_profiles_[i]; + } + NOTREACHED(); + return *(new ProfileInfo()); +} + +#if defined(OS_WIN) +void ImporterList::DetectIEProfiles() { + // IE always exists and don't have multiple profiles. + ProfileInfo* ie = new ProfileInfo(); + ie->description = l10n_util::GetString(IDS_IMPORT_FROM_IE); + ie->browser_type = MS_IE; + ie->source_path.clear(); + ie->app_path.clear(); + ie->services_supported = HISTORY | FAVORITES | COOKIES | PASSWORDS | + SEARCH_ENGINES; + source_profiles_.push_back(ie); +} +#endif + +void ImporterList::DetectFirefoxProfiles() { + DictionaryValue root; + std::wstring ini_file = GetProfilesINI().ToWStringHack(); + ParseProfileINI(ini_file, &root); + + std::wstring source_path; + for (int i = 0; ; ++i) { + std::wstring current_profile = L"Profile" + IntToWString(i); + if (!root.HasKey(current_profile)) { + // Profiles are continuously numbered. So we exit when we can't + // find the i-th one. + break; + } + std::wstring is_relative, path, profile_path; + if (root.GetString(current_profile + L".IsRelative", &is_relative) && + root.GetString(current_profile + L".Path", &path)) { +#if defined(OS_WIN) + string16 path16 = WideToUTF16Hack(path); + ReplaceSubstringsAfterOffset( + &path16, 0, ASCIIToUTF16("/"), ASCIIToUTF16("\\")); + path.assign(UTF16ToWideHack(path16)); +#endif + + // IsRelative=1 means the folder path would be relative to the + // path of profiles.ini. IsRelative=0 refers to a custom profile + // location. + if (is_relative == L"1") { + profile_path = file_util::GetDirectoryFromPath(ini_file); + file_util::AppendToPath(&profile_path, path); + } else { + profile_path = path; + } + + // We only import the default profile when multiple profiles exist, + // since the other profiles are used mostly by developers for testing. + // Otherwise, Profile0 will be imported. + std::wstring is_default; + if ((root.GetString(current_profile + L".Default", &is_default) && + is_default == L"1") || i == 0) { + source_path = profile_path; + // We break out of the loop when we have found the default profile. + if (is_default == L"1") + break; + } + } + } + + // Detects which version of Firefox is installed. + ProfileType firefox_type; + std::wstring app_path; + int version = 0; +#if defined(OS_WIN) + version = GetCurrentFirefoxMajorVersionFromRegistry(); +#endif + if (version != 2 && version != 3) + GetFirefoxVersionAndPathFromProfile(source_path, &version, &app_path); + + if (version == 2) { + firefox_type = FIREFOX2; + } else if (version == 3) { + firefox_type = FIREFOX3; + } else { + // Ignores other versions of firefox. + return; + } + + if (!source_path.empty()) { + ProfileInfo* firefox = new ProfileInfo(); + firefox->description = l10n_util::GetString(IDS_IMPORT_FROM_FIREFOX); + firefox->browser_type = firefox_type; + firefox->source_path = source_path; +#if defined(OS_WIN) + firefox->app_path = GetFirefoxInstallPathFromRegistry(); +#endif + if (firefox->app_path.empty()) + firefox->app_path = app_path; + firefox->services_supported = HISTORY | FAVORITES | COOKIES | PASSWORDS | + SEARCH_ENGINES; + source_profiles_.push_back(firefox); + } +} + +void ImporterList::DetectGoogleToolbarProfiles() { + if (!FirstRun::IsChromeFirstRun()) { + ProfileInfo* google_toolbar = new ProfileInfo(); + google_toolbar->browser_type = GOOGLE_TOOLBAR5; + google_toolbar->description = l10n_util::GetString( + IDS_IMPORT_FROM_GOOGLE_TOOLBAR); + google_toolbar->source_path.clear(); + google_toolbar->app_path.clear(); + google_toolbar->services_supported = FAVORITES; + source_profiles_.push_back(google_toolbar); + } +} + +#if defined(OS_MACOSX) +void ImporterList::DetectSafariProfiles() { + uint16 items = NONE; + if (SafariImporter::CanImport(mac_util::GetUserLibraryPath(), &items)) { + ProfileInfo* safari = new ProfileInfo(); + safari->browser_type = SAFARI; + safari->description = l10n_util::GetString(IDS_IMPORT_FROM_SAFARI); + safari->source_path.clear(); + safari->app_path.clear(); + safari->services_supported = items; + source_profiles_.push_back(safari); + } +} +#endif // OS_MACOSX diff --git a/chrome/browser/importer/importer_list.h b/chrome/browser/importer/importer_list.h new file mode 100644 index 0000000..4574a29 --- /dev/null +++ b/chrome/browser/importer/importer_list.h @@ -0,0 +1,77 @@ +// Copyright (c) 2009 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_LIST_H_ +#define CHROME_BROWSER_IMPORTER_IMPORTER_LIST_H_ + +#include <string> +#include <vector> + +#include "build/build_config.h" + +#include "base/basictypes.h" + +class Importer; +struct ProfileInfo; + +// An enumeration of the type of browsers that we support to import +// settings and data from them. +enum ProfileType { +#if defined(OS_WIN) + MS_IE, +#endif + FIREFOX2, + FIREFOX3, +#if defined(OS_MACOSX) + SAFARI, +#endif + GOOGLE_TOOLBAR5, + // Identifies a 'bookmarks.html' file. + BOOKMARKS_HTML +}; + +class ImporterList { + public: + ImporterList(); + ~ImporterList(); + + // Detects the installed browsers and their associated profiles, then + // stores their information in a list. It returns the list of description + // of all profiles. + void DetectSourceProfiles(); + + Importer* CreateImporterByType(ProfileType type); + + // Returns the number of different browser profiles you can import from. + int GetAvailableProfileCount(); + + // Returns the name of the profile at the 'index' slot. The profiles are + // ordered such that the profile at index 0 is the likely default browser. + std::wstring GetSourceProfileNameAt(int index) const; + + // Returns the ProfileInfo at the specified index. The ProfileInfo should be + // passed to StartImportSettings(). + const ProfileInfo& GetSourceProfileInfoAt(int index) const; + + // Returns the ProfileInfo with the given browser type. + const ProfileInfo& GetSourceProfileInfoForBrowserType(int browser_type) const; + + // Helper methods for detecting available profiles. +#if defined(OS_WIN) + void DetectIEProfiles(); +#endif + void DetectFirefoxProfiles(); + void DetectGoogleToolbarProfiles(); +#if defined(OS_MACOSX) + void DetectSafariProfiles(); +#endif + + private: + // The list of profiles with the default one first. + std::vector<ProfileInfo*> source_profiles_; + + DISALLOW_COPY_AND_ASSIGN(ImporterList); +}; + +#endif // CHROME_BROWSER_IMPORTER_IMPORTER_LIST_H_ diff --git a/chrome/browser/importer/importer_unittest.cc b/chrome/browser/importer/importer_unittest.cc index 739465d..b6ef1f6 100644 --- a/chrome/browser/importer/importer_unittest.cc +++ b/chrome/browser/importer/importer_unittest.cc @@ -20,6 +20,7 @@ #include "base/stl_util-inl.h" #include "chrome/browser/importer/ie_importer.h" #include "chrome/browser/importer/importer.h" +#include "chrome/browser/importer/importer_bridge.h" #include "chrome/browser/password_manager/ie7_password.h" #include "chrome/browser/profile.h" #include "chrome/browser/search_engines/template_url.h" diff --git a/chrome/browser/importer/mork_reader.cc b/chrome/browser/importer/mork_reader.cc index 5352902..75dd7dc 100644 --- a/chrome/browser/importer/mork_reader.cc +++ b/chrome/browser/importer/mork_reader.cc @@ -51,6 +51,7 @@ #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" using base::Time; @@ -544,8 +545,7 @@ void AddToHistory(MorkReader::ColumnDataList* column_values, // It sets up the file stream and loops over the lines in the file to // parse them, then adds the resulting row set to history. -void ImportHistoryFromFirefox2(std::wstring file, MessageLoop* loop, - ProfileWriter* writer) { +void ImportHistoryFromFirefox2(std::wstring file, ImporterBridge* bridge) { MorkReader reader; reader.Read(file); @@ -582,6 +582,5 @@ void ImportHistoryFromFirefox2(std::wstring file, MessageLoop* loop, for (MorkReader::iterator i = reader.begin(); i != reader.end(); ++i) AddToHistory(i->second, data, &rows); if (!rows.empty()) - loop->PostTask(FROM_HERE, NewRunnableMethod(writer, - &ProfileWriter::AddHistoryPage, rows)); + bridge->SetHistoryItems(rows); } diff --git a/chrome/browser/importer/mork_reader.h b/chrome/browser/importer/mork_reader.h index d4844a0..4cdc010 100644 --- a/chrome/browser/importer/mork_reader.h +++ b/chrome/browser/importer/mork_reader.h @@ -50,6 +50,7 @@ #include "base/basictypes.h" +class ImporterBridge; class MessageLoop; class ProfileWriter; @@ -160,7 +161,6 @@ class MorkReader { }; // ImportHistoryFromFirefox2 is the main entry point to the importer. -void ImportHistoryFromFirefox2(std::wstring file, MessageLoop* loop, - ProfileWriter* writer); +void ImportHistoryFromFirefox2(std::wstring file, ImporterBridge* bridge); #endif // CHROME_BROWSER_IMPORTER_MORK_READER_H__ diff --git a/chrome/browser/importer/safari_importer.h b/chrome/browser/importer/safari_importer.h index e443242..827880f 100644 --- a/chrome/browser/importer/safari_importer.h +++ b/chrome/browser/importer/safari_importer.h @@ -32,9 +32,7 @@ class SafariImporter : public Importer { // Importer methods. virtual void StartImport(ProfileInfo profile_info, uint16 items, - ProfileWriter* writer, - MessageLoop* delegate_loop, - ImporterHost* host); + ImporterBridge* bridge); // Does this user account have a Safari Profile and if so, what items diff --git a/chrome/browser/importer/safari_importer.mm b/chrome/browser/importer/safari_importer.mm index e08e582..bcf3432 100644 --- a/chrome/browser/importer/safari_importer.mm +++ b/chrome/browser/importer/safari_importer.mm @@ -16,6 +16,7 @@ #include "base/string16.h" #include "base/sys_string_conversions.h" #include "base/time.h" +#include "chrome/browser/importer/importer_bridge.h" #include "chrome/common/sqlite_utils.h" #include "chrome/common/url_constants.h" #include "googleurl/src/gurl.h" @@ -76,32 +77,30 @@ bool SafariImporter::CanImport(const FilePath& library_dir, } void SafariImporter::StartImport(ProfileInfo profile_info, - uint16 services_supported, ProfileWriter* writer, - MessageLoop* delegate_loop, - ImporterHost* host) { - writer_ = writer; - importer_host_ = host; + uint16 services_supported, + ImporterBridge* bridge) { + bridge_ = bridge; // The order here is important! - NotifyStarted(); + bridge_->NotifyStarted(); if ((services_supported & HOME_PAGE) && !cancelled()) ImportHomepage(); // Doesn't have a UI item. if ((services_supported & HISTORY) && !cancelled()) { - NotifyItemStarted(HISTORY); + bridge_->NotifyItemStarted(HISTORY); ImportHistory(); - NotifyItemEnded(HISTORY); + bridge_->NotifyItemEnded(HISTORY); } if ((services_supported & FAVORITES) && !cancelled()) { - NotifyItemStarted(FAVORITES); + bridge_->NotifyItemStarted(FAVORITES); ImportBookmarks(); - NotifyItemEnded(FAVORITES); + bridge_->NotifyItemEnded(FAVORITES); } if ((services_supported & PASSWORDS) && !cancelled()) { - NotifyItemStarted(PASSWORDS); + bridge_->NotifyItemStarted(PASSWORDS); ImportPasswords(); - NotifyItemEnded(PASSWORDS); + bridge_->NotifyItemEnded(PASSWORDS); } - NotifyEnded(); + bridge_->NotifyEnded(); } void SafariImporter::ImportBookmarks() { @@ -110,10 +109,12 @@ void SafariImporter::ImportBookmarks() { // Write bookmarks into profile. if (!bookmarks.empty() && !cancelled()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddBookmarkEntry, bookmarks, - l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_SAFARI), - import_to_bookmark_bar() ? ProfileWriter::IMPORT_TO_BOOKMARK_BAR : 0)); + const std::wstring& first_folder_name = + l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_SAFARI); + int options = 0; + if (import_to_bookmark_bar()) + options = ProfileWriter::IMPORT_TO_BOOKMARK_BAR; + bridge_->AddBookmarkEntries(bookmarks, first_folder_name, options); } // Import favicons. @@ -124,8 +125,7 @@ void SafariImporter::ImportBookmarks() { if (!favicon_map.empty() && !cancelled()) { std::vector<history::ImportedFavIconUsage> favicons; LoadFaviconData(db.get(), favicon_map, &favicons); - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddFavicons, favicons)); + bridge_->SetFavIcons(favicons); } } @@ -317,8 +317,7 @@ void SafariImporter::ImportHistory() { ParseHistoryItems(&rows); if (!rows.empty() && !cancelled()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddHistoryPage, rows)); + bridge_->SetHistoryItems(rows); } } @@ -406,7 +405,6 @@ void SafariImporter::ImportHomepage() { string16 hompeage_str = base::SysNSStringToUTF16(homepage_ns.get()); GURL homepage(hompeage_str); if (homepage.is_valid()) { - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddHomepage, homepage)); + bridge_->AddHomePage(homepage); } } diff --git a/chrome/browser/importer/safari_importer_unittest.mm b/chrome/browser/importer/safari_importer_unittest.mm index 7a4d99f..f9ea738 100644 --- a/chrome/browser/importer/safari_importer_unittest.mm +++ b/chrome/browser/importer/safari_importer_unittest.mm @@ -11,6 +11,7 @@ #include "base/sys_string_conversions.h" #include "chrome/common/chrome_paths.h" #include "chrome/test/file_test_utils.h" +#include "chrome/browser/importer/importer_bridge.h" #include "testing/platform_test.h" // In order to test the Safari import functionality effectively, we store a diff --git a/chrome/browser/importer/toolbar_importer.cc b/chrome/browser/importer/toolbar_importer.cc index 20e10da..a215fd8 100644 --- a/chrome/browser/importer/toolbar_importer.cc +++ b/chrome/browser/importer/toolbar_importer.cc @@ -10,6 +10,7 @@ #include "base/string_util.h" #include "base/rand_util.h" #include "chrome/browser/first_run.h" +#include "chrome/browser/importer/importer_bridge.h" #include "chrome/common/libxml_utils.h" #include "grit/generated_resources.h" #include "net/base/cookie_monster.h" @@ -90,19 +91,14 @@ Toolbar5Importer::~Toolbar5Importer() { void Toolbar5Importer::StartImport(ProfileInfo profile_info, uint16 items, - ProfileWriter* writer, - MessageLoop* delagate_loop, - ImporterHost* host) { - DCHECK(writer); - DCHECK(host); - - importer_host_ = host; - delagate_loop_ = delagate_loop; - writer_ = writer; + ImporterBridge* bridge) { + DCHECK(bridge); + + bridge_ = bridge; items_to_import_ = items; state_ = INITIALIZED; - NotifyStarted(); + bridge_->NotifyStarted(); ContinueImport(); } @@ -117,9 +113,9 @@ void Toolbar5Importer::Cancel() { // If we are conducting network operations, post a message to the importer // thread for synchronization. - if (NULL != delagate_loop_) { - if (delagate_loop_ != MessageLoop::current()) { - delagate_loop_->PostTask( + if (NULL != bridge_->delegate_loop_) { + if (bridge_->delegate_loop_ != MessageLoop::current()) { + bridge_->delegate_loop_->PostTask( FROM_HERE, NewRunnableMethod(this, &Toolbar5Importer::Cancel)); } else { @@ -201,17 +197,17 @@ void Toolbar5Importer::EndImport() { data_fetcher_ = NULL; } - NotifyEnded(); + bridge_->NotifyEnded(); } } void Toolbar5Importer::BeginImportBookmarks() { - NotifyItemStarted(FAVORITES); + bridge_->NotifyItemStarted(FAVORITES); GetAuthenticationFromServer(); } void Toolbar5Importer::EndImportBookmarks() { - NotifyItemEnded(FAVORITES); + bridge_->NotifyItemEnded(FAVORITES); ContinueImport(); } @@ -582,11 +578,10 @@ bool Toolbar5Importer::ExtractFoldersFromXmlReader( void Toolbar5Importer::AddBookmarksToChrome( const std::vector<ProfileWriter::BookmarkEntry>& bookmarks) { if (!bookmarks.empty() && !cancelled()) { + const std::wstring& first_folder_name = + l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_GOOGLE_TOOLBAR); int options = ProfileWriter::ADD_IF_UNIQUE | (import_to_bookmark_bar() ? ProfileWriter::IMPORT_TO_BOOKMARK_BAR : 0); - main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddBookmarkEntry, bookmarks, - l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_GOOGLE_TOOLBAR), - options)); + bridge_->AddBookmarkEntries(bookmarks, first_folder_name, options); } } diff --git a/chrome/browser/importer/toolbar_importer.h b/chrome/browser/importer/toolbar_importer.h index 24b5b28..61cdfb0 100644 --- a/chrome/browser/importer/toolbar_importer.h +++ b/chrome/browser/importer/toolbar_importer.h @@ -14,6 +14,7 @@ #include "chrome/browser/importer/importer.h" #include "chrome/browser/net/url_fetcher.h" +class ImporterBridge; class XmlReader; // Currently the only configuration information we need is to check whether or @@ -40,9 +41,7 @@ class Toolbar5Importer : public URLFetcher::Delegate, public Importer { // of Importer::StartImport. virtual void StartImport(ProfileInfo profile_info, uint16 items, - ProfileWriter* writer, - MessageLoop* delegate_loop, - ImporterHost* host); + ImporterBridge* bridge); // Importer view call this method when the user clicks the cancel button // in the ImporterView UI. We need to post a message to our loop |