1 // Copyright 2013 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 IOS_WEB_PUBLIC_WEB_STATE_USER_DATA_H_
6 #define IOS_WEB_PUBLIC_WEB_STATE_USER_DATA_H_
7 
8 #include "base/memory/ptr_util.h"
9 #include "base/supports_user_data.h"
10 #import "ios/web/public/web_state.h"
11 
12 // This macro declares a static variable inside the class that inherits from
13 // WebStateUserData. The address of this static variable is used as the key to
14 // store/retrieve an instance of the class on/from a WebState.
15 #define WEB_STATE_USER_DATA_KEY_DECL() static constexpr int kUserDataKey = 0
16 
17 // This macro instantiates the static variable declared by the previous macro.
18 // It must live in a .mm/.cc file to ensure that there is only one instantiation
19 // of the static variable.
20 #define WEB_STATE_USER_DATA_KEY_IMPL(Type) const int Type::kUserDataKey;
21 
22 namespace web {
23 
24 // A base class for classes attached to, and scoped to, the lifetime of a
25 // WebState. For example:
26 //
27 // --- in foo_tab_helper.h ---
28 // class FooTabHelper : public web::WebStateUserData<FooTabHelper> {
29 //  public:
30 //   ~FooTabHelper() override;
31 //   // ... more public stuff here ...
32 //  private:
33 //   explicit FooTabHelper(web::WebState* web_state);
34 //   friend class web::WebStateUserData<FooTabHelper>;
35 //   WEB_STATE_USER_DATA_KEY_DECL();
36 //   // ... more private stuff here ...
37 // };
38 //
39 // --- in foo_tab_helper.cc ---
40 // WEB_STATE_USER_DATA_KEY_IMPL(FooTabHelper)
41 template <typename T>
42 class WebStateUserData : public base::SupportsUserData::Data {
43  public:
44   // Creates an object of type T, and attaches it to the specified WebState.
45   // If an instance is already attached, does nothing.
CreateForWebState(WebState * web_state)46   static void CreateForWebState(WebState* web_state) {
47     if (!FromWebState(web_state))
48       web_state->SetUserData(UserDataKey(), base::WrapUnique(new T(web_state)));
49   }
50 
51   // Retrieves the instance of type T that was attached to the specified
52   // WebState (via CreateForWebState above) and returns it. If no instance
53   // of the type was attached, returns nullptr.
FromWebState(WebState * web_state)54   static T* FromWebState(WebState* web_state) {
55     return static_cast<T*>(web_state->GetUserData(UserDataKey()));
56   }
FromWebState(const WebState * web_state)57   static const T* FromWebState(const WebState* web_state) {
58     return static_cast<const T*>(web_state->GetUserData(UserDataKey()));
59   }
60 
61   // Removes the instance attached to the specified WebState.
RemoveFromWebState(WebState * web_state)62   static void RemoveFromWebState(WebState* web_state) {
63     web_state->RemoveUserData(UserDataKey());
64   }
65 
UserDataKey()66   static const void* UserDataKey() { return &T::kUserDataKey; }
67 };
68 
69 }  // namespace web
70 
71 #endif  // IOS_WEB_PUBLIC_WEB_STATE_USER_DATA_H_
72