OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_MUS_WS_IDS_H_ | 5 #ifndef COMPONENTS_MUS_WS_IDS_H_ |
6 #define COMPONENTS_MUS_WS_IDS_H_ | 6 #define COMPONENTS_MUS_WS_IDS_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <tuple> | 10 #include <tuple> |
11 | 11 |
| 12 #include "base/hash.h" |
12 #include "components/mus/common/types.h" | 13 #include "components/mus/common/types.h" |
13 #include "components/mus/common/util.h" | 14 #include "components/mus/common/util.h" |
14 | 15 |
15 namespace mus { | 16 namespace mus { |
16 | |
17 namespace ws { | 17 namespace ws { |
18 | 18 |
19 // Connection id is used to indicate no connection. That is, no WindowTreeImpl | 19 // Connection id is used to indicate no connection. That is, no WindowTreeImpl |
20 // ever gets this id. | 20 // ever gets this id. |
21 const ConnectionSpecificId kInvalidConnectionId = 0; | 21 const ConnectionSpecificId kInvalidConnectionId = 0; |
22 | 22 |
23 // Adds a bit of type safety to window ids. | 23 // Every window has a unique id associated with it (WindowId). The id is a |
| 24 // combination of the id assigned to the connection (the high order bits) and |
| 25 // a unique id for the window. Each client (WindowTreeImpl) refers to the |
| 26 // window by an id assigned by the client (ClientWindowId). To facilitate this |
| 27 // WindowTreeImpl maintains a mapping between WindowId and ClientWindowId. |
| 28 // |
| 29 // This model works when the client initiates creation of windows, which is |
| 30 // the typical use case. Embed roots and the WindowManager are special, they |
| 31 // get access to windows created by other connections. These clients see the |
| 32 // id assigned on the server. Such clients have to take care that they only |
| 33 // create windows using their connection id. To do otherwise could result in |
| 34 // multiple windows having the same ClientWindowId. WindowTreeImpl enforces |
| 35 // that embed roots use the connection id in creating the window id to avoid |
| 36 // possible conflicts. |
24 struct WindowId { | 37 struct WindowId { |
25 WindowId(ConnectionSpecificId connection_id, ConnectionSpecificId window_id) | 38 WindowId(ConnectionSpecificId connection_id, ConnectionSpecificId window_id) |
26 : connection_id(connection_id), window_id(window_id) {} | 39 : connection_id(connection_id), window_id(window_id) {} |
27 WindowId() : connection_id(0), window_id(0) {} | 40 WindowId() : connection_id(0), window_id(0) {} |
28 | 41 |
29 bool operator==(const WindowId& other) const { | 42 bool operator==(const WindowId& other) const { |
30 return other.connection_id == connection_id && other.window_id == window_id; | 43 return other.connection_id == connection_id && other.window_id == window_id; |
31 } | 44 } |
32 | 45 |
33 bool operator!=(const WindowId& other) const { return !(*this == other); } | 46 bool operator!=(const WindowId& other) const { return !(*this == other); } |
34 | 47 |
35 bool operator<(const WindowId& other) const { | 48 bool operator<(const WindowId& other) const { |
36 return std::tie(connection_id, window_id) < | 49 return std::tie(connection_id, window_id) < |
37 std::tie(other.connection_id, other.window_id); | 50 std::tie(other.connection_id, other.window_id); |
38 } | 51 } |
39 | 52 |
40 ConnectionSpecificId connection_id; | 53 ConnectionSpecificId connection_id; |
41 ConnectionSpecificId window_id; | 54 ConnectionSpecificId window_id; |
42 }; | 55 }; |
43 | 56 |
| 57 // Used for ids assigned by the client. |
| 58 struct ClientWindowId { |
| 59 explicit ClientWindowId(Id id) : id(id) {} |
| 60 ClientWindowId() : id(0u) {} |
| 61 |
| 62 bool operator==(const ClientWindowId& other) const { return other.id == id; } |
| 63 |
| 64 bool operator!=(const ClientWindowId& other) const { |
| 65 return !(*this == other); |
| 66 } |
| 67 |
| 68 bool operator<(const ClientWindowId& other) const { return id < other.id; } |
| 69 |
| 70 Id id; |
| 71 }; |
| 72 |
44 inline WindowId WindowIdFromTransportId(Id id) { | 73 inline WindowId WindowIdFromTransportId(Id id) { |
45 return WindowId(HiWord(id), LoWord(id)); | 74 return WindowId(HiWord(id), LoWord(id)); |
46 } | 75 } |
47 | |
48 inline Id WindowIdToTransportId(const WindowId& id) { | 76 inline Id WindowIdToTransportId(const WindowId& id) { |
49 return (id.connection_id << 16) | id.window_id; | 77 return (id.connection_id << 16) | id.window_id; |
50 } | 78 } |
51 | 79 |
52 // Returns a WindowId that is reserved to indicate no window. That is, no window | 80 // Returns a WindowId that is reserved to indicate no window. That is, no window |
53 // will ever be created with this id. | 81 // will ever be created with this id. |
54 inline WindowId InvalidWindowId() { | 82 inline WindowId InvalidWindowId() { |
55 return WindowId(kInvalidConnectionId, 0); | 83 return WindowId(kInvalidConnectionId, 0); |
56 } | 84 } |
57 | 85 |
58 // Returns a root window id with a given index offset. | 86 // Returns a root window id with a given index offset. |
59 inline WindowId RootWindowId(uint16_t index) { | 87 inline WindowId RootWindowId(uint16_t index) { |
60 return WindowId(kInvalidConnectionId, 2 + index); | 88 return WindowId(kInvalidConnectionId, 2 + index); |
61 } | 89 } |
62 | 90 |
63 } // namespace ws | 91 } // namespace ws |
64 | |
65 } // namespace mus | 92 } // namespace mus |
66 | 93 |
| 94 namespace BASE_HASH_NAMESPACE { |
| 95 |
| 96 template <> |
| 97 struct hash<mus::ws::ClientWindowId> { |
| 98 size_t operator()(const mus::ws::ClientWindowId& id) const { |
| 99 return hash<mus::Id>()(id.id); |
| 100 } |
| 101 }; |
| 102 |
| 103 template <> |
| 104 struct hash<mus::ws::WindowId> { |
| 105 size_t operator()(const mus::ws::WindowId& id) const { |
| 106 return hash<mus::Id>()(WindowIdToTransportId(id)); |
| 107 } |
| 108 }; |
| 109 |
| 110 } // namespace BASE_HASH_NAMESPACE |
| 111 |
67 #endif // COMPONENTS_MUS_WS_IDS_H_ | 112 #endif // COMPONENTS_MUS_WS_IDS_H_ |
OLD | NEW |