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/render_frame_host.h" | |
| 10 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 11 | 12 |
| 12 namespace content { | 13 namespace content { |
| 13 | 14 |
| 14 // A base class for classes attached to, and scoped to, the lifetime of a | 15 // A base class for classes attached to, and scoped to, the lifetime of a |
| 15 // WebContents. For example: | 16 // WebContents. For example: |
| 16 // | 17 // |
| 17 // --- in foo_tab_helper.h --- | 18 // --- in foo_tab_helper.h --- |
| 18 // class FooTabHelper : public content::WebContentsUserData<FooTabHelper> { | 19 // class FooTabHelper : public content::WebContentsUserData<FooTabHelper> { |
| 19 // public: | 20 // public: |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 42 // WebContents (via CreateForWebContents above) and returns it. If no instance | 43 // WebContents (via CreateForWebContents above) and returns it. If no instance |
| 43 // of the type was attached, returns NULL. | 44 // of the type was attached, returns NULL. |
| 44 static T* FromWebContents(WebContents* contents) { | 45 static T* FromWebContents(WebContents* contents) { |
| 45 DCHECK(contents); | 46 DCHECK(contents); |
| 46 return static_cast<T*>(contents->GetUserData(UserDataKey())); | 47 return static_cast<T*>(contents->GetUserData(UserDataKey())); |
| 47 } | 48 } |
| 48 static const T* FromWebContents(const WebContents* contents) { | 49 static const T* FromWebContents(const WebContents* contents) { |
| 49 DCHECK(contents); | 50 DCHECK(contents); |
| 50 return static_cast<const T*>(contents->GetUserData(UserDataKey())); | 51 return static_cast<const T*>(contents->GetUserData(UserDataKey())); |
| 51 } | 52 } |
| 53 static T* FromFrameID(int render_process_id, int render_frame_id) { | |
|
Fady Samuel
2014/06/20 22:54:33
Don't modify this file.
Xi Han
2014/06/24 13:55:33
Done.
| |
| 54 content::RenderFrameHost* render_frame_host = | |
| 55 content::RenderFrameHost::FromID(render_process_id, render_frame_id); | |
| 56 if (!render_frame_host) { | |
| 57 return NULL; | |
| 58 } | |
| 59 content::WebContents* web_contents = | |
| 60 content::WebContents::FromRenderFrameHost(render_frame_host); | |
| 61 return FromWebContents(web_contents); | |
| 62 } | |
| 52 | 63 |
| 53 protected: | 64 protected: |
| 54 static inline void* UserDataKey() { | 65 static inline void* UserDataKey() { |
| 55 return &kLocatorKey; | 66 return &kLocatorKey; |
| 56 } | 67 } |
| 57 | 68 |
| 58 private: | 69 private: |
| 59 // The user data key. | 70 // The user data key. |
| 60 static int kLocatorKey; | 71 static int kLocatorKey; |
| 61 }; | 72 }; |
| 62 | 73 |
| 63 // The macro to define the locator key. This key must be defined in the .cc file | 74 // 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 | 75 // of the tab helper otherwise different instances for different template types |
| 65 // will be collapsed by the Visual Studio linker. | 76 // will be collapsed by the Visual Studio linker. |
| 66 // | 77 // |
| 67 // The "= 0" is surprising, but is required to effect a definition rather than | 78 // 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 | 79 // 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) | 80 // specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13) |
| 70 // | 81 // |
| 71 #define DEFINE_WEB_CONTENTS_USER_DATA_KEY(TYPE) \ | 82 #define DEFINE_WEB_CONTENTS_USER_DATA_KEY(TYPE) \ |
| 72 template<> \ | 83 template<> \ |
| 73 int content::WebContentsUserData<TYPE>::kLocatorKey = 0 | 84 int content::WebContentsUserData<TYPE>::kLocatorKey = 0 |
| 74 | 85 |
| 75 } // namespace content | 86 } // namespace content |
| 76 | 87 |
| 77 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_USER_DATA_H_ | 88 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_USER_DATA_H_ |
| OLD | NEW |