| 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..17d2fab6f90b744fdbe00133444d12e8da0f8cb9
|
| --- /dev/null
|
| +++ b/ios/public/provider/web/web_state_user_data.h
|
| @@ -0,0 +1,72 @@
|
| +// 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.h ---
|
| +// class Foo : public ios::WebStateUserData<Foo> {
|
| +// public:
|
| +// virtual ~Foo();
|
| +// // ... more public stuff here ...
|
| +// private:
|
| +// explicit Foo(ios::WebState* web_state);
|
| +// friend class ios::WebStateUserData<Foo>;
|
| +// // ... more private stuff here ...
|
| +// }
|
| +// --- in foo.cc ---
|
| +// DEFINE_WEB_CONTENTS_USER_DATA_KEY(Foo);
|
| +//
|
| +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 should be defined in the .cc
|
| +// file of the derived class.
|
| +//
|
| +// 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_
|
|
|