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