OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/common/child_process_info.h" | |
6 | |
7 #include <limits> | |
8 | |
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" | |
16 | |
17 ChildProcessInfo::ChildProcessInfo(content::ProcessType type, int id) : | |
18 type_(type) { | |
19 if (id == -1) | |
20 id_ = GenerateChildProcessUniqueId(); | |
21 else | |
22 id_ = id; | |
23 } | |
24 | |
25 ChildProcessInfo::ChildProcessInfo(const ChildProcessInfo& original) | |
26 : type_(original.type_), | |
27 name_(original.name_), | |
28 version_(original.version_), | |
29 id_(original.id_), | |
30 process_(original.process_) { | |
31 } | |
32 | |
33 ChildProcessInfo::~ChildProcessInfo() { | |
34 } | |
35 | |
36 ChildProcessInfo& ChildProcessInfo::operator=( | |
37 const ChildProcessInfo& original) { | |
38 if (&original != this) { | |
39 type_ = original.type_; | |
40 name_ = original.name_; | |
41 version_ = original.version_; | |
42 id_ = original.id_; | |
43 process_ = original.process_; | |
44 } | |
45 return *this; | |
46 } | |
47 | |
48 std::string ChildProcessInfo::GenerateRandomChannelID(void* instance) { | |
49 // Note: the string must start with the current process id, this is how | |
50 // child processes determine the pid of the parent. | |
51 // Build the channel ID. This is composed of a unique identifier for the | |
52 // parent browser process, an identifier for the child instance, and a random | |
53 // component. We use a random component so that a hacked child process can't | |
54 // cause denial of service by causing future named pipe creation to fail. | |
55 return base::StringPrintf("%d.%p.%d", | |
56 base::GetCurrentProcId(), instance, | |
57 base::RandInt(0, std::numeric_limits<int>::max())); | |
58 } | |
59 | |
60 // static | |
61 int ChildProcessInfo::GenerateChildProcessUniqueId() { | |
62 // This function must be threadsafe. | |
63 static base::subtle::Atomic32 last_unique_child_id = 0; | |
64 return base::subtle::NoBarrier_AtomicIncrement(&last_unique_child_id, 1); | |
65 } | |
OLD | NEW |