blob: a6ee14c01fcb10b95cc2d20e5aa3c13ab02f67e7 [file] [log] [blame]
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +09001// 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.
4
5#include "ipc/ipc_channel.h"
6
7#include <limits>
8
9#include "base/atomic_sequence_num.h"
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090010#include "base/rand_util.h"
avi@chromium.orge7eaf392013-06-11 15:32:18 +090011#include "base/strings/stringprintf.h"
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090012
thakis@chromium.orgb78cb8e2012-09-24 13:49:10 +090013#if !defined(OS_NACL)
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090014namespace {
15
16// Global atomic used to guarantee channel IDs are unique.
17base::StaticAtomicSequenceNumber g_last_id;
18
19} // namespace
thakis@chromium.orgb78cb8e2012-09-24 13:49:10 +090020#endif
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090021
22namespace IPC {
23
24// static
25std::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.
28 //
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
bbudge@chromium.orgb562dcc2012-06-16 09:35:01 +090034 int process_id = base::GetCurrentProcId();
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090035 return base::StringPrintf("%d.%u.%d",
bbudge@chromium.orgaa4d16a2012-04-24 06:18:19 +090036 process_id,
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090037 g_last_id.GetNext(),
38 base::RandInt(0, std::numeric_limits<int32>::max()));
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090039}
40
41} // namespace IPC
42