summaryrefslogtreecommitdiffstats
path: root/chrome/browser/common
diff options
context:
space:
mode:
authorjoi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-09 18:38:30 +0000
committerjoi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-09 18:38:30 +0000
commit46b3c98e14058daa45e7240bb23693e59d8cdd42 (patch)
treecd6b01073500b65fb59166b9e41dedbebd1fe70a /chrome/browser/common
parentfcbb87039f53b838106817f3c2466f65d5d85e3a (diff)
downloadchromium_src-46b3c98e14058daa45e7240bb23693e59d8cdd42.zip
chromium_src-46b3c98e14058daa45e7240bb23693e59d8cdd42.tar.gz
chromium_src-46b3c98e14058daa45e7240bb23693e59d8cdd42.tar.bz2
Move WebContentsUserData to content/public/browser.
Change its unit test to be a content_unittest and not depend on chrome/. BUG=none Review URL: https://codereview.chromium.org/11029049 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@160883 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/common')
-rw-r--r--chrome/browser/common/web_contents_user_data.h67
-rw-r--r--chrome/browser/common/web_contents_user_data_unittest.cc105
2 files changed, 0 insertions, 172 deletions
diff --git a/chrome/browser/common/web_contents_user_data.h b/chrome/browser/common/web_contents_user_data.h
deleted file mode 100644
index 370d9cb..0000000
--- a/chrome/browser/common/web_contents_user_data.h
+++ /dev/null
@@ -1,67 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_
-#define CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_
-
-#include "base/logging.h"
-#include "base/supports_user_data.h"
-#include "content/public/browser/web_contents.h"
-
-// A base class for classes attached to, and scoped to, the lifetime of a
-// WebContents. For example:
-//
-// --- in foo_tab_helper.h ---
-// class FooTabHelper : public WebContentsUserData<FooTabHelper> {
-// public:
-// virtual ~FooTabHelper();
-// // ... more public stuff here ...
-// private:
-// explicit FooTabHelper(content::WebContents* contents);
-// friend class WebContentsUserData<FooTabHelper>;
-// // ... more private stuff here ...
-// }
-// --- in foo_tab_helper.cc ---
-// DEFINE_WEB_CONTENTS_USER_DATA_KEY(FooTabHelper)
-//
-template <typename T>
-class WebContentsUserData : public base::SupportsUserData::Data {
- public:
- // Creates an object of type T, and attaches it to the specified WebContents.
- // If an instance is already attached, does nothing.
- static void CreateForWebContents(content::WebContents* contents) {
- DCHECK(contents);
- if (!FromWebContents(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) {
- DCHECK(contents);
- return static_cast<T*>(contents->GetUserData(&kLocatorKey));
- }
- static const T* FromWebContents(const content::WebContents* contents) {
- DCHECK(contents);
- 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
deleted file mode 100644
index 1ee3a6a..0000000
--- a/chrome/browser/common/web_contents_user_data_unittest.cc
+++ /dev/null
@@ -1,105 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/common/web_contents_user_data.h"
-
-#include "base/memory/scoped_ptr.h"
-#include "chrome/test/base/chrome_render_view_host_test_harness.h"
-#include "chrome/test/base/testing_profile.h"
-#include "content/public/browser/web_contents.h"
-#include "content/public/test/web_contents_tester.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-class WebContentsAttachedClass1
- : public WebContentsUserData<WebContentsAttachedClass1> {
- public:
- virtual ~WebContentsAttachedClass1() {}
- private:
- explicit WebContentsAttachedClass1(content::WebContents* contents) {}
- friend class WebContentsUserData<WebContentsAttachedClass1>;
-};
-
-class WebContentsAttachedClass2
- : public WebContentsUserData<WebContentsAttachedClass2> {
- public:
- virtual ~WebContentsAttachedClass2() {}
- private:
- explicit WebContentsAttachedClass2(content::WebContents* contents) {}
- friend class WebContentsUserData<WebContentsAttachedClass2>;
-};
-
-DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsAttachedClass1)
-DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsAttachedClass2)
-
-typedef ChromeRenderViewHostTestHarness WebContentsUserDataTest;
-
-TEST_F(WebContentsUserDataTest, OneInstanceTwoAttachments) {
- content::WebContents* contents = web_contents();
- WebContentsAttachedClass1* class1 =
- WebContentsAttachedClass1::FromWebContents(contents);
- ASSERT_EQ(NULL, class1);
- WebContentsAttachedClass2* class2 =
- WebContentsAttachedClass2::FromWebContents(contents);
- ASSERT_EQ(NULL, class2);
-
- WebContentsAttachedClass1::CreateForWebContents(contents);
- class1 = WebContentsAttachedClass1::FromWebContents(contents);
- ASSERT_TRUE(class1 != NULL);
- class2 = WebContentsAttachedClass2::FromWebContents(contents);
- ASSERT_EQ(NULL, class2);
-
- WebContentsAttachedClass2::CreateForWebContents(contents);
- WebContentsAttachedClass1* class1again =
- WebContentsAttachedClass1::FromWebContents(contents);
- ASSERT_TRUE(class1again != NULL);
- class2 = WebContentsAttachedClass2::FromWebContents(contents);
- ASSERT_TRUE(class2 != NULL);
- ASSERT_EQ(class1, class1again);
- ASSERT_NE(static_cast<void*>(class1), static_cast<void*>(class2));
-}
-
-TEST_F(WebContentsUserDataTest, TwoInstancesOneAttachment) {
- content::WebContents* contents1 = web_contents();
- scoped_ptr<content::WebContents> contents2(
- content::WebContentsTester::CreateTestWebContents(profile(), NULL));
-
- WebContentsAttachedClass1* one_class =
- WebContentsAttachedClass1::FromWebContents(contents1);
- ASSERT_EQ(NULL, one_class);
- WebContentsAttachedClass1* two_class =
- WebContentsAttachedClass1::FromWebContents(contents2.get());
- ASSERT_EQ(NULL, two_class);
-
- WebContentsAttachedClass1::CreateForWebContents(contents1);
- one_class = WebContentsAttachedClass1::FromWebContents(contents1);
- ASSERT_TRUE(one_class != NULL);
- two_class = WebContentsAttachedClass1::FromWebContents(contents2.get());
- ASSERT_EQ(NULL, two_class);
-
- WebContentsAttachedClass1::CreateForWebContents(contents2.get());
- WebContentsAttachedClass1* one_class_again =
- WebContentsAttachedClass1::FromWebContents(contents1);
- ASSERT_TRUE(one_class_again != NULL);
- two_class = WebContentsAttachedClass1::FromWebContents(contents2.get());
- ASSERT_TRUE(two_class != NULL);
- ASSERT_EQ(one_class, one_class_again);
- ASSERT_NE(one_class, two_class);
-}
-
-TEST_F(WebContentsUserDataTest, Idempotence) {
- content::WebContents* contents = web_contents();
- WebContentsAttachedClass1* clazz =
- WebContentsAttachedClass1::FromWebContents(contents);
- ASSERT_EQ(NULL, clazz);
-
- WebContentsAttachedClass1::CreateForWebContents(contents);
- clazz = WebContentsAttachedClass1::FromWebContents(contents);
- ASSERT_TRUE(clazz != NULL);
-
- WebContentsAttachedClass1::CreateForWebContents(contents);
- WebContentsAttachedClass1* class_again =
- WebContentsAttachedClass1::FromWebContents(contents);
- ASSERT_TRUE(class_again != NULL);
- ASSERT_EQ(clazz, class_again);
-}