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

Unified 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, 3 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/common/web_contents_user_data.h
diff --git a/chrome/browser/common/web_contents_user_data.h b/chrome/browser/common/web_contents_user_data.h
index 2cffb588e864a5829bf989fe13b4586ca352d3a8..364997ebd93c428d62979db2e0ca0d1d3344c2e5 100644
--- a/chrome/browser/common/web_contents_user_data.h
+++ b/chrome/browser/common/web_contents_user_data.h
@@ -18,12 +18,11 @@
// // ... more public stuff here ...
// private:
// explicit FooTabHelper(content::WebContents* contents);
-// static int kUserDataKey;
// friend class WebContentsUserData<FooTabHelper>;
// // ... more private stuff here ...
// }
// --- in foo_tab_helper.cc ---
-// int FooTabHelper::kUserDataKey;
+// DEFINE_WEB_CONTENTS_USER_DATA_KEY(FooTabHelper)
//
template <typename T>
class WebContentsUserData : public base::SupportsUserData::Data {
@@ -32,18 +31,33 @@ class WebContentsUserData : public base::SupportsUserData::Data {
// If an instance is already attached, does nothing.
static void CreateForWebContents(content::WebContents* contents) {
if (!FromWebContents(contents))
- contents->SetUserData(&T::kUserDataKey, new T(contents));
+ contents->SetUserData(&kLocatorKey, 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));
+ return static_cast<T*>(contents->GetUserData(&kLocatorKey));
}
static const T* FromWebContents(const content::WebContents* contents) {
- return static_cast<const T*>(contents->GetUserData(&T::kUserDataKey));
+ return static_cast<const T*>(contents->GetUserData(&kLocatorKey));
}
+
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.
+ // The user data key.
+ static int kLocatorKey;
};
+// The macro to define the locator key. This key must be defined in the .cc file
+// of the tab helper otherwise different instances for different template types
+// 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
+//
+// 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_CONTENTS_USER_DATA_KEY(TYPE) \
+template<> \
+int WebContentsUserData<TYPE>::kLocatorKey = 0;
+
#endif // CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_
« 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