OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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_LOCAL_FRAME_ID_H_ | 5 #ifndef CC_SURFACES_LOCAL_FRAME_ID_H_ |
6 #define CC_SURFACES_LOCAL_FRAME_ID_H_ | 6 #define CC_SURFACES_LOCAL_FRAME_ID_H_ |
7 | 7 |
8 #include <inttypes.h> | 8 #include <inttypes.h> |
9 #include <tuple> | 9 #include <tuple> |
10 | 10 |
11 #include "base/hash.h" | 11 #include "base/hash.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/unguessable_token.h" | |
14 | 13 |
15 namespace cc { | 14 namespace cc { |
16 | 15 |
17 class LocalFrameId { | 16 class LocalFrameId { |
18 public: | 17 public: |
19 constexpr LocalFrameId() : local_id_(0) {} | 18 constexpr LocalFrameId() : local_id_(0), nonce_(0) {} |
20 | 19 |
21 constexpr LocalFrameId(const LocalFrameId& other) | 20 constexpr LocalFrameId(const LocalFrameId& other) |
22 : local_id_(other.local_id_), nonce_(other.nonce_) {} | 21 : local_id_(other.local_id_), nonce_(other.nonce_) {} |
23 | 22 |
24 constexpr LocalFrameId(uint32_t local_id, const base::UnguessableToken& nonce) | 23 constexpr LocalFrameId(uint32_t local_id, uint64_t nonce) |
25 : local_id_(local_id), nonce_(nonce) {} | 24 : local_id_(local_id), nonce_(nonce) {} |
26 | 25 |
27 constexpr bool is_valid() const { | 26 constexpr bool is_valid() const { return local_id_ != 0 && nonce_ != 0; } |
28 return local_id_ != 0 && !nonce_.is_empty(); | |
29 } | |
30 | 27 |
31 constexpr uint32_t local_id() const { return local_id_; } | 28 constexpr uint32_t local_id() const { return local_id_; } |
32 | 29 |
33 constexpr const base::UnguessableToken& nonce() const { return nonce_; } | 30 constexpr uint64_t nonce() const { return nonce_; } |
34 | 31 |
35 bool operator==(const LocalFrameId& other) const { | 32 bool operator==(const LocalFrameId& other) const { |
36 return local_id_ == other.local_id_ && nonce_ == other.nonce_; | 33 return local_id_ == other.local_id_ && nonce_ == other.nonce_; |
37 } | 34 } |
38 | 35 |
39 bool operator!=(const LocalFrameId& other) const { return !(*this == other); } | 36 bool operator!=(const LocalFrameId& other) const { return !(*this == other); } |
40 | 37 |
41 bool operator<(const LocalFrameId& other) const { | 38 bool operator<(const LocalFrameId& other) const { |
42 return std::tie(local_id_, nonce_) < | 39 return std::tie(local_id_, nonce_) < |
43 std::tie(other.local_id_, other.nonce_); | 40 std::tie(other.local_id_, other.nonce_); |
44 } | 41 } |
45 | 42 |
46 size_t hash() const { | 43 size_t hash() const { return base::HashInts(local_id_, nonce_); } |
47 return base::HashInts( | |
48 local_id_, static_cast<uint64_t>(base::UnguessableTokenHash()(nonce_))); | |
49 } | |
50 | 44 |
51 std::string ToString() const { | 45 std::string ToString() const { |
52 return base::StringPrintf("LocalFrameId(%d, %s" PRIu64 ")", local_id_, | 46 return base::StringPrintf("LocalFrameId(%d, %" PRIu64 ")", local_id_, |
53 nonce_.ToString().c_str()); | 47 nonce_); |
54 } | 48 } |
55 | 49 |
56 private: | 50 private: |
57 uint32_t local_id_; | 51 uint32_t local_id_; |
58 base::UnguessableToken nonce_; | 52 uint64_t nonce_; |
59 }; | 53 }; |
60 | 54 |
61 struct LocalFrameIdHash { | 55 struct LocalFrameIdHash { |
62 size_t operator()(const LocalFrameId& key) const { return key.hash(); } | 56 size_t operator()(const LocalFrameId& key) const { return key.hash(); } |
63 }; | 57 }; |
64 | 58 |
65 } // namespace cc | 59 } // namespace cc |
66 | 60 |
67 #endif // CC_SURFACES_LOCAL_FRAME_ID_H_ | 61 #endif // CC_SURFACES_LOCAL_FRAME_ID_H_ |
OLD | NEW |