[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | #include "chrome/browser/profile_import_process_host.h" |
| 6 | |
| 7 | #include "app/l10n_util.h" |
| 8 | #include "base/command_line.h" |
| 9 | #include "base/message_loop.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 10 | #include "base/string_number_conversions.h" |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 11 | #include "base/values.h" |
| 12 | #include "chrome/browser/importer/firefox_importer_utils.h" |
| 13 | #include "chrome/browser/importer/importer_messages.h" |
| 14 | #include "chrome/common/chrome_switches.h" |
| 15 | #include "grit/generated_resources.h" |
| 16 | #include "ipc/ipc_switches.h" |
| 17 | |
| 18 | ProfileImportProcessHost::ProfileImportProcessHost( |
| 19 | ResourceDispatcherHost* resource_dispatcher, |
| 20 | ImportProcessClient* import_process_client, |
| 21 | ChromeThread::ID thread_id) |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 22 | : BrowserChildProcessHost(PROFILE_IMPORT_PROCESS, resource_dispatcher), |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 23 | import_process_client_(import_process_client), |
| 24 | thread_id_(thread_id) { |
| 25 | } |
| 26 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 27 | ProfileImportProcessHost::~ProfileImportProcessHost() { |
| 28 | } |
| 29 | |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 30 | bool ProfileImportProcessHost::StartProfileImportProcess( |
| 31 | const importer::ProfileInfo& profile_info, int items, |
| 32 | bool import_to_bookmark_bar) { |
| 33 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 34 | if (!StartProcess()) |
| 35 | return false; |
| 36 | |
| 37 | // Dictionary of all localized strings that could be needed by the importer |
| 38 | // in the external process. |
| 39 | DictionaryValue localized_strings; |
| 40 | localized_strings.SetString( |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 41 | base::IntToString(IDS_BOOKMARK_GROUP_FROM_FIREFOX), |
| 42 | l10n_util::GetStringUTF8(IDS_BOOKMARK_GROUP_FROM_FIREFOX)); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 43 | localized_strings.SetString( |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 44 | base::IntToString(IDS_BOOKMARK_GROUP_FROM_SAFARI), |
| 45 | l10n_util::GetStringUTF8(IDS_BOOKMARK_GROUP_FROM_SAFARI)); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 46 | localized_strings.SetString( |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 47 | base::IntToString(IDS_IMPORT_FROM_FIREFOX), |
| 48 | l10n_util::GetStringUTF8(IDS_IMPORT_FROM_FIREFOX)); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 49 | localized_strings.SetString( |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 50 | base::IntToString(IDS_IMPORT_FROM_GOOGLE_TOOLBAR), |
| 51 | l10n_util::GetStringUTF8(IDS_IMPORT_FROM_GOOGLE_TOOLBAR)); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 52 | localized_strings.SetString( |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 53 | base::IntToString(IDS_IMPORT_FROM_SAFARI), |
| 54 | l10n_util::GetStringUTF8(IDS_IMPORT_FROM_SAFARI)); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 55 | |
| 56 | Send(new ProfileImportProcessMsg_StartImport( |
| 57 | profile_info, items, localized_strings, import_to_bookmark_bar)); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | bool ProfileImportProcessHost::CancelProfileImportProcess() { |
| 62 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 63 | Send(new ProfileImportProcessMsg_CancelImport()); |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | bool ProfileImportProcessHost::ReportImportItemFinished( |
| 68 | importer::ImportItem item) { |
| 69 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 70 | Send(new ProfileImportProcessMsg_ReportImportItemFinished(item)); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | FilePath ProfileImportProcessHost::GetProfileImportProcessCmd() { |
| 75 | return GetChildPath(true); |
| 76 | } |
| 77 | |
| 78 | bool ProfileImportProcessHost::StartProcess() { |
| 79 | set_name(L"profile import process"); |
| 80 | |
| 81 | if (!CreateChannel()) |
| 82 | return false; |
| 83 | |
| 84 | FilePath exe_path = GetProfileImportProcessCmd(); |
| 85 | if (exe_path.empty()) { |
| 86 | NOTREACHED() << "Unable to get profile import process binary name."; |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | CommandLine* cmd_line = new CommandLine(exe_path); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 91 | cmd_line->AppendSwitchASCII(switches::kProcessType, |
| 92 | switches::kProfileImportProcess); |
| 93 | cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id()); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 94 | |
| 95 | SetCrashReporterCommandLine(cmd_line); |
| 96 | |
| 97 | const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); |
| 98 | if (browser_command_line.HasSwitch(switches::kChromeFrame)) |
| 99 | cmd_line->AppendSwitch(switches::kChromeFrame); |
| 100 | |
| 101 | #if defined(OS_MACOSX) |
| 102 | base::environment_vector env; |
| 103 | std::string dylib_path = GetFirefoxDylibPath().value(); |
| 104 | if (!dylib_path.empty()) |
| 105 | env.push_back(std::make_pair("DYLD_FALLBACK_LIBRARY_PATH", dylib_path)); |
| 106 | |
| 107 | Launch(false, env, cmd_line); |
| 108 | #elif defined(OS_WIN) |
| 109 | FilePath no_exposed_directory; |
| 110 | |
| 111 | Launch(no_exposed_directory, cmd_line); |
| 112 | #else |
| 113 | base::environment_vector env; |
| 114 | |
| 115 | Launch(false, env, cmd_line); |
| 116 | #endif |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | void ProfileImportProcessHost::OnMessageReceived(const IPC::Message& message) { |
| 122 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 123 | ChromeThread::PostTask( |
| 124 | thread_id_, FROM_HERE, |
| 125 | NewRunnableMethod(import_process_client_.get(), |
| 126 | &ImportProcessClient::OnMessageReceived, |
| 127 | message)); |
| 128 | } |
| 129 | |
| 130 | void ProfileImportProcessHost::OnProcessCrashed() { |
| 131 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 132 | ChromeThread::PostTask( |
| 133 | thread_id_, FROM_HERE, |
| 134 | NewRunnableMethod(import_process_client_.get(), |
| 135 | &ImportProcessClient::OnProcessCrashed)); |
| 136 | } |
| 137 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 138 | bool ProfileImportProcessHost::CanShutdown() { |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | URLRequestContext* ProfileImportProcessHost::GetRequestContext( |
| 143 | uint32 request_id, |
| 144 | const ViewHostMsg_Resource_Request& request_data) { |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | ProfileImportProcessHost::ImportProcessClient::ImportProcessClient() {} |
| 149 | |
| 150 | ProfileImportProcessHost::ImportProcessClient::~ImportProcessClient() {} |
| 151 | |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 152 | void ProfileImportProcessHost::ImportProcessClient::OnMessageReceived( |
| 153 | const IPC::Message& message) { |
| 154 | IPC_BEGIN_MESSAGE_MAP(ProfileImportProcessHost, message) |
| 155 | // Notification messages about the state of the import process. |
| 156 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_Import_Started, |
| 157 | ImportProcessClient::OnImportStart) |
| 158 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_Import_Finished, |
| 159 | ImportProcessClient::OnImportFinished) |
| 160 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_ImportItem_Started, |
| 161 | ImportProcessClient::OnImportItemStart) |
| 162 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_ImportItem_Finished, |
| 163 | ImportProcessClient::OnImportItemFinished) |
| 164 | |
| 165 | // Data messages containing items to be written to the user profile. |
| 166 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyHistoryImportStart, |
| 167 | ImportProcessClient::OnHistoryImportStart) |
| 168 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyHistoryImportGroup, |
| 169 | ImportProcessClient::OnHistoryImportGroup) |
| 170 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyHomePageImportReady, |
| 171 | ImportProcessClient::OnHomePageImportReady) |
| 172 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyBookmarksImportStart, |
| 173 | ImportProcessClient::OnBookmarksImportStart) |
| 174 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyBookmarksImportGroup, |
| 175 | ImportProcessClient::OnBookmarksImportGroup) |
| 176 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyFavIconsImportStart, |
| 177 | ImportProcessClient::OnFavIconsImportStart) |
| 178 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyFavIconsImportGroup, |
| 179 | ImportProcessClient::OnFavIconsImportGroup) |
| 180 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyPasswordFormReady, |
| 181 | ImportProcessClient::OnPasswordFormImportReady) |
| 182 | IPC_MESSAGE_HANDLER(ProfileImportProcessHostMsg_NotifyKeywordsReady, |
| 183 | ImportProcessClient::OnKeywordsImportReady) |
| 184 | IPC_END_MESSAGE_MAP_EX() |
| 185 | } |