Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: chrome/browser/common/web_contents_user_data.h

Issue 10993064: Make using WebContentsUserData simpler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_ 5 #ifndef CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_
6 #define CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_ 6 #define CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_
7 7
8 #include "base/supports_user_data.h" 8 #include "base/supports_user_data.h"
9 #include "content/public/browser/web_contents.h" 9 #include "content/public/browser/web_contents.h"
10 10
11 // A base class for classes attached to, and scoped to, the lifetime of a 11 // A base class for classes attached to, and scoped to, the lifetime of a
12 // WebContents. For example: 12 // WebContents. For example:
13 // 13 //
14 // --- in foo_tab_helper.h --- 14 // --- in foo_tab_helper.h ---
15 // class FooTabHelper : public WebContentsUserData<FooTabHelper> { 15 // class FooTabHelper : public WebContentsUserData<FooTabHelper> {
16 // public: 16 // public:
17 // virtual ~FooTabHelper(); 17 // virtual ~FooTabHelper();
18 // // ... more public stuff here ... 18 // // ... more public stuff here ...
19 // private: 19 // private:
20 // explicit FooTabHelper(content::WebContents* contents); 20 // explicit FooTabHelper(content::WebContents* contents);
21 // static int kUserDataKey;
22 // friend class WebContentsUserData<FooTabHelper>; 21 // friend class WebContentsUserData<FooTabHelper>;
23 // // ... more private stuff here ... 22 // // ... more private stuff here ...
24 // } 23 // }
25 // --- in foo_tab_helper.cc --- 24 // --- in foo_tab_helper.cc ---
26 // int FooTabHelper::kUserDataKey; 25 // DEFINE_WEB_CONTENTS_USER_DATA_KEY(FooTabHelper)
27 // 26 //
28 template <typename T> 27 template <typename T>
29 class WebContentsUserData : public base::SupportsUserData::Data { 28 class WebContentsUserData : public base::SupportsUserData::Data {
30 public: 29 public:
31 // Creates an object of type T, and attaches it to the specified WebContents. 30 // Creates an object of type T, and attaches it to the specified WebContents.
32 // If an instance is already attached, does nothing. 31 // If an instance is already attached, does nothing.
33 static void CreateForWebContents(content::WebContents* contents) { 32 static void CreateForWebContents(content::WebContents* contents) {
34 if (!FromWebContents(contents)) 33 if (!FromWebContents(contents))
35 contents->SetUserData(&T::kUserDataKey, new T(contents)); 34 contents->SetUserData(&kLocatorKey, new T(contents));
36 } 35 }
37 36
38 // Retrieves the instance of type T that was attached to the specified 37 // Retrieves the instance of type T that was attached to the specified
39 // WebContents (via CreateForWebContents above) and returns it. If no instance 38 // WebContents (via CreateForWebContents above) and returns it. If no instance
40 // of the type was attached, returns NULL. 39 // of the type was attached, returns NULL.
41 static T* FromWebContents(content::WebContents* contents) { 40 static T* FromWebContents(content::WebContents* contents) {
42 return static_cast<T*>(contents->GetUserData(&T::kUserDataKey)); 41 return static_cast<T*>(contents->GetUserData(&kLocatorKey));
43 } 42 }
44 static const T* FromWebContents(const content::WebContents* contents) { 43 static const T* FromWebContents(const content::WebContents* contents) {
45 return static_cast<const T*>(contents->GetUserData(&T::kUserDataKey)); 44 return static_cast<const T*>(contents->GetUserData(&kLocatorKey));
46 } 45 }
46
awong 2012/10/10 17:03:46 Can this be private?
Avi (use Gerrit) 2012/10/10 18:18:54 I don't know. Let me try. I'd be happy if it were.
47 // The user data key.
48 static int kLocatorKey;
47 }; 49 };
48 50
51 // The macro to define the locator key. This key must be defined in the .cc file
52 // of the tab helper otherwise different instances for different template types
53 // will be collapsed by the Visual Studio linker.
awong 2012/10/10 17:03:46 This comment still makes me a bit uneasy. Since w
Avi (use Gerrit) 2012/10/10 18:18:54 I wish I knew. I'm trying to remember who I had th
54 //
55 // The "= 0" is surprising, but is required to effect a definition rather than
56 // a declaration. Without it, this would be merely a declaration of a template
57 // specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13)
58 //
59 #define DEFINE_WEB_CONTENTS_USER_DATA_KEY(TYPE) \
60 template<> \
61 int WebContentsUserData<TYPE>::kLocatorKey = 0;
62
49 #endif // CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_ 63 #endif // CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_
OLDNEW
« no previous file with comments | « chrome/browser/captive_portal/captive_portal_tab_helper.cc ('k') | chrome/browser/common/web_contents_user_data_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698