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 CC_SURFACES_SURFACE_ID_H_ | 5 #ifndef CC_SURFACES_SURFACE_ID_H_ |
6 #define CC_SURFACES_SURFACE_ID_H_ | 6 #define CC_SURFACES_SURFACE_ID_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <functional> | 11 #include <functional> |
12 | 12 |
13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
14 #include "base/hash.h" | 14 #include "base/hash.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/unguessable_token.h" | |
16 | 17 |
17 namespace cc { | 18 namespace cc { |
18 | 19 |
19 class SurfaceId { | 20 class SurfaceId { |
20 public: | 21 public: |
21 SurfaceId() : client_id_(0), local_id_(0), nonce_(0) {} | 22 SurfaceId() : client_id_(0), local_id_(0), nonce_() {} |
Fady Samuel
2016/09/30 20:30:41
no need for calling the default constructor for no
Alex Z.
2016/10/05 18:10:45
Done.
| |
22 | 23 |
23 SurfaceId(const SurfaceId& other) | 24 SurfaceId(const SurfaceId& other) |
24 : client_id_(other.client_id_), | 25 : client_id_(other.client_id_), |
25 local_id_(other.local_id_), | 26 local_id_(other.local_id_), |
26 nonce_(other.nonce_) {} | 27 nonce_(other.nonce_) {} |
27 | 28 |
28 // A SurfaceId consists of three components: client Id, local Id, and nonce. | 29 // A SurfaceId consists of three components: client Id, local Id, and nonce. |
29 // A |client_id| is a display compositor service allocated ID that | 30 // A |client_id| is a display compositor service allocated ID that |
30 // uniquely identifies a client. | 31 // uniquely identifies a client. |
31 // A |local_id| is a sequentially allocated ID generated by the display | 32 // A |local_id| is a sequentially allocated ID generated by the display |
32 // compositor client that uniquely identifies a surface. | 33 // compositor client that uniquely identifies a surface. |
33 // A |nonce| is a cryptographically secure random int that makes a SurfaceId | 34 // A |nonce| is a cryptographically secure random int that makes a SurfaceId |
34 // unguessable by other clients. | 35 // unguessable by other clients. |
35 SurfaceId(uint32_t client_id, uint32_t local_id, uint64_t nonce) | 36 SurfaceId(uint32_t client_id, |
37 uint32_t local_id, | |
38 const base::UnguessableToken& nonce) | |
36 : client_id_(client_id), local_id_(local_id), nonce_(nonce) {} | 39 : client_id_(client_id), local_id_(local_id), nonce_(nonce) {} |
37 | 40 |
41 SurfaceId(uint32_t client_id, uint32_t local_id) | |
42 : client_id_(client_id), local_id_(local_id) { | |
43 nonce_ = base::UnguessableToken::Create(); | |
danakj
2016/09/30 20:45:12
do this as a constructor initializer
Alex Z.
2016/10/05 18:10:45
Done.
| |
44 } | |
45 | |
38 bool is_null() const { | 46 bool is_null() const { |
39 return client_id_ == 0 && nonce_ == 0 && local_id_ == 0; | 47 return client_id_ == 0 && nonce_ == base::UnguessableToken() && |
Fady Samuel
2016/09/30 20:30:41
nonce_.is_empty()
Alex Z.
2016/10/05 18:10:45
Done.
| |
48 local_id_ == 0; | |
40 } | 49 } |
41 | 50 |
42 size_t hash() const { | 51 size_t hash() const { |
43 size_t interim = base::HashInts(client_id_, local_id_); | 52 size_t interim = base::HashInts(client_id_, local_id_); |
44 return base::HashInts(static_cast<uint64_t>(interim), nonce_); | 53 return base::HashInts(static_cast<uint64_t>(interim), |
danakj
2016/09/30 20:45:12
drop the static cast
Alex Z.
2016/10/05 18:10:45
Done.
| |
54 base::UnguessableTokenHash()(nonce_)); | |
45 } | 55 } |
46 | 56 |
47 uint32_t client_id() const { return client_id_; } | 57 uint32_t client_id() const { return client_id_; } |
48 | 58 |
49 uint32_t local_id() const { return local_id_; } | 59 uint32_t local_id() const { return local_id_; } |
50 | 60 |
51 uint64_t nonce() const { return nonce_; } | 61 base::UnguessableToken nonce() const { return nonce_; } |
Fady Samuel
2016/09/30 20:30:41
const base::UnguessableToken&
Alex Z.
2016/10/05 18:10:45
Done.
| |
52 | 62 |
53 std::string ToString() const { | 63 std::string ToString() const { |
54 return base::StringPrintf("%d:%d:%" PRIu64, client_id_, local_id_, nonce_); | 64 return base::StringPrintf("%d:%d" PRIu64, client_id_, local_id_) |
65 .append(nonce_.ToString()); | |
danakj
2016/09/30 20:45:11
you need a space between fields tho. i'd rather yo
Alex Z.
2016/10/05 18:10:45
Done.
| |
55 } | 66 } |
56 | 67 |
57 bool operator==(const SurfaceId& other) const { | 68 bool operator==(const SurfaceId& other) const { |
58 return client_id_ == other.client_id_ && local_id_ == other.local_id_ && | 69 return client_id_ == other.client_id_ && local_id_ == other.local_id_ && |
59 nonce_ == other.nonce_; | 70 nonce_ == other.nonce_; |
60 } | 71 } |
61 | 72 |
62 bool operator!=(const SurfaceId& other) const { return !(*this == other); } | 73 bool operator!=(const SurfaceId& other) const { return !(*this == other); } |
63 | 74 |
64 bool operator<(const SurfaceId& other) const { | 75 bool operator<(const SurfaceId& other) const { |
65 return std::tie(client_id_, local_id_, nonce_) < | 76 return std::tie(client_id_, local_id_, nonce_) < |
66 std::tie(other.client_id_, other.local_id_, other.nonce_); | 77 std::tie(other.client_id_, other.local_id_, other.nonce_); |
67 } | 78 } |
68 | 79 |
69 private: | 80 private: |
70 // See SurfaceIdAllocator::GenerateId. | 81 // See SurfaceIdAllocator::GenerateId. |
71 uint32_t client_id_; | 82 uint32_t client_id_; |
72 uint32_t local_id_; | 83 uint32_t local_id_; |
73 uint64_t nonce_; | 84 base::UnguessableToken nonce_; |
74 }; | 85 }; |
75 | 86 |
76 struct SurfaceIdHash { | 87 struct SurfaceIdHash { |
77 size_t operator()(const SurfaceId& key) const { return key.hash(); } | 88 size_t operator()(const SurfaceId& key) const { return key.hash(); } |
78 }; | 89 }; |
79 | 90 |
80 } // namespace cc | 91 } // namespace cc |
81 | 92 |
82 #endif // CC_SURFACES_SURFACE_ID_H_ | 93 #endif // CC_SURFACES_SURFACE_ID_H_ |
OLD | NEW |