web_state_user_data.h 2.6 KB

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