| 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 CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ | 5 #ifndef CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ |
| 6 #define CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ | 6 #define CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ |
| 7 | 7 |
| 8 #include "base/supports_user_data.h" | 8 #include "base/supports_user_data.h" |
| 9 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
| 10 | 10 |
| 11 // A base class for classes attached to, and scoped to, the lifetime of a | 11 // A base class for classes attached to, and scoped to, the lifetime of a |
| 12 // WebContents. For example: | 12 // WebContents. For example: |
| 13 // | 13 // |
| 14 // --- in foo_tab_helper.h --- | 14 // --- in foo_tab_helper.h --- |
| 15 // class FooTabHelper : public WebContentsUserData<FooTabHelper> { | 15 // class FooTabHelper : public WebContentsUserData<FooTabHelper> { |
| 16 // public: | 16 // public: |
| 17 // virtual ~FooTabHelper(); |
| 18 // // ... more public stuff here ... |
| 19 // private: |
| 17 // explicit FooTabHelper(content::WebContents* contents); | 20 // explicit FooTabHelper(content::WebContents* contents); |
| 18 // virtual ~FooTabHelper(); | |
| 19 // static int kUserDataKey; | 21 // static int kUserDataKey; |
| 20 // // ... more stuff here ... | 22 // friend class WebContentsUserData<FooTabHelper>; |
| 23 // // ... more private stuff here ... |
| 21 // } | 24 // } |
| 22 // --- in foo_tab_helper.cc --- | 25 // --- in foo_tab_helper.cc --- |
| 23 // int FooTabHelper::kUserDataKey; | 26 // int FooTabHelper::kUserDataKey; |
| 24 // | 27 // |
| 25 template <typename T> | 28 template <typename T> |
| 26 class WebContentsUserData : public base::SupportsUserData::Data { | 29 class WebContentsUserData : public base::SupportsUserData::Data { |
| 27 public: | 30 public: |
| 28 // Creates an object of type T, and attaches it to the specified WebContents. | 31 // Creates an object of type T, and attaches it to the specified WebContents. |
| 29 // If an instance is already attached, does nothing. | 32 // If an instance is already attached, does nothing. |
| 30 static void CreateForWebContents(content::WebContents* contents) { | 33 static void CreateForWebContents(content::WebContents* contents) { |
| 31 if (!FromWebContents(contents)) | 34 if (!FromWebContents(contents)) |
| 32 contents->SetUserData(&T::kUserDataKey, new T(contents)); | 35 contents->SetUserData(&T::kUserDataKey, new T(contents)); |
| 33 } | 36 } |
| 34 | 37 |
| 35 // Retrieves the instance of type T that was attached to the specified | 38 // Retrieves the instance of type T that was attached to the specified |
| 36 // WebContents (via CreateForWebContents above) and returns it. If no instance | 39 // WebContents (via CreateForWebContents above) and returns it. If no instance |
| 37 // of the type was attached, returns NULL. | 40 // of the type was attached, returns NULL. |
| 38 static T* FromWebContents(content::WebContents* contents) { | 41 static T* FromWebContents(content::WebContents* contents) { |
| 39 return static_cast<T*>(contents->GetUserData(&T::kUserDataKey)); | 42 return static_cast<T*>(contents->GetUserData(&T::kUserDataKey)); |
| 40 } | 43 } |
| 41 static const T* FromWebContents(const content::WebContents* contents) { | 44 static const T* FromWebContents(const content::WebContents* contents) { |
| 42 return static_cast<const T*>(contents->GetUserData(&T::kUserDataKey)); | 45 return static_cast<const T*>(contents->GetUserData(&T::kUserDataKey)); |
| 43 } | 46 } |
| 44 }; | 47 }; |
| 45 | 48 |
| 46 #endif // CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ | 49 #endif // CHROME_BROWSER_TAB_CONTENTS_WEB_CONTENTS_USER_DATA_H_ |
| OLD | NEW |