Commit 8233a668 authored by Sigurdur Asgeirsson's avatar Sigurdur Asgeirsson Committed by Commit Bot

RC: Add an inspection interface to DB implementation.

Bug: 874968
Change-Id: I32c3ab375b033f64436f47cc1fa2186a5b63178a
Reviewed-on: https://chromium-review.googlesource.com/1187181
Commit-Queue: Sigurður Ásgeirsson <siggi@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Reviewed-by: default avatarChris Hamilton <chrisha@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585840}
parent 37dedbdf
......@@ -2793,6 +2793,8 @@ jumbo_split_static_library("browser") {
"resource_coordinator/local_site_characteristics_data_store.h",
"resource_coordinator/local_site_characteristics_data_store_factory.cc",
"resource_coordinator/local_site_characteristics_data_store_factory.h",
"resource_coordinator/local_site_characteristics_data_store_inspector.cc",
"resource_coordinator/local_site_characteristics_data_store_inspector.h",
"resource_coordinator/local_site_characteristics_data_writer.cc",
"resource_coordinator/local_site_characteristics_data_writer.h",
"resource_coordinator/local_site_characteristics_database.h",
......
......@@ -204,18 +204,7 @@ LocalSiteCharacteristicsDataImpl::~LocalSiteCharacteristicsDataImpl() {
// not completed, add some metrics to measure if this is really an issue.
if (is_dirty_ && fully_initialized_) {
DCHECK(site_characteristics_.IsInitialized());
// Update the proto with the most current performance measurement averages.
if (cpu_usage_estimate_.num_datums() ||
private_footprint_kb_estimate_.num_datums()) {
auto* estimates = site_characteristics_.mutable_load_time_estimates();
if (cpu_usage_estimate_.num_datums())
estimates->set_avg_cpu_usage_us(cpu_usage_estimate_.value());
if (private_footprint_kb_estimate_.num_datums()) {
estimates->set_avg_footprint_kb(private_footprint_kb_estimate_.value());
}
}
database_->WriteSiteCharacteristicsIntoDB(origin_, site_characteristics_);
database_->WriteSiteCharacteristicsIntoDB(origin_, FlushStateToProto());
}
}
......@@ -455,5 +444,21 @@ void LocalSiteCharacteristicsDataImpl::DecrementNumLoadedBackgroundTabs() {
IncrementFeatureObservationDuration(iter, extra_observation_duration);
}
const SiteCharacteristicsProto&
LocalSiteCharacteristicsDataImpl::FlushStateToProto() {
// Update the proto with the most current performance measurement averages.
if (cpu_usage_estimate_.num_datums() ||
private_footprint_kb_estimate_.num_datums()) {
auto* estimates = site_characteristics_.mutable_load_time_estimates();
if (cpu_usage_estimate_.num_datums())
estimates->set_avg_cpu_usage_us(cpu_usage_estimate_.value());
if (private_footprint_kb_estimate_.num_datums()) {
estimates->set_avg_footprint_kb(private_footprint_kb_estimate_.value());
}
}
return site_characteristics_;
}
} // namespace internal
} // namespace resource_coordinator
......@@ -133,9 +133,11 @@ class LocalSiteCharacteristicsDataImpl
protected:
friend class base::RefCounted<LocalSiteCharacteristicsDataImpl>;
friend class resource_coordinator::LocalSiteCharacteristicsDataStore;
// Friend all the tests.
friend class LocalSiteCharacteristicsDataImplTest;
friend class resource_coordinator::LocalSiteCharacteristicsDataReaderTest;
friend class resource_coordinator::LocalSiteCharacteristicsDataStore;
friend class resource_coordinator::LocalSiteCharacteristicsDataStoreTest;
friend class resource_coordinator::LocalSiteCharacteristicsDataWriterTest;
......@@ -213,6 +215,9 @@ class LocalSiteCharacteristicsDataImpl
// local feature observation durations if necessary.
void DecrementNumLoadedBackgroundTabs();
// Flush any state that's maintained in member variables to the proto.
const SiteCharacteristicsProto& FlushStateToProto();
// This site's characteristics, contains the features and other values are
// measured.
SiteCharacteristicsProto site_characteristics_;
......
......@@ -26,7 +26,7 @@ constexpr char kSiteCharacteristicsDirectoryName[] =
LocalSiteCharacteristicsDataStore::LocalSiteCharacteristicsDataStore(
Profile* profile)
: history_observer_(this) {
: history_observer_(this), profile_(profile) {
DCHECK(base::FeatureList::IsEnabled(features::kSiteCharacteristicsDatabase));
database_ = std::make_unique<LevelDBSiteCharacteristicsDatabase>(
......@@ -36,10 +36,14 @@ LocalSiteCharacteristicsDataStore::LocalSiteCharacteristicsDataStore(
HistoryServiceFactory::GetForProfileWithoutCreating(profile);
if (history)
history_observer_.Add(history);
// Register the debug interface against the profile.
LocalSiteCharacteristicsDataStoreInspector::SetForProfile(this, profile);
}
LocalSiteCharacteristicsDataStore::~LocalSiteCharacteristicsDataStore() =
default;
LocalSiteCharacteristicsDataStore::~LocalSiteCharacteristicsDataStore() {
LocalSiteCharacteristicsDataStoreInspector::SetForProfile(nullptr, profile_);
}
std::unique_ptr<SiteCharacteristicsDataReader>
LocalSiteCharacteristicsDataStore::GetReaderForOrigin(
......@@ -68,6 +72,42 @@ bool LocalSiteCharacteristicsDataStore::IsRecordingForTesting() {
return true;
}
const char* LocalSiteCharacteristicsDataStore::GetDataStoreName() {
return "LocalSiteCharacteristicsDataStore";
}
std::vector<url::Origin>
LocalSiteCharacteristicsDataStore::GetAllInMemoryOrigins() {
std::vector<url::Origin> ret;
ret.reserve(origin_data_map_.size());
for (const auto& entry : origin_data_map_)
ret.push_back(entry.first);
return ret;
}
void LocalSiteCharacteristicsDataStore::GetDatabaseSize(
DatabaseSizeCallback on_have_data) {
// TODO(siggi): implement me.
std::move(on_have_data).Run(base::nullopt, base::nullopt);
}
bool LocalSiteCharacteristicsDataStore::GetaDataForOrigin(
const url::Origin& origin,
std::unique_ptr<SiteCharacteristicsProto>* data) {
DCHECK_NE(nullptr, data);
const auto it = origin_data_map_.find(origin);
if (it == origin_data_map_.end())
return false;
std::unique_ptr<SiteCharacteristicsProto> ret =
std::make_unique<SiteCharacteristicsProto>();
ret->CopyFrom(it->second->FlushStateToProto());
*data = std::move(ret);
return true;
}
internal::LocalSiteCharacteristicsDataImpl*
LocalSiteCharacteristicsDataStore::GetOrCreateFeatureImpl(
const url::Origin& origin) {
......
......@@ -11,6 +11,7 @@
#include "base/scoped_observer.h"
#include "base/sequence_checker.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_impl.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_store_inspector.h"
#include "chrome/browser/resource_coordinator/site_characteristics_data_store.h"
#include "chrome/browser/resource_coordinator/site_characteristics_data_writer.h"
#include "components/history/core/browser/history_service_observer.h"
......@@ -28,6 +29,7 @@ class LocalSiteCharacteristicsDatabase;
// LocalSiteCharacteristicsNonRecordingDataStore class should be used instead.
class LocalSiteCharacteristicsDataStore
: public SiteCharacteristicsDataStore,
public LocalSiteCharacteristicsDataStoreInspector,
public internal::LocalSiteCharacteristicsDataImpl::OnDestroyDelegate,
public history::HistoryServiceObserver {
public:
......@@ -57,6 +59,14 @@ class LocalSiteCharacteristicsDataStore
database_ = std::move(database);
}
// LocalSiteCharacteristicsDataStoreInspector:
const char* GetDataStoreName() override;
std::vector<url::Origin> GetAllInMemoryOrigins() override;
void GetDatabaseSize(DatabaseSizeCallback on_have_data) override;
bool GetaDataForOrigin(
const url::Origin& origin,
std::unique_ptr<SiteCharacteristicsProto>* data) override;
private:
FRIEND_TEST_ALL_PREFIXES(LocalSiteCharacteristicsDataStoreTest, EndToEnd);
FRIEND_TEST_ALL_PREFIXES(LocalSiteCharacteristicsDataStoreTest,
......@@ -87,6 +97,9 @@ class LocalSiteCharacteristicsDataStore
std::unique_ptr<LocalSiteCharacteristicsDatabase> database_;
// The profile this data store is associated with.
Profile* profile_;
DISALLOW_COPY_AND_ASSIGN(LocalSiteCharacteristicsDataStore);
};
......
......@@ -9,6 +9,7 @@
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_store.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_store_inspector.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_non_recording_data_store.h"
#include "chrome/browser/resource_coordinator/tab_manager_features.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
......@@ -72,6 +73,8 @@ LocalSiteCharacteristicsDataStoreFactory::GetExistingDataStoreForContext(
KeyedService* LocalSiteCharacteristicsDataStoreFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
SiteCharacteristicsDataStore* data_store = nullptr;
Profile* profile = Profile::FromBrowserContext(context);
DCHECK(profile);
if (context->IsOffTheRecord()) {
content::BrowserContext* parent_context =
chrome::GetBrowserContextRedirectedInIncognito(context);
......@@ -79,14 +82,15 @@ KeyedService* LocalSiteCharacteristicsDataStoreFactory::BuildServiceInstanceFor(
// Off the record profiles correspond to incognito profile and are derived
// from a parent profile that is on record.
DCHECK(!parent_context->IsOffTheRecord());
LocalSiteCharacteristicsDataStoreInspector* parent_debug =
LocalSiteCharacteristicsDataStoreInspector::GetForProfile(
Profile::FromBrowserContext(context));
SiteCharacteristicsDataStore* data_store_for_readers =
GetExistingDataStoreForContext(parent_context);
DCHECK(data_store_for_readers);
data_store = new LocalSiteCharacteristicsNonRecordingDataStore(
data_store_for_readers);
profile, parent_debug, data_store_for_readers);
} else {
Profile* profile = Profile::FromBrowserContext(context);
DCHECK(profile);
data_store = new LocalSiteCharacteristicsDataStore(profile);
}
DCHECK(data_store);
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_store_inspector.h"
#include "base/macros.h"
#include "chrome/browser/profiles/profile.h"
namespace resource_coordinator {
namespace {
const void* const kSiteCharacteristicsDataStoreInspectorUserKey =
&kSiteCharacteristicsDataStoreInspectorUserKey;
class SiteCharacteristicsUserData : public base::SupportsUserData::Data {
public:
explicit SiteCharacteristicsUserData(
LocalSiteCharacteristicsDataStoreInspector* inspector)
: inspector_(inspector) {}
LocalSiteCharacteristicsDataStoreInspector* inspector() const {
return inspector_;
}
private:
LocalSiteCharacteristicsDataStoreInspector* inspector_;
};
} // namespace
// static
LocalSiteCharacteristicsDataStoreInspector*
LocalSiteCharacteristicsDataStoreInspector::GetForProfile(Profile* profile) {
SiteCharacteristicsUserData* data = static_cast<SiteCharacteristicsUserData*>(
profile->GetUserData(kSiteCharacteristicsDataStoreInspectorUserKey));
if (!data)
return nullptr;
return data->inspector();
}
// static
void LocalSiteCharacteristicsDataStoreInspector::SetForProfile(
LocalSiteCharacteristicsDataStoreInspector* inspector,
Profile* profile) {
if (inspector) {
DCHECK_EQ(nullptr, GetForProfile(profile));
profile->SetUserData(
kSiteCharacteristicsDataStoreInspectorUserKey,
std::make_unique<SiteCharacteristicsUserData>(inspector));
} else {
DCHECK_NE(nullptr, GetForProfile(profile));
profile->RemoveUserData(kSiteCharacteristicsDataStoreInspectorUserKey);
}
}
} // namespace resource_coordinator
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_RESOURCE_COORDINATOR_LOCAL_SITE_CHARACTERISTICS_DATA_STORE_INSPECTOR_H_
#define CHROME_BROWSER_RESOURCE_COORDINATOR_LOCAL_SITE_CHARACTERISTICS_DATA_STORE_INSPECTOR_H_
#include <cstdint>
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/optional.h"
#include "base/supports_user_data.h"
#include "chrome/browser/resource_coordinator/site_characteristics.pb.h"
#include "url/origin.h"
class Profile;
namespace resource_coordinator {
// An interface that allows LocalSite data stores to expose diagnostic
// information for the associated web UI.
class LocalSiteCharacteristicsDataStoreInspector {
public:
// Retrieves the instance associated with a given profile, or nullptr
// if none is associated with that profile.
static LocalSiteCharacteristicsDataStoreInspector* GetForProfile(
Profile* profile);
// Returns the name of the data store, which should uniquely identify the kind
// of storage it implements.
virtual const char* GetDataStoreName() = 0;
// Retrieves the origins that are current represented by in-memory data
// at the present time.
virtual std::vector<url::Origin> GetAllInMemoryOrigins() = 0;
// Retrieves the number of rows and the on-disk size of the DB. Invokes
// the |on_have_data| callback once the data has been collected, or once it's
// determined that the data can't be retrieved.
// On callback |num_rows| is the number of rows in the database, or -1 if
// the number can't be determined. |on_disk_size_kb| is the on-disk size of
// the database, or -1 if the on-disk size can't be determined.
using DatabaseSizeCallback =
base::OnceCallback<void(base::Optional<int64_t> num_rows,
base::Optional<int64_t> on_disk_size_kb)>;
virtual void GetDatabaseSize(DatabaseSizeCallback on_have_data) = 0;
// Retrieves the in-memory data for a given origin.
// On return |data| contains the available data for |origin|, or nullptr
// if no data is available for this origin.
virtual bool GetaDataForOrigin(
const url::Origin& origin,
/* TODO(siggi): more properties, like "dirty bit" */
std::unique_ptr<SiteCharacteristicsProto>* data) = 0;
protected:
// Sets the inspector instance associated with a given profile.
// If |inspector| is nullptr the association is cleared.
// The caller must ensure that |inspector|'s registration is cleared before
// |inspector| or |profile| are deleted.
// The intent is for this to be called from implementation class' constructors
// and destructors.
static void SetForProfile(
LocalSiteCharacteristicsDataStoreInspector* inspector,
Profile* profile);
};
} // namespace resource_coordinator
#endif // CHROME_BROWSER_RESOURCE_COORDINATOR_LOCAL_SITE_CHARACTERISTICS_DATA_STORE_INSPECTOR_H_
......@@ -8,6 +8,7 @@
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_impl.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_store_inspector.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/time.h"
......@@ -197,4 +198,36 @@ TEST_F(LocalSiteCharacteristicsDataStoreTest, HistoryServiceObserver) {
writer2->NotifySiteUnloaded();
}
TEST_F(LocalSiteCharacteristicsDataStoreTest, InspectorWorks) {
// Make sure the inspector interface was registered at construction.
LocalSiteCharacteristicsDataStoreInspector* inspector =
LocalSiteCharacteristicsDataStoreInspector::GetForProfile(&profile_);
EXPECT_NE(nullptr, inspector);
EXPECT_EQ(data_store_.get(), inspector);
EXPECT_STREQ("LocalSiteCharacteristicsDataStore",
inspector->GetDataStoreName());
// We expect an empty data store at the outset.
EXPECT_EQ(0U, inspector->GetAllInMemoryOrigins().size());
std::unique_ptr<SiteCharacteristicsProto> data;
EXPECT_FALSE(inspector->GetaDataForOrigin(kTestOrigin, &data));
EXPECT_EQ(nullptr, data.get());
{
// Add an entry, see that it's reflected in the inspector interface.
auto writer = data_store_->GetWriterForOrigin(kTestOrigin,
TabVisibility::kBackground);
EXPECT_EQ(1U, inspector->GetAllInMemoryOrigins().size());
EXPECT_TRUE(inspector->GetaDataForOrigin(kTestOrigin, &data));
ASSERT_NE(nullptr, data.get());
}
// Make sure the interface is unregistered from the profile on destruction.
data_store_.reset();
EXPECT_EQ(nullptr, LocalSiteCharacteristicsDataStoreInspector::GetForProfile(
&profile_));
}
} // namespace resource_coordinator
......@@ -12,13 +12,21 @@ namespace resource_coordinator {
LocalSiteCharacteristicsNonRecordingDataStore::
LocalSiteCharacteristicsNonRecordingDataStore(
Profile* profile,
LocalSiteCharacteristicsDataStoreInspector* data_store_inspector,
SiteCharacteristicsDataStore* data_store_for_readers)
: data_store_for_readers_(data_store_for_readers) {
: data_store_for_readers_(data_store_for_readers),
data_store_inspector_(data_store_inspector),
profile_(profile) {
DCHECK(data_store_for_readers_);
// Register the debug interface against the profile.
LocalSiteCharacteristicsDataStoreInspector::SetForProfile(this, profile);
}
LocalSiteCharacteristicsNonRecordingDataStore::
~LocalSiteCharacteristicsNonRecordingDataStore() = default;
~LocalSiteCharacteristicsNonRecordingDataStore() {
LocalSiteCharacteristicsDataStoreInspector::SetForProfile(nullptr, profile_);
}
std::unique_ptr<SiteCharacteristicsDataReader>
LocalSiteCharacteristicsNonRecordingDataStore::GetReaderForOrigin(
......@@ -40,4 +48,33 @@ bool LocalSiteCharacteristicsNonRecordingDataStore::IsRecordingForTesting() {
return false;
}
const char* LocalSiteCharacteristicsNonRecordingDataStore::GetDataStoreName() {
return "LocalSiteCharacteristicsNonRecordingDataStore";
}
std::vector<url::Origin>
LocalSiteCharacteristicsNonRecordingDataStore::GetAllInMemoryOrigins() {
if (!data_store_inspector_)
return std::vector<url::Origin>();
return data_store_inspector_->GetAllInMemoryOrigins();
}
void LocalSiteCharacteristicsNonRecordingDataStore::GetDatabaseSize(
DatabaseSizeCallback on_have_data) {
if (!data_store_inspector_)
std::move(on_have_data).Run(base::nullopt, base::nullopt);
data_store_inspector_->GetDatabaseSize(std::move(on_have_data));
}
bool LocalSiteCharacteristicsNonRecordingDataStore::GetaDataForOrigin(
const url::Origin& origin,
std::unique_ptr<SiteCharacteristicsProto>* data) {
if (!data_store_inspector_)
return false;
return data_store_inspector_->GetaDataForOrigin(origin, data);
}
} // namespace resource_coordinator
......@@ -6,8 +6,11 @@
#define CHROME_BROWSER_RESOURCE_COORDINATOR_LOCAL_SITE_CHARACTERISTICS_NON_RECORDING_DATA_STORE_H_
#include "base/macros.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_store_inspector.h"
#include "chrome/browser/resource_coordinator/site_characteristics_data_store.h"
class Profile;
namespace resource_coordinator {
// Specialization of a SiteCharacteristicsDataStore whose
......@@ -15,10 +18,17 @@ namespace resource_coordinator {
// SiteCharacteristicsDataReader are obtained from another
// SiteCharacteristicsDataStore.
class LocalSiteCharacteristicsNonRecordingDataStore
: public SiteCharacteristicsDataStore {
: public SiteCharacteristicsDataStore,
public LocalSiteCharacteristicsDataStoreInspector {
public:
// |data_store_for_readers| should outlive this object.
explicit LocalSiteCharacteristicsNonRecordingDataStore(
// |profile| is the profile this data store is associated with.
// |data_store_inspector| is the inspector instance this instance will
// delegate to, may be null, but this is typically the inspector instance
// associated with |data_store_for_readers|. |data_store_for_readers| should
// outlive this object.
LocalSiteCharacteristicsNonRecordingDataStore(
Profile* profile,
LocalSiteCharacteristicsDataStoreInspector* data_store_inspector,
SiteCharacteristicsDataStore* data_store_for_readers);
~LocalSiteCharacteristicsNonRecordingDataStore() override;
......@@ -30,12 +40,26 @@ class LocalSiteCharacteristicsNonRecordingDataStore
TabVisibility tab_visibility) override;
bool IsRecordingForTesting() override;
// LocalSiteCharacteristicsDataStoreInspector:
const char* GetDataStoreName() override;
std::vector<url::Origin> GetAllInMemoryOrigins() override;
void GetDatabaseSize(DatabaseSizeCallback on_have_data) override;
bool GetaDataForOrigin(
const url::Origin& origin,
std::unique_ptr<SiteCharacteristicsProto>* data) override;
private:
// The data store to use to create the readers served by this data store. E.g.
// during an incognito session it should point to the data store used by the
// parent session.
SiteCharacteristicsDataStore* data_store_for_readers_;
// The inspector implementation this instance delegates to.
LocalSiteCharacteristicsDataStoreInspector* data_store_inspector_;
// The profile this data store is associated with.
Profile* profile_;
DISALLOW_COPY_AND_ASSIGN(LocalSiteCharacteristicsNonRecordingDataStore);
};
......
......@@ -6,6 +6,7 @@
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_store.h"
#include "chrome/browser/resource_coordinator/local_site_characteristics_data_store_inspector.h"
#include "chrome/browser/resource_coordinator/tab_manager_features.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread_bundle.h"
......@@ -15,32 +16,50 @@
namespace resource_coordinator {
using LocalSiteCharacteristicsNonRecordingDataStoreTest = ::testing::Test;
namespace {
const url::Origin kTestOrigin = url::Origin::Create(GURL("http://www.foo.com"));
class LocalSiteCharacteristicsNonRecordingDataStoreTest : public testing::Test {
public:
void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(
features::kSiteCharacteristicsDatabase);
recording_data_store_ =
std::make_unique<LocalSiteCharacteristicsDataStore>(&parent_profile_);
non_recording_data_store_ =
std::make_unique<LocalSiteCharacteristicsNonRecordingDataStore>(
&profile_, recording_data_store_.get(),
recording_data_store_.get());
WaitForAsyncOperationsToComplete();
}
void WaitForAsyncOperationsToComplete() {
test_browser_thread_bundle_.RunUntilIdle();
}
protected:
base::test::ScopedFeatureList scoped_feature_list_;
content::TestBrowserThreadBundle test_browser_thread_bundle_;
TestingProfile parent_profile_;
TestingProfile profile_;
std::unique_ptr<LocalSiteCharacteristicsDataStore> recording_data_store_;
std::unique_ptr<LocalSiteCharacteristicsNonRecordingDataStore>
non_recording_data_store_;
};
} // namespace
TEST_F(LocalSiteCharacteristicsNonRecordingDataStoreTest, EndToEnd) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kSiteCharacteristicsDatabase);
content::TestBrowserThreadBundle test_browser_thread_bundle;
TestingProfile profile;
const url::Origin kTestOrigin =
url::Origin::Create(GURL("http://www.foo.com"));
std::unique_ptr<LocalSiteCharacteristicsDataStore> recording_data_store =
std::make_unique<LocalSiteCharacteristicsDataStore>(&profile);
LocalSiteCharacteristicsNonRecordingDataStore non_recording_data_store(
recording_data_store.get());
test_browser_thread_bundle.RunUntilIdle();
// Ensures that the observation made via a writer created by the non
// recording data store aren't recorded.
auto reader = non_recording_data_store.GetReaderForOrigin(kTestOrigin);
auto reader = non_recording_data_store_->GetReaderForOrigin(kTestOrigin);
EXPECT_TRUE(reader);
auto fake_writer = non_recording_data_store.GetWriterForOrigin(
auto fake_writer = non_recording_data_store_->GetWriterForOrigin(
kTestOrigin, TabVisibility::kBackground);
EXPECT_TRUE(fake_writer);
auto real_writer = recording_data_store->GetWriterForOrigin(
auto real_writer = recording_data_store_->GetWriterForOrigin(
kTestOrigin, TabVisibility::kBackground);
EXPECT_TRUE(real_writer);
......@@ -66,15 +85,48 @@ TEST_F(LocalSiteCharacteristicsNonRecordingDataStoreTest, EndToEnd) {
real_writer.reset();
reader.reset();
test_browser_thread_bundle.RunUntilIdle();
WaitForAsyncOperationsToComplete();
recording_data_store.reset();
recording_data_store_.reset();
// Wait for the database of the recording store to be flushed to disk before
// terminating this test, otherwise the profile might get deleted while the
// database is still being flushed to disk and this could prevent from
// deleting its temporary directory.
test_browser_thread_bundle.RunUntilIdle();
WaitForAsyncOperationsToComplete();
}
TEST_F(LocalSiteCharacteristicsNonRecordingDataStoreTest, InspectorWorks) {
// Make sure the inspector interface was registered at construction.
LocalSiteCharacteristicsDataStoreInspector* inspector =
LocalSiteCharacteristicsDataStoreInspector::GetForProfile(&profile_);
EXPECT_NE(nullptr, inspector);
EXPECT_EQ(non_recording_data_store_.get(), inspector);
EXPECT_STREQ("LocalSiteCharacteristicsNonRecordingDataStore",
inspector->GetDataStoreName());
// We expect an empty data store at the outset.
EXPECT_EQ(0U, inspector->GetAllInMemoryOrigins().size());
std::unique_ptr<SiteCharacteristicsProto> data;
EXPECT_FALSE(inspector->GetaDataForOrigin(kTestOrigin, &data));
EXPECT_EQ(nullptr, data.get());
{
// Add an entry through the writing data store, see that it's reflected in
// the inspector interface.
auto writer = recording_data_store_->GetWriterForOrigin(
kTestOrigin, TabVisibility::kBackground);
EXPECT_EQ(1U, inspector->GetAllInMemoryOrigins().size());
EXPECT_TRUE(inspector->GetaDataForOrigin(kTestOrigin, &data));
ASSERT_NE(nullptr, data.get());
}
// Make sure the interface is unregistered from the profile on destruction.
non_recording_data_store_.reset();
EXPECT_EQ(nullptr, LocalSiteCharacteristicsDataStoreInspector::GetForProfile(
&profile_));
}
} // namespace resource_coordinator
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