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

enable ephemeral storage keep alive by default #8526

Merged
merged 15 commits into from
Apr 22, 2021
Merged

enable ephemeral storage keep alive by default #8526

merged 15 commits into from
Apr 22, 2021

Conversation

bridiver
Copy link
Collaborator

@bridiver bridiver commented Apr 13, 2021

Resolves brave/brave-browser#15415

Submitter Checklist:

  • I confirm that no security/privacy review is needed, or that I have requested one
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally: npm run test -- brave_browser_tests, npm run test -- brave_unit_tests, npm run lint, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

@bridiver bridiver marked this pull request as ready for review April 21, 2021 01:35
@bridiver bridiver requested a review from a team as a code owner April 21, 2021 01:35
@bridiver bridiver requested review from goodov, pes10k and iefremov April 21, 2021 01:35
@bridiver bridiver self-assigned this Apr 21, 2021
@bridiver bridiver changed the title clear keepalive references on tab close enable ephemeral storage keep alive by default Apr 21, 2021
@@ -85,6 +92,33 @@ void EphemeralStorageTabHelper::ReadyToCommitNavigation(
CreateEphemeralStorageAreasForDomainAndURL(new_domain, new_url);
}

void EphemeralStorageTabHelper::ClearEphemeralLifetimeKeepalive(
const content::TLDEphemeralLifetimeKey& key) {
ClearLocalStorageKeepAlive(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this would be simpler if TLDEphemeralLifetime owned local_storage_namespace_, but I'd rather do that refactoring in a follow-up

@@ -33,7 +33,8 @@ namespace {
constexpr char kSessionStorageSuffix[] = "/ephemeral-session-storage";
constexpr char kLocalStorageSuffix[] = "/ephemeral-local-storage";

const base::TimeDelta kStorageKeepAliveDelay = base::TimeDelta::FromSeconds(30);
const base::TimeDelta kStorageKeepAliveDelay = base::TimeDelta::FromSeconds(
Copy link
Member

Choose a reason for hiding this comment

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

this way kStorageKeepAliveDelay cannot be controlled via Griffin or --enable-features, because the value is set before FieldTrials initialization.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah, right, I need to lazy initialize it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

actually I don't really need this const at all

@@ -100,6 +105,10 @@ class EphemeralStorageBrowserTest : public InProcessBrowserTest {
https_server_.GetURL("b.com", "/ephemeral_storage.html");
c_site_ephemeral_storage_url_ =
https_server_.GetURL("c.com", "/ephemeral_storage.html");

ephemeral_storage::EphemeralStorageTabHelper::
SetKeepAliveTimeDelayForTesting(
Copy link
Member

Choose a reason for hiding this comment

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

why not ScopedFeatureList::InitAndEnableFeatureWithParameters?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

is that still ok to use when the feature is enabled by default? If so I'll remove SetKeepAliveTimeDelayForTesting

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess it must be ok because it still has InitAndEnable for kBraveEphemeralStorage

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

actually this seems to complicate running in both the enabled and disabled state so I'm going to leave it as-is

Copy link
Member

Choose a reason for hiding this comment

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

what about creating ScopedEphemeralStorageKeepAliveTimeDelayForTesting? it can be used as a member in test classes:

class Test : ... {
...
 private:
  // Maybe the delay can be set to 2, but can be overriden so both variants can work:
  ScopedEphemeralStorageKeepAliveTimeDelayForTesting
      scoped_ephemeral_storage_keep_alive_;
  ScopedEphemeralStorageKeepAliveTimeDelayForTesting
      scoped_ephemeral_storage_keep_alive_manual_{2};
};

Bonus: it can reset the value to the default state at test shutdown.

Also you can create a utility function for tests which will run a RunLoop with PostDelayedTask using the time delay set by the scoped object. The function can be a member of the scoped object.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that would still require a method on EphemeralStorageTabHelper to set the actual delay so not sure that's worth the extra effort since we always want the short delay in testing

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

going with this instead #8526 (comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm going to look at this in a follow-up so I can get this merged

// after keepalive values should be cleared
ui_test_utils::NavigateToURL(browser(), b_site_ephemeral_storage_url_);
ASSERT_TRUE(WaitForLoadStop(web_contents));
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(kKeepAliveInterval));
Copy link
Member

Choose a reason for hiding this comment

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

this way you hang the whole process. consider this instead:

base::RunLoop run_loop;
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
    FROM_HERE, run_loop.QuitClosure(),
    base::TimeDelta::FromSeconds(kKeepAliveInterval));
run_loop.Run();

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good call, I was digging around trying to remember how to wait and saw other tests using base::PlatformThread::Sleep so I thought I had misremembered that there was a better way :)

Copy link
Contributor

Choose a reason for hiding this comment

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

but why not ScopedMockTimeMessageLoopTaskRunner ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wasn't aware of its existence, but looks like a good option because it doesn't require waiting at all and also eliminates a potential race condition if the tests are very slow

Copy link
Collaborator

@mkarolin mkarolin left a comment

Choose a reason for hiding this comment

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

chromium_src ++

@bridiver bridiver force-pushed the cf-crash-2 branch 4 times, most recently from b5c2e09 to 5f7459e Compare April 21, 2021 20:10
@@ -273,8 +277,9 @@ class PermissionLifetimeManagerWithOriginMonitorBrowserTest
: public PermissionLifetimeManagerBrowserTest {
public:
PermissionLifetimeManagerWithOriginMonitorBrowserTest() {
scoped_feature_list_.InitAndEnableFeature(
Copy link
Member

Choose a reason for hiding this comment

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

scoped_feature_list_ in this class can be removed.

@@ -53,6 +53,8 @@ class CONTENT_EXPORT TLDEphemeralLifetime
// Add a callback to a callback list to be called on destruction.
void RegisterOnDestroyCallback(OnDestroyCallback callback);

const TLDEphemeralLifetimeKey& key() { return key_; }
Copy link
Member

Choose a reason for hiding this comment

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

const

@@ -100,6 +105,10 @@ class EphemeralStorageBrowserTest : public InProcessBrowserTest {
https_server_.GetURL("b.com", "/ephemeral_storage.html");
c_site_ephemeral_storage_url_ =
https_server_.GetURL("c.com", "/ephemeral_storage.html");

ephemeral_storage::EphemeralStorageTabHelper::
SetKeepAliveTimeDelayForTesting(
Copy link
Member

Choose a reason for hiding this comment

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

what about creating ScopedEphemeralStorageKeepAliveTimeDelayForTesting? it can be used as a member in test classes:

class Test : ... {
...
 private:
  // Maybe the delay can be set to 2, but can be overriden so both variants can work:
  ScopedEphemeralStorageKeepAliveTimeDelayForTesting
      scoped_ephemeral_storage_keep_alive_;
  ScopedEphemeralStorageKeepAliveTimeDelayForTesting
      scoped_ephemeral_storage_keep_alive_manual_{2};
};

Bonus: it can reset the value to the default state at test shutdown.

Also you can create a utility function for tests which will run a RunLoop with PostDelayedTask using the time delay set by the scoped object. The function can be a member of the scoped object.

@iefremov
Copy link
Contributor

would be nice to have a problem description somewhere just for better understanding goals of the change


// Navigate away and then navigate back to the original site.
ui_test_utils::NavigateToURL(browser(), b_site_ephemeral_storage_url_);
ASSERT_TRUE(WaitForLoadStop(web_contents));
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need these, ui_test_utils::NavigateToURL blocks until loading stops on its own

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yea, I added this because I was seeing strange behavior and then forgot to remove it

ClearLocalStorageKeepAlive(
StringToSessionStorageId(key.second, kLocalStorageSuffix));

for (auto it = keep_alive_tld_ephemeral_lifetime_list_.begin();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather do base::ranges::find() and then erase: this is 2 times shorter and also safer, because the current loop looks as a common pitfall (without immediate return). If return is occasionally refactored/removed, we could be in trouble

@pes10k
Copy link
Contributor

pes10k commented Apr 22, 2021

@iefremov this issue has a fuller description of why the "keep alive" functionality is needed. brave/brave-browser#14943

@bridiver
Copy link
Collaborator Author

test failures are unrelated known crashes for rewards

@bridiver bridiver merged commit e446573 into master Apr 22, 2021
@bridiver bridiver deleted the cf-crash-2 branch April 22, 2021 22:23
@bridiver bridiver added this to the 1.25.x - Nightly milestone Apr 22, 2021

void EphemeralStorageTabHelper::ClearLocalStorageKeepAlive(
const std::string& id) {
auto it = base::ranges::find_if(
Copy link
Contributor

Choose a reason for hiding this comment

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

in theory this is

base::ranges::find(method_data, id, &content::SessionStorageNamespace::id)

but not sure how well it works with scoped_refptrs

bridiver added a commit that referenced this pull request May 20, 2021
enable ephemeral storage keep alive by default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

enable ephemeral storage keepalive by default
5 participants