| OLD | NEW |
| (Empty) |
| 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 #include "content/browser/utility_process_host.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "content/browser/browser_child_process_host_impl.h" | |
| 13 #include "content/common/child_process_host_impl.h" | |
| 14 #include "content/common/utility_messages.h" | |
| 15 #include "content/public/browser/content_browser_client.h" | |
| 16 #include "content/public/common/content_switches.h" | |
| 17 #include "ipc/ipc_switches.h" | |
| 18 #include "ui/base/ui_base_switches.h" | |
| 19 #include "webkit/plugins/plugin_switches.h" | |
| 20 | |
| 21 using content::BrowserThread; | |
| 22 using content::ChildProcessHost; | |
| 23 | |
| 24 UtilityProcessHost::Client::Client() { | |
| 25 } | |
| 26 | |
| 27 UtilityProcessHost::Client::~Client() { | |
| 28 } | |
| 29 | |
| 30 void UtilityProcessHost::Client::OnProcessCrashed(int exit_code) { | |
| 31 } | |
| 32 | |
| 33 bool UtilityProcessHost::Client::OnMessageReceived( | |
| 34 const IPC::Message& message) { | |
| 35 return false; | |
| 36 } | |
| 37 | |
| 38 UtilityProcessHost::UtilityProcessHost(Client* client, | |
| 39 BrowserThread::ID client_thread_id) | |
| 40 : client_(client), | |
| 41 client_thread_id_(client_thread_id), | |
| 42 is_batch_mode_(false), | |
| 43 no_sandbox_(false), | |
| 44 #if defined(OS_LINUX) | |
| 45 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), | |
| 46 #else | |
| 47 child_flags_(ChildProcessHost::CHILD_NORMAL), | |
| 48 #endif | |
| 49 use_linux_zygote_(false), | |
| 50 started_(false) { | |
| 51 process_.reset( | |
| 52 new BrowserChildProcessHostImpl(content::PROCESS_TYPE_UTILITY, this)); | |
| 53 } | |
| 54 | |
| 55 UtilityProcessHost::~UtilityProcessHost() { | |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 57 DCHECK(!is_batch_mode_); | |
| 58 } | |
| 59 | |
| 60 bool UtilityProcessHost::Send(IPC::Message* message) { | |
| 61 if (!StartProcess()) | |
| 62 return false; | |
| 63 | |
| 64 return process_->Send(message); | |
| 65 } | |
| 66 | |
| 67 bool UtilityProcessHost::StartBatchMode() { | |
| 68 CHECK(!is_batch_mode_); | |
| 69 is_batch_mode_ = StartProcess(); | |
| 70 Send(new UtilityMsg_BatchMode_Started()); | |
| 71 return is_batch_mode_; | |
| 72 } | |
| 73 | |
| 74 void UtilityProcessHost::EndBatchMode() { | |
| 75 CHECK(is_batch_mode_); | |
| 76 is_batch_mode_ = false; | |
| 77 Send(new UtilityMsg_BatchMode_Finished()); | |
| 78 } | |
| 79 | |
| 80 FilePath UtilityProcessHost::GetUtilityProcessCmd() { | |
| 81 return ChildProcessHost::GetChildPath(child_flags_); | |
| 82 } | |
| 83 | |
| 84 bool UtilityProcessHost::StartProcess() { | |
| 85 if (started_) | |
| 86 return true; | |
| 87 started_ = true; | |
| 88 | |
| 89 if (is_batch_mode_) | |
| 90 return true; | |
| 91 // Name must be set or metrics_service will crash in any test which | |
| 92 // launches a UtilityProcessHost. | |
| 93 process_->SetName(ASCIIToUTF16("utility process")); | |
| 94 | |
| 95 std::string channel_id = process_->GetHost()->CreateChannel(); | |
| 96 if (channel_id.empty()) | |
| 97 return false; | |
| 98 | |
| 99 FilePath exe_path = GetUtilityProcessCmd(); | |
| 100 if (exe_path.empty()) { | |
| 101 NOTREACHED() << "Unable to get utility process binary name."; | |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 CommandLine* cmd_line = new CommandLine(exe_path); | |
| 106 cmd_line->AppendSwitchASCII(switches::kProcessType, | |
| 107 switches::kUtilityProcess); | |
| 108 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id); | |
| 109 std::string locale = | |
| 110 content::GetContentClient()->browser()->GetApplicationLocale(); | |
| 111 cmd_line->AppendSwitchASCII(switches::kLang, locale); | |
| 112 | |
| 113 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); | |
| 114 if (browser_command_line.HasSwitch(switches::kChromeFrame)) | |
| 115 cmd_line->AppendSwitch(switches::kChromeFrame); | |
| 116 if (no_sandbox_ || browser_command_line.HasSwitch(switches::kNoSandbox)) | |
| 117 cmd_line->AppendSwitch(switches::kNoSandbox); | |
| 118 if (browser_command_line.HasSwitch(switches::kDebugPluginLoading)) | |
| 119 cmd_line->AppendSwitch(switches::kDebugPluginLoading); | |
| 120 | |
| 121 #if defined(OS_POSIX) | |
| 122 // TODO(port): Sandbox this on Linux. Also, zygote this to work with | |
| 123 // Linux updating. | |
| 124 bool has_cmd_prefix = browser_command_line.HasSwitch( | |
| 125 switches::kUtilityCmdPrefix); | |
| 126 if (has_cmd_prefix) { | |
| 127 // launch the utility child process with some prefix (usually "xterm -e gdb | |
| 128 // --args"). | |
| 129 cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative( | |
| 130 switches::kUtilityCmdPrefix)); | |
| 131 } | |
| 132 | |
| 133 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, exposed_dir_); | |
| 134 #endif | |
| 135 | |
| 136 bool use_zygote = false; | |
| 137 | |
| 138 #if defined(OS_LINUX) | |
| 139 use_zygote = !no_sandbox_ && use_linux_zygote_; | |
| 140 #endif | |
| 141 | |
| 142 process_->Launch( | |
| 143 #if defined(OS_WIN) | |
| 144 exposed_dir_, | |
| 145 #elif defined(OS_POSIX) | |
| 146 use_zygote, | |
| 147 env_, | |
| 148 #endif | |
| 149 cmd_line); | |
| 150 | |
| 151 return true; | |
| 152 } | |
| 153 | |
| 154 bool UtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | |
| 155 BrowserThread::PostTask( | |
| 156 client_thread_id_, FROM_HERE, | |
| 157 base::Bind(base::IgnoreResult(&Client::OnMessageReceived), | |
| 158 client_.get(), message)); | |
| 159 return true; | |
| 160 } | |
| 161 | |
| 162 void UtilityProcessHost::OnProcessCrashed(int exit_code) { | |
| 163 BrowserThread::PostTask( | |
| 164 client_thread_id_, FROM_HERE, | |
| 165 base::Bind(&Client::OnProcessCrashed, client_.get(), exit_code)); | |
| 166 } | |
| OLD | NEW |