chrome: Enforce matching context for new user and incognito profiles

Selecting a new user or incognito profile via Chrome UI may result in
the creation of a new Profile object. If that occurs, we should find
or create a matching CefBrowserContext and CefRequestContext instead
of reusing an existing mismatched context (e.g. using the original
context for an incognito window would be a mismatch). If a new
CefRequestContext will be created the client can now implement
CefBrowserProcessHandler::GetDefaultRequestContextHandler() to
provide the handler for that context.

To test with a new user profile:
1. Click "Profile" icon, select "Add". Now you have 2+ profiles.
2. Click "Profile" icon, select the other user name to create a new
   window using the other user profile.
3. The new window should launch successfully.

To test with a new incognito profile:
1. Select "New Incognito window" from the 3-dot menu.
2. The new window should launch successfully.

To test DevTools window creation for the new profile:
1. Right-click in the new window, select Inspect.
2. The DevTools window should launch successfully.
This commit is contained in:
Marshall Greenblatt
2024-02-08 13:41:18 -05:00
parent a79981bf7f
commit cc3d77eec5
14 changed files with 244 additions and 75 deletions

View File

@ -38,6 +38,16 @@ class CefRequestContextImpl : public CefRequestContext {
static CefRefPtr<CefRequestContextImpl> GetOrCreateForRequestContext(
CefRefPtr<CefRequestContext> request_context);
// Returns a CefRequestContextImpl for the specified |browser_context| and
// optional |handler|. If |handler| is nullptr, and a CefRequestContextImpl
// without a handler currently exists for |browser_context|, then that
// existing CefRequestContextImpl will be returned. Otherwise, a new
// CefRequestContextImpl will be created with the specified |handler|. Must be
// called on the UI thread.
static CefRefPtr<CefRequestContextImpl> GetOrCreateForBrowserContext(
CefBrowserContext* browser_context,
CefRefPtr<CefRequestContextHandler> handler);
// Returns the BrowserContext for the specified |request_context|. Will return
// the global BrowserContext if |request_context| is NULL.
static content::BrowserContext* GetBrowserContext(
@ -145,10 +155,12 @@ class CefRequestContextImpl : public CefRequestContext {
// True if wrapping the global context.
bool is_global = false;
// Wrap an existing (non-global) browser context. When specifying this value
// GetOrCreateRequestContext() must be called on the UI thread.
CefBrowserContext* browser_context = nullptr;
// |settings| or |other| will be set when creating a new CefRequestContext
// via the API. When wrapping an existing CefBrowserContext* both will be
// empty and Initialize(CefBrowserContext*) will be called immediately after
// CefRequestContextImpl construction.
// via the API.
CefRequestContextSettings settings;
CefRefPtr<CefRequestContextImpl> other;
@ -156,22 +168,19 @@ class CefRequestContextImpl : public CefRequestContext {
CefRefPtr<CefRequestContextHandler> handler;
// Used to uniquely identify CefRequestContext objects before an associated
// CefBrowserContext has been created. Should be set when a new
// CefBrowserContext has been created. Should be set when creating a new
// CefRequestContext via the API.
int unique_id = -1;
};
static CefRefPtr<CefRequestContextImpl> GetOrCreateRequestContext(
const Config& config);
Config&& config);
explicit CefRequestContextImpl(const Config& config);
explicit CefRequestContextImpl(Config&& config);
void Initialize();
void BrowserContextInitialized();
// Make sure the browser context exists. Only called on the UI thread.
void EnsureBrowserContext();
void ClearCertificateExceptionsInternal(
CefRefPtr<CefCompletionCallback> callback,
CefBrowserContext::Getter browser_context_getter);