1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_IMPORTER_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_ 6 #define CHROME_BROWSER_IMPORTER_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_ 7 #pragma once 8 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/compiler_specific.h" 14 #include "base/string16.h" 15 #include "chrome/browser/importer/importer_data_types.h" 16 #include "chrome/browser/importer/profile_writer.h" 17 #include "chrome/browser/importer/profile_import_process_client.h" 18 #include "content/browser/browser_thread.h" 19 20 class ExternalProcessImporterHost; 21 class InProcessImporterBridge; 22 class ProfileImportProcessHost; 23 24 namespace history { 25 class URLRow; 26 struct ImportedFaviconUsage; 27 } 28 29 // This class is the client for the ProfileImportProcessHost. It collects 30 // notifications from this process host and feeds data back to the importer 31 // host, who actually does the writing. 32 class ExternalProcessImporterClient : public ProfileImportProcessClient { 33 public: 34 ExternalProcessImporterClient(ExternalProcessImporterHost* importer_host, 35 const importer::SourceProfile& source_profile, 36 uint16 items, 37 InProcessImporterBridge* bridge, 38 bool import_to_bookmark_bar); 39 virtual ~ExternalProcessImporterClient(); 40 41 // Launches the task to start the external process. 42 void Start(); 43 44 // Called by the ExternalProcessImporterHost on import cancel. 45 void Cancel(); 46 47 // Notifies the ImporterHost that import has finished, and calls Release(). 48 void Cleanup(); 49 50 private: 51 // Creates a new ProfileImportProcessHost, which launches the import process. 52 void StartImportProcessOnIOThread(BrowserThread::ID thread_id); 53 54 // Cancel import process on IO thread. 55 void CancelImportProcessOnIOThread(); 56 57 // Report item completely downloaded on IO thread. 58 void NotifyItemFinishedOnIOThread(importer::ImportItem import_item); 59 60 // Begin ProfileImportProcessClient implementation. 61 virtual void OnProcessCrashed(int exit_status) OVERRIDE; 62 virtual void OnImportStart() OVERRIDE; 63 virtual void OnImportFinished(bool succeeded, 64 const std::string& error_msg) OVERRIDE; 65 virtual void OnImportItemStart(int item) OVERRIDE; 66 virtual void OnImportItemFinished(int item) OVERRIDE; 67 68 // Called on first message received when importing history; gives total 69 // number of rows to be imported. 70 virtual void OnHistoryImportStart(size_t total_history_rows_count) OVERRIDE; 71 72 // Called when a group of URLRows has been received. 73 virtual void OnHistoryImportGroup( 74 const std::vector<history::URLRow>& history_rows_group, 75 int visit_source) OVERRIDE; 76 77 // Called when the home page has been received. 78 virtual void OnHomePageImportReady(const GURL& home_page) OVERRIDE; 79 80 // First message received when importing bookmarks. 81 // |first_folder_name| can be NULL. 82 // |options| is described in ProfileWriter::BookmarkOptions. 83 // |total_bookmarks_count| is the total number of bookmarks to be imported. 84 virtual void OnBookmarksImportStart(const string16& first_folder_name, 85 int options, 86 size_t total_bookmarks_count) OVERRIDE; 87 88 // Called when a group of bookmarks has been received. 89 virtual void OnBookmarksImportGroup( 90 const std::vector<ProfileWriter::BookmarkEntry>& bookmarks_group) 91 OVERRIDE; 92 93 // First message received when importing favicons. |total_favicons_size| 94 // gives the total number of favicons to be imported. 95 virtual void OnFaviconsImportStart(size_t total_favicons_count) OVERRIDE; 96 97 // Called when a group of favicons has been received. 98 virtual void OnFaviconsImportGroup( 99 const std::vector<history::ImportedFaviconUsage>& favicons_group) 100 OVERRIDE; 101 102 // Called when the passwordform has been received. 103 virtual void OnPasswordFormImportReady( 104 const webkit_glue::PasswordForm& form) OVERRIDE; 105 106 // Called when search engines have been received. 107 virtual void OnKeywordsImportReady( 108 const std::vector<TemplateURL>& template_urls, 109 int default_keyword_index, 110 bool unique_on_host_and_path) OVERRIDE; 111 112 // End ProfileImportProcessClient implementation. 113 114 // These variables store data being collected from the importer until the 115 // entire group has been collected and is ready to be written to the profile. 116 std::vector<history::URLRow> history_rows_; 117 std::vector<ProfileWriter::BookmarkEntry> bookmarks_; 118 std::vector<history::ImportedFaviconUsage> favicons_; 119 120 // Usually some variation on IDS_BOOKMARK_GROUP_...; the name of the folder 121 // under which imported bookmarks will be placed. 122 string16 bookmarks_first_folder_name_; 123 124 // Determines how bookmarks should be added (ProfileWriter::BookmarkOptions). 125 int bookmarks_options_; 126 127 // Total number of bookmarks to import. 128 size_t total_bookmarks_count_; 129 130 // Total number of history items to import. 131 size_t total_history_rows_count_; 132 133 // Total number of favicons to import. 134 size_t total_favicons_count_; 135 136 // Notifications received from the ProfileImportProcessHost are passed back 137 // to process_importer_host_, which calls the ProfileWriter to record the 138 // import data. When the import process is done, process_importer_host_ 139 // deletes itself. 140 ExternalProcessImporterHost* process_importer_host_; 141 142 // Handles sending messages to the external process. Deletes itself when 143 // the external process dies (see ChildProcessHost::OnChildDied). 144 ProfileImportProcessHost* profile_import_process_host_; 145 146 // Data to be passed from the importer host to the external importer. 147 const importer::SourceProfile& source_profile_; 148 uint16 items_; 149 bool import_to_bookmark_bar_; 150 151 // Takes import data coming over IPC and delivers it to be written by the 152 // ProfileWriter. Released by ExternalProcessImporterClient in its 153 // destructor. 154 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