containers: Allow NTP to be contained. (#34081)

* containers: Allow NTP to be contained.

* Review fixes.

* Fix include format.
This commit is contained in:
Aleksei Khoroshilov
2026-03-10 14:52:01 +00:00
committed by GitHub
parent ea4500147b
commit 79489598b0
5 changed files with 99 additions and 20 deletions
@@ -19,6 +19,7 @@
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
@@ -1560,4 +1561,36 @@ IN_PROC_BROWSER_TEST_F(ContainersDisabledAfterRestoreBrowserTest,
<< local_storage_result;
}
IN_PROC_BROWSER_TEST_F(ContainersBrowserTest,
NewTabPageInheritsStoragePartitionConfig) {
const GURL new_tab_url(chrome::kChromeUINewTabURL);
// Open a new tab page with a container storage partition config
NavigateParams params(browser(), new_tab_url, ui::PAGE_TRANSITION_LINK);
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
params.storage_partition_config = content::StoragePartitionConfig::Create(
browser()->profile(), kContainersStoragePartitionDomain, "test-container",
browser()->profile()->IsOffTheRecord());
ui_test_utils::NavigateToURL(&params);
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(web_contents);
// Verify the storage partition config is set correctly
content::StoragePartition* storage_partition =
web_contents->GetPrimaryMainFrame()->GetStoragePartition();
ASSERT_TRUE(storage_partition);
content::StoragePartitionConfig expected_config =
content::StoragePartitionConfig::Create(
browser()->profile(), kContainersStoragePartitionDomain,
"test-container", browser()->profile()->IsOffTheRecord());
EXPECT_EQ(expected_config, storage_partition->GetConfig());
EXPECT_EQ("test-container", storage_partition->GetConfig().partition_name());
EXPECT_EQ(kContainersStoragePartitionDomain,
storage_partition->GetConfig().partition_domain());
}
} // namespace containers
@@ -6,25 +6,29 @@
#include "chrome/browser/tab_contents/tab_util.h"
#include "brave/components/containers/buildflags/buildflags.h"
#include "content/public/browser/site_instance.h"
#if BUILDFLAG(ENABLE_CONTAINERS)
// Extend GetSiteInstanceForNewTab to accept an optional StoragePartitionConfig.
// When a config is present it delegates to CreateForFixedStoragePartition;
// otherwise it falls back to CreateForURL.
#define GetSiteInstanceForNewTab(...) \
GetSiteInstanceForNewTab( \
__VA_ARGS__, \
std::optional<content::StoragePartitionConfig> storage_partition_config)
#define BRAVE_GET_SITE_INSTANCE_FOR_NEW_TAB \
if (storage_partition_config) { \
return SiteInstance::CreateForFixedStoragePartition( \
profile, url, *storage_partition_config); \
}
#else
#define BRAVE_GET_SITE_INSTANCE_FOR_NEW_TAB
// Redirect CreateForURL to our helper which conditionally calls
// CreateForFixedStoragePartition when a StoragePartitionConfig is present,
// ensuring navigations respect the passed config.
#define CreateForURL(...) \
CreateForURLWithOptionalFixedStoragePartition(__VA_ARGS__, \
storage_partition_config)
#endif // BUILDFLAG(ENABLE_CONTAINERS)
#include <chrome/browser/tab_contents/tab_util.cc> // IWYU pragma: export
#if BUILDFLAG(ENABLE_CONTAINERS)
#undef CreateForURL
#undef GetSiteInstanceForNewTab
#undef BRAVE_GET_SITE_INSTANCE_FOR_NEW_TAB
#endif // BUILDFLAG(ENABLE_CONTAINERS)
@@ -0,0 +1,28 @@
/* Copyright (c) 2026 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "content/browser/site_instance_impl.h"
#include <content/browser/site_instance_impl.cc>
namespace content {
// Used by the containers feature so that SiteInstance::GetSiteInstanceForNewTab
// can apply a container's StoragePartitionConfig when opening a new tab page,
// rather than always falling back to the default partition via CreateForURL.
scoped_refptr<SiteInstance>
SiteInstance::CreateForURLWithOptionalFixedStoragePartition(
BrowserContext* browser_context,
const GURL& url,
base::optional_ref<StoragePartitionConfig> storage_partition_config) {
if (storage_partition_config) {
return CreateForFixedStoragePartition(browser_context, url,
*storage_partition_config);
} else {
return CreateForURL(browser_context, url);
}
}
} // namespace content
@@ -0,0 +1,26 @@
/* Copyright (c) 2026 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_SITE_INSTANCE_H_
#define BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_SITE_INSTANCE_H_
#include "base/types/optional_ref.h"
// Extends SiteInstance with CreateForURLWithOptionalFixedStoragePartition, a
// helper used by the containers feature to apply a StoragePartitionConfig. When
// a config is present it delegates to CreateForFixedStoragePartition; otherwise
// it falls back to CreateForURL.
#define CreateForURL(...) \
CreateForURL(__VA_ARGS__); \
static scoped_refptr<SiteInstance> \
CreateForURLWithOptionalFixedStoragePartition( \
__VA_ARGS__, \
base::optional_ref<StoragePartitionConfig> storage_partition_config)
#include <content/public/browser/site_instance.h> // IWYU pragma: export
#undef CreateForURL
#endif // BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_SITE_INSTANCE_H_
@@ -1,12 +0,0 @@
diff --git a/chrome/browser/tab_contents/tab_util.cc b/chrome/browser/tab_contents/tab_util.cc
index 4ccf096e97dafd9add0bb46c01397f294d4a6b84..209abdfe6cda1a01eb425a27af3a2aaf375918d7 100644
--- a/chrome/browser/tab_contents/tab_util.cc
+++ b/chrome/browser/tab_contents/tab_util.cc
@@ -48,6 +48,7 @@ scoped_refptr<SiteInstance> GetSiteInstanceForNewTab(Profile* profile,
if (!SiteInstance::ShouldAssignSiteForURL(url))
return nullptr;
+ BRAVE_GET_SITE_INSTANCE_FOR_NEW_TAB
return SiteInstance::CreateForURL(profile, url);
}