blob: 1109975d740352cbad67af0e765fbfcc1da3fd3f [file] [log] [blame]
// Copyright (c) 2012 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.
// This class gathers state related to a single user profile.
#include <string>
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "build/build_config.h"
#include "components/domain_reliability/clear_mode.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
#include "services/network/public/mojom/network_service.mojom.h"
#if !defined(OS_ANDROID)
class ChromeZoomLevelPrefs;
class ExtensionSpecialStoragePolicy;
class PrefService;
class PrefStore;
class TestingProfile;
namespace base {
class SequencedTaskRunner;
namespace content {
class WebUI;
namespace network {
class SharedURLLoaderFactory;
namespace user_prefs {
class PrefRegistrySyncable;
// Instead of adding more members to Profile, consider creating a
// KeyedService. See
class Profile : public content::BrowserContext {
enum CreateStatus {
// Profile services were not created due to a local error (e.g., disk full).
// Profile services were not created due to a remote error (e.g., network
// down during limited-user registration).
// Profile created but before initializing extensions and promo resources.
// Profile is created, extensions and promo resources are initialized.
// Profile creation (supervised-user registration, generally) was canceled
// by the user.
MAX_CREATE_STATUS // For histogram display.
enum CreateMode {
enum ExitType {
// A normal shutdown. The user clicked exit/closed last window of the
// profile.
// The exit was the result of the system shutting down.
enum ProfileType {
REGULAR_PROFILE, // Login user's normal profile
INCOGNITO_PROFILE, // Login user's off-the-record profile
GUEST_PROFILE, // Guest session's profile
class Delegate {
virtual ~Delegate();
// Called when creation of the profile is finished.
virtual void OnProfileCreated(Profile* profile,
bool success,
bool is_new_profile) = 0;
// Key used to bind profile to the widget with which it is associated.
static const char kProfileKey[];
~Profile() override;
// Profile prefs are registered as soon as the prefs are loaded for the first
// time.
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
// Create a new profile given a path. If |create_mode| is
// CREATE_MODE_ASYNCHRONOUS then the profile is initialized asynchronously.
// Can return null if |create_mode| is CREATE_MODE_SYNCHRONOUS and the
// creation of the profile directory fails.
static Profile* CreateProfile(const base::FilePath& path,
Delegate* delegate,
CreateMode create_mode);
// Returns the profile corresponding to the given browser context.
static Profile* FromBrowserContext(content::BrowserContext* browser_context);
// Returns the profile corresponding to the given WebUI.
static Profile* FromWebUI(content::WebUI* web_ui);
// content::BrowserContext implementation ------------------------------------
// Typesafe upcast.
virtual TestingProfile* AsTestingProfile();
// Returns sequenced task runner where browser context dependent I/O
// operations should be performed.
virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() = 0;
// Returns the username associated with this profile, if any. In non-test
// implementations, this is usually the Google-services email address.
virtual std::string GetProfileUserName() const = 0;
// Returns the profile type.
virtual ProfileType GetProfileType() const = 0;
// Return the incognito version of this profile. The returned pointer
// is owned by the receiving profile. If the receiving profile is off the
// record, the same profile is returned.
// WARNING: This will create the OffTheRecord profile if it doesn't already
// exist. If this isn't what you want, you need to check
// HasOffTheRecordProfile() first.
virtual Profile* GetOffTheRecordProfile() = 0;
// Destroys the incognito profile.
virtual void DestroyOffTheRecordProfile() = 0;
// True if an incognito profile exists.
virtual bool HasOffTheRecordProfile() = 0;
// Return the original "recording" profile. This method returns this if the
// profile is not incognito.
virtual Profile* GetOriginalProfile() = 0;
// Return the original "recording" profile. This method returns this if the
// profile is not incognito.
virtual const Profile* GetOriginalProfile() const = 0;
// Returns whether the profile is supervised (either a legacy supervised
// user or a child account; see SupervisedUserService).
virtual bool IsSupervised() const = 0;
// Returns whether the profile is associated with a child account.
virtual bool IsChild() const = 0;
// Returns whether the profile is a legacy supervised user profile.
virtual bool IsLegacySupervised() const = 0;
// Accessor. The instance is created upon first access.
virtual ExtensionSpecialStoragePolicy*
GetExtensionSpecialStoragePolicy() = 0;
// Retrieves a pointer to the PrefService that manages the
// preferences for this user profile.
virtual PrefService* GetPrefs() = 0;
virtual const PrefService* GetPrefs() const = 0;
#if !defined(OS_ANDROID)
// Retrieves a pointer to the PrefService that manages the default zoom
// level and the per-host zoom levels for this user profile.
// TODO(wjmaclean): Remove this when HostZoomMap migrates to StoragePartition.
virtual ChromeZoomLevelPrefs* GetZoomLevelPrefs();
// Retrieves a pointer to the PrefService that manages the preferences
// for OffTheRecord Profiles. This PrefService is lazily created the first
// time that this method is called.
virtual PrefService* GetOffTheRecordPrefs() = 0;
// Like GetOffTheRecordPrefs but gives a read-only view of prefs that can be
// used even if there's no OTR profile at the moment
// (i.e. HasOffTheRecordProfile is false).
virtual PrefService* GetReadOnlyOffTheRecordPrefs();
// Returns the main request context.
virtual net::URLRequestContextGetter* GetRequestContext() = 0;
// Returns a callback (which must be executed on the IO thread) that returns
// the cookie store for the chrome-extensions:// scheme.
virtual base::OnceCallback<net::CookieStore*()>
GetExtensionsCookieStoreGetter() = 0;
// Returns the main URLLoaderFactory.
virtual scoped_refptr<network::SharedURLLoaderFactory>
GetURLLoaderFactory() = 0;
// Return whether 2 profiles are the same. 2 profiles are the same if they
// represent the same profile. This can happen if there is pointer equality
// or if one profile is the incognito version of another profile (or vice
// versa).
virtual bool IsSameProfile(Profile* profile) = 0;
// Returns the time the profile was started. This is not the time the profile
// was created, rather it is the time the user started chrome and logged into
// this profile. For the single profile case, this corresponds to the time
// the user started chrome.
virtual base::Time GetStartTime() const = 0;
// Returns the last directory that was chosen for uploading or opening a file.
virtual base::FilePath last_selected_directory() = 0;
virtual void set_last_selected_directory(const base::FilePath& path) = 0;
#if defined(OS_CHROMEOS)
enum AppLocaleChangedVia {
// Caused by chrome://settings change.
// Locale has been reverted via LocaleChangeGuard.
// From login screen.
// From login to a public session.
// From AllowedLanguages policy.
// From demo session.
// From system tray.
// Source unknown.
// Changes application locale for a profile.
virtual void ChangeAppLocale(
const std::string& locale, AppLocaleChangedVia via) = 0;
// Called after login.
virtual void OnLogin() = 0;
// Initializes Chrome OS's preferences.
virtual void InitChromeOSPreferences() = 0;
#endif // defined(OS_CHROMEOS)
// Returns the home page for this profile.
virtual GURL GetHomePage() = 0;
// Returns whether or not the profile was created by a version of Chrome
// more recent (or equal to) the one specified.
virtual bool WasCreatedByVersionOrLater(const std::string& version) = 0;
std::string GetDebugName();
// Returns whether it is a guest session.
virtual bool IsGuestSession() const;
// Returns whether it is a system profile.
virtual bool IsSystemProfile() const;
// Did the user restore the last session? This is set by SessionRestore.
void set_restored_last_session(bool restored_last_session) {
restored_last_session_ = restored_last_session;
bool restored_last_session() const {
return restored_last_session_;
// Sets the ExitType for the profile. This may be invoked multiple times
// during shutdown; only the first such change (the transition from
// EXIT_CRASHED to one of the other values) is written to prefs, any
// later calls are ignored.
// NOTE: this is invoked internally on a normal shutdown, but is public so
// that it can be invoked when the user logs out/powers down (WM_ENDSESSION),
// or to handle backgrounding/foregrounding on mobile.
virtual void SetExitType(ExitType exit_type) = 0;
// Returns how the last session was shutdown.
virtual ExitType GetLastSessionExitType() = 0;
// Returns whether session cookies are restored and saved. The value is
// ignored for in-memory profiles.
virtual bool ShouldRestoreOldSessionCookies();
virtual bool ShouldPersistSessionCookies();
// Creates NetworkContext for the specified isolated app (or for the profile
// itself, if |relative_path| is empty).
virtual network::mojom::NetworkContextPtr CreateNetworkContext(
bool in_memory,
const base::FilePath& relative_partition_path);
// Stop sending accessibility events until ResumeAccessibilityEvents().
// Calls to Pause nest; no events will be sent until the number of
// Resume calls matches the number of Pause calls received.
void PauseAccessibilityEvents() {
void ResumeAccessibilityEvents() {
DCHECK_GT(accessibility_pause_level_, 0);
bool ShouldSendAccessibilityEvents() {
return 0 == accessibility_pause_level_;
// Returns whether the profile is new. A profile is new if the browser has
// not been shut down since the profile was created.
// This method is virtual in order to be overridden for tests.
virtual bool IsNewProfile();
// Checks whether sync is configurable by the user. Returns false if sync is
// disallowed by the command line or controlled by configuration management.
bool IsSyncAllowed();
// Send NOTIFICATION_PROFILE_DESTROYED for this Profile, if it has not
// already been sent. It is necessary because most Profiles are destroyed by
// ProfileDestroyer, but in tests, some are not.
void MaybeSendDestroyedNotification();
// Creates an OffTheRecordProfile which points to this Profile.
Profile* CreateOffTheRecordProfile();
#if !defined(OS_ANDROID)
// Convenience method to retrieve the default zoom level for the default
// storage partition.
double GetDefaultZoomLevelForProfile();
void set_is_guest_profile(bool is_guest_profile) {
is_guest_profile_ = is_guest_profile;
void set_is_system_profile(bool is_system_profile) {
is_system_profile_ = is_system_profile;
// Returns a newly created ExtensionPrefStore suitable for the supplied
// Profile.
static PrefStore* CreateExtensionPrefStore(Profile*,
bool incognito_pref_store);
bool restored_last_session_;
// Used to prevent the notification that this Profile is destroyed from
// being sent twice.
bool sent_destroyed_notification_;
// Accessibility events will only be propagated when the pause
// level is zero. PauseAccessibilityEvents and ResumeAccessibilityEvents
// increment and decrement the level, respectively, rather than set it to
// true or false, so that calls can be nested.
int accessibility_pause_level_;
bool is_guest_profile_;
// A non-browsing profile not associated to a user. Sample use: User-Manager.
bool is_system_profile_;
// The comparator for profile pointers as key in a map.
struct ProfileCompare {
bool operator()(Profile* a, Profile* b) const;