[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1 | // Copyright (c) 2009 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/common/child_process_info.h" |
| 6 | |
[email protected] | 6dffde32 | 2009-02-18 03:47:48 | [diff] [blame] | 7 | #include <limits> |
| 8 | |
[email protected] | a92b864 | 2009-05-05 23:38:56 | [diff] [blame] | 9 | #include "app/l10n_util.h" |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 10 | #include "base/atomicops.h" |
[email protected] | 7cf1b6ce | 2010-03-20 06:37:01 | [diff] [blame] | 11 | #include "base/i18n/rtl.h" |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 6dffde32 | 2009-02-18 03:47:48 | [diff] [blame] | 13 | #include "base/process_util.h" |
| 14 | #include "base/rand_util.h" |
| 15 | #include "base/string_util.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 16 | #include "grit/generated_resources.h" |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 17 | |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 18 | ChildProcessInfo::ChildProcessInfo(const ChildProcessInfo& original) |
| 19 | : type_(original.type_), |
| 20 | name_(original.name_), |
[email protected] | 7e3ceeb4 | 2010-06-21 22:27:35 | [diff] [blame] | 21 | version_(original.version_), |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 22 | id_(original.id_), |
| 23 | process_(original.process_) { |
| 24 | } |
| 25 | |
| 26 | ChildProcessInfo::~ChildProcessInfo() { |
| 27 | } |
| 28 | |
| 29 | ChildProcessInfo& ChildProcessInfo::operator=( |
| 30 | const ChildProcessInfo& original) { |
| 31 | if (&original != this) { |
| 32 | type_ = original.type_; |
| 33 | name_ = original.name_; |
[email protected] | 7e3ceeb4 | 2010-06-21 22:27:35 | [diff] [blame] | 34 | version_ = original.version_; |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 35 | id_ = original.id_; |
| 36 | process_ = original.process_; |
| 37 | } |
| 38 | return *this; |
| 39 | } |
| 40 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 41 | std::wstring ChildProcessInfo::GetTypeNameInEnglish( |
| 42 | ChildProcessInfo::ProcessType type) { |
| 43 | switch (type) { |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 44 | case BROWSER_PROCESS: |
| 45 | return L"Browser"; |
| 46 | case RENDER_PROCESS: |
| 47 | return L"Tab"; |
| 48 | case PLUGIN_PROCESS: |
| 49 | return L"Plug-in"; |
| 50 | case WORKER_PROCESS: |
| 51 | return L"Web Worker"; |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 52 | case UTILITY_PROCESS: |
| 53 | return L"Utility"; |
[email protected] | 2735e369 | 2009-09-25 18:19:39 | [diff] [blame] | 54 | case PROFILE_IMPORT_PROCESS: |
| 55 | return L"Profile Import helper"; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 56 | case ZYGOTE_PROCESS: |
| 57 | return L"Zygote"; |
| 58 | case SANDBOX_HELPER_PROCESS: |
| 59 | return L"Sandbox helper"; |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 60 | case NACL_LOADER_PROCESS: |
[email protected] | 663177b | 2009-10-14 03:38:36 | [diff] [blame] | 61 | return L"Native Client module"; |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 62 | case NACL_BROKER_PROCESS: |
| 63 | return L"Native Client broker"; |
[email protected] | 6c112aed | 2010-06-14 21:23:43 | [diff] [blame] | 64 | case GPU_PROCESS: |
| 65 | return L"GPU"; |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 66 | case UNKNOWN_PROCESS: |
| 67 | default: |
| 68 | DCHECK(false) << "Unknown child process type!"; |
| 69 | return L"Unknown"; |
| 70 | } |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | std::wstring ChildProcessInfo::GetLocalizedTitle() const { |
| 74 | std::wstring title = name_; |
| 75 | if (type_ == ChildProcessInfo::PLUGIN_PROCESS && title.empty()) |
| 76 | title = l10n_util::GetString(IDS_TASK_MANAGER_UNKNOWN_PLUGIN_NAME); |
| 77 | |
[email protected] | 7e3ceeb4 | 2010-06-21 22:27:35 | [diff] [blame] | 78 | // Explicitly mark name as LTR if there is no strong RTL character, |
| 79 | // to avoid the wrong concatenation result similar to "!Yahoo! Mail: the |
| 80 | // best web-based Email: NIGULP", in which "NIGULP" stands for the Hebrew |
| 81 | // or Arabic word for "plugin". |
| 82 | base::i18n::AdjustStringForLocaleDirection(title, &title); |
| 83 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 84 | int message_id; |
| 85 | if (type_ == ChildProcessInfo::PLUGIN_PROCESS) { |
| 86 | message_id = IDS_TASK_MANAGER_PLUGIN_PREFIX; |
[email protected] | 7e3ceeb4 | 2010-06-21 22:27:35 | [diff] [blame] | 87 | return l10n_util::GetStringF(message_id, title, version_.c_str()); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 88 | } else if (type_ == ChildProcessInfo::WORKER_PROCESS) { |
| 89 | message_id = IDS_TASK_MANAGER_WORKER_PREFIX; |
[email protected] | 24d61732 | 2009-09-11 19:54:16 | [diff] [blame] | 90 | } else if (type_ == ChildProcessInfo::UTILITY_PROCESS) { |
| 91 | message_id = IDS_TASK_MANAGER_UTILITY_PREFIX; |
[email protected] | 2735e369 | 2009-09-25 18:19:39 | [diff] [blame] | 92 | } else if (type_ == ChildProcessInfo::PROFILE_IMPORT_PROCESS) { |
| 93 | message_id = IDS_TASK_MANAGER_PROFILE_IMPORT_PREFIX; |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 94 | } else if (type_ == ChildProcessInfo::NACL_LOADER_PROCESS) { |
[email protected] | 663177b | 2009-10-14 03:38:36 | [diff] [blame] | 95 | message_id = IDS_TASK_MANAGER_NACL_PREFIX; |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 96 | } else if (type_ == ChildProcessInfo::NACL_BROKER_PROCESS) { |
| 97 | message_id = IDS_TASK_MANAGER_NACL_BROKER_PREFIX; |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 98 | } else { |
| 99 | DCHECK(false) << "Need localized name for child process type."; |
| 100 | return title; |
| 101 | } |
| 102 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 103 | return l10n_util::GetStringF(message_id, title); |
| 104 | } |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 105 | |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 106 | ChildProcessInfo::ChildProcessInfo(ProcessType type, int id) : type_(type) { |
| 107 | if (id == -1) |
| 108 | id_ = GenerateChildProcessUniqueId(); |
| 109 | else |
| 110 | id_ = id; |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 113 | std::string ChildProcessInfo::GenerateRandomChannelID(void* instance) { |
[email protected] | 6dffde32 | 2009-02-18 03:47:48 | [diff] [blame] | 114 | // Note: the string must start with the current process id, this is how |
| 115 | // child processes determine the pid of the parent. |
| 116 | // Build the channel ID. This is composed of a unique identifier for the |
| 117 | // parent browser process, an identifier for the child instance, and a random |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 118 | // component. We use a random component so that a hacked child process can't |
[email protected] | 6dffde32 | 2009-02-18 03:47:48 | [diff] [blame] | 119 | // cause denial of service by causing future named pipe creation to fail. |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 120 | return StringPrintf("%d.%p.%d", |
[email protected] | 6dffde32 | 2009-02-18 03:47:48 | [diff] [blame] | 121 | base::GetCurrentProcId(), instance, |
| 122 | base::RandInt(0, std::numeric_limits<int>::max())); |
[email protected] | a436d92 | 2009-02-13 23:16:42 | [diff] [blame] | 123 | } |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 124 | |
| 125 | // static |
| 126 | int ChildProcessInfo::GenerateChildProcessUniqueId() { |
| 127 | // This function must be threadsafe. |
| 128 | static base::subtle::Atomic32 last_unique_child_id = 0; |
| 129 | return base::subtle::NoBarrier_AtomicIncrement(&last_unique_child_id, 1); |
| 130 | } |