Roll src/third_party/WebKit f103b33:23b201b (svn 202622:202623)
[chromium-blink-merge.git] / ipc / ipc_channel.cc
blob6772e1c55794d1dec4b4df8392b07e73ca738e3e
1 // Copyright (c) 2012 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 "ipc/ipc_channel.h"
7 #include <stdint.h>
9 #include <limits>
11 #include "base/atomic_sequence_num.h"
12 #include "base/rand_util.h"
13 #include "base/strings/stringprintf.h"
15 namespace {
17 // Global atomic used to guarantee channel IDs are unique.
18 base::StaticAtomicSequenceNumber g_last_id;
20 } // namespace
22 namespace IPC {
24 // static
25 std::string Channel::GenerateUniqueRandomChannelID() {
26 // Note: the string must start with the current process id, this is how
27 // some child processes determine the pid of the parent.
29 // This is composed of a unique incremental identifier, the process ID of
30 // the creator, an identifier for the child instance, and a strong random
31 // component. The strong random component prevents other processes from
32 // hijacking or squatting on predictable channel names.
33 #if defined(OS_NACL_NONSFI)
34 // The seccomp sandbox disallows use of getpid(), so we provide a
35 // dummy PID.
36 int process_id = -1;
37 #else
38 int process_id = base::GetCurrentProcId();
39 #endif
40 return base::StringPrintf("%d.%u.%d",
41 process_id,
42 g_last_id.GetNext(),
43 base::RandInt(0, std::numeric_limits<int32_t>::max()));
46 Channel::OutputElement::OutputElement(Message* message)
47 : message_(message), buffer_(nullptr), length_(0) {}
49 Channel::OutputElement::OutputElement(void* buffer, size_t length)
50 : message_(nullptr), buffer_(buffer), length_(length) {}
52 Channel::OutputElement::~OutputElement() {
53 free(buffer_);
56 } // namespace IPC