Commit 5b0a572a authored by kkhorimoto's avatar kkhorimoto Committed by Commit Bot

Create BrowserUserData.

This CL updates Browser to subclass base::SupportsUserData, and adds
BrowserUserData, which includes functionality analogous to
WebStateUserData.

BUG=none

Review-Url: https://codereview.chromium.org/2945713002
Cr-Commit-Position: refs/heads/master@{#481821}
parent 87cb08e7
...@@ -15,6 +15,7 @@ source_set("browser_list") { ...@@ -15,6 +15,7 @@ source_set("browser_list") {
"browser_list_session_service_factory.mm", "browser_list_session_service_factory.mm",
"browser_list_session_service_impl.h", "browser_list_session_service_impl.h",
"browser_list_session_service_impl.mm", "browser_list_session_service_impl.mm",
"browser_user_data.h",
"browser_web_state_list_delegate.h", "browser_web_state_list_delegate.h",
"browser_web_state_list_delegate.mm", "browser_web_state_list_delegate.mm",
] ]
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include "base/macros.h" #include "base/macros.h"
#include "base/supports_user_data.h"
class WebStateList; class WebStateList;
class WebStateListDelegate; class WebStateListDelegate;
...@@ -21,10 +22,10 @@ class ChromeBrowserState; ...@@ -21,10 +22,10 @@ class ChromeBrowserState;
// Browser holds the state backing a collection of Tabs and the attached // Browser holds the state backing a collection of Tabs and the attached
// UI elements (Tab strip, ...). // UI elements (Tab strip, ...).
class Browser { class Browser : public base::SupportsUserData {
public: public:
explicit Browser(ios::ChromeBrowserState* browser_state); explicit Browser(ios::ChromeBrowserState* browser_state);
~Browser(); ~Browser() override;
WebStateList& web_state_list() { return *web_state_list_.get(); } WebStateList& web_state_list() { return *web_state_list_.get(); }
const WebStateList& web_state_list() const { return *web_state_list_.get(); } const WebStateList& web_state_list() const { return *web_state_list_.get(); }
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_SHARED_CHROME_BROWSER_UI_BROWSER_LIST_BROWSER_USER_DATA_H_
#define IOS_SHARED_CHROME_BROWSER_UI_BROWSER_LIST_BROWSER_USER_DATA_H_
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/supports_user_data.h"
#import "ios/shared/chrome/browser/ui/browser_list/browser.h"
// A base class for classes attached to, and scoped to, the lifetime of a
// Browser. For example:
//
// --- in foo.h ---
// class Foo : public BrowserUserData<Foo> {
// public:
// ~Foo() override;
// // ... more public stuff here ...
// private:
// explicit Foo(Browser* browser);
// friend class BrowserUserData<Foo>;
// // ... more private stuff here ...
// }
// --- in foo.mm ---
// DEFINE_BROWSER_USER_DATA_KEY(Foo);
//
template <typename T>
class BrowserUserData : public base::SupportsUserData::Data {
public:
// Creates an object of type T, and attaches it to the specified Browser.
// If an instance is already attached, does nothing.
static void CreateForBrowser(Browser* browser) {
DCHECK(browser);
if (!FromBrowser(browser)) {
browser->SetUserData(UserDataKey(), base::WrapUnique(new T(browser)));
}
}
// Retrieves the instance of type T that was attached to the specified
// Browser (via CreateForBrowser above) and returns it. If no
// instance of the type was attached, returns nullptr.
static T* FromBrowser(Browser* browser) {
return static_cast<T*>(browser->GetUserData(UserDataKey()));
}
static const T* FromBrowser(const Browser* browser) {
return static_cast<const T*>(browser->GetUserData(UserDataKey()));
}
// Removes the instance attached to the specified WebState.
static void RemoveFromBrowser(Browser* browser) {
browser->RemoveUserData(UserDataKey());
}
protected:
static inline void* UserDataKey() { return &kLocatorKey; }
private:
// The user data key.
static int kLocatorKey;
};
// The macro to define the locator key. This key should be defined in the
// implementation file of the derived class.
//
// The "= 0" is surprising, but is required to effect a definition rather than
// a declaration. Without it, this would be merely a declaration of a template
// specialization. (C++98: 14.7.3.15; C++11: 14.7.3.13)
//
#define DEFINE_BROWSER_USER_DATA_KEY(TYPE) \
template <> \
int BrowserUserData<TYPE>::kLocatorKey = 0
#endif // IOS_SHARED_CHROME_BROWSER_UI_BROWSER_LIST_BROWSER_USER_DATA_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment