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 // --- in foo_tab_helper.h --- | |
15 // class FooTabHelper : public WebContentsUserData<FooTabHelper> { | |
16 // public: | |
17 // explicit FooTabHelper(content::WebContents* contents); | |
18 // virtual ~FooTabHelper(); | |
19 // 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
| |
20 // // ... more stuff here ... | |
21 // } | |
22 // --- in foo_tab_helper.cc --- | |
23 // int FooTabHelper::kUserDataKey; | |
24 // | |
25 template <typename T> | |
26 class WebContentsUserData : public base::SupportsUserData::Data { | |
27 public: | |
28 // Creates an object of type T, and attaches it to the specified WebContents. | |
29 static void CreateForWebContents(content::WebContents* contents) { | |
30 contents->SetUserData(&T::kUserDataKey, new T(contents)); | |
31 } | |
32 | |
33 // Retrieves the instance of type T that was attached to the specified | |
34 // WebContents (via CreateForWebContents above) and returns it. If no instance | |
35 // of the type was attached, returns NULL. | |
36 static T* FromWebContents(content::WebContents* contents) { | |
37 return static_cast<T*>(contents->GetUserData(&T::kUserDataKey)); | |
38 } | |
39 }; | |
40 | |
41 #endif // CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ | |
OLD | NEW |