1 // Copyright 2012 The Chromium Authors 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 BASE_SUPPORTS_USER_DATA_H_ 6 #define BASE_SUPPORTS_USER_DATA_H_ 7 8 #include <map> 9 #include <memory> 10 11 #include "base/base_export.h" 12 #include "base/memory/scoped_refptr.h" 13 #include "base/sequence_checker.h" 14 #include "third_party/abseil-cpp/absl/container/flat_hash_map.h" 15 #include "third_party/abseil-cpp/absl/types/variant.h" 16 17 namespace base { 18 19 // This is a helper for classes that want to allow users to stash random data by 20 // key. At destruction all the objects will be destructed. 21 class BASE_EXPORT SupportsUserData { 22 public: 23 SupportsUserData(); 24 SupportsUserData(SupportsUserData&&); 25 SupportsUserData& operator=(SupportsUserData&&); 26 SupportsUserData(const SupportsUserData&) = delete; 27 SupportsUserData& operator=(const SupportsUserData&) = delete; 28 29 // Derive from this class and add your own data members to associate extra 30 // information with this object. Alternatively, add this as a public base 31 // class to any class with a virtual destructor. 32 class BASE_EXPORT Data { 33 public: 34 virtual ~Data() = default; 35 36 // Returns a copy of |this|; null if copy is not supported. 37 virtual std::unique_ptr<Data> Clone(); 38 }; 39 40 // The user data allows the clients to associate data with this object. 41 // |key| must not be null--that value is too vulnerable for collision. 42 // NOTE: SetUserData() with an empty unique_ptr behaves the same as 43 // RemoveUserData(). 44 Data* GetUserData(const void* key) const; 45 [[nodiscard]] std::unique_ptr<Data> TakeUserData(const void* key); 46 void SetUserData(const void* key, std::unique_ptr<Data> data); 47 void RemoveUserData(const void* key); 48 49 // Adds all data from |other|, that is clonable, to |this|. That is, this 50 // iterates over the data in |other|, and any data that returns non-null from 51 // Clone() is added to |this|. 52 void CloneDataFrom(const SupportsUserData& other); 53 54 // SupportsUserData is not thread-safe, and on debug build will assert it is 55 // only used on one execution sequence. Calling this method allows the caller 56 // to hand the SupportsUserData instance across execution sequences. Use only 57 // if you are taking full control of the synchronization of that hand over. 58 void DetachFromSequence(); 59 60 protected: 61 virtual ~SupportsUserData(); 62 63 // Clear all user data from this object. This can be used if the subclass 64 // needs to provide reset functionality. 65 void ClearAllUserData(); 66 67 private: 68 // Currently a variant for A/B testing purposes. 69 using DataMap = std::map<const void*, std::unique_ptr<Data>>; 70 using FlatDataMap = absl::flat_hash_map<const void*, std::unique_ptr<Data>>; 71 using MapVariants = absl::variant<DataMap, FlatDataMap>; 72 73 // Externally-defined data accessible by key. 74 MapVariants user_data_; 75 // Guards usage of |user_data_| 76 SEQUENCE_CHECKER(sequence_checker_); 77 }; 78 79 // Adapter class that releases a refcounted object when the 80 // SupportsUserData::Data object is deleted. 81 template <typename T> 82 class UserDataAdapter : public SupportsUserData::Data { 83 public: Get(const SupportsUserData * supports_user_data,const void * key)84 static T* Get(const SupportsUserData* supports_user_data, const void* key) { 85 UserDataAdapter* data = 86 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key)); 87 return data ? static_cast<T*>(data->object_.get()) : nullptr; 88 } 89 UserDataAdapter(T * object)90 explicit UserDataAdapter(T* object) : object_(object) {} 91 UserDataAdapter(const UserDataAdapter&) = delete; 92 UserDataAdapter& operator=(const UserDataAdapter&) = delete; 93 ~UserDataAdapter() override = default; 94 release()95 T* release() { return object_.release(); } 96 97 private: 98 scoped_refptr<T> const object_; 99 }; 100 101 } // namespace base 102 103 #endif // BASE_SUPPORTS_USER_DATA_H_ 104