Chromium Code Reviews| Index: chrome/browser/tab_contents/web_contents_user_data.h |
| diff --git a/chrome/browser/tab_contents/web_contents_user_data.h b/chrome/browser/tab_contents/web_contents_user_data.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..00fd9ab5051af4b469bf08583b26b54ec2b7a21d |
| --- /dev/null |
| +++ b/chrome/browser/tab_contents/web_contents_user_data.h |
| @@ -0,0 +1,41 @@ |
| +// Copyright (c) 2012 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 CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ |
| +#define CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ |
| + |
| +#include "base/supports_user_data.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +// A base class for classes attached to, and scoped to, the lifetime of a |
| +// WebContents. For example: |
| +// |
| +// --- in foo_tab_helper.h --- |
| +// class FooTabHelper : public WebContentsUserData<FooTabHelper> { |
| +// public: |
| +// explicit FooTabHelper(content::WebContents* contents); |
| +// virtual ~FooTabHelper(); |
| +// static int kUserDataKey; |
|
sky
2012/09/12 00:03:13
If this isn't constant, shouldn't it be named user
Avi (use Gerrit)
2012/09/12 00:08:44
The key to the puzzle is that its value is irrelev
|
| +// // ... more stuff here ... |
| +// } |
| +// --- in foo_tab_helper.cc --- |
| +// int FooTabHelper::kUserDataKey; |
| +// |
| +template <typename T> |
| +class WebContentsUserData : public base::SupportsUserData::Data { |
| + public: |
| + // Creates an object of type T, and attaches it to the specified WebContents. |
| + static void CreateForWebContents(content::WebContents* contents) { |
| + contents->SetUserData(&T::kUserDataKey, new T(contents)); |
| + } |
| + |
| + // Retrieves the instance of type T that was attached to the specified |
| + // WebContents (via CreateForWebContents above) and returns it. If no instance |
| + // of the type was attached, returns NULL. |
| + static T* FromWebContents(content::WebContents* contents) { |
| + return static_cast<T*>(contents->GetUserData(&T::kUserDataKey)); |
| + } |
| +}; |
| + |
| +#endif // CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ |