xref: /aosp_15_r20/external/cronet/ipc/ipc_channel.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 "ipc/ipc_channel.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <limits>
11 
12 #include "base/atomic_sequence_num.h"
13 #include "base/rand_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "build/build_config.h"
16 
17 namespace {
18 
19 // Global atomic used to guarantee channel IDs are unique.
20 base::AtomicSequenceNumber g_last_id;
21 
22 }  // namespace
23 
24 namespace IPC {
25 
26 // static
27 constexpr size_t Channel::kMaximumMessageSize;
28 
29 // static
GenerateUniqueRandomChannelID()30 std::string Channel::GenerateUniqueRandomChannelID() {
31   // Note: the string must start with the current process id, this is how
32   // some child processes determine the pid of the parent.
33   //
34   // This is composed of a unique incremental identifier, the process ID of
35   // the creator, an identifier for the child instance, and a strong random
36   // component. The strong random component prevents other processes from
37   // hijacking or squatting on predictable channel names.
38   int process_id = base::GetCurrentProcId();
39   return base::StringPrintf("%d.%u.%d",
40       process_id,
41       g_last_id.GetNext(),
42       base::RandInt(0, std::numeric_limits<int32_t>::max()));
43 }
44 
45 }  // namespace IPC
46