Commit 162d66cc authored by Elly Fong-Jones's avatar Elly Fong-Jones Committed by Commit Bot

docs: fix up associated data pattern doc

This change fixes some markdown syntax issues, variable naming, etc. It
also adds mentions of some iOS-specific classes.

Bug: None
Change-Id: I308088245f8579f59757b03af4d1a6143b808cfc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2105957
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Reviewed-by: default avatardanakj <danakj@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#750939}
parent 5dddff8c
...@@ -48,17 +48,17 @@ The associated data pattern would look like this: ...@@ -48,17 +48,17 @@ The associated data pattern would look like this:
class Pokemon { class Pokemon {
... ...
class Data { struct Data {
virtual ~Data(); virtual ~Data();
}; };
map<Key, unique_ptr<Data>> user_data_; map<Key, unique_ptr<Data>> user_data_;
... ...
}; };
class PokemonNurse::PokemonData : public Pokemon::Data { class PokemonNurse::PokemonData : public Pokemon::Data {
Time last_checkup_; Time last_checkup_;
}; };
bool PokemonNurse::NeedsCheckup(Pokemon* p) { bool PokemonNurse::NeedsCheckup(Pokemon* p) {
if (!p->user_data_[PokemonNurse::kDataKey]) if (!p->user_data_[PokemonNurse::kDataKey])
p->user_data_[PokemonNurse::kDataKey] = make_unique<PokemonData>(); p->user_data_[PokemonNurse::kDataKey] = make_unique<PokemonData>();
...@@ -72,34 +72,36 @@ aware of the per-Pokemon data belonging to PokemonNurse. ...@@ -72,34 +72,36 @@ aware of the per-Pokemon data belonging to PokemonNurse.
## Use this pattern when: ## Use this pattern when:
* You have a central class C, and many places in your code want to store some * You have a central class C, and many places in your code want to store some
data for each instance of C. data for each instance of C.
* C can't directly contain your data for layering or code structure reasons. * C can't directly contain your data for layering or code structure reasons.
* The data being stored is ephemeral and your consumer classes don't care about * The data being stored is ephemeral and your consumer classes don't care
its lifetime. about its lifetime.
## Don't use this pattern when: ## Don't use this pattern when:
* The data being stored must expose any behavior other than a destructor to the * The data being stored must expose any behavior other than a destructor to
central class; in that case, you need a richer pattern than this one. the central class; in that case, you need a richer pattern than this one.
* The data being stored has a complex lifetime. * The data being stored has a complex lifetime.
* Deleting the data being stored has side effects. * Deleting the data being stored has side effects.
* There might be a very large amount of associated data entries; in that case, * There might be a very large amount (thousands) of associated data entries
a domain-specific data model will provide better efficiency. for a particular client class; in that case, a domain-specific data model
* The central class C and the consumer classes are close enough that it would will provide better efficiency.
make sense for C to store the data directly. * The central class C and the consumer classes are close enough that it would
make sense for C to store the data directly.
## Alternatives / See also: ## Alternatives / See also:
* Storing data for consumer classes directly on the object of class C. * Storing data for consumer classes directly on the object of class C.
* Having the consumer store a map between objects of class C and the consumer's * Having the consumer store a map between objects of class C and the
C-specific data. consumer's C-specific data.
## How to use this pattern in Chromium: ## How to use this pattern in Chromium:
The two most commonly-used instances of this pattern in Chromium are The two most commonly-used instances of this pattern in Chromium are
[SupportsUserData] (especially [WebContentsUserData]) and [KeyedService] [SupportsUserData] (especially [WebContentsUserData] and [WebStateUserData]) and
(usually via [BrowserContextKeyedServiceFactory]). [KeyedService] (usually via [BrowserContextKeyedServiceFactory] and
[BrowserStateKeyedServiceFactory]).
### SupportsUserData ### SupportsUserData
...@@ -163,8 +165,9 @@ provides a way to store data associated with a Profile. For example, if you ...@@ -163,8 +165,9 @@ provides a way to store data associated with a Profile. For example, if you
were creating a UserNicenessController, you might do: were creating a UserNicenessController, you might do:
class UserNicenessData : public KeyedService { class UserNicenessData : public KeyedService {
public:
static UserNicenessData* FromProfile(Profile* profile); static UserNicenessData* FromProfile(Profile* profile);
int niceness_; int niceness;
// Maybe also: // Maybe also:
// void Shutdown() override; // void Shutdown() override;
...@@ -186,7 +189,7 @@ were creating a UserNicenessController, you might do: ...@@ -186,7 +189,7 @@ were creating a UserNicenessController, you might do:
} }
// in code somewhere: // in code somewhere:
UserNicenessData::FromProfile(profile)->niceness_++; UserNicenessData::FromProfile(profile)->niceness++;
Any code that needs the UserNicenessData for a given Profile can call Any code that needs the UserNicenessData for a given Profile can call
`UserNicenessData::FromProfile`, and KeyedService and `UserNicenessData::FromProfile`, and KeyedService and
...@@ -212,3 +215,4 @@ attaches a KeyedService to a [BrowserState]. ...@@ -212,3 +215,4 @@ attaches a KeyedService to a [BrowserState].
[SupportsUserData]: https://chromium.googlesource.com/chromium/src/+/HEAD/base/supports_user_data.h [SupportsUserData]: https://chromium.googlesource.com/chromium/src/+/HEAD/base/supports_user_data.h
[TabHelper]: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/tab_helpers.md [TabHelper]: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/tab_helpers.md
[WebContentsUserData]: https://chromium.googlesource.com/chromium/src/+/HEAD/content/public/browser/web_contents_user_data.h [WebContentsUserData]: https://chromium.googlesource.com/chromium/src/+/HEAD/content/public/browser/web_contents_user_data.h
[WebStateUserData]: https://chromium.googlesource.com/chromium/src/+/HEAD/ios/web/public/web_state_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