Print Preview: Refactoring print/cancel button and print summary.
[chromium-blink-merge.git] / content / common / child_process_info.cc
blobfad7e031de1909a3e548854724acd2d9b507669f
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"
7 #include <limits>
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) :
18 type_(type),
19 renderer_type_(RENDERER_UNKNOWN) {
20 if (id == -1)
21 id_ = GenerateChildProcessUniqueId();
22 else
23 id_ = id;
26 ChildProcessInfo::ChildProcessInfo(const ChildProcessInfo& original)
27 : type_(original.type_),
28 renderer_type_(original.renderer_type_),
29 name_(original.name_),
30 version_(original.version_),
31 id_(original.id_),
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_;
45 id_ = original.id_;
46 process_ = original.process_;
48 return *this;
51 // static
52 std::string ChildProcessInfo::GetTypeNameInEnglish(
53 ChildProcessInfo::ProcessType type) {
54 switch (type) {
55 case BROWSER_PROCESS:
56 return "Browser";
57 case RENDER_PROCESS:
58 return "Tab";
59 case PLUGIN_PROCESS:
60 return "Plug-in";
61 case WORKER_PROCESS:
62 return "Web Worker";
63 case UTILITY_PROCESS:
64 return "Utility";
65 case PROFILE_IMPORT_PROCESS:
66 return "Profile Import helper";
67 case ZYGOTE_PROCESS:
68 return "Zygote";
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";
75 case GPU_PROCESS:
76 return "GPU";
77 case PPAPI_PLUGIN_PROCESS:
78 return "Pepper Plugin";
79 case PPAPI_BROKER_PROCESS:
80 return "Pepper Plugin Broker";
81 case UNKNOWN_PROCESS:
82 default:
83 DCHECK(false) << "Unknown child process type!";
84 return "Unknown";
88 // static
89 std::string ChildProcessInfo::GetRendererTypeNameInEnglish(
90 ChildProcessInfo::RendererProcessType type) {
91 switch (type) {
92 case RENDERER_NORMAL:
93 return "Tab";
94 case RENDERER_CHROME:
95 return "Tab (Chrome)";
96 case RENDERER_EXTENSION:
97 return "Extension";
98 case RENDERER_DEVTOOLS:
99 return "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:
107 default:
108 NOTREACHED() << "Unknown renderer process type!";
109 return "Unknown";
113 // static
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()));
134 // static
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);