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