summaryrefslogtreecommitdiffstats
path: root/chrome/browser/common
diff options
context:
space:
mode:
authoravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-09-28 17:10:05 +0000
committeravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-09-28 17:10:05 +0000
commitad4c6ff09282846c73ad201f5769c744aca574b7 (patch)
treeaf5e3c9ff4c3f8e876b70c88f8d13a2c4f423e62 /chrome/browser/common
parent2860a1f4a8fffd36b47eaace3b86cf06cc3faa2e (diff)
downloadchromium_src-ad4c6ff09282846c73ad201f5769c744aca574b7.zip
chromium_src-ad4c6ff09282846c73ad201f5769c744aca574b7.tar.gz
chromium_src-ad4c6ff09282846c73ad201f5769c744aca574b7.tar.bz2
Revert "Revert 159158 - Make using WebContentsUserData simpler."
This reverts commit r159277. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@159280 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/common')
-rw-r--r--chrome/browser/common/web_contents_user_data.h24
-rw-r--r--chrome/browser/common/web_contents_user_data_unittest.cc10
2 files changed, 21 insertions, 13 deletions
diff --git a/chrome/browser/common/web_contents_user_data.h b/chrome/browser/common/web_contents_user_data.h
index 2cffb58..364997e 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));
}
+
+ // 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.
+//
+// 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_
diff --git a/chrome/browser/common/web_contents_user_data_unittest.cc b/chrome/browser/common/web_contents_user_data_unittest.cc
index cb35bac..1ee3a6a 100644
--- a/chrome/browser/common/web_contents_user_data_unittest.cc
+++ b/chrome/browser/common/web_contents_user_data_unittest.cc
@@ -11,15 +11,12 @@
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
-namespace {
-
class WebContentsAttachedClass1
: public WebContentsUserData<WebContentsAttachedClass1> {
public:
virtual ~WebContentsAttachedClass1() {}
private:
explicit WebContentsAttachedClass1(content::WebContents* contents) {}
- static int kUserDataKey;
friend class WebContentsUserData<WebContentsAttachedClass1>;
};
@@ -29,14 +26,11 @@ class WebContentsAttachedClass2
virtual ~WebContentsAttachedClass2() {}
private:
explicit WebContentsAttachedClass2(content::WebContents* contents) {}
- static int kUserDataKey;
friend class WebContentsUserData<WebContentsAttachedClass2>;
};
-int WebContentsAttachedClass1::kUserDataKey;
-int WebContentsAttachedClass2::kUserDataKey;
-
-} // namespace
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsAttachedClass1)
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsAttachedClass2)
typedef ChromeRenderViewHostTestHarness WebContentsUserDataTest;