OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef IOS_SHARED_CHROME_BROWSER_UI_BROWSER_LIST_BROWSER_USER_DATA_H_ |
| 6 #define IOS_SHARED_CHROME_BROWSER_UI_BROWSER_LIST_BROWSER_USER_DATA_H_ |
| 7 |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/supports_user_data.h" |
| 11 #import "ios/shared/chrome/browser/ui/browser_list/browser.h" |
| 12 |
| 13 // A base class for classes attached to, and scoped to, the lifetime of a |
| 14 // Browser. For example: |
| 15 // |
| 16 // --- in foo.h --- |
| 17 // class Foo : public BrowserUserData<Foo> { |
| 18 // public: |
| 19 // ~Foo() override; |
| 20 // // ... more public stuff here ... |
| 21 // private: |
| 22 // explicit Foo(Browser* browser); |
| 23 // friend class BrowserUserData<Foo>; |
| 24 // // ... more private stuff here ... |
| 25 // } |
| 26 // --- in foo.mm --- |
| 27 // DEFINE_BROWSER_USER_DATA_KEY(Foo); |
| 28 // |
| 29 template <typename T> |
| 30 class BrowserUserData : public base::SupportsUserData::Data { |
| 31 public: |
| 32 // Creates an object of type T, and attaches it to the specified Browser. |
| 33 // If an instance is already attached, does nothing. |
| 34 static void CreateForBrowser(Browser* browser) { |
| 35 DCHECK(browser); |
| 36 if (!FromBrowser(browser)) { |
| 37 browser->SetUserData(UserDataKey(), base::WrapUnique(new T(browser))); |
| 38 } |
| 39 } |
| 40 |
| 41 // Retrieves the instance of type T that was attached to the specified |
| 42 // Browser (via CreateForBrowser above) and returns it. If no |
| 43 // instance of the type was attached, returns nullptr. |
| 44 static T* FromBrowser(Browser* browser) { |
| 45 return static_cast<T*>(browser->GetUserData(UserDataKey())); |
| 46 } |
| 47 static const T* FromBrowser(const Browser* browser) { |
| 48 return static_cast<const T*>(browser->GetUserData(UserDataKey())); |
| 49 } |
| 50 // Removes the instance attached to the specified WebState. |
| 51 static void RemoveFromBrowser(Browser* browser) { |
| 52 browser->RemoveUserData(UserDataKey()); |
| 53 } |
| 54 |
| 55 protected: |
| 56 static inline void* UserDataKey() { return &kLocatorKey; } |
| 57 |
| 58 private: |
| 59 // The user data key. |
| 60 static int kLocatorKey; |
| 61 }; |
| 62 |
| 63 // The macro to define the locator key. This key should be defined in the |
| 64 // implementation file of the derived class. |
| 65 // |
| 66 // The "= 0" is surprising, but is required to effect a definition rather than |
| 67 // a declaration. Without it, this would be merely a declaration of a template |
| 68 // specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13) |
| 69 // |
| 70 #define DEFINE_BROWSER_USER_DATA_KEY(TYPE) \ |
| 71 template <> \ |
| 72 int BrowserUserData<TYPE>::kLocatorKey = 0 |
| 73 |
| 74 #endif // IOS_SHARED_CHROME_BROWSER_UI_BROWSER_LIST_BROWSER_USER_DATA_H_ |
OLD | NEW |