Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/utility_process_host.h" | 5 #include "content/browser/utility_process_host.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | |
| 9 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/utility_messages.h" | 11 #include "content/common/utility_messages.h" |
| 14 #include "content/common/indexed_db_key.h" | |
| 15 #include "content/common/serialized_script_value.h" | |
| 16 #include "ipc/ipc_switches.h" | 12 #include "ipc/ipc_switches.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | |
| 18 #include "ui/base/ui_base_switches.h" | 13 #include "ui/base/ui_base_switches.h" |
| 19 | 14 |
| 15 UtilityProcessHost::Client::Client() { | |
| 16 } | |
| 17 | |
| 18 UtilityProcessHost::Client::~Client() { | |
| 19 } | |
| 20 | |
| 21 void UtilityProcessHost::Client::OnProcessCrashed(int exit_code) { | |
| 22 } | |
| 23 | |
| 24 bool UtilityProcessHost::Client::OnMessageReceived( | |
| 25 const IPC::Message& message) { | |
| 26 return false; | |
| 27 } | |
| 28 | |
| 20 UtilityProcessHost::UtilityProcessHost(Client* client, | 29 UtilityProcessHost::UtilityProcessHost(Client* client, |
| 21 BrowserThread::ID client_thread_id) | 30 BrowserThread::ID client_thread_id) |
| 22 : BrowserChildProcessHost(UTILITY_PROCESS), | 31 : BrowserChildProcessHost(UTILITY_PROCESS), |
| 23 client_(client), | 32 client_(client), |
| 24 client_thread_id_(client_thread_id), | 33 client_thread_id_(client_thread_id), |
| 25 is_batch_mode_(false) { | 34 is_batch_mode_(false) { |
| 26 } | 35 } |
| 27 | 36 |
| 28 UtilityProcessHost::~UtilityProcessHost() { | 37 UtilityProcessHost::~UtilityProcessHost() { |
| 29 DCHECK(!is_batch_mode_); | 38 DCHECK(!is_batch_mode_); |
| 30 } | 39 } |
| 31 | 40 |
| 32 bool UtilityProcessHost::StartExtensionUnpacker(const FilePath& extension) { | 41 bool UtilityProcessHost::Send(IPC::Message* message) { |
| 33 // Grant the subprocess access to the entire subdir the extension file is | 42 if (!StartProcess()) |
|
Matt Perry
2011/06/09 01:40:33
It's not obvious that Send can only be called once
| |
| 34 // in, so that it can unpack to that dir. | |
| 35 if (!StartProcess(extension.DirName())) | |
| 36 return false; | 43 return false; |
| 37 | 44 |
| 38 Send(new UtilityMsg_UnpackExtension(extension)); | 45 return BrowserChildProcessHost::Send(message); |
| 39 return true; | |
| 40 } | |
| 41 | |
| 42 bool UtilityProcessHost::StartWebResourceUnpacker(const std::string& data) { | |
| 43 if (!StartProcess(FilePath())) | |
| 44 return false; | |
| 45 | |
| 46 Send(new UtilityMsg_UnpackWebResource(data)); | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 bool UtilityProcessHost::StartUpdateManifestParse(const std::string& xml) { | |
| 51 if (!StartProcess(FilePath())) | |
| 52 return false; | |
| 53 | |
| 54 Send(new UtilityMsg_ParseUpdateManifest(xml)); | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 bool UtilityProcessHost::StartImageDecoding( | |
| 59 const std::vector<unsigned char>& encoded_data) { | |
| 60 if (!StartProcess(FilePath())) | |
| 61 return false; | |
| 62 | |
| 63 Send(new UtilityMsg_DecodeImage(encoded_data)); | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 bool UtilityProcessHost::StartImageDecodingBase64( | |
| 68 const std::string& base64_encoded_data) { | |
| 69 if (!StartProcess(FilePath())) | |
| 70 return false; | |
| 71 | |
| 72 Send(new UtilityMsg_DecodeImageBase64(base64_encoded_data)); | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 bool UtilityProcessHost::StartIDBKeysFromValuesAndKeyPath( | |
| 77 int id, const std::vector<SerializedScriptValue>& serialized_values, | |
| 78 const string16& key_path) { | |
| 79 if (!StartProcess(FilePath())) | |
| 80 return false; | |
| 81 | |
| 82 Send(new UtilityMsg_IDBKeysFromValuesAndKeyPath( | |
| 83 id, serialized_values, key_path)); | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 bool UtilityProcessHost::StartInjectIDBKey( | |
| 88 const IndexedDBKey& key, const SerializedScriptValue& value, | |
| 89 const string16& key_path) { | |
| 90 if (!StartProcess(FilePath())) | |
| 91 return false; | |
| 92 | |
| 93 Send(new UtilityMsg_InjectIDBKey(key, value, key_path)); | |
| 94 return true; | |
| 95 } | |
| 96 | |
| 97 bool UtilityProcessHost::StartJSONParsing(const std::string& json) { | |
| 98 if (!StartProcess(FilePath())) | |
| 99 return false; | |
| 100 Send(new UtilityMsg_ParseJSON(json)); | |
| 101 return true; | |
| 102 } | 46 } |
| 103 | 47 |
| 104 bool UtilityProcessHost::StartBatchMode() { | 48 bool UtilityProcessHost::StartBatchMode() { |
| 105 CHECK(!is_batch_mode_); | 49 CHECK(!is_batch_mode_); |
| 106 is_batch_mode_ = StartProcess(FilePath()); | 50 is_batch_mode_ = StartProcess(); |
| 107 Send(new UtilityMsg_BatchMode_Started()); | 51 Send(new UtilityMsg_BatchMode_Started()); |
| 108 return is_batch_mode_; | 52 return is_batch_mode_; |
| 109 } | 53 } |
| 110 | 54 |
| 111 void UtilityProcessHost::EndBatchMode() { | 55 void UtilityProcessHost::EndBatchMode() { |
| 112 CHECK(is_batch_mode_); | 56 CHECK(is_batch_mode_); |
| 113 is_batch_mode_ = false; | 57 is_batch_mode_ = false; |
| 114 Send(new UtilityMsg_BatchMode_Finished()); | 58 Send(new UtilityMsg_BatchMode_Finished()); |
| 115 } | 59 } |
| 116 | 60 |
| 117 FilePath UtilityProcessHost::GetUtilityProcessCmd() { | 61 FilePath UtilityProcessHost::GetUtilityProcessCmd() { |
| 118 return GetChildPath(true); | 62 return GetChildPath(true); |
| 119 } | 63 } |
| 120 | 64 |
| 121 bool UtilityProcessHost::StartProcess(const FilePath& exposed_dir) { | 65 bool UtilityProcessHost::StartProcess() { |
| 122 if (is_batch_mode_) | 66 if (is_batch_mode_) |
| 123 return true; | 67 return true; |
| 124 // Name must be set or metrics_service will crash in any test which | 68 // Name must be set or metrics_service will crash in any test which |
| 125 // launches a UtilityProcessHost. | 69 // launches a UtilityProcessHost. |
| 126 set_name(L"utility process"); | 70 set_name(L"utility process"); |
| 127 | 71 |
| 128 if (!CreateChannel()) | 72 if (!CreateChannel()) |
| 129 return false; | 73 return false; |
| 130 | 74 |
| 131 FilePath exe_path = GetUtilityProcessCmd(); | 75 FilePath exe_path = GetUtilityProcessCmd(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 157 // Linux updating. | 101 // Linux updating. |
| 158 bool has_cmd_prefix = browser_command_line.HasSwitch( | 102 bool has_cmd_prefix = browser_command_line.HasSwitch( |
| 159 switches::kUtilityCmdPrefix); | 103 switches::kUtilityCmdPrefix); |
| 160 if (has_cmd_prefix) { | 104 if (has_cmd_prefix) { |
| 161 // launch the utility child process with some prefix (usually "xterm -e gdb | 105 // launch the utility child process with some prefix (usually "xterm -e gdb |
| 162 // --args"). | 106 // --args"). |
| 163 cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative( | 107 cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative( |
| 164 switches::kUtilityCmdPrefix)); | 108 switches::kUtilityCmdPrefix)); |
| 165 } | 109 } |
| 166 | 110 |
| 167 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, exposed_dir); | 111 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, exposed_dir_); |
| 168 #endif | 112 #endif |
| 169 | 113 |
| 170 Launch( | 114 Launch( |
| 171 #if defined(OS_WIN) | 115 #if defined(OS_WIN) |
| 172 exposed_dir, | 116 exposed_dir_, |
| 173 #elif defined(OS_POSIX) | 117 #elif defined(OS_POSIX) |
| 174 false, | 118 false, |
| 175 base::environment_vector(), | 119 base::environment_vector(), |
| 176 #endif | 120 #endif |
| 177 cmd_line); | 121 cmd_line); |
| 178 | 122 |
| 179 return true; | 123 return true; |
| 180 } | 124 } |
| 181 | 125 |
| 182 bool UtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | 126 bool UtilityProcessHost::OnMessageReceived(const IPC::Message& message) { |
| 183 BrowserThread::PostTask( | 127 BrowserThread::PostTask( |
| 184 client_thread_id_, FROM_HERE, | 128 client_thread_id_, FROM_HERE, |
| 185 NewRunnableMethod(client_.get(), &Client::OnMessageReceived, message)); | 129 NewRunnableMethod(client_.get(), &Client::OnMessageReceived, message)); |
| 186 return true; | 130 return true; |
| 187 } | 131 } |
| 188 | 132 |
| 189 void UtilityProcessHost::OnProcessCrashed(int exit_code) { | 133 void UtilityProcessHost::OnProcessCrashed(int exit_code) { |
| 190 BrowserThread::PostTask( | 134 BrowserThread::PostTask( |
| 191 client_thread_id_, FROM_HERE, | 135 client_thread_id_, FROM_HERE, |
| 192 NewRunnableMethod(client_.get(), &Client::OnProcessCrashed, exit_code)); | 136 NewRunnableMethod(client_.get(), &Client::OnProcessCrashed, exit_code)); |
| 193 } | 137 } |
| 194 | 138 |
| 195 bool UtilityProcessHost::CanShutdown() { | 139 bool UtilityProcessHost::CanShutdown() { |
| 196 return true; | 140 return true; |
| 197 } | 141 } |
| 198 | |
| 199 bool UtilityProcessHost::Client::OnMessageReceived( | |
| 200 const IPC::Message& message) { | |
| 201 bool handled = true; | |
| 202 IPC_BEGIN_MESSAGE_MAP(UtilityProcessHost, message) | |
| 203 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackExtension_Succeeded, | |
| 204 Client::OnUnpackExtensionSucceeded) | |
| 205 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackExtension_Failed, | |
| 206 Client::OnUnpackExtensionFailed) | |
| 207 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackWebResource_Succeeded, | |
| 208 Client::OnUnpackWebResourceSucceeded) | |
| 209 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackWebResource_Failed, | |
| 210 Client::OnUnpackWebResourceFailed) | |
| 211 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseUpdateManifest_Succeeded, | |
| 212 Client::OnParseUpdateManifestSucceeded) | |
| 213 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseUpdateManifest_Failed, | |
| 214 Client::OnParseUpdateManifestFailed) | |
| 215 IPC_MESSAGE_HANDLER(UtilityHostMsg_DecodeImage_Succeeded, | |
| 216 Client::OnDecodeImageSucceeded) | |
| 217 IPC_MESSAGE_HANDLER(UtilityHostMsg_DecodeImage_Failed, | |
| 218 Client::OnDecodeImageFailed) | |
| 219 IPC_MESSAGE_HANDLER(UtilityHostMsg_IDBKeysFromValuesAndKeyPath_Succeeded, | |
| 220 Client::OnIDBKeysFromValuesAndKeyPathSucceeded) | |
| 221 IPC_MESSAGE_HANDLER(UtilityHostMsg_IDBKeysFromValuesAndKeyPath_Failed, | |
| 222 Client::OnIDBKeysFromValuesAndKeyPathFailed) | |
| 223 IPC_MESSAGE_HANDLER(UtilityHostMsg_InjectIDBKey_Finished, | |
| 224 Client::OnInjectIDBKeyFinished) | |
| 225 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseJSON_Succeeded, | |
| 226 Client::OnJSONParseSucceeded) | |
| 227 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseJSON_Failed, | |
| 228 Client::OnJSONParseFailed) | |
| 229 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 230 IPC_END_MESSAGE_MAP_EX() | |
| 231 return handled; | |
| 232 } | |
| OLD | NEW |