Skip to content

Commit 4805dd7

Browse files
committed
Update Containers storage partition utils before session/sync changes.
1 parent e63f454 commit 4805dd7

4 files changed

Lines changed: 264 additions & 50 deletions

File tree

browser/containers/containers_browsertest.cc

Lines changed: 149 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
namespace containers {
3535

36+
constexpr char kTestContainerId[] = "test-container-id";
37+
3638
class ContainersBrowserTest : public InProcessBrowserTest {
3739
public:
3840
ContainersBrowserTest() : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
@@ -48,6 +50,11 @@ class ContainersBrowserTest : public InProcessBrowserTest {
4850

4951
~ContainersBrowserTest() override = default;
5052

53+
void SetUp() override {
54+
set_open_about_blank_on_browser_launch(false);
55+
InProcessBrowserTest::SetUp();
56+
}
57+
5158
void SetUpCommandLine(base::CommandLine* command_line) override {
5259
InProcessBrowserTest::SetUpCommandLine(command_line);
5360
command_line->AppendSwitchASCII(
@@ -332,6 +339,73 @@ IN_PROC_BROWSER_TEST_F(ContainersBrowserTest, IsolateCookiesAndStorage) {
332339
GetIndexedDBJS("test_key")));
333340
}
334341

342+
IN_PROC_BROWSER_TEST_F(ContainersBrowserTest,
343+
PRE_StoragePersistenceAcrossSessions) {
344+
const GURL url("https://a.test/simple.html");
345+
346+
// Navigate to the page
347+
NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK);
348+
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
349+
params.storage_partition_config = content::StoragePartitionConfig::Create(
350+
browser()->profile(), kContainersStoragePartitionDomain, kTestContainerId,
351+
browser()->profile()->IsOffTheRecord());
352+
ui_test_utils::NavigateToURL(&params);
353+
354+
content::WebContents* web_contents =
355+
browser()->tab_strip_model()->GetActiveWebContents();
356+
ASSERT_TRUE(web_contents);
357+
358+
// Set persistent storage data
359+
EXPECT_TRUE(content::ExecJs(
360+
web_contents, SetCookieJS("persistent_cookie", "persistent_value")));
361+
EXPECT_TRUE(content::ExecJs(
362+
web_contents, SetLocalStorageJS("persistent_key", "persistent_value")));
363+
364+
EXPECT_TRUE(content::ExecJs(
365+
web_contents, SetIndexedDBJS("persistent_key", "persistent_value")));
366+
367+
// Verify data is set
368+
content::EvalJsResult cookie_result =
369+
content::EvalJs(web_contents, GetCookiesJS());
370+
EXPECT_TRUE(cookie_result.ExtractString().find(
371+
"persistent_cookie=persistent_value") != std::string::npos);
372+
373+
EXPECT_EQ("persistent_value",
374+
content::EvalJs(web_contents, GetLocalStorageJS("persistent_key")));
375+
EXPECT_EQ("persistent_value",
376+
content::EvalJs(web_contents, GetIndexedDBJS("persistent_key")));
377+
}
378+
379+
IN_PROC_BROWSER_TEST_F(ContainersBrowserTest,
380+
StoragePersistenceAcrossSessions) {
381+
const GURL url("https://a.test/simple.html");
382+
383+
// Navigate to the page
384+
NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK);
385+
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
386+
params.storage_partition_config = content::StoragePartitionConfig::Create(
387+
browser()->profile(), kContainersStoragePartitionDomain, kTestContainerId,
388+
browser()->profile()->IsOffTheRecord());
389+
ui_test_utils::NavigateToURL(&params);
390+
391+
content::WebContents* web_contents_reloaded =
392+
browser()->tab_strip_model()->GetActiveWebContents();
393+
ASSERT_TRUE(web_contents_reloaded);
394+
395+
// Verify persistent data is still available after reload
396+
content::EvalJsResult cookie_result_reloaded =
397+
content::EvalJs(web_contents_reloaded, GetCookiesJS());
398+
EXPECT_TRUE(cookie_result_reloaded.ExtractString().find(
399+
"persistent_cookie=persistent_value") != std::string::npos);
400+
401+
EXPECT_EQ("persistent_value",
402+
content::EvalJs(web_contents_reloaded,
403+
GetLocalStorageJS("persistent_key")));
404+
EXPECT_EQ(
405+
"persistent_value",
406+
content::EvalJs(web_contents_reloaded, GetIndexedDBJS("persistent_key")));
407+
}
408+
335409
IN_PROC_BROWSER_TEST_F(ContainersBrowserTest,
336410
LinkNavigationInheritsContainerStoragePartition) {
337411
const GURL url("https://a.test/simple.html");
@@ -340,8 +414,8 @@ IN_PROC_BROWSER_TEST_F(ContainersBrowserTest,
340414
NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK);
341415
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
342416
params.storage_partition_config = content::StoragePartitionConfig::Create(
343-
browser()->profile(), kContainersStoragePartitionDomain,
344-
"container-for-links", browser()->profile()->IsOffTheRecord());
417+
browser()->profile(), kContainersStoragePartitionDomain, kTestContainerId,
418+
browser()->profile()->IsOffTheRecord());
345419
ui_test_utils::NavigateToURL(&params);
346420

347421
content::WebContents* container_web_contents =
@@ -385,6 +459,13 @@ IN_PROC_BROWSER_TEST_F(ContainersBrowserTest,
385459
// Verify the new tab is on the correct URL
386460
EXPECT_EQ(url, new_tab_contents->GetLastCommittedURL());
387461

462+
content::StoragePartition* storage_partition =
463+
new_tab_contents->GetPrimaryMainFrame()->GetStoragePartition();
464+
ASSERT_TRUE(storage_partition);
465+
EXPECT_EQ(kContainersStoragePartitionDomain,
466+
storage_partition->GetConfig().partition_domain());
467+
EXPECT_EQ(kTestContainerId, storage_partition->GetConfig().partition_name());
468+
388469
// Verify the new tab has access to the same container storage partition
389470
content::EvalJsResult cookie_result =
390471
content::EvalJs(new_tab_contents, GetCookiesJS());
@@ -899,4 +980,70 @@ IN_PROC_BROWSER_TEST_F(ContainersBrowserTest, ShouldShowTabAccent) {
899980
EXPECT_FALSE(tab_in_container->ShouldShowLargeAccentIcon());
900981
}
901982

983+
IN_PROC_BROWSER_TEST_F(ContainersBrowserTest,
984+
PRE_ServiceWorkerPersistenceAcrossSessions) {
985+
const GURL url("https://a.test/containers/container_test.html");
986+
const GURL worker_url("https://a.test/containers/container_worker.js");
987+
const std::string scope = "https://a.test/containers/";
988+
989+
// Navigate to the page with a container
990+
NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK);
991+
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
992+
params.storage_partition_config = content::StoragePartitionConfig::Create(
993+
browser()->profile(), kContainersStoragePartitionDomain, kTestContainerId,
994+
browser()->profile()->IsOffTheRecord());
995+
ui_test_utils::NavigateToURL(&params);
996+
997+
content::WebContents* web_contents =
998+
browser()->tab_strip_model()->GetActiveWebContents();
999+
ASSERT_TRUE(web_contents);
1000+
1001+
// Register service worker
1002+
EXPECT_TRUE(content::ExecJs(
1003+
web_contents, RegisterServiceWorkerJS(worker_url.spec(), scope)));
1004+
1005+
// Verify service worker is registered
1006+
EXPECT_EQ(
1007+
"registered",
1008+
content::EvalJs(web_contents, CheckServiceWorkerRegisteredJS(scope)));
1009+
1010+
// Set some persistent storage data that the service worker might use
1011+
EXPECT_TRUE(content::ExecJs(
1012+
web_contents, SetLocalStorageJS("sw_data", "persistent_value")));
1013+
EXPECT_TRUE(content::ExecJs(web_contents,
1014+
SetCookieJS("sw_cookie", "persistent_cookie")));
1015+
}
1016+
1017+
IN_PROC_BROWSER_TEST_F(ContainersBrowserTest,
1018+
ServiceWorkerPersistenceAcrossSessions) {
1019+
const GURL url("https://a.test/containers/container_test.html");
1020+
const std::string scope = "https://a.test/containers/";
1021+
1022+
// Navigate to the page with the same container
1023+
NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK);
1024+
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
1025+
params.storage_partition_config = content::StoragePartitionConfig::Create(
1026+
browser()->profile(), kContainersStoragePartitionDomain, kTestContainerId,
1027+
browser()->profile()->IsOffTheRecord());
1028+
ui_test_utils::NavigateToURL(&params);
1029+
1030+
content::WebContents* web_contents_reloaded =
1031+
browser()->tab_strip_model()->GetActiveWebContents();
1032+
ASSERT_TRUE(web_contents_reloaded);
1033+
1034+
// Verify service worker is still registered after browser restart
1035+
EXPECT_EQ("registered",
1036+
content::EvalJs(web_contents_reloaded,
1037+
CheckServiceWorkerRegisteredJS(scope)));
1038+
1039+
// Verify persistent storage data is still available
1040+
EXPECT_EQ("persistent_value", content::EvalJs(web_contents_reloaded,
1041+
GetLocalStorageJS("sw_data")));
1042+
1043+
content::EvalJsResult cookie_result =
1044+
content::EvalJs(web_contents_reloaded, GetCookiesJS());
1045+
EXPECT_TRUE(cookie_result.ExtractString().find(
1046+
"sw_cookie=persistent_cookie") != std::string::npos);
1047+
}
1048+
9021049
} // namespace containers

components/containers/content/browser/storage_partition_utils.cc

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
#include "brave/components/containers/content/browser/storage_partition_utils.h"
77

8+
#include <algorithm>
9+
10+
#include "base/strings/string_util.h"
811
#include "brave/components/containers/core/common/features.h"
912
#include "content/public/browser/site_instance.h"
1013
#include "content/public/browser/storage_partition_config.h"
@@ -15,9 +18,23 @@ namespace containers {
1518
bool IsContainersStoragePartition(
1619
const content::StoragePartitionConfig& partition_config) {
1720
CHECK(base::FeatureList::IsEnabled(features::kContainers));
18-
return partition_config.partition_domain() ==
19-
kContainersStoragePartitionDomain &&
20-
!partition_config.partition_name().empty();
21+
return IsContainersStoragePartitionKey(partition_config.partition_domain(),
22+
partition_config.partition_name());
23+
}
24+
25+
bool IsContainersStoragePartitionKey(std::string_view partition_domain,
26+
std::string_view partition_name) {
27+
CHECK(base::FeatureList::IsEnabled(features::kContainers));
28+
return partition_domain == kContainersStoragePartitionDomain &&
29+
IsValidStoragePartitionKeyComponent(partition_name);
30+
}
31+
32+
bool IsValidStoragePartitionKeyComponent(std::string_view component) {
33+
CHECK(base::FeatureList::IsEnabled(features::kContainers));
34+
return !component.empty() &&
35+
std::ranges::all_of(component, [](const char& c) {
36+
return base::IsAsciiAlphaNumeric(c) || c == '-';
37+
});
2138
}
2239

2340
std::optional<content::StoragePartitionConfig> MaybeInheritStoragePartition(
@@ -33,6 +50,7 @@ std::optional<content::StoragePartitionConfig> MaybeInheritStoragePartition(
3350
}
3451

3552
std::string GetContainerIdForWebContents(content::WebContents* web_contents) {
53+
CHECK(base::FeatureList::IsEnabled(features::kContainers));
3654
if (!web_contents) {
3755
return std::string();
3856
}

components/containers/content/browser/storage_partition_utils.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ class WebContents;
2020
namespace containers {
2121

2222
// The partition domain identifier used for all containers storage partitions.
23-
inline constexpr char kContainersStoragePartitionDomain[] =
24-
"containers-default";
23+
inline constexpr char kContainersStoragePartitionDomain[] = "containers";
2524

2625
// Checks whether a given StoragePartitionConfig belongs to Containers.
27-
// Partition domain should match kContainersStoragePartitionDomain and partition
28-
// name should be non-empty.
2926
COMPONENT_EXPORT(CONTAINERS_CONTENT_BROWSER)
3027
bool IsContainersStoragePartition(
3128
const content::StoragePartitionConfig& partition_config);
3229

30+
// Checks whether a given StoragePartitionConfig partition domain and name
31+
// belongs to Containers.
32+
COMPONENT_EXPORT(CONTAINERS_CONTENT_BROWSER)
33+
bool IsContainersStoragePartitionKey(std::string_view partition_domain,
34+
std::string_view partition_name);
35+
36+
// Checks whether a storage partition key component is not empty and contains
37+
// only valid characters for Containers storage partitions. Valid characters are
38+
// ASCII alphanumeric characters and '-'.
39+
COMPONENT_EXPORT(CONTAINERS_CONTENT_BROWSER)
40+
bool IsValidStoragePartitionKeyComponent(std::string_view component);
41+
3342
// Returns the StoragePartitionConfig if it is a Containers storage partition,
3443
// otherwise returns std::nullopt. Used to conditionally inherit
3544
// StoragePartitionConfig when creating a new SiteInstance.

0 commit comments

Comments
 (0)