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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 // DEFINE_WEB_CONTENTS_USER_DATA_KEY(FooTabHelper) | 28 // DEFINE_WEB_CONTENTS_USER_DATA_KEY(FooTabHelper) |
29 // | 29 // |
30 template <typename T> | 30 template <typename T> |
31 class WebContentsUserData : public base::SupportsUserData::Data { | 31 class WebContentsUserData : public base::SupportsUserData::Data { |
32 public: | 32 public: |
33 // Creates an object of type T, and attaches it to the specified WebContents. | 33 // Creates an object of type T, and attaches it to the specified WebContents. |
34 // If an instance is already attached, does nothing. | 34 // If an instance is already attached, does nothing. |
35 static void CreateForWebContents(WebContents* contents) { | 35 static void CreateForWebContents(WebContents* contents) { |
36 DCHECK(contents); | 36 DCHECK(contents); |
37 if (!FromWebContents(contents)) | 37 if (!FromWebContents(contents)) |
38 contents->SetUserData(&kLocatorKey, new T(contents)); | 38 contents->SetUserData(UserDataKey(), new T(contents)); |
39 } | 39 } |
40 | 40 |
41 // Retrieves the instance of type T that was attached to the specified | 41 // Retrieves the instance of type T that was attached to the specified |
42 // WebContents (via CreateForWebContents above) and returns it. If no instance | 42 // WebContents (via CreateForWebContents above) and returns it. If no instance |
43 // of the type was attached, returns NULL. | 43 // of the type was attached, returns NULL. |
44 static T* FromWebContents(WebContents* contents) { | 44 static T* FromWebContents(WebContents* contents) { |
45 DCHECK(contents); | 45 DCHECK(contents); |
46 return static_cast<T*>(contents->GetUserData(&kLocatorKey)); | 46 return static_cast<T*>(contents->GetUserData(UserDataKey())); |
47 } | 47 } |
48 static const T* FromWebContents(const WebContents* contents) { | 48 static const T* FromWebContents(const WebContents* contents) { |
49 DCHECK(contents); | 49 DCHECK(contents); |
50 return static_cast<const T*>(contents->GetUserData(&kLocatorKey)); | 50 return static_cast<const T*>(contents->GetUserData(UserDataKey())); |
51 } | 51 } |
52 | 52 |
| 53 protected: |
| 54 static inline void* UserDataKey() { |
| 55 return &kLocatorKey; |
| 56 } |
| 57 |
| 58 private: |
53 // The user data key. | 59 // The user data key. |
54 static int kLocatorKey; | 60 static int kLocatorKey; |
55 }; | 61 }; |
56 | 62 |
57 // The macro to define the locator key. This key must be defined in the .cc file | 63 // The macro to define the locator key. This key must be defined in the .cc file |
58 // of the tab helper otherwise different instances for different template types | 64 // of the tab helper otherwise different instances for different template types |
59 // will be collapsed by the Visual Studio linker. | 65 // will be collapsed by the Visual Studio linker. |
60 // | 66 // |
61 // The "= 0" is surprising, but is required to effect a definition rather than | 67 // The "= 0" is surprising, but is required to effect a definition rather than |
62 // a declaration. Without it, this would be merely a declaration of a template | 68 // a declaration. Without it, this would be merely a declaration of a template |
63 // specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13) | 69 // specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13) |
64 // | 70 // |
65 #define DEFINE_WEB_CONTENTS_USER_DATA_KEY(TYPE) \ | 71 #define DEFINE_WEB_CONTENTS_USER_DATA_KEY(TYPE) \ |
66 template<> \ | 72 template<> \ |
67 int content::WebContentsUserData<TYPE>::kLocatorKey = 0; | 73 int content::WebContentsUserData<TYPE>::kLocatorKey = 0; |
68 | 74 |
69 } // namespace content | 75 } // namespace content |
70 | 76 |
71 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_USER_DATA_H_ | 77 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_USER_DATA_H_ |
OLD | NEW |