Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_USER_DATA_H_ | 5 #ifndef CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_USER_DATA_H_ |
| 6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_USER_DATA_H_ | 6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_USER_DATA_H_ |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/supports_user_data.h" | 9 #include "base/supports_user_data.h" |
| 10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
| 11 | 11 |
| 12 #if defined(COMPONENT_BUILD) && defined(WIN32) && defined(EXPORT_WEB_CONTENT_USE R_DATA) | |
| 13 #define EXPORT_DECL __declspec(dllexport) | |
| 14 #else | |
| 15 #define EXPORT_DECL | |
| 16 #endif | |
| 17 | |
| 12 namespace content { | 18 namespace content { |
| 13 | 19 |
| 14 // A base class for classes attached to, and scoped to, the lifetime of a | 20 // A base class for classes attached to, and scoped to, the lifetime of a |
| 15 // WebContents. For example: | 21 // WebContents. For example: |
| 16 // | 22 // |
| 17 // --- in foo_tab_helper.h --- | 23 // --- in foo_tab_helper.h --- |
| 18 // class FooTabHelper : public content::WebContentsUserData<FooTabHelper> { | 24 // class FooTabHelper : public content::WebContentsUserData<FooTabHelper> { |
| 19 // public: | 25 // public: |
| 20 // virtual ~FooTabHelper(); | 26 // virtual ~FooTabHelper(); |
| 21 // // ... more public stuff here ... | 27 // // ... more public stuff here ... |
| 22 // private: | 28 // private: |
| 23 // explicit FooTabHelper(content::WebContents* contents); | 29 // explicit FooTabHelper(content::WebContents* contents); |
| 24 // friend class content::WebContentsUserData<FooTabHelper>; | 30 // friend class content::WebContentsUserData<FooTabHelper>; |
| 25 // // ... more private stuff here ... | 31 // // ... more private stuff here ... |
| 26 // } | 32 // } |
| 27 // --- in foo_tab_helper.cc --- | 33 // --- in foo_tab_helper.cc --- |
| 28 // DEFINE_WEB_CONTENTS_USER_DATA_KEY(FooTabHelper); | 34 // DEFINE_WEB_CONTENTS_USER_DATA_KEY(FooTabHelper); |
| 29 // | 35 // |
| 30 template <typename T> | 36 template <typename T> |
| 31 class WebContentsUserData : public base::SupportsUserData::Data { | 37 class EXPORT_DECL WebContentsUserData : public base::SupportsUserData::Data { |
| 32 public: | 38 public: |
| 33 // Creates an object of type T, and attaches it to the specified WebContents. | 39 // Creates an object of type T, and attaches it to the specified WebContents. |
| 34 // If an instance is already attached, does nothing. | 40 // If an instance is already attached, does nothing. |
| 35 static void CreateForWebContents(WebContents* contents) { | 41 static void CreateForWebContents(WebContents* contents) { |
| 36 DCHECK(contents); | 42 DCHECK(contents); |
| 37 if (!FromWebContents(contents)) | 43 if (!FromWebContents(contents)) |
| 38 contents->SetUserData(UserDataKey(), new T(contents)); | 44 contents->SetUserData(UserDataKey(), new T(contents)); |
| 39 } | 45 } |
| 40 | 46 |
| 41 // Retrieves the instance of type T that was attached to the specified | 47 // Retrieves the instance of type T that was attached to the specified |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 60 static int kLocatorKey; | 66 static int kLocatorKey; |
| 61 }; | 67 }; |
| 62 | 68 |
| 63 // The macro to define the locator key. This key must be defined in the .cc file | 69 // The macro to define the locator key. This key must be defined in the .cc file |
| 64 // of the tab helper otherwise different instances for different template types | 70 // of the tab helper otherwise different instances for different template types |
| 65 // will be collapsed by the Visual Studio linker. | 71 // will be collapsed by the Visual Studio linker. |
| 66 // | 72 // |
| 67 // The "= 0" is surprising, but is required to effect a definition rather than | 73 // The "= 0" is surprising, but is required to effect a definition rather than |
| 68 // a declaration. Without it, this would be merely a declaration of a template | 74 // a declaration. Without it, this would be merely a declaration of a template |
| 69 // specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13) | 75 // specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13) |
| 70 // | 76 // |
|
kaiwang
2013/04/18 22:14:36
Please ignore the change below. It's not necessary
| |
| 77 #define DEFINE_EXPORTED_WEB_CONTENTS_USER_DATA_KEY(TYPE, EXPORT_DECLARATION) \ | |
| 78 template<> EXPORT_DECLARATION \ | |
| 79 int content::WebContentsUserData<TYPE>::kLocatorKey = 0 | |
| 80 | |
| 71 #define DEFINE_WEB_CONTENTS_USER_DATA_KEY(TYPE) \ | 81 #define DEFINE_WEB_CONTENTS_USER_DATA_KEY(TYPE) \ |
| 72 template<> \ | 82 DEFINE_EXPORTED_WEB_CONTENTS_USER_DATA_KEY(TYPE, ) |
| 73 int content::WebContentsUserData<TYPE>::kLocatorKey = 0 | |
| 74 | 83 |
| 75 } // namespace content | 84 } // namespace content |
| 76 | 85 |
| 77 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_USER_DATA_H_ | 86 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_USER_DATA_H_ |
| OLD | NEW |