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