0
Files
src/components/user_prefs/user_prefs.h
Bryant Chandler 8712ae0224 [user_prefs] Add IsInitialized API
This allows users of PrefService to check if there is a pref service
attached as there are environments where this can be the case.

Change-Id: I30a3b80e5ab1e4c01a14b04bc51a5189108a693f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4924031
Commit-Queue: Bryant Chandler <bryantchandler@chromium.org>
Reviewed-by: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1208300}
2023-10-11 16:23:56 +00:00

48 lines
1.5 KiB
C++

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_USER_PREFS_USER_PREFS_H_
#define COMPONENTS_USER_PREFS_USER_PREFS_H_
#include "base/memory/raw_ptr.h"
#include "base/supports_user_data.h"
#include "components/user_prefs/user_prefs_export.h"
class PrefService;
namespace user_prefs {
// Components may use preferences associated with a given user. These hang off
// of base::SupportsUserData and can be retrieved using UserPrefs::Get().
//
// It is up to the embedder to create and own the PrefService and attach it to
// base::SupportsUserData using the UserPrefs::Set() function.
class USER_PREFS_EXPORT UserPrefs : public base::SupportsUserData::Data {
public:
UserPrefs(const UserPrefs&) = delete;
UserPrefs& operator=(const UserPrefs&) = delete;
~UserPrefs() override;
// Returns true if there is a PrefService attached to the given context.
static bool IsInitialized(base::SupportsUserData* context);
// Retrieves the PrefService for a given context.
static PrefService* Get(base::SupportsUserData* context);
// Hangs the specified |prefs| off of |context|. Should be called
// only once per context.
static void Set(base::SupportsUserData* context, PrefService* prefs);
private:
explicit UserPrefs(PrefService* prefs);
// Non-owning; owned by embedder.
raw_ptr<PrefService, AcrossTasksDanglingUntriaged> prefs_;
};
} // namespace user_prefs
#endif // COMPONENTS_USER_PREFS_USER_PREFS_H_