Commit 5b73d37f authored by Sophie Chang's avatar Sophie Chang Committed by Chromium LUCI CQ

Share store for incognito and regular profile for hints

Bug: 1158343,1151087
Change-Id: Icb9c055daafe808f727bc7cbd372336c37bdccf7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2601637Reviewed-by: default avatarMichael Crouse <mcrouse@chromium.org>
Commit-Queue: Sophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#839135}
parent 872c9009
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include "chrome/test/base/testing_browser_process.h" #include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h" #include "chrome/test/base/testing_profile_manager.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "components/optimization_guide/optimization_guide_prefs.h" #include "components/optimization_guide/optimization_guide_prefs.h"
#include "components/optimization_guide/optimization_guide_service.h" #include "components/optimization_guide/optimization_guide_service.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
...@@ -38,14 +37,11 @@ class MockOptimizationGuideHintsManager : public OptimizationGuideHintsManager { ...@@ -38,14 +37,11 @@ class MockOptimizationGuideHintsManager : public OptimizationGuideHintsManager {
MockOptimizationGuideHintsManager( MockOptimizationGuideHintsManager(
optimization_guide::OptimizationGuideService* optimization_guide_service, optimization_guide::OptimizationGuideService* optimization_guide_service,
Profile* profile, Profile* profile,
base::FilePath file_path,
leveldb_proto::ProtoDatabaseProvider* db_provider,
PrefService* pref_service) PrefService* pref_service)
: OptimizationGuideHintsManager(optimization_guide_service, : OptimizationGuideHintsManager(optimization_guide_service,
profile, profile,
file_path,
pref_service, pref_service,
db_provider, /*hint_store=*/nullptr,
/*top_host_provider=*/nullptr, /*top_host_provider=*/nullptr,
/*url_loader_factory=*/nullptr) {} /*url_loader_factory=*/nullptr) {}
~MockOptimizationGuideHintsManager() override = default; ~MockOptimizationGuideHintsManager() override = default;
...@@ -98,18 +94,14 @@ class OptimizationGuideBridgeTest : public testing::Test { ...@@ -98,18 +94,14 @@ class OptimizationGuideBridgeTest : public testing::Test {
optimization_guide_service_ = optimization_guide_service_ =
std::make_unique<optimization_guide::OptimizationGuideService>( std::make_unique<optimization_guide::OptimizationGuideService>(
task_environment_.GetMainThreadTaskRunner()); task_environment_.GetMainThreadTaskRunner());
db_provider_ = std::make_unique<leveldb_proto::ProtoDatabaseProvider>(
temp_dir_.GetPath());
optimization_guide_hints_manager_ = optimization_guide_hints_manager_ =
std::make_unique<MockOptimizationGuideHintsManager>( std::make_unique<MockOptimizationGuideHintsManager>(
optimization_guide_service_.get(), profile_, temp_dir_.GetPath(), optimization_guide_service_.get(), profile_, pref_service_.get());
db_provider_.get(), pref_service_.get());
} }
void TearDown() override { void TearDown() override {
optimization_guide_hints_manager_->Shutdown(); optimization_guide_hints_manager_->Shutdown();
optimization_guide_hints_manager_.reset(); optimization_guide_hints_manager_.reset();
db_provider_.reset();
optimization_guide_service_.reset(); optimization_guide_service_.reset();
} }
...@@ -133,7 +125,6 @@ class OptimizationGuideBridgeTest : public testing::Test { ...@@ -133,7 +125,6 @@ class OptimizationGuideBridgeTest : public testing::Test {
TestingProfile* profile_; TestingProfile* profile_;
std::unique_ptr<optimization_guide::OptimizationGuideService> std::unique_ptr<optimization_guide::OptimizationGuideService>
optimization_guide_service_; optimization_guide_service_;
std::unique_ptr<leveldb_proto::ProtoDatabaseProvider> db_provider_;
base::ScopedTempDir temp_dir_; base::ScopedTempDir temp_dir_;
std::unique_ptr<TestingPrefServiceSimple> pref_service_; std::unique_ptr<TestingPrefServiceSimple> pref_service_;
}; };
......
...@@ -253,9 +253,8 @@ bool ShouldIgnoreNewlyRegisteredOptimizationType( ...@@ -253,9 +253,8 @@ bool ShouldIgnoreNewlyRegisteredOptimizationType(
OptimizationGuideHintsManager::OptimizationGuideHintsManager( OptimizationGuideHintsManager::OptimizationGuideHintsManager(
optimization_guide::OptimizationGuideService* optimization_guide_service, optimization_guide::OptimizationGuideService* optimization_guide_service,
Profile* profile, Profile* profile,
const base::FilePath& profile_path,
PrefService* pref_service, PrefService* pref_service,
leveldb_proto::ProtoDatabaseProvider* database_provider, optimization_guide::OptimizationGuideStore* hint_store,
optimization_guide::TopHostProvider* top_host_provider, optimization_guide::TopHostProvider* top_host_provider,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: optimization_guide_service_(optimization_guide_service), : optimization_guide_service_(optimization_guide_service),
...@@ -264,13 +263,7 @@ OptimizationGuideHintsManager::OptimizationGuideHintsManager( ...@@ -264,13 +263,7 @@ OptimizationGuideHintsManager::OptimizationGuideHintsManager(
profile_(profile), profile_(profile),
pref_service_(pref_service), pref_service_(pref_service),
hint_cache_(std::make_unique<optimization_guide::HintCache>( hint_cache_(std::make_unique<optimization_guide::HintCache>(
optimization_guide::features::ShouldPersistHintsToDisk() hint_store,
? std::make_unique<optimization_guide::OptimizationGuideStore>(
database_provider,
profile_path.AddExtensionASCII(
optimization_guide::kOptimizationGuideHintStore),
background_task_runner_)
: nullptr,
optimization_guide::features::MaxHostKeyedHintCacheSize())), optimization_guide::features::MaxHostKeyedHintCacheSize())),
page_navigation_hints_fetchers_( page_navigation_hints_fetchers_(
optimization_guide::features::MaxConcurrentPageNavigationFetches()), optimization_guide::features::MaxConcurrentPageNavigationFetches()),
...@@ -346,7 +339,9 @@ void OptimizationGuideHintsManager::OnHintsComponentAvailable( ...@@ -346,7 +339,9 @@ void OptimizationGuideHintsManager::OnHintsComponentAvailable(
} }
std::unique_ptr<optimization_guide::StoreUpdateData> update_data = std::unique_ptr<optimization_guide::StoreUpdateData> update_data =
hint_cache_->MaybeCreateUpdateDataForComponentHints(info.version); profile_->IsOffTheRecord()
? nullptr
: hint_cache_->MaybeCreateUpdateDataForComponentHints(info.version);
// Processes the hints from the newly available component on a background // Processes the hints from the newly available component on a background
// thread, providing a StoreUpdateData for component update from the hint // thread, providing a StoreUpdateData for component update from the hint
...@@ -399,7 +394,8 @@ OptimizationGuideHintsManager::ProcessHintsComponent( ...@@ -399,7 +394,8 @@ OptimizationGuideHintsManager::ProcessHintsComponent(
// aren't hints sent down via the component, but we need to figure out // aren't hints sent down via the component, but we need to figure out
// threading since these hints are now stored in memory prior to being // threading since these hints are now stored in memory prior to being
// persisted. // persisted.
if (update_data) { // Don't store hints in the store if it's off the record.
if (update_data && !profile_->IsOffTheRecord()) {
bool did_process_hints = hint_cache_->ProcessAndCacheHints( bool did_process_hints = hint_cache_->ProcessAndCacheHints(
config->mutable_hints(), update_data.get()); config->mutable_hints(), update_data.get());
optimization_guide::RecordProcessHintsComponentResult( optimization_guide::RecordProcessHintsComponentResult(
...@@ -504,8 +500,10 @@ void OptimizationGuideHintsManager::OnHintCacheInitialized() { ...@@ -504,8 +500,10 @@ void OptimizationGuideHintsManager::OnHintCacheInitialized() {
optimization_guide::switches::ParseComponentConfigFromCommandLine(); optimization_guide::switches::ParseComponentConfigFromCommandLine();
if (manual_config) { if (manual_config) {
std::unique_ptr<optimization_guide::StoreUpdateData> update_data = std::unique_ptr<optimization_guide::StoreUpdateData> update_data =
hint_cache_->MaybeCreateUpdateDataForComponentHints( profile_->IsOffTheRecord()
base::Version(kManualConfigComponentVersion)); ? nullptr
: hint_cache_->MaybeCreateUpdateDataForComponentHints(
base::Version(kManualConfigComponentVersion));
hint_cache_->ProcessAndCacheHints(manual_config->mutable_hints(), hint_cache_->ProcessAndCacheHints(manual_config->mutable_hints(),
update_data.get()); update_data.get());
// Allow |UpdateComponentHints| to block startup so that the first // Allow |UpdateComponentHints| to block startup so that the first
...@@ -922,8 +920,10 @@ void OptimizationGuideHintsManager::RegisterOptimizationTypes( ...@@ -922,8 +920,10 @@ void OptimizationGuideHintsManager::RegisterOptimizationTypes(
base::Optional<double> value = previously_registered_opt_types->FindBoolKey( base::Optional<double> value = previously_registered_opt_types->FindBoolKey(
optimization_guide::proto::OptimizationType_Name(optimization_type)); optimization_guide::proto::OptimizationType_Name(optimization_type));
if (!value) { if (!value) {
if (!ShouldIgnoreNewlyRegisteredOptimizationType(optimization_type)) if (!profile_->IsOffTheRecord() &&
!ShouldIgnoreNewlyRegisteredOptimizationType(optimization_type)) {
should_clear_hints_for_new_type_ = true; should_clear_hints_for_new_type_ = true;
}
previously_registered_opt_types->SetBoolKey( previously_registered_opt_types->SetBoolKey(
optimization_guide::proto::OptimizationType_Name(optimization_type), optimization_guide::proto::OptimizationType_Name(optimization_type),
true); true);
...@@ -1259,13 +1259,13 @@ void OptimizationGuideHintsManager::OnNavigationStartOrRedirect( ...@@ -1259,13 +1259,13 @@ void OptimizationGuideHintsManager::OnNavigationStartOrRedirect(
base::OnceClosure callback) { base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
LoadHintForNavigation(navigation_handle, std::move(callback));
if (optimization_guide::switches:: if (optimization_guide::switches::
DisableFetchingHintsAtNavigationStartForTesting()) { DisableFetchingHintsAtNavigationStartForTesting()) {
return; return;
} }
LoadHintForNavigation(navigation_handle, std::move(callback));
MaybeFetchHintsForNavigation(navigation_handle); MaybeFetchHintsForNavigation(navigation_handle);
} }
...@@ -1357,6 +1357,11 @@ void OptimizationGuideHintsManager::OnNavigationFinish( ...@@ -1357,6 +1357,11 @@ void OptimizationGuideHintsManager::OnNavigationFinish(
} }
} }
optimization_guide::OptimizationGuideStore*
OptimizationGuideHintsManager::hint_store() {
return hint_cache_->hint_store();
}
bool OptimizationGuideHintsManager::HasAllInformationForDecisionAvailable( bool OptimizationGuideHintsManager::HasAllInformationForDecisionAvailable(
const GURL& navigation_url, const GURL& navigation_url,
optimization_guide::proto::OptimizationType optimization_type) { optimization_guide::proto::OptimizationType optimization_type) {
......
...@@ -31,18 +31,10 @@ ...@@ -31,18 +31,10 @@
#include "net/nqe/effective_connection_type.h" #include "net/nqe/effective_connection_type.h"
#include "services/network/public/cpp/network_quality_tracker.h" #include "services/network/public/cpp/network_quality_tracker.h"
namespace base {
class FilePath;
} // namespace base
namespace content { namespace content {
class NavigationHandle; class NavigationHandle;
} // namespace content } // namespace content
namespace leveldb_proto {
class ProtoDatabaseProvider;
} // namespace leveldb_proto
namespace network { namespace network {
class SharedURLLoaderFactory; class SharedURLLoaderFactory;
} // namespace network } // namespace network
...@@ -53,6 +45,7 @@ class HintsFetcherFactory; ...@@ -53,6 +45,7 @@ class HintsFetcherFactory;
class OptimizationFilter; class OptimizationFilter;
class OptimizationMetadata; class OptimizationMetadata;
class OptimizationGuideService; class OptimizationGuideService;
class OptimizationGuideStore;
enum class OptimizationTargetDecision; enum class OptimizationTargetDecision;
enum class OptimizationTypeDecision; enum class OptimizationTypeDecision;
class StoreUpdateData; class StoreUpdateData;
...@@ -71,9 +64,8 @@ class OptimizationGuideHintsManager ...@@ -71,9 +64,8 @@ class OptimizationGuideHintsManager
OptimizationGuideHintsManager( OptimizationGuideHintsManager(
optimization_guide::OptimizationGuideService* optimization_guide_service, optimization_guide::OptimizationGuideService* optimization_guide_service,
Profile* profile, Profile* profile,
const base::FilePath& profile_path,
PrefService* pref_service, PrefService* pref_service,
leveldb_proto::ProtoDatabaseProvider* database_provider, optimization_guide::OptimizationGuideStore* hint_store,
optimization_guide::TopHostProvider* top_host_provider, optimization_guide::TopHostProvider* top_host_provider,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory); scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
...@@ -164,6 +156,9 @@ class OptimizationGuideHintsManager ...@@ -164,6 +156,9 @@ class OptimizationGuideHintsManager
// |navigation_redirect_chain| has finished. // |navigation_redirect_chain| has finished.
void OnNavigationFinish(const std::vector<GURL>& navigation_redirect_chain); void OnNavigationFinish(const std::vector<GURL>& navigation_redirect_chain);
// Returns the persistent store for |this|.
optimization_guide::OptimizationGuideStore* hint_store();
// Add hints to the cache with the provided metadata. For testing only. // Add hints to the cache with the provided metadata. For testing only.
void AddHintForTesting( void AddHintForTesting(
const GURL& url, const GURL& url,
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "components/optimization_guide/optimization_guide_features.h" #include "components/optimization_guide/optimization_guide_features.h"
#include "components/optimization_guide/optimization_guide_prefs.h" #include "components/optimization_guide/optimization_guide_prefs.h"
#include "components/optimization_guide/optimization_guide_service.h" #include "components/optimization_guide/optimization_guide_service.h"
#include "components/optimization_guide/optimization_guide_store.h"
#include "components/optimization_guide/optimization_guide_switches.h" #include "components/optimization_guide/optimization_guide_switches.h"
#include "components/optimization_guide/proto_database_provider_test_base.h" #include "components/optimization_guide/proto_database_provider_test_base.h"
#include "components/optimization_guide/top_host_provider.h" #include "components/optimization_guide/top_host_provider.h"
...@@ -302,9 +303,12 @@ class OptimizationGuideHintsManagerTest ...@@ -302,9 +303,12 @@ class OptimizationGuideHintsManagerTest
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_); &test_url_loader_factory_);
hint_store_ = std::make_unique<optimization_guide::OptimizationGuideStore>(
db_provider_.get(), temp_dir(),
task_environment_.GetMainThreadTaskRunner());
hints_manager_ = std::make_unique<OptimizationGuideHintsManager>( hints_manager_ = std::make_unique<OptimizationGuideHintsManager>(
optimization_guide_service_.get(), &testing_profile_, temp_dir(), optimization_guide_service_.get(), &testing_profile_,
pref_service_.get(), db_provider_.get(), top_host_provider, pref_service_.get(), hint_store_.get(), top_host_provider,
url_loader_factory_); url_loader_factory_);
hints_manager_->SetClockForTesting(task_environment_.GetMockClock()); hints_manager_->SetClockForTesting(task_environment_.GetMockClock());
...@@ -319,6 +323,7 @@ class OptimizationGuideHintsManagerTest ...@@ -319,6 +323,7 @@ class OptimizationGuideHintsManagerTest
void ResetHintsManager() { void ResetHintsManager() {
hints_manager_->Shutdown(); hints_manager_->Shutdown();
hints_manager_.reset(); hints_manager_.reset();
hint_store_.reset();
RunUntilIdle(); RunUntilIdle();
} }
...@@ -442,6 +447,7 @@ class OptimizationGuideHintsManagerTest ...@@ -442,6 +447,7 @@ class OptimizationGuideHintsManagerTest
base::test::ScopedFeatureList scoped_feature_list_; base::test::ScopedFeatureList scoped_feature_list_;
TestingProfile testing_profile_; TestingProfile testing_profile_;
std::unique_ptr<content::TestWebContentsFactory> web_contents_factory_; std::unique_ptr<content::TestWebContentsFactory> web_contents_factory_;
std::unique_ptr<optimization_guide::OptimizationGuideStore> hint_store_;
std::unique_ptr<OptimizationGuideHintsManager> hints_manager_; std::unique_ptr<OptimizationGuideHintsManager> hints_manager_;
std::unique_ptr<TestOptimizationGuideService> optimization_guide_service_; std::unique_ptr<TestOptimizationGuideService> optimization_guide_service_;
std::unique_ptr<TestingPrefServiceSimple> pref_service_; std::unique_ptr<TestingPrefServiceSimple> pref_service_;
......
...@@ -126,6 +126,7 @@ void OptimizationGuideKeyedService::Initialize() { ...@@ -126,6 +126,7 @@ void OptimizationGuideKeyedService::Initialize() {
// For incognito profiles, we act in "read-only" mode of the original // For incognito profiles, we act in "read-only" mode of the original
// profile's store and do not fetch any new hints or models. // profile's store and do not fetch any new hints or models.
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory; scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory;
optimization_guide::OptimizationGuideStore* hint_store;
optimization_guide::OptimizationGuideStore* optimization_guide::OptimizationGuideStore*
prediction_model_and_features_store; prediction_model_and_features_store;
if (profile->IsOffTheRecord()) { if (profile->IsOffTheRecord()) {
...@@ -133,6 +134,7 @@ void OptimizationGuideKeyedService::Initialize() { ...@@ -133,6 +134,7 @@ void OptimizationGuideKeyedService::Initialize() {
OptimizationGuideKeyedServiceFactory::GetForProfile( OptimizationGuideKeyedServiceFactory::GetForProfile(
profile->GetOriginalProfile()); profile->GetOriginalProfile());
DCHECK(original_ogks); DCHECK(original_ogks);
hint_store = original_ogks->GetHintsManager()->hint_store();
prediction_model_and_features_store = prediction_model_and_features_store =
original_ogks->GetPredictionManager()->model_and_features_store(); original_ogks->GetPredictionManager()->model_and_features_store();
} else { } else {
...@@ -147,6 +149,18 @@ void OptimizationGuideKeyedService::Initialize() { ...@@ -147,6 +149,18 @@ void OptimizationGuideKeyedService::Initialize() {
ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial( ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial(
"SyntheticOptimizationGuideRemoteFetching", "SyntheticOptimizationGuideRemoteFetching",
optimization_guide_fetching_enabled ? "Enabled" : "Disabled"); optimization_guide_fetching_enabled ? "Enabled" : "Disabled");
hint_store_ =
optimization_guide::features::ShouldPersistHintsToDisk()
? std::make_unique<optimization_guide::OptimizationGuideStore>(
proto_db_provider,
profile_path.AddExtensionASCII(
optimization_guide::kOptimizationGuideHintStore),
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT}))
: nullptr;
hint_store = hint_store_.get();
prediction_model_and_features_store_ = prediction_model_and_features_store_ =
std::make_unique<optimization_guide::OptimizationGuideStore>( std::make_unique<optimization_guide::OptimizationGuideStore>(
proto_db_provider, proto_db_provider,
...@@ -160,8 +174,8 @@ void OptimizationGuideKeyedService::Initialize() { ...@@ -160,8 +174,8 @@ void OptimizationGuideKeyedService::Initialize() {
} }
hints_manager_ = std::make_unique<OptimizationGuideHintsManager>( hints_manager_ = std::make_unique<OptimizationGuideHintsManager>(
g_browser_process->optimization_guide_service(), profile, profile_path, g_browser_process->optimization_guide_service(), profile,
profile->GetPrefs(), proto_db_provider, top_host_provider_.get(), profile->GetPrefs(), hint_store, top_host_provider_.get(),
url_loader_factory); url_loader_factory);
prediction_manager_ = std::make_unique<optimization_guide::PredictionManager>( prediction_manager_ = std::make_unique<optimization_guide::PredictionManager>(
prediction_model_and_features_store, top_host_provider_.get(), prediction_model_and_features_store, top_host_provider_.get(),
......
...@@ -148,17 +148,20 @@ class OptimizationGuideKeyedService ...@@ -148,17 +148,20 @@ class OptimizationGuideKeyedService
content::BrowserContext* browser_context_; content::BrowserContext* browser_context_;
// The store of hints.
std::unique_ptr<optimization_guide::OptimizationGuideStore> hint_store_;
// Manages the storing, loading, and fetching of hints. // Manages the storing, loading, and fetching of hints.
std::unique_ptr<OptimizationGuideHintsManager> hints_manager_; std::unique_ptr<OptimizationGuideHintsManager> hints_manager_;
// Manages the storing, loading, and evaluating of optimization target
// prediction models.
std::unique_ptr<optimization_guide::PredictionManager> prediction_manager_;
// The store of optimization target prediction models and features. // The store of optimization target prediction models and features.
std::unique_ptr<optimization_guide::OptimizationGuideStore> std::unique_ptr<optimization_guide::OptimizationGuideStore>
prediction_model_and_features_store_; prediction_model_and_features_store_;
// Manages the storing, loading, and evaluating of optimization target
// prediction models.
std::unique_ptr<optimization_guide::PredictionManager> prediction_manager_;
// The top host provider to use for fetching information for the user's top // The top host provider to use for fetching information for the user's top
// hosts. Will be null if the user has not consented to this type of browser // hosts. Will be null if the user has not consented to this type of browser
// behavior. // behavior.
......
...@@ -671,41 +671,68 @@ IN_PROC_BROWSER_TEST_F( ...@@ -671,41 +671,68 @@ IN_PROC_BROWSER_TEST_F(
#endif #endif
} }
// TODO(crbug/1158343): Re-enable when flake is fixed.
IN_PROC_BROWSER_TEST_F( IN_PROC_BROWSER_TEST_F(
OptimizationGuideKeyedServiceDataSaverUserWithInfobarShownTest, OptimizationGuideKeyedServiceDataSaverUserWithInfobarShownTest,
DISABLED_IncognitoCanStillReadFromComponentHints) { IncognitoCanStillReadFromComponentHints) {
// Instantiate off the record Optimization Guide Service.
OptimizationGuideKeyedServiceFactory::GetForProfile(
browser()->profile()->GetPrimaryOTRProfile())
->RegisterOptimizationTypes({optimization_guide::proto::NOSCRIPT});
// Wait until initialization logic finishes running and component pushed to // Wait until initialization logic finishes running and component pushed to
// both incognito and regular browsers. // both incognito and regular browsers.
PushHintsComponentAndWaitForCompletion(); PushHintsComponentAndWaitForCompletion();
base::RunLoop().RunUntilIdle();
// Set up incognito browser and incognito OptimizationGuideKeyedService // Set up incognito browser and incognito OptimizationGuideKeyedService
// consumer. // consumer.
Browser* otr_browser = CreateIncognitoBrowser(browser()->profile()); Browser* otr_browser = CreateIncognitoBrowser(browser()->profile());
auto otr_consumer =
std::make_unique<OptimizationGuideConsumerWebContentsObserver>(
otr_browser->tab_strip_model()->GetActiveWebContents());
std::unique_ptr<base::RunLoop> run_loop = std::make_unique<base::RunLoop>();
otr_consumer->set_callback(base::BindOnce(
[](base::RunLoop* run_loop,
optimization_guide::OptimizationGuideDecision decision,
const optimization_guide::OptimizationMetadata& metadata) {
// Should still get decision in incognito.
EXPECT_EQ(decision,
optimization_guide::OptimizationGuideDecision::kTrue);
run_loop->Quit();
},
run_loop.get()));
// Navigate to a URL that has a hint from a component. // Instantiate off the record Optimization Guide Service.
OptimizationGuideKeyedService* otr_ogks =
OptimizationGuideKeyedServiceFactory::GetForProfile(
browser()->profile()->GetPrimaryOTRProfile());
otr_ogks->RegisterOptimizationTypes({optimization_guide::proto::NOSCRIPT});
// Navigate to a URL that has a hint from a component and wait for that hint
// to have loaded.
base::HistogramTester histogram_tester;
ui_test_utils::NavigateToURL(otr_browser, url_with_hints()); ui_test_utils::NavigateToURL(otr_browser, url_with_hints());
run_loop->Run(); RetryForHistogramUntilCountReached(histogram_tester,
"OptimizationGuide.LoadedHint.Result", 1);
EXPECT_EQ(
optimization_guide::OptimizationGuideDecision::kTrue,
otr_ogks->CanApplyOptimization(
url_with_hints(), optimization_guide::proto::NOSCRIPT, nullptr));
}
IN_PROC_BROWSER_TEST_F(
OptimizationGuideKeyedServiceDataSaverUserWithInfobarShownTest,
IncognitoStillProcessesBloomFilter) {
PushHintsComponentAndWaitForCompletion();
CreateIncognitoBrowser(browser()->profile());
// Instantiate off the record Optimization Guide Service.
OptimizationGuideKeyedService* otr_ogks =
OptimizationGuideKeyedServiceFactory::GetForProfile(
browser()->profile()->GetPrimaryOTRProfile());
base::HistogramTester histogram_tester;
// Register an optimization type with an optimization filter.
otr_ogks->RegisterOptimizationTypes(
{optimization_guide::proto::FAST_HOST_HINTS});
// Wait until filter is loaded. This histogram will record twice: once when
// the config is found and once when the filter is created.
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.OptimizationFilterStatus.FastHostHints", 2);
EXPECT_EQ(optimization_guide::OptimizationGuideDecision::kFalse,
otr_ogks->CanApplyOptimization(
GURL("https://blockedhost.com/whatever"),
optimization_guide::proto::FAST_HOST_HINTS, nullptr));
histogram_tester.ExpectUniqueSample(
"OptimizationGuide.ApplyDecision.FastHostHints",
static_cast<int>(optimization_guide::OptimizationTypeDecision::
kNotAllowedByOptimizationFilter),
1);
} }
class OptimizationGuideKeyedServiceCommandLineOverridesTest class OptimizationGuideKeyedServiceCommandLineOverridesTest
......
...@@ -15,10 +15,9 @@ ...@@ -15,10 +15,9 @@
namespace optimization_guide { namespace optimization_guide {
HintCache::HintCache( HintCache::HintCache(OptimizationGuideStore* optimization_guide_store,
std::unique_ptr<OptimizationGuideStore> optimization_guide_store, int max_memory_cache_host_keyed_hints)
int max_memory_cache_host_keyed_hints) : optimization_guide_store_(optimization_guide_store),
: optimization_guide_store_(std::move(optimization_guide_store)),
host_keyed_cache_(max_memory_cache_host_keyed_hints), host_keyed_cache_(max_memory_cache_host_keyed_hints),
url_keyed_hint_cache_(features::MaxURLKeyedHintCacheSize()), url_keyed_hint_cache_(features::MaxURLKeyedHintCacheSize()),
clock_(base::DefaultClock::GetInstance()) {} clock_(base::DefaultClock::GetInstance()) {}
......
...@@ -37,9 +37,8 @@ class HintCache { ...@@ -37,9 +37,8 @@ class HintCache {
// Construct the HintCache with an optional backing store and max host-keyed // Construct the HintCache with an optional backing store and max host-keyed
// cache size. If a backing store is not provided, all hints will only be // cache size. If a backing store is not provided, all hints will only be
// stored in-memory. // stored in-memory.
explicit HintCache( explicit HintCache(OptimizationGuideStore* optimization_guide_store,
std::unique_ptr<OptimizationGuideStore> optimization_guide_store, int max_host_keyed_memory_cache_size);
int max_host_keyed_memory_cache_size);
~HintCache(); ~HintCache();
// Initializes the backing store contained within the hint cache, if provided, // Initializes the backing store contained within the hint cache, if provided,
...@@ -138,6 +137,11 @@ class HintCache { ...@@ -138,6 +137,11 @@ class HintCache {
// Returns whether the persistent hint store owned by this is available. // Returns whether the persistent hint store owned by this is available.
bool IsHintStoreAvailable() const; bool IsHintStoreAvailable() const;
// Returns the persistent store for |this|.
optimization_guide::OptimizationGuideStore* hint_store() {
return optimization_guide_store_;
}
// Override |clock_| for testing. // Override |clock_| for testing.
void SetClockForTesting(const base::Clock* clock); void SetClockForTesting(const base::Clock* clock);
...@@ -165,8 +169,9 @@ class HintCache { ...@@ -165,8 +169,9 @@ class HintCache {
const OptimizationGuideStore::EntryKey& store_hint_entry_key, const OptimizationGuideStore::EntryKey& store_hint_entry_key,
std::unique_ptr<MemoryHint> hint); std::unique_ptr<MemoryHint> hint);
// The backing store used with this hint cache. Set during construction. // The backing store used with this hint cache. Set during construction. Not
const std::unique_ptr<OptimizationGuideStore> optimization_guide_store_; // owned. Guaranteed to outlive |this|.
OptimizationGuideStore* optimization_guide_store_;
// The cache of host-keyed hints loaded from the store. Maps store // The cache of host-keyed hints loaded from the store. Maps store
// EntryKey to Hint proto. This serves two purposes: // EntryKey to Hint proto. This serves two purposes:
......
...@@ -52,12 +52,13 @@ class HintCacheTest : public ProtoDatabaseProviderTestBase, ...@@ -52,12 +52,13 @@ class HintCacheTest : public ProtoDatabaseProviderTestBase,
bool purge_existing_data = false) { bool purge_existing_data = false) {
auto database_path = temp_dir_.GetPath(); auto database_path = temp_dir_.GetPath();
auto database_task_runner = task_environment_.GetMainThreadTaskRunner(); auto database_task_runner = task_environment_.GetMainThreadTaskRunner();
hint_cache_ = std::make_unique<HintCache>( optimization_guide_store_ =
IsBackedByPersistentStore() IsBackedByPersistentStore()
? std::make_unique<OptimizationGuideStore>( ? std::make_unique<OptimizationGuideStore>(
db_provider_.get(), database_path, database_task_runner) db_provider_.get(), database_path, database_task_runner)
: nullptr, : nullptr;
memory_cache_size); hint_cache_ = std::make_unique<HintCache>(optimization_guide_store_.get(),
memory_cache_size);
is_store_initialized_ = false; is_store_initialized_ = false;
hint_cache_->Initialize(purge_existing_data, hint_cache_->Initialize(purge_existing_data,
base::BindOnce(&HintCacheTest::OnStoreInitialized, base::BindOnce(&HintCacheTest::OnStoreInitialized,
...@@ -70,6 +71,7 @@ class HintCacheTest : public ProtoDatabaseProviderTestBase, ...@@ -70,6 +71,7 @@ class HintCacheTest : public ProtoDatabaseProviderTestBase,
void DestroyHintCache() { void DestroyHintCache() {
hint_cache_.reset(); hint_cache_.reset();
optimization_guide_store_.reset();
loaded_hint_ = nullptr; loaded_hint_ = nullptr;
is_store_initialized_ = false; is_store_initialized_ = false;
are_component_hints_updated_ = false; are_component_hints_updated_ = false;
...@@ -165,6 +167,7 @@ class HintCacheTest : public ProtoDatabaseProviderTestBase, ...@@ -165,6 +167,7 @@ class HintCacheTest : public ProtoDatabaseProviderTestBase,
base::test::TaskEnvironment task_environment_{ base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME}; base::test::TaskEnvironment::TimeSource::MOCK_TIME};
std::unique_ptr<OptimizationGuideStore> optimization_guide_store_;
std::unique_ptr<HintCache> hint_cache_; std::unique_ptr<HintCache> hint_cache_;
const proto::Hint* loaded_hint_; const proto::Hint* loaded_hint_;
......
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