1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_IMPORTER_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_ 6 #define CHROME_BROWSER_IMPORTER_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/compiler_specific.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/strings/string16.h" 15 #include "chrome/common/importer/importer_autofill_form_data_entry.h" 16 #include "chrome/common/importer/importer_data_types.h" 17 #include "chrome/common/importer/importer_url_row.h" 18 #include "components/history/core/browser/history_types.h" 19 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/utility_process_host_client.h" 21 22 class ExternalProcessImporterHost; 23 struct ImportedBookmarkEntry; 24 struct ImportedFaviconUsage; 25 class InProcessImporterBridge; 26 27 namespace autofill { 28 struct PasswordForm; 29 } 30 31 namespace content{ 32 class UtilityProcessHost; 33 } 34 35 namespace importer { 36 #if defined(OS_WIN) 37 struct ImporterIE7PasswordInfo; 38 #endif 39 struct ImporterAutofillFormDataEntry; 40 struct URLKeywordInfo; 41 } 42 43 // This class is the client for the out of process profile importing. It 44 // collects notifications from this process host and feeds data back to the 45 // importer host, who actually does the writing. 46 class ExternalProcessImporterClient : public content::UtilityProcessHostClient { 47 public: 48 ExternalProcessImporterClient( 49 base::WeakPtr<ExternalProcessImporterHost> importer_host, 50 const importer::SourceProfile& source_profile, 51 uint16 items, 52 InProcessImporterBridge* bridge); 53 54 // Launches the task to start the external process. 55 void Start(); 56 57 // Called by the ExternalProcessImporterHost on import cancel. 58 void Cancel(); 59 60 // UtilityProcessHostClient implementation: 61 virtual void OnProcessCrashed(int exit_code) OVERRIDE; 62 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 63 64 // Message handlers 65 void OnImportStart(); 66 void OnImportFinished(bool succeeded, const std::string& error_msg); 67 void OnImportItemStart(int item); 68 void OnImportItemFinished(int item); 69 void OnHistoryImportStart(size_t total_history_rows_count); 70 void OnHistoryImportGroup( 71 const std::vector<ImporterURLRow>& history_rows_group, 72 int visit_source); 73 void OnHomePageImportReady(const GURL& home_page); 74 void OnBookmarksImportStart(const base::string16& first_folder_name, 75 size_t total_bookmarks_count); 76 void OnBookmarksImportGroup( 77 const std::vector<ImportedBookmarkEntry>& bookmarks_group); 78 void OnFaviconsImportStart(size_t total_favicons_count); 79 void OnFaviconsImportGroup( 80 const std::vector<ImportedFaviconUsage>& favicons_group); 81 void OnPasswordFormImportReady(const autofill::PasswordForm& form); 82 void OnKeywordsImportReady( 83 const std::vector<importer::URLKeywordInfo>& url_keywords, 84 bool unique_on_host_and_path); 85 void OnFirefoxSearchEngineDataReceived( 86 const std::vector<std::string> search_engine_data); 87 void OnAutofillFormDataImportStart( 88 size_t total_autofill_form_data_entry_count); 89 void OnAutofillFormDataImportGroup(const std::vector< 90 ImporterAutofillFormDataEntry>& autofill_form_data_entry_group); 91 #if defined(OS_WIN) 92 void OnIE7PasswordReceived( 93 const importer::ImporterIE7PasswordInfo& importer_password_info); 94 #endif 95 96 protected: 97 virtual ~ExternalProcessImporterClient(); 98 99 private: 100 // Notifies the importerhost that import has finished, and calls Release(). 101 void Cleanup(); 102 103 // Cancel import process on IO thread. 104 void CancelImportProcessOnIOThread(); 105 106 // Report item completely downloaded on IO thread. 107 void NotifyItemFinishedOnIOThread(importer::ImportItem import_item); 108 109 // Creates a new UtilityProcessHost, which launches the import process. 110 void StartProcessOnIOThread(content::BrowserThread::ID thread_id); 111 112 // These variables store data being collected from the importer until the 113 // entire group has been collected and is ready to be written to the profile. 114 std::vector<ImporterURLRow> history_rows_; 115 std::vector<ImportedBookmarkEntry> bookmarks_; 116 std::vector<ImportedFaviconUsage> favicons_; 117 std::vector<ImporterAutofillFormDataEntry> autofill_form_data_; 118 119 // Usually some variation on IDS_BOOKMARK_GROUP_...; the name of the folder 120 // under which imported bookmarks will be placed. 121 base::string16 bookmarks_first_folder_name_; 122 123 // Total number of bookmarks to import. 124 size_t total_bookmarks_count_; 125 126 // Total number of history items to import. 127 size_t total_history_rows_count_; 128 129 // Total number of favicons to import. 130 size_t total_favicons_count_; 131 132 // Total number of autofill form data entries to import. 133 size_t total_autofill_form_data_entry_count_; 134 135 // Notifications received from the ProfileImportProcessHost are passed back 136 // to process_importer_host_, which calls the ProfileWriter to record the 137 // import data. When the import process is done, process_importer_host_ 138 // deletes itself. This is a weak ptr so that any messages received after 139 // the host has deleted itself are ignored (e.g., it's possible to receive 140 // OnProcessCrashed() after NotifyImportEnded()). 141 base::WeakPtr<ExternalProcessImporterHost> process_importer_host_; 142 143 // Handles sending messages to the external process. Deletes itself when 144 // the external process dies (see 145 // BrowserChildProcessHost::OnChildDisconnected). 146 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; 147 148 // Data to be passed from the importer host to the external importer. 149 importer::SourceProfile source_profile_; 150 uint16 items_; 151 152 // Takes import data coming over IPC and delivers it to be written by the 153 // ProfileWriter. 154 scoped_refptr<InProcessImporterBridge> bridge_; 155 156 // True if import process has been cancelled. 157 bool cancelled_; 158 159 DISALLOW_COPY_AND_ASSIGN(ExternalProcessImporterClient); 160 }; 161 162 #endif // CHROME_BROWSER_IMPORTER_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_ 163