Commit 724022dd authored by Sebastien Marchand's avatar Sebastien Marchand Committed by Commit Bot

SessionRestore: Prioritize tabs that can use background notifications

Change-Id: I1e58983e062d03f2c8532d6b7ed282509a625371
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1943349
Commit-Queue: Sébastien Marchand <sebmarchand@chromium.org>
Reviewed-by: default avatarChris Hamilton <chrisha@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720751}
parent a5bfe842
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
#include "chrome/browser/permissions/permission_manager.h"
#include "chrome/browser/permissions/permission_result.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_store_factory.h" #include "chrome/browser/resource_coordinator/local_site_characteristics_data_store_factory.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/browser_list.h"
...@@ -117,6 +119,7 @@ class TabDataAccess { ...@@ -117,6 +119,7 @@ class TabDataAccess {
// Set the |TabData::used_in_bg| bit based on the data provided by |reader|. // Set the |TabData::used_in_bg| bit based on the data provided by |reader|.
static void SetUsedInBgFromSiteCharacteristicsDataReader( static void SetUsedInBgFromSiteCharacteristicsDataReader(
TabData* tab_data, TabData* tab_data,
content::WebContents* contents,
std::unique_ptr<SiteCharacteristicsDataReader> reader); std::unique_ptr<SiteCharacteristicsDataReader> reader);
// Callback that is invoked when the SiteCharacteristicsDataReader associated // Callback that is invoked when the SiteCharacteristicsDataReader associated
...@@ -157,7 +160,8 @@ void TabDataAccess::SetUsedInBgFromSiteCharacteristicsDB( ...@@ -157,7 +160,8 @@ void TabDataAccess::SetUsedInBgFromSiteCharacteristicsDB(
url::Origin::Create(contents->GetLastCommittedURL())); url::Origin::Create(contents->GetLastCommittedURL()));
if (reader->DataLoaded()) { if (reader->DataLoaded()) {
SetUsedInBgFromSiteCharacteristicsDataReader(tab_data, std::move(reader)); SetUsedInBgFromSiteCharacteristicsDataReader(tab_data, contents,
std::move(reader));
DCHECK(tab_data->used_in_bg.has_value()); DCHECK(tab_data->used_in_bg.has_value());
if (tab_data->used_in_bg) if (tab_data->used_in_bg)
++policy->tabs_used_in_bg_; ++policy->tabs_used_in_bg_;
...@@ -173,6 +177,7 @@ void TabDataAccess::SetUsedInBgFromSiteCharacteristicsDB( ...@@ -173,6 +177,7 @@ void TabDataAccess::SetUsedInBgFromSiteCharacteristicsDB(
void TabDataAccess::SetUsedInBgFromSiteCharacteristicsDataReader( void TabDataAccess::SetUsedInBgFromSiteCharacteristicsDataReader(
TabData* tab_data, TabData* tab_data,
content::WebContents* contents,
std::unique_ptr<SiteCharacteristicsDataReader> reader) { std::unique_ptr<SiteCharacteristicsDataReader> reader) {
static const performance_manager::SiteFeatureUsage kInUse = static const performance_manager::SiteFeatureUsage kInUse =
performance_manager::SiteFeatureUsage::kSiteFeatureInUse; performance_manager::SiteFeatureUsage::kSiteFeatureInUse;
...@@ -190,8 +195,14 @@ void TabDataAccess::SetUsedInBgFromSiteCharacteristicsDataReader( ...@@ -190,8 +195,14 @@ void TabDataAccess::SetUsedInBgFromSiteCharacteristicsDataReader(
// TODO(sebmarchand): Consider that the tabs that are still under observation // TODO(sebmarchand): Consider that the tabs that are still under observation
// could be used in background. // could be used in background.
// TODO(sebmarchand): Instead of checking if the tab has used notifications in auto notif_permission =
// the past check if it has the permission to use this feature. PermissionManager::Get(
Profile::FromBrowserContext(contents->GetBrowserContext()))
->GetPermissionStatus(ContentSettingsType::NOTIFICATIONS,
contents->GetLastCommittedURL(),
contents->GetLastCommittedURL());
if (notif_permission.content_setting == CONTENT_SETTING_ALLOW)
used_in_bg = true;
// Persist this data and detach from the reader. We need to detach from the // Persist this data and detach from the reader. We need to detach from the
// reader in a separate task because this callback is actually being invoked // reader in a separate task because this callback is actually being invoked
...@@ -214,7 +225,8 @@ void TabDataAccess::OnSiteDataLoaded( ...@@ -214,7 +225,8 @@ void TabDataAccess::OnSiteDataLoaded(
DCHECK(it != policy->tab_data_.end()); DCHECK(it != policy->tab_data_.end());
auto* tab_data = it->second.get(); auto* tab_data = it->second.get();
SetUsedInBgFromSiteCharacteristicsDataReader(tab_data, std::move(reader)); SetUsedInBgFromSiteCharacteristicsDataReader(tab_data, contents,
std::move(reader));
// Score the tab and notify observers if the score has changed. // Score the tab and notify observers if the score has changed.
if (policy->RescoreTabAfterDataLoaded(contents, tab_data)) if (policy->RescoreTabAfterDataLoaded(contents, tab_data))
......
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h" #include "base/test/simple_test_tick_clock.h"
#include "chrome/browser/notifications/notification_permission_context.h"
#include "chrome/browser/permissions/permission_request_manager.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_unittest_utils.h" #include "chrome/browser/resource_coordinator/local_site_characteristics_data_unittest_utils.h"
#include "chrome/browser/resource_coordinator/tab_manager_features.h" #include "chrome/browser/resource_coordinator/tab_manager_features.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h" #include "chrome/test/base/chrome_render_view_host_test_harness.h"
...@@ -388,6 +390,40 @@ TEST_F(SessionRestorePolicyTest, ShouldLoadBackgroundData) { ...@@ -388,6 +390,40 @@ TEST_F(SessionRestorePolicyTest, ShouldLoadBackgroundData) {
EXPECT_TRUE(policy_->ShouldLoad(contents1_.get())); EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
} }
TEST_F(SessionRestorePolicyTest, NotificationPermissionSetUsedInBgBit) {
CreatePolicy(true);
WaitForFinalTabScores();
auto iter = policy_->tab_data_.find(contents1_.get());
EXPECT_TRUE(iter != policy_->tab_data_.end());
EXPECT_FALSE(iter->second->UsedInBg());
// Allow |contents1_| to display notifications, this should cause the
// |used_in_bg| bit to change to true.
const GURL kTestURL("https://foo.com/");
NotificationPermissionContext::UpdatePermission(profile(), kTestURL,
CONTENT_SETTING_ALLOW);
content::WebContentsTester::For(contents1_.get())
->SetLastCommittedURL(kTestURL);
// Adding/Removing the tab for scoring will cause the callback to be called a
// few times, ignore this.
EXPECT_CALL(mock_, NotifyTabScoreChanged(::testing::_, ::testing::_))
.Times(::testing::AnyNumber());
policy_->RemoveTabForScoring(contents1_.get());
policy_->AddTabForScoring(contents1_.get());
WaitForFinalTabScores();
iter = policy_->tab_data_.find(contents1_.get());
EXPECT_TRUE(iter != policy_->tab_data_.end());
EXPECT_TRUE(iter->second->UsedInBg());
NotificationPermissionContext::UpdatePermission(profile(), kTestURL,
CONTENT_SETTING_DEFAULT);
}
TEST_F(SessionRestorePolicyTest, MultipleAllTabsDoneCallbacks) { TEST_F(SessionRestorePolicyTest, MultipleAllTabsDoneCallbacks) {
CreatePolicy(true); CreatePolicy(true);
WaitForFinalTabScores(); WaitForFinalTabScores();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment