ipc_channel.cc 1.3 KB

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