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
3 // found in the LICENSE file.
5 #include "content/common/child_process_info.h"
9 #include "base/atomicops.h"
10 #include "base/i18n/rtl.h"
11 #include "base/logging.h"
12 #include "base/process_util.h"
13 #include "base/rand_util.h"
14 #include "base/stringprintf.h"
15 #include "base/utf_string_conversions.h"
17 ChildProcessInfo::ChildProcessInfo(ProcessType type
, int id
) :
19 renderer_type_(RENDERER_UNKNOWN
) {
21 id_
= GenerateChildProcessUniqueId();
26 ChildProcessInfo::ChildProcessInfo(const ChildProcessInfo
& original
)
27 : type_(original
.type_
),
28 renderer_type_(original
.renderer_type_
),
29 name_(original
.name_
),
30 version_(original
.version_
),
32 process_(original
.process_
) {
35 ChildProcessInfo::~ChildProcessInfo() {
38 ChildProcessInfo
& ChildProcessInfo::operator=(
39 const ChildProcessInfo
& original
) {
40 if (&original
!= this) {
41 type_
= original
.type_
;
42 renderer_type_
= original
.renderer_type_
;
43 name_
= original
.name_
;
44 version_
= original
.version_
;
46 process_
= original
.process_
;
52 std::string
ChildProcessInfo::GetTypeNameInEnglish(
53 ChildProcessInfo::ProcessType type
) {
65 case PROFILE_IMPORT_PROCESS
:
66 return "Profile Import helper";
69 case SANDBOX_HELPER_PROCESS
:
70 return "Sandbox helper";
71 case NACL_LOADER_PROCESS
:
72 return "Native Client module";
73 case NACL_BROKER_PROCESS
:
74 return "Native Client broker";
77 case PPAPI_PLUGIN_PROCESS
:
78 return "Pepper Plugin";
79 case PPAPI_BROKER_PROCESS
:
80 return "Pepper Plugin Broker";
83 DCHECK(false) << "Unknown child process type!";
89 std::string
ChildProcessInfo::GetRendererTypeNameInEnglish(
90 ChildProcessInfo::RendererProcessType type
) {
95 return "Tab (Chrome)";
96 case RENDERER_EXTENSION
:
98 case RENDERER_DEVTOOLS
:
100 case RENDERER_INTERSTITIAL
:
101 return "Interstitial";
102 case RENDERER_NOTIFICATION
:
103 return "Notification";
104 case RENDERER_BACKGROUND_APP
:
105 return "Background App";
106 case RENDERER_UNKNOWN
:
108 NOTREACHED() << "Unknown renderer process type!";
114 std::string
ChildProcessInfo::GetFullTypeNameInEnglish(
115 ChildProcessInfo::ProcessType type
,
116 ChildProcessInfo::RendererProcessType rtype
) {
117 if (type
== RENDER_PROCESS
)
118 return GetRendererTypeNameInEnglish(rtype
);
119 return GetTypeNameInEnglish(type
);
122 std::string
ChildProcessInfo::GenerateRandomChannelID(void* instance
) {
123 // Note: the string must start with the current process id, this is how
124 // child processes determine the pid of the parent.
125 // Build the channel ID. This is composed of a unique identifier for the
126 // parent browser process, an identifier for the child instance, and a random
127 // component. We use a random component so that a hacked child process can't
128 // cause denial of service by causing future named pipe creation to fail.
129 return base::StringPrintf("%d.%p.%d",
130 base::GetCurrentProcId(), instance
,
131 base::RandInt(0, std::numeric_limits
<int>::max()));
135 int ChildProcessInfo::GenerateChildProcessUniqueId() {
136 // This function must be threadsafe.
137 static base::subtle::Atomic32 last_unique_child_id
= 0;
138 return base::subtle::NoBarrier_AtomicIncrement(&last_unique_child_id
, 1);