blob: f08cb552601578ba5cc6cd8994364debe7912fb3 [file] [log] [blame]
[email protected]a27a9382009-02-11 23:55:101// 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]6dffde322009-02-18 03:47:487#include <limits>
8
[email protected]a92b8642009-05-05 23:38:569#include "app/l10n_util.h"
[email protected]76543b92009-08-31 17:27:4510#include "base/atomicops.h"
[email protected]7cf1b6ce2010-03-20 06:37:0111#include "base/i18n/rtl.h"
[email protected]a27a9382009-02-11 23:55:1012#include "base/logging.h"
[email protected]6dffde322009-02-18 03:47:4813#include "base/process_util.h"
14#include "base/rand_util.h"
15#include "base/string_util.h"
[email protected]34ac8f32009-02-22 23:03:2716#include "grit/generated_resources.h"
[email protected]a27a9382009-02-11 23:55:1017
[email protected]76543b92009-08-31 17:27:4518ChildProcessInfo::ChildProcessInfo(const ChildProcessInfo& original)
19 : type_(original.type_),
20 name_(original.name_),
[email protected]7e3ceeb42010-06-21 22:27:3521 version_(original.version_),
[email protected]76543b92009-08-31 17:27:4522 id_(original.id_),
23 process_(original.process_) {
24}
25
26ChildProcessInfo::~ChildProcessInfo() {
27}
28
29ChildProcessInfo& ChildProcessInfo::operator=(
30 const ChildProcessInfo& original) {
31 if (&original != this) {
32 type_ = original.type_;
33 name_ = original.name_;
[email protected]7e3ceeb42010-06-21 22:27:3534 version_ = original.version_;
[email protected]76543b92009-08-31 17:27:4535 id_ = original.id_;
36 process_ = original.process_;
37 }
38 return *this;
39}
40
[email protected]a27a9382009-02-11 23:55:1041std::wstring ChildProcessInfo::GetTypeNameInEnglish(
42 ChildProcessInfo::ProcessType type) {
43 switch (type) {
[email protected]a436d922009-02-13 23:16:4244 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]1fca1492009-05-15 22:23:4352 case UTILITY_PROCESS:
53 return L"Utility";
[email protected]2735e3692009-09-25 18:19:3954 case PROFILE_IMPORT_PROCESS:
55 return L"Profile Import helper";
[email protected]54fd1d32009-09-01 00:12:5856 case ZYGOTE_PROCESS:
57 return L"Zygote";
58 case SANDBOX_HELPER_PROCESS:
59 return L"Sandbox helper";
[email protected]103607e2010-02-01 18:57:0960 case NACL_LOADER_PROCESS:
[email protected]663177b2009-10-14 03:38:3661 return L"Native Client module";
[email protected]aef8d5ae2010-03-17 22:40:5262 case NACL_BROKER_PROCESS:
63 return L"Native Client broker";
[email protected]6c112aed2010-06-14 21:23:4364 case GPU_PROCESS:
65 return L"GPU";
[email protected]a436d922009-02-13 23:16:4266 case UNKNOWN_PROCESS:
67 default:
68 DCHECK(false) << "Unknown child process type!";
69 return L"Unknown";
70 }
[email protected]a27a9382009-02-11 23:55:1071}
72
73std::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]7e3ceeb42010-06-21 22:27:3578 // 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]a27a9382009-02-11 23:55:1084 int message_id;
85 if (type_ == ChildProcessInfo::PLUGIN_PROCESS) {
86 message_id = IDS_TASK_MANAGER_PLUGIN_PREFIX;
[email protected]7e3ceeb42010-06-21 22:27:3587 return l10n_util::GetStringF(message_id, title, version_.c_str());
[email protected]a27a9382009-02-11 23:55:1088 } else if (type_ == ChildProcessInfo::WORKER_PROCESS) {
89 message_id = IDS_TASK_MANAGER_WORKER_PREFIX;
[email protected]24d617322009-09-11 19:54:1690 } else if (type_ == ChildProcessInfo::UTILITY_PROCESS) {
91 message_id = IDS_TASK_MANAGER_UTILITY_PREFIX;
[email protected]2735e3692009-09-25 18:19:3992 } else if (type_ == ChildProcessInfo::PROFILE_IMPORT_PROCESS) {
93 message_id = IDS_TASK_MANAGER_PROFILE_IMPORT_PREFIX;
[email protected]103607e2010-02-01 18:57:0994 } else if (type_ == ChildProcessInfo::NACL_LOADER_PROCESS) {
[email protected]663177b2009-10-14 03:38:3695 message_id = IDS_TASK_MANAGER_NACL_PREFIX;
[email protected]aef8d5ae2010-03-17 22:40:5296 } else if (type_ == ChildProcessInfo::NACL_BROKER_PROCESS) {
97 message_id = IDS_TASK_MANAGER_NACL_BROKER_PREFIX;
[email protected]a27a9382009-02-11 23:55:1098 } else {
99 DCHECK(false) << "Need localized name for child process type.";
100 return title;
101 }
102
[email protected]a27a9382009-02-11 23:55:10103 return l10n_util::GetStringF(message_id, title);
104}
[email protected]a436d922009-02-13 23:16:42105
[email protected]76543b92009-08-31 17:27:45106ChildProcessInfo::ChildProcessInfo(ProcessType type, int id) : type_(type) {
107 if (id == -1)
108 id_ = GenerateChildProcessUniqueId();
109 else
110 id_ = id;
[email protected]a436d922009-02-13 23:16:42111}
112
[email protected]9a3a293b2009-06-04 22:28:16113std::string ChildProcessInfo::GenerateRandomChannelID(void* instance) {
[email protected]6dffde322009-02-18 03:47:48114 // 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]f0a51fb52009-03-05 12:46:38118 // component. We use a random component so that a hacked child process can't
[email protected]6dffde322009-02-18 03:47:48119 // cause denial of service by causing future named pipe creation to fail.
[email protected]34b2b002009-11-20 06:53:28120 return StringPrintf("%d.%p.%d",
[email protected]6dffde322009-02-18 03:47:48121 base::GetCurrentProcId(), instance,
122 base::RandInt(0, std::numeric_limits<int>::max()));
[email protected]a436d922009-02-13 23:16:42123}
[email protected]76543b92009-08-31 17:27:45124
125// static
126int 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}