Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: extensions/common/api/messaging/port_id.h

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

Powered by Google App Engine
This is Rietveld 408576698