OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #ifndef EXTENSIONS_COMMON_API_MESSAGING_PORT_ID_H_ |
| 6 #define EXTENSIONS_COMMON_API_MESSAGING_PORT_ID_H_ |
| 7 |
| 8 #include <utility> |
| 9 |
| 10 #include "base/unguessable_token.h" |
| 11 |
| 12 namespace extensions { |
| 13 |
| 14 // A unique identifier for the Channel a port is on; i.e., the two-way |
| 15 // communication between opener and receiver(s). We can use the pair of |
| 16 // <context_id, port_number> which is the same between opener and receivers |
| 17 // but unique amongst all other connections. |
| 18 using ChannelId = std::pair<base::UnguessableToken, int>; |
| 19 |
| 20 // A unique identifier for an extension port. The id is composed of three parts: |
| 21 // - context_id: An UnguessableToken that uniquely identifies the creation |
| 22 // context of the port. |
| 23 // - port_number: A simple identifer that uniquely identifies the port *within |
| 24 // the creation context*. That is, each creation context may |
| 25 // have a port with number '1', but there should only be a single |
| 26 // port with the number '1' in each. |
| 27 // - is_opener: Whether or not this port id is for the opener port (false |
| 28 // indicating it is the receiver port). |
| 29 // A few more notes: |
| 30 // - There should only be a single existent opener port. That is, in all the |
| 31 // contexts, there should only be one with a given context_id, port_number, |
| 32 // and is_opener set to true. However, each port can have multiple receivers, |
| 33 // thus there may be multiple ports with a given context_id, port_number, and |
| 34 // is_opener set to false. |
| 35 // - The context_id and port_number refer to the values at *creation*, and are |
| 36 // conceptually immutable. Receiver ports will always have a context id other |
| 37 // than the one they are hosted in (since we don't dispatch messages to the |
| 38 // same context). Only in the case of opener ports do these identify the |
| 39 // current context. |
| 40 // - Context id and port number are set in the renderer process. Theoretically, |
| 41 // this means that multiple contexts could have the same id. However, GUIDs |
| 42 // are sufficiently random as to be globally unique in practice (the chance |
| 43 // of a duplicate is about the same as the sun exploding right now). |
| 44 struct PortId { |
| 45 // See class comments for the description of these fields. |
| 46 base::UnguessableToken context_id; |
| 47 int port_number = 0; |
| 48 bool is_opener = false; |
| 49 |
| 50 PortId(); |
| 51 PortId(const base::UnguessableToken& context_id, |
| 52 int port_number, |
| 53 bool is_opener); |
| 54 ~PortId(); |
| 55 PortId(PortId&& other); |
| 56 PortId(const PortId& other); |
| 57 PortId& operator=(const PortId& other); |
| 58 |
| 59 ChannelId GetChannelId() const { |
| 60 return std::make_pair(context_id, port_number); |
| 61 } |
| 62 |
| 63 // Returns the identifier for the opposite port(s). That is, a call on a |
| 64 // receiver port returns the ID of the opener, and a call on the opener port |
| 65 // returns the ID of all receivers. |
| 66 PortId GetOppositePortId() const { |
| 67 return PortId(context_id, port_number, !is_opener); |
| 68 } |
| 69 |
| 70 bool operator==(const PortId& other) const; |
| 71 bool operator<(const PortId& other) const; |
| 72 }; |
| 73 |
| 74 } // namespace extensions |
| 75 |
| 76 #endif // EXTENSIONS_COMMON_API_MESSAGING_PORT_ID_H_ |
OLD | NEW |