| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_USER_DATA_H_ | 5 #ifndef IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_USER_DATA_H_ |
| 6 #define IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_USER_DATA_H_ | 6 #define IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_USER_DATA_H_ |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/ptr_util.h" |
| 9 #include "base/supports_user_data.h" | 10 #include "base/supports_user_data.h" |
| 10 #import "ios/web/public/web_state/web_state.h" | 11 #import "ios/web/public/web_state/web_state.h" |
| 11 | 12 |
| 12 namespace web { | 13 namespace web { |
| 13 | 14 |
| 14 // A base class for classes attached to, and scoped to, the lifetime of a | 15 // A base class for classes attached to, and scoped to, the lifetime of a |
| 15 // WebState. For example: | 16 // WebState. For example: |
| 16 // | 17 // |
| 17 // --- in foo.h --- | 18 // --- in foo.h --- |
| 18 // class Foo : public web::WebStateUserData<Foo> { | 19 // class Foo : public web::WebStateUserData<Foo> { |
| 19 // public: | 20 // public: |
| 20 // ~Foo() override; | 21 // ~Foo() override; |
| 21 // // ... more public stuff here ... | 22 // // ... more public stuff here ... |
| 22 // private: | 23 // private: |
| 23 // explicit Foo(web::WebState* web_state); | 24 // explicit Foo(web::WebState* web_state); |
| 24 // friend class web::WebStateUserData<Foo>; | 25 // friend class web::WebStateUserData<Foo>; |
| 25 // // ... more private stuff here ... | 26 // // ... more private stuff here ... |
| 26 // } | 27 // } |
| 27 // --- in foo.cc --- | 28 // --- in foo.cc --- |
| 28 // DEFINE_WEB_STATE_USER_DATA_KEY(Foo); | 29 // DEFINE_WEB_STATE_USER_DATA_KEY(Foo); |
| 29 // | 30 // |
| 30 template <typename T> | 31 template <typename T> |
| 31 class WebStateUserData : public base::SupportsUserData::Data { | 32 class WebStateUserData : public base::SupportsUserData::Data { |
| 32 public: | 33 public: |
| 33 // Creates an object of type T, and attaches it to the specified WebState. | 34 // Creates an object of type T, and attaches it to the specified WebState. |
| 34 // If an instance is already attached, does nothing. | 35 // If an instance is already attached, does nothing. |
| 35 static void CreateForWebState(WebState* web_state) { | 36 static void CreateForWebState(WebState* web_state) { |
| 36 DCHECK(web_state); | 37 DCHECK(web_state); |
| 37 if (!FromWebState(web_state)) | 38 if (!FromWebState(web_state)) |
| 38 web_state->SetUserData(UserDataKey(), new T(web_state)); | 39 web_state->SetUserData(UserDataKey(), base::WrapUnique(new T(web_state))); |
| 39 } | 40 } |
| 40 | 41 |
| 41 // Retrieves the instance of type T that was attached to the specified | 42 // Retrieves the instance of type T that was attached to the specified |
| 42 // WebState (via CreateForWebState above) and returns it. If no instance | 43 // WebState (via CreateForWebState above) and returns it. If no instance |
| 43 // of the type was attached, returns null. | 44 // of the type was attached, returns null. |
| 44 static T* FromWebState(WebState* web_state) { | 45 static T* FromWebState(WebState* web_state) { |
| 45 return static_cast<T*>(web_state->GetUserData(UserDataKey())); | 46 return static_cast<T*>(web_state->GetUserData(UserDataKey())); |
| 46 } | 47 } |
| 47 static const T* FromWebState(const WebState* web_state) { | 48 static const T* FromWebState(const WebState* web_state) { |
| 48 return static_cast<const T*>(web_state->GetUserData(UserDataKey())); | 49 return static_cast<const T*>(web_state->GetUserData(UserDataKey())); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 67 // a declaration. Without it, this would be merely a declaration of a template | 68 // a declaration. Without it, this would be merely a declaration of a template |
| 68 // specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13) | 69 // specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13) |
| 69 // | 70 // |
| 70 #define DEFINE_WEB_STATE_USER_DATA_KEY(TYPE) \ | 71 #define DEFINE_WEB_STATE_USER_DATA_KEY(TYPE) \ |
| 71 template <> \ | 72 template <> \ |
| 72 int web::WebStateUserData<TYPE>::kLocatorKey = 0 | 73 int web::WebStateUserData<TYPE>::kLocatorKey = 0 |
| 73 | 74 |
| 74 } // namespace web | 75 } // namespace web |
| 75 | 76 |
| 76 #endif // IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_USER_DATA_H_ | 77 #endif // IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_USER_DATA_H_ |
| OLD | NEW |