OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ | |
6 #define CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ | |
7 | |
8 #include "base/supports_user_data.h" | |
9 #include "content/public/browser/web_contents.h" | |
10 | |
11 // A base class for classes attached to, and scoped to, the lifetime of a | |
12 // WebContents. For example: | |
13 // | |
14 // class FooTabHelper : public WebContentsUserData<FooTabHelper> { | |
15 // public: | |
16 // explicit FooTabHelper(content::WebContents* contents); | |
17 // virtual ~FooTabHelper(); | |
18 // | |
19 template <typename T> | |
20 class WebContentsUserData : public base::SupportsUserData::Data { | |
21 public: | |
22 // Creates an object of type T, and attaches it to the specified WebContents. | |
23 static void CreateForWebContents(content::WebContents* contents) { | |
24 void* key = reinterpret_cast<void*>(&CreateForWebContents); | |
25 contents->SetUserData(key, new T(contents)); | |
26 } | |
27 | |
28 // Retrieves the instance of type T that was attached to the specified | |
29 // WebContents (via CreateForWebContents above) and returns it. If no instance | |
30 // of the type was attached, returns NULL. | |
31 static T* FromWebContents(content::WebContents* contents) { | |
32 void* key = reinterpret_cast<void*>(&CreateForWebContents); | |
33 return static_cast<T*>(contents->GetUserData(key)); | |
34 } | |
35 }; | |
36 | |
37 #endif // CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ | |
OLD | NEW |