1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139

content / public / browser / global_routing_id.h [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_PUBLIC_BROWSER_GLOBAL_ROUTING_ID_H_
#define CONTENT_PUBLIC_BROWSER_GLOBAL_ROUTING_ID_H_

#include <compare>
#include <ostream>

#include "base/hash/hash.h"
#include "base/i18n/number_formatting.h"
#include "content/common/content_export.h"
#include "content/public/common/content_constants.h"
#include "ipc/ipc_message.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value_forward.h"

namespace perfetto::protos::pbzero {
class GlobalRenderFrameHostId;
}  // namespace perfetto::protos::pbzero

namespace content {

// Uniquely identifies a target that legacy IPCs can be routed to.
//
// These IDs can be considered to be unique for the lifetime of the browser
// process. While they are finite and thus must eventually roll over, this case
// may be considered sufficiently rare as to be ignorable.
struct CONTENT_EXPORT GlobalRoutingID {
  GlobalRoutingID() = default;

  GlobalRoutingID(int child_id, int route_id)
      : child_id(child_id), route_id(route_id) {}

  // The unique ID of the child process (this is different from OS's PID / this
  // should come from RenderProcessHost::GetID()).
  int child_id = kInvalidChildProcessUniqueId;

  // The route ID.
  int route_id = -1;

  constexpr friend auto operator<=>(const GlobalRoutingID&,
                                    const GlobalRoutingID&) = default;
  constexpr friend bool operator==(const GlobalRoutingID&,
                                   const GlobalRoutingID&) = default;
};

inline std::ostream& operator<<(std::ostream& os, const GlobalRoutingID& id) {
  os << "GlobalRoutingID(" << id.child_id << ", " << id.route_id << ")";
  return os;
}

// Same as GlobalRoutingID except the route_id must be a RenderFrameHost routing
// id.
//
// These IDs can be considered to be unique for the lifetime of the browser
// process. While they are finite and thus must eventually roll over, this case
// may be considered sufficiently rare as to be ignorable.
struct CONTENT_EXPORT GlobalRenderFrameHostId {
  GlobalRenderFrameHostId() = default;

  GlobalRenderFrameHostId(int child_id, int frame_routing_id)
      : child_id(child_id), frame_routing_id(frame_routing_id) {}

  // GlobalRenderFrameHostId is copyable.
  GlobalRenderFrameHostId(const GlobalRenderFrameHostId&) = default;
  GlobalRenderFrameHostId& operator=(const GlobalRenderFrameHostId&) = default;

  // The unique ID of the child process (this is different from OS's PID / this
  // should come from RenderProcessHost::GetID()).
  int child_id = 0;

  // The route ID of a RenderFrame - should come from
  // RenderFrameHost::GetRoutingID().
  int frame_routing_id = MSG_ROUTING_NONE;

  constexpr friend auto operator<=>(const GlobalRenderFrameHostId&,
                                    const GlobalRenderFrameHostId&) = default;
  constexpr friend bool operator==(const GlobalRenderFrameHostId&,
                                   const GlobalRenderFrameHostId&) = default;

  explicit operator bool() const {
    return frame_routing_id != MSG_ROUTING_NONE;
  }

  using TraceProto = perfetto::protos::pbzero::GlobalRenderFrameHostId;
  // Write a representation of this object into proto.
  void WriteIntoTrace(perfetto::TracedProto<TraceProto> proto) const;
};

// Similar to GlobalRenderFrameHostId except that it uses FrameTokens instead
// of routing ids.
//
// These tokens can be considered to be unique for the lifetime of the browser
// process.
struct GlobalRenderFrameHostToken {
  GlobalRenderFrameHostToken() = default;

  // GlobalRenderFrameHostToken is copyable.
  GlobalRenderFrameHostToken(const GlobalRenderFrameHostToken&) = default;
  GlobalRenderFrameHostToken& operator=(const GlobalRenderFrameHostToken&) =
      default;

  GlobalRenderFrameHostToken(int child_id,
                             const blink::LocalFrameToken& frame_token)
      : child_id(child_id), frame_token(frame_token) {}

  // The unique ID of the child process (this is different from OS's PID / this
  // should come from RenderProcessHost::GetID()).
  int child_id = kInvalidChildProcessUniqueId;

  // The `LocalFrameToken` of blink::WebLocalFrame - should come from
  // RenderFrameHost::GetFrameToken().
  blink::LocalFrameToken frame_token;

  constexpr friend auto operator<=>(const GlobalRenderFrameHostToken&,
                                    const GlobalRenderFrameHostToken&) =
      default;
  constexpr friend bool operator==(const GlobalRenderFrameHostToken&,
                                   const GlobalRenderFrameHostToken&) = default;
};

inline std::ostream& operator<<(std::ostream& os,
                                const GlobalRenderFrameHostId& id) {
  os << "GlobalRenderFrameHostId(" << id.child_id << ", " << id.frame_routing_id
     << ")";
  return os;
}

struct GlobalRenderFrameHostIdHasher {
  std::size_t operator()(const GlobalRenderFrameHostId& id) const {
    return base::HashInts(id.child_id, id.frame_routing_id);
  }
};

}  // namespace content

#endif  // CONTENT_PUBLIC_BROWSER_GLOBAL_ROUTING_ID_H_