Chromium Code Reviews| Index: ios/public/provider/web/web_state_user_data.h |
| diff --git a/ios/public/provider/web/web_state_user_data.h b/ios/public/provider/web/web_state_user_data.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cc602e75849dc3b6211da79a6bf6155b4f672d0a |
| --- /dev/null |
| +++ b/ios/public/provider/web/web_state_user_data.h |
| @@ -0,0 +1,73 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef IOS_PUBLIC_PROVIDER_WEB_WEB_STATE_USER_DATA_H_ |
| +#define IOS_PUBLIC_PROVIDER_WEB_WEB_STATE_USER_DATA_H_ |
| + |
| +#include "ios/public/consumer/base/supports_user_data.h" |
| +#include "ios/public/provider/web/web_state.h" |
| + |
| +namespace ios { |
| + |
| +// A base class for classes attached to, and scoped to, the lifetime of a |
| +// WebState. For example: |
| +// |
| +// --- in foo_tab_helper.h --- |
|
stuartmorgan
2013/07/02 15:24:26
Let's avoid tab, per the previous comment.
blundell
2013/07/02 16:16:37
Done.
|
| +// class FooTabHelper : public content::WebStateUserData<FooTabHelper> { |
| +// public: |
| +// virtual ~FooTabHelper(); |
| +// // ... more public stuff here ... |
| +// private: |
| +// explicit FooTabHelper(content::WebState* web_state); |
| +// friend class content::WebStateUserData<FooTabHelper>; |
| +// // ... more private stuff here ... |
| +// } |
| +// --- in foo_tab_helper.cc --- |
| +// DEFINE_WEB_CONTENTS_USER_DATA_KEY(FooTabHelper); |
| +// |
| +template <typename T> |
| +class WebStateUserData : public ios::SupportsUserData::Data { |
| + public: |
| + // Creates an object of type T, and attaches it to the specified WebState. |
| + // If an instance is already attached, does nothing. |
| + static void CreateForWebState(WebState* web_state) { |
| + if (!FromWebState(web_state)) |
| + web_state->SetUserData(UserDataKey(), new T(web_state)); |
| + } |
| + |
| + // Retrieves the instance of type T that was attached to the specified |
| + // WebState (via CreateForWebState above) and returns it. If no instance |
| + // of the type was attached, returns NULL. |
| + static T* FromWebState(WebState* web_state) { |
| + return static_cast<T*>(web_state->GetUserData(UserDataKey())); |
| + } |
| + static const T* FromWebState(const WebState* web_state) { |
| + return static_cast<const T*>(web_state->GetUserData(UserDataKey())); |
| + } |
| + |
| + protected: |
| + static inline void* UserDataKey() { |
| + return &kLocatorKey; |
| + } |
| + |
| + private: |
| + // The user data key. |
| + static int kLocatorKey; |
| +}; |
| + |
| +// The macro to define the locator key. This key must be defined in the .cc file |
| +// of the tab helper otherwise different instances for different template types |
|
stuartmorgan
2013/07/02 15:24:26
Fix "tab helper"
blundell
2013/07/02 16:16:37
Done.
|
| +// will be collapsed by the Visual Studio linker. |
|
stuartmorgan
2013/07/02 15:24:26
Pretty sure this isn't a concern for iOS :) Just s
blundell
2013/07/02 16:16:37
Done.
|
| +// |
| +// The "= 0" is surprising, but is required to effect a definition rather than |
| +// a declaration. Without it, this would be merely a declaration of a template |
| +// specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13) |
| +// |
| +#define DEFINE_WEB_STATE_USER_DATA_KEY(TYPE) \ |
| +template<> \ |
| +int ios::WebStateUserData<TYPE>::kLocatorKey = 0 |
| + |
| +} // namespace ios |
| + |
| +#endif // IOS_PUBLIC_PROVIDER_WEB_WEB_STATE_USER_DATA_H_ |