Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore cleanup cookies at startup for command line incognito mode #27111

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

#include "brave/components/brave_shields/content/browser/brave_shields_util.h"
#include "chrome/browser/browsing_data/chrome_browsing_data_remover_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browsing_data_filter_builder.h"
Expand Down Expand Up @@ -111,4 +115,15 @@ void BraveEphemeralStorageServiceDelegate::CleanupFirstPartyStorageArea(
origin_type, std::move(filter_builder));
}

bool BraveEphemeralStorageServiceDelegate::DoesProfileHaveAnyBrowserWindow()
const {
Profile* profile = Profile::FromBrowserContext(context_);
for (Browser* browser : chrome::FindAllBrowsersWithProfile(profile)) {
if (browser->window()) {
return true;
}
}
return false;
}

} // namespace ephemeral_storage
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class BraveEphemeralStorageServiceDelegate
void CleanupTLDEphemeralArea(const TLDEphemeralAreaKey& key) override;
void CleanupFirstPartyStorageArea(
const std::string& registerable_domain) override;
bool DoesProfileHaveAnyBrowserWindow() const override;

private:
raw_ptr<content::BrowserContext> context_ = nullptr;
Expand Down
12 changes: 6 additions & 6 deletions browser/ephemeral_storage/ephemeral_storage_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,21 @@ content::EvalJsResult EphemeralStorageBrowserTest::GetCookiesInFrame(
return content::EvalJs(host, "document.cookie");
}

size_t EphemeralStorageBrowserTest::WaitForCleanupAfterKeepAlive(Browser* b) {
if (!b) {
b = browser();
size_t EphemeralStorageBrowserTest::WaitForCleanupAfterKeepAlive(
Profile* profile) {
if (!profile) {
profile = browser()->profile();
}
const size_t fired_cnt = EphemeralStorageServiceFactory::GetInstance()
->GetForContext(b->profile())
->GetForContext(profile)
->FireCleanupTimersForTesting();

// NetworkService closes existing connections when a data removal action
// linked to these connections is performed. This leads to rare page open
// failures when the timing is "just right". Do a no-op removal here to make
// sure the queued Ephemeral Storage cleanup was complete.
BrowsingDataRemoverObserver data_remover_observer;
content::BrowsingDataRemover* remover =
b->profile()->GetBrowsingDataRemover();
content::BrowsingDataRemover* remover = profile->GetBrowsingDataRemover();
remover->AddObserver(&data_remover_observer);
remover->RemoveAndReply(base::Time(), base::Time::Max(), 0, 0,
&data_remover_observer);
Expand Down
2 changes: 1 addition & 1 deletion browser/ephemeral_storage/ephemeral_storage_browsertest.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class EphemeralStorageBrowserTest : public InProcessBrowserTest {
StorageType storage_type);
void SetCookieInFrame(content::RenderFrameHost* host, std::string cookie);
content::EvalJsResult GetCookiesInFrame(content::RenderFrameHost* host);
size_t WaitForCleanupAfterKeepAlive(Browser* browser = nullptr);
size_t WaitForCleanupAfterKeepAlive(Profile* profile = nullptr);
void ExpectValuesFromFramesAreEmpty(const base::Location& location,
const ValuesFromFrames& values);
void ExpectValuesFromFrameAreEmpty(const base::Location& location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/browser/ephemeral_storage/ephemeral_storage_browsertest.h"

#include "brave/components/brave_shields/content/browser/brave_shields_util.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
Expand Down Expand Up @@ -206,7 +206,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultBrowserTest,
// After keepalive values should be cleared.
ASSERT_TRUE(ui_test_utils::NavigateToURL(incognito_browser,
b_site_ephemeral_storage_url_));
WaitForCleanupAfterKeepAlive(incognito_browser);
WaitForCleanupAfterKeepAlive(incognito_browser->profile());
ASSERT_TRUE(ui_test_utils::NavigateToURL(incognito_browser,
a_site_ephemeral_storage_url_));

Expand Down Expand Up @@ -570,4 +570,57 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultDisabledBrowserTest,
EXPECT_EQ(1u, GetAllCookies().size());
}

class EphemeralStorageForgetByDefaultIncognitoBrowserTest
: public EphemeralStorageForgetByDefaultBrowserTest {
public:
EphemeralStorageForgetByDefaultIncognitoBrowserTest() {
scoped_feature_list_.InitAndEnableFeature(
net::features::kBraveForgetFirstPartyStorage);
}
~EphemeralStorageForgetByDefaultIncognitoBrowserTest() override = default;

void SetUpCommandLine(base::CommandLine* command_line) override {
EphemeralStorageForgetByDefaultBrowserTest::SetUpCommandLine(command_line);
if (IsPreTestToEnableIncognito()) {
command_line->AppendSwitch(switches::kIncognito);
}
}

static bool IsPreTestToEnableIncognito() {
const testing::TestInfo* const test_info =
testing::UnitTest::GetInstance()->current_test_info();
return base::StartsWith(test_info->name(), "PRE_DontForgetFirstParty");
}

private:
base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultIncognitoBrowserTest,
PRE_PRE_DontForgetFirstPartyIfNoBrowserWindowIsActive) {
const GURL a_site_set_cookie_url(
"https://a.com/set-cookie?name=acom;path=/"
";SameSite=None;Secure;Max-Age=600");
brave_shields::SetForgetFirstPartyStorageEnabled(content_settings(), true,
a_site_set_cookie_url);

// Cookies should NOT exist for a.com.
EXPECT_EQ(0u, GetAllCookies().size());

EXPECT_TRUE(LoadURLInNewTab(a_site_set_cookie_url));

// Cookies SHOULD exist for a.com.
EXPECT_EQ(1u, GetAllCookies().size());
}

IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultIncognitoBrowserTest,
PRE_DontForgetFirstPartyIfNoBrowserWindowIsActive) {
EXPECT_TRUE(browser()->profile()->IsOffTheRecord());
WaitForCleanupAfterKeepAlive(browser()->profile()->GetOriginalProfile());
}

IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultIncognitoBrowserTest,
DontForgetFirstPartyIfNoBrowserWindowIsActive) {
EXPECT_EQ(1u, GetAllCookies().size());
}
} // namespace ephemeral_storage
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,16 @@ TEST_F(EphemeralStorageServiceForgetFirstPartyTest, CleanupOnRestart) {
1u);
}

// Cleanup should happen in 5 seconds after the startup.
// No cleanup should happen at the restart when the profile has no browser
// window associated with it.
{
ScopedVerifyAndClearExpectations verify(mock_delegate_);
ScopedVerifyAndClearExpectations verify_observer(&mock_observer_);
EXPECT_CALL(*mock_delegate_,
CleanupFirstPartyStorageArea(ephemeral_domain));
task_environment_.FastForwardBy(base::Seconds(5));
EXPECT_EQ(
profile_.GetPrefs()->GetList(kFirstPartyStorageOriginsToCleanup).size(),
0u);
1u);
EXPECT_EQ(mock_delegate_->DoesProfileHaveAnyBrowserWindow(), false);
}
}

Expand Down
1 change: 1 addition & 0 deletions components/ephemeral_storage/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ static_library("ephemeral_storage") {
"ephemeral_storage_pref_names.h",
"ephemeral_storage_service.cc",
"ephemeral_storage_service.h",
"ephemeral_storage_service_delegate.cc",
"ephemeral_storage_service_delegate.h",
"ephemeral_storage_service_observer.h",
"ephemeral_storage_types.h",
Expand Down
5 changes: 5 additions & 0 deletions components/ephemeral_storage/ephemeral_storage_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ void EphemeralStorageService::ScheduleFirstPartyStorageAreasCleanupOnStartup() {

void EphemeralStorageService::CleanupFirstPartyStorageAreasOnStartup() {
DCHECK(!context_->IsOffTheRecord());

if (!delegate_->DoesProfileHaveAnyBrowserWindow()) {
first_party_storage_areas_to_cleanup_on_startup_.clear();
Copy link
Member

@goodov goodov Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.. this will prevent the cleanup when a normal window is opened from the incognito session (for ex. when you press Ctrl+N in the incognito window).

I think the right thing is to schedule the cleanup only after the window has appeared. I went with this approach here: #27176

I hope you don't mind that I went with a separate PR as it will speedup things a bit and we finally fix this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goodov Your approach better solves the problem. Thanks for looking into it!

return;
}
ScopedListPrefUpdate pref_update(prefs_, kFirstPartyStorageOriginsToCleanup);
for (const auto& url_to_cleanup :
first_party_storage_areas_to_cleanup_on_startup_) {
Expand Down
14 changes: 14 additions & 0 deletions components/ephemeral_storage/ephemeral_storage_service_delegate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Copyright (c) 2024 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 "brave/components/ephemeral_storage/ephemeral_storage_service_delegate.h"

namespace ephemeral_storage {

bool EphemeralStorageServiceDelegate::DoesProfileHaveAnyBrowserWindow() const {
return false;
}

} // namespace ephemeral_storage
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class EphemeralStorageServiceDelegate {
// Cleanups non-ephemeral first party storage areas (cache, dom storage).
virtual void CleanupFirstPartyStorageArea(
const std::string& registerable_domain) {}
virtual bool DoesProfileHaveAnyBrowserWindow() const;
};

} // namespace ephemeral_storage
Expand Down