Commit 9159239c authored by Sebastien Marchand's avatar Sebastien Marchand Committed by Commit Bot

[PM] Cleanup the site data writer logic

This cleanup some things inherited from the previous implementation,
some properties were tracked multiple time (in the PageNode itself, in
the SiteDataWriter and in the NodeData object), this removes some of the
duplication and privileges using data directly from the PageNode.

This also renames the NotifySiteVisibilityChanged function to a pair of
NotifySiteForegrounded/NotifySiteBackgrounded function for consistency
with the other functions of the SiteDataWriter class.

Change-Id: I6412e2f79d8f330214567a439c33727e87210ce0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2283297Reviewed-by: default avatarChris Hamilton <chrisha@chromium.org>
Commit-Queue: Sébastien Marchand <sebmarchand@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792438}
parent 2e8c5a83
...@@ -85,7 +85,7 @@ void SiteDataCacheFacade::WaitUntilCacheInitializedForTesting() { ...@@ -85,7 +85,7 @@ void SiteDataCacheFacade::WaitUntilCacheInitializedForTesting() {
auto* cache = auto* cache =
cache_factory->GetDataCacheForBrowserContext( cache_factory->GetDataCacheForBrowserContext(
browser_context_id); browser_context_id);
if (cache->IsRecordingForTesting()) { if (cache->IsRecording()) {
static_cast<SiteDataCacheImpl*>(cache) static_cast<SiteDataCacheImpl*>(cache)
->SetInitializationCallbackForTesting( ->SetInitializationCallbackForTesting(
std::move(quit_closure)); std::move(quit_closure));
...@@ -104,6 +104,7 @@ void SiteDataCacheFacade::OnURLsDeleted( ...@@ -104,6 +104,7 @@ void SiteDataCacheFacade::OnURLsDeleted(
SiteDataCacheFactory* cache_factory) { SiteDataCacheFactory* cache_factory) {
auto* cache = auto* cache =
cache_factory->GetDataCacheForBrowserContext(browser_context_id); cache_factory->GetDataCacheForBrowserContext(browser_context_id);
if (cache->IsRecording())
static_cast<SiteDataCacheImpl*>(cache)->ClearAllSiteData(); static_cast<SiteDataCacheImpl*>(cache)->ClearAllSiteData();
}, },
browser_context_->UniqueId()); browser_context_->UniqueId());
...@@ -132,8 +133,10 @@ void SiteDataCacheFacade::OnURLsDeleted( ...@@ -132,8 +133,10 @@ void SiteDataCacheFacade::OnURLsDeleted(
SiteDataCacheFactory* cache_factory) { SiteDataCacheFactory* cache_factory) {
auto* cache = auto* cache =
cache_factory->GetDataCacheForBrowserContext(browser_context_id); cache_factory->GetDataCacheForBrowserContext(browser_context_id);
if (cache->IsRecording()) {
static_cast<SiteDataCacheImpl*>(cache)->ClearSiteDataForOrigins( static_cast<SiteDataCacheImpl*>(cache)->ClearSiteDataForOrigins(
origins_to_remove); origins_to_remove);
}
}, },
browser_context_->UniqueId(), std::move(origins_to_remove)); browser_context_->UniqueId(), std::move(origins_to_remove));
SiteDataCacheFacadeFactory::GetInstance() SiteDataCacheFacadeFactory::GetInstance()
......
...@@ -43,7 +43,8 @@ class SiteDataNodeData : public NodeAttachedDataImpl<SiteDataNodeData>, ...@@ -43,7 +43,8 @@ class SiteDataNodeData : public NodeAttachedDataImpl<SiteDataNodeData>,
public: public:
struct Traits : public NodeAttachedDataOwnedByNodeType<PageNodeImpl> {}; struct Traits : public NodeAttachedDataOwnedByNodeType<PageNodeImpl> {};
explicit SiteDataNodeData(const PageNodeImpl* page_node) {} explicit SiteDataNodeData(const PageNodeImpl* page_node)
: page_node_(page_node) {}
~SiteDataNodeData() override = default; ~SiteDataNodeData() override = default;
// NodeAttachedData: // NodeAttachedData:
...@@ -66,6 +67,8 @@ class SiteDataNodeData : public NodeAttachedDataImpl<SiteDataNodeData>, ...@@ -66,6 +67,8 @@ class SiteDataNodeData : public NodeAttachedDataImpl<SiteDataNodeData>,
void OnTitleUpdated(); void OnTitleUpdated();
void OnFaviconUpdated(); void OnFaviconUpdated();
void Reset();
SiteDataWriter* writer() const override { SiteDataWriter* writer() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return writer_.get(); return writer_.get();
...@@ -90,22 +93,17 @@ class SiteDataNodeData : public NodeAttachedDataImpl<SiteDataNodeData>, ...@@ -90,22 +93,17 @@ class SiteDataNodeData : public NodeAttachedDataImpl<SiteDataNodeData>,
void MaybeNotifyBackgroundFeatureUsage(void (SiteDataWriter::*method)(), void MaybeNotifyBackgroundFeatureUsage(void (SiteDataWriter::*method)(),
FeatureType feature_type); FeatureType feature_type);
// Update |backgrounded_time_| depending on the visibility of the page. TabVisibility GetPageNodeVisibility() {
void UpdateBackgroundedTime(); return page_node_->is_visible() ? TabVisibility::kForeground
: TabVisibility::kBackground;
}
// The SiteDataCache used to serve writers for the PageNode owned by this // The SiteDataCache used to serve writers for the PageNode owned by this
// object. // object.
SiteDataCache* data_cache_ = nullptr; SiteDataCache* data_cache_ = nullptr;
bool is_visible_ = false; // The PageNode that owns this object.
bool is_loaded_ = false; const PageNodeImpl* page_node_ = nullptr;
// The Origin tracked by the writer.
url::Origin last_origin_;
// The time at which this tab has been backgrounded, null if this tab is
// currently visible.
base::TimeTicks backgrounded_time_;
// The time at which this tab switched to the loaded state, null if this tab // The time at which this tab switched to the loaded state, null if this tab
// is not currently loaded. // is not currently loaded.
...@@ -123,35 +121,32 @@ void SiteDataNodeData::OnMainFrameUrlChanged(const GURL& url, ...@@ -123,35 +121,32 @@ void SiteDataNodeData::OnMainFrameUrlChanged(const GURL& url,
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
url::Origin origin = url::Origin::Create(url); url::Origin origin = url::Origin::Create(url);
if (origin == last_origin_) if (writer_ && origin == writer_->Origin())
return; return;
// If the origin has changed then the writer should be invalidated. // If the origin has changed then the writer should be invalidated.
writer_.reset(); Reset();
if (!url.SchemeIsHTTPOrHTTPS()) if (!url.SchemeIsHTTPOrHTTPS())
return; return;
last_origin_ = origin; writer_ = data_cache_->GetWriterForOrigin(origin);
writer_ = data_cache_->GetWriterForOrigin(
origin, page_is_visible ? TabVisibility::kForeground
: TabVisibility::kBackground);
is_visible_ = page_is_visible; // The writer is assumed to be in an unloaded state by default, set the proper
UpdateBackgroundedTime(); // loading state if necessary.
if (!page_node_->is_loading())
OnIsLoadingChanged(false);
} }
void SiteDataNodeData::OnIsLoadingChanged(bool is_loading) { void SiteDataNodeData::OnIsLoadingChanged(bool is_loading) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!writer_) if (!writer_)
return; return;
if (is_loading) { if (is_loading && !loaded_time_.is_null()) {
is_loaded_ = false; writer_->NotifySiteUnloaded(GetPageNodeVisibility());
writer_->NotifySiteUnloaded();
loaded_time_ = base::TimeTicks(); loaded_time_ = base::TimeTicks();
} else { } else if (!is_loading) {
is_loaded_ = true; writer_->NotifySiteLoaded(GetPageNodeVisibility());
writer_->NotifySiteLoaded();
loaded_time_ = base::TimeTicks::Now(); loaded_time_ = base::TimeTicks::Now();
} }
} }
...@@ -160,10 +155,11 @@ void SiteDataNodeData::OnIsVisibleChanged(bool is_visible) { ...@@ -160,10 +155,11 @@ void SiteDataNodeData::OnIsVisibleChanged(bool is_visible) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!writer_) if (!writer_)
return; return;
is_visible_ = is_visible; if (is_visible) {
UpdateBackgroundedTime(); writer_->NotifySiteForegrounded(!page_node_->is_loading());
writer_->NotifySiteVisibilityChanged(is_visible ? TabVisibility::kForeground } else {
: TabVisibility::kBackground); writer_->NotifySiteBackgrounded(!page_node_->is_loading());
}
} }
void SiteDataNodeData::OnIsAudibleChanged(bool audible) { void SiteDataNodeData::OnIsAudibleChanged(bool audible) {
...@@ -190,6 +186,15 @@ void SiteDataNodeData::OnFaviconUpdated() { ...@@ -190,6 +186,15 @@ void SiteDataNodeData::OnFaviconUpdated() {
FeatureType::kFaviconChange); FeatureType::kFaviconChange);
} }
void SiteDataNodeData::Reset() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (writer_ && !loaded_time_.is_null() && !page_node_->is_loading()) {
writer_->NotifySiteUnloaded(GetPageNodeVisibility());
loaded_time_ = base::TimeTicks();
}
writer_.reset();
}
bool SiteDataNodeData::ShouldIgnoreFeatureUsageEvent(FeatureType feature_type) { bool SiteDataNodeData::ShouldIgnoreFeatureUsageEvent(FeatureType feature_type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// The feature usage should be ignored if there's no writer for this page. // The feature usage should be ignored if there's no writer for this page.
...@@ -197,11 +202,11 @@ bool SiteDataNodeData::ShouldIgnoreFeatureUsageEvent(FeatureType feature_type) { ...@@ -197,11 +202,11 @@ bool SiteDataNodeData::ShouldIgnoreFeatureUsageEvent(FeatureType feature_type) {
return true; return true;
// Ignore all features happening before the website gets fully loaded. // Ignore all features happening before the website gets fully loaded.
if (!is_loaded_) if (page_node_->is_loading())
return true; return true;
// Ignore events if the tab is not in background. // Ignore events if the tab is not in background.
if (is_visible_) if (GetPageNodeVisibility() == TabVisibility::kForeground)
return true; return true;
if (feature_type == FeatureType::kTitleChange || if (feature_type == FeatureType::kTitleChange ||
...@@ -215,8 +220,7 @@ bool SiteDataNodeData::ShouldIgnoreFeatureUsageEvent(FeatureType feature_type) { ...@@ -215,8 +220,7 @@ bool SiteDataNodeData::ShouldIgnoreFeatureUsageEvent(FeatureType feature_type) {
// Ignore events happening shortly after the tab being backgrounded, they're // Ignore events happening shortly after the tab being backgrounded, they're
// usually false positives. // usually false positives.
DCHECK(!backgrounded_time_.is_null()); if ((page_node_->TimeSinceLastVisibilityChange() <
if ((base::TimeTicks::Now() - backgrounded_time_ <
kFeatureUsagePostBackgroundGracePeriod)) { kFeatureUsagePostBackgroundGracePeriod)) {
return true; return true;
} }
...@@ -235,15 +239,6 @@ void SiteDataNodeData::MaybeNotifyBackgroundFeatureUsage( ...@@ -235,15 +239,6 @@ void SiteDataNodeData::MaybeNotifyBackgroundFeatureUsage(
(writer_.get()->*method)(); (writer_.get()->*method)();
} }
void SiteDataNodeData::UpdateBackgroundedTime() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (is_visible_) {
backgrounded_time_ = base::TimeTicks();
} else {
backgrounded_time_ = base::TimeTicks::Now();
}
}
SiteDataNodeData* GetSiteDataNodeDataFromPageNode(const PageNode* page_node) { SiteDataNodeData* GetSiteDataNodeDataFromPageNode(const PageNode* page_node) {
auto* page_node_impl = PageNodeImpl::FromNode(page_node); auto* page_node_impl = PageNodeImpl::FromNode(page_node);
DCHECK(page_node_impl); DCHECK(page_node_impl);
...@@ -254,12 +249,6 @@ SiteDataNodeData* GetSiteDataNodeDataFromPageNode(const PageNode* page_node) { ...@@ -254,12 +249,6 @@ SiteDataNodeData* GetSiteDataNodeDataFromPageNode(const PageNode* page_node) {
} // namespace } // namespace
// static
SiteDataRecorder::Data* SiteDataRecorder::Data::GetForTesting(
const PageNode* page_node) {
return GetSiteDataNodeDataFromPageNode(page_node);
}
SiteDataRecorder::SiteDataRecorder() { SiteDataRecorder::SiteDataRecorder() {
DETACH_FROM_SEQUENCE(sequence_checker_); DETACH_FROM_SEQUENCE(sequence_checker_);
} }
...@@ -281,6 +270,12 @@ void SiteDataRecorder::OnPageNodeAdded(const PageNode* page_node) { ...@@ -281,6 +270,12 @@ void SiteDataRecorder::OnPageNodeAdded(const PageNode* page_node) {
SetPageNodeDataCache(page_node); SetPageNodeDataCache(page_node);
} }
void SiteDataRecorder::OnBeforePageNodeRemoved(const PageNode* page_node) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* data = GetSiteDataNodeDataFromPageNode(page_node);
data->Reset();
}
void SiteDataRecorder::OnMainFrameUrlChanged(const PageNode* page_node) { void SiteDataRecorder::OnMainFrameUrlChanged(const PageNode* page_node) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* data = GetSiteDataNodeDataFromPageNode(page_node); auto* data = GetSiteDataNodeDataFromPageNode(page_node);
...@@ -342,4 +337,10 @@ void SiteDataRecorder::SetPageNodeDataCache(const PageNode* page_node) { ...@@ -342,4 +337,10 @@ void SiteDataRecorder::SetPageNodeDataCache(const PageNode* page_node) {
SiteDataNodeData::Data::Data() = default; SiteDataNodeData::Data::Data() = default;
SiteDataNodeData::Data::~Data() = default; SiteDataNodeData::Data::~Data() = default;
// static
SiteDataRecorder::Data* SiteDataRecorder::Data::GetForTesting(
const PageNode* page_node) {
return GetSiteDataNodeDataFromPageNode(page_node);
}
} // namespace performance_manager } // namespace performance_manager
...@@ -33,6 +33,7 @@ class SiteDataRecorder : public GraphOwned, ...@@ -33,6 +33,7 @@ class SiteDataRecorder : public GraphOwned,
// PageNode::ObserverDefaultImpl: // PageNode::ObserverDefaultImpl:
void OnPageNodeAdded(const PageNode* page_node) override; void OnPageNodeAdded(const PageNode* page_node) override;
void OnBeforePageNodeRemoved(const PageNode* page_node) override;
void OnMainFrameUrlChanged(const PageNode* page_node) override; void OnMainFrameUrlChanged(const PageNode* page_node) override;
void OnIsLoadingChanged(const PageNode* page_node) override; void OnIsLoadingChanged(const PageNode* page_node) override;
void OnIsVisibleChanged(const PageNode* page_node) override; void OnIsVisibleChanged(const PageNode* page_node) override;
......
...@@ -40,7 +40,7 @@ class LenientMockDataWriter : public SiteDataWriter { ...@@ -40,7 +40,7 @@ class LenientMockDataWriter : public SiteDataWriter {
public: public:
LenientMockDataWriter(const url::Origin& origin, LenientMockDataWriter(const url::Origin& origin,
scoped_refptr<internal::SiteDataImpl> impl) scoped_refptr<internal::SiteDataImpl> impl)
: SiteDataWriter(impl, TabVisibility::kBackground), origin_(origin) {} : SiteDataWriter(impl), origin_(origin) {}
~LenientMockDataWriter() override { ~LenientMockDataWriter() override {
if (on_destroy_indicator_) if (on_destroy_indicator_)
*on_destroy_indicator_ = true; *on_destroy_indicator_ = true;
...@@ -48,10 +48,10 @@ class LenientMockDataWriter : public SiteDataWriter { ...@@ -48,10 +48,10 @@ class LenientMockDataWriter : public SiteDataWriter {
LenientMockDataWriter(const LenientMockDataWriter& other) = delete; LenientMockDataWriter(const LenientMockDataWriter& other) = delete;
LenientMockDataWriter& operator=(const LenientMockDataWriter&) = delete; LenientMockDataWriter& operator=(const LenientMockDataWriter&) = delete;
MOCK_METHOD0(NotifySiteLoaded, void()); MOCK_METHOD1(NotifySiteLoaded, void(TabVisibility));
MOCK_METHOD0(NotifySiteUnloaded, void()); MOCK_METHOD1(NotifySiteUnloaded, void(TabVisibility));
MOCK_METHOD1(NotifySiteVisibilityChanged, MOCK_METHOD1(NotifySiteForegrounded, void(bool));
void(performance_manager::TabVisibility)); MOCK_METHOD1(NotifySiteBackgrounded, void(bool));
MOCK_METHOD0(NotifyUpdatesFaviconInBackground, void()); MOCK_METHOD0(NotifyUpdatesFaviconInBackground, void());
MOCK_METHOD0(NotifyUpdatesTitleInBackground, void()); MOCK_METHOD0(NotifyUpdatesTitleInBackground, void());
MOCK_METHOD0(NotifyUsesAudioInBackground, void()); MOCK_METHOD0(NotifyUsesAudioInBackground, void());
...@@ -87,14 +87,13 @@ class MockDataCache : public SiteDataCache { ...@@ -87,14 +87,13 @@ class MockDataCache : public SiteDataCache {
return nullptr; return nullptr;
} }
std::unique_ptr<SiteDataWriter> GetWriterForOrigin( std::unique_ptr<SiteDataWriter> GetWriterForOrigin(
const url::Origin& origin, const url::Origin& origin) override {
performance_manager::TabVisibility tab_visibility) override {
scoped_refptr<internal::SiteDataImpl> fake_impl = base::WrapRefCounted( scoped_refptr<internal::SiteDataImpl> fake_impl = base::WrapRefCounted(
new internal::SiteDataImpl(origin, &delegate_, &data_store_)); new internal::SiteDataImpl(origin, &delegate_, &data_store_));
return std::make_unique<MockDataWriter>(origin, fake_impl); return std::make_unique<MockDataWriter>(origin, fake_impl);
} }
bool IsRecordingForTesting() const override { return true; } bool IsRecording() const override { return true; }
int Size() const override { return 0; } int Size() const override { return 0; }
private: private:
...@@ -241,13 +240,11 @@ TEST_F(SiteDataRecorderTest, FeatureEventsGetForwardedWhenInBackground) { ...@@ -241,13 +240,11 @@ TEST_F(SiteDataRecorderTest, FeatureEventsGetForwardedWhenInBackground) {
EXPECT_EQ(url::Origin::Create(kTestUrl1), mock_writer->Origin()); EXPECT_EQ(url::Origin::Create(kTestUrl1), mock_writer->Origin());
node_impl = PageNodeImpl::FromNode(page_node.get()); node_impl = PageNodeImpl::FromNode(page_node.get());
EXPECT_CALL(*mock_writer, NotifySiteLoaded()); EXPECT_CALL(*mock_writer, NotifySiteLoaded(TabVisibility::kBackground));
node_impl->SetIsLoading(false); node_impl->SetIsLoading(false);
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
EXPECT_CALL(*mock_writer, EXPECT_CALL(*mock_writer, NotifySiteForegrounded(true));
NotifySiteVisibilityChanged(
performance_manager::TabVisibility::kForeground));
})); }));
web_contents()->WasShown(); web_contents()->WasShown();
...@@ -263,9 +260,7 @@ TEST_F(SiteDataRecorderTest, FeatureEventsGetForwardedWhenInBackground) { ...@@ -263,9 +260,7 @@ TEST_F(SiteDataRecorderTest, FeatureEventsGetForwardedWhenInBackground) {
node_impl->SetIsAudible(true); node_impl->SetIsAudible(true);
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
EXPECT_CALL(*mock_writer, EXPECT_CALL(*mock_writer, NotifySiteBackgrounded(true));
NotifySiteVisibilityChanged(
performance_manager::TabVisibility::kBackground));
})); }));
web_contents()->WasHidden(); web_contents()->WasHidden();
...@@ -289,12 +284,8 @@ TEST_F(SiteDataRecorderTest, FeatureEventsGetForwardedWhenInBackground) { ...@@ -289,12 +284,8 @@ TEST_F(SiteDataRecorderTest, FeatureEventsGetForwardedWhenInBackground) {
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
// Brievly switch the tab to foreground to reset the last backgrounded time. // Brievly switch the tab to foreground to reset the last backgrounded time.
EXPECT_CALL(*mock_writer, EXPECT_CALL(*mock_writer, NotifySiteForegrounded(true));
NotifySiteVisibilityChanged( EXPECT_CALL(*mock_writer, NotifySiteBackgrounded(true));
performance_manager::TabVisibility::kForeground));
EXPECT_CALL(*mock_writer,
NotifySiteVisibilityChanged(
performance_manager::TabVisibility::kBackground));
})); }));
web_contents()->WasShown(); web_contents()->WasShown();
web_contents()->WasHidden(); web_contents()->WasHidden();
...@@ -320,7 +311,11 @@ TEST_F(SiteDataRecorderTest, FeatureEventsGetForwardedWhenInBackground) { ...@@ -320,7 +311,11 @@ TEST_F(SiteDataRecorderTest, FeatureEventsGetForwardedWhenInBackground) {
node_impl->OnFaviconUpdated(); node_impl->OnFaviconUpdated();
node_impl->OnTitleUpdated(); node_impl->OnTitleUpdated();
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
EXPECT_CALL(*mock_writer, NotifySiteUnloaded(TabVisibility::kBackground));
})); }));
NavigatePageNodeOnUIThread(web_contents(), GURL("about://blank"));
} }
TEST_F(SiteDataRecorderTest, FeatureEventsIgnoredWhenLoadingInBackground) { TEST_F(SiteDataRecorderTest, FeatureEventsIgnoredWhenLoadingInBackground) {
...@@ -355,15 +350,11 @@ TEST_F(SiteDataRecorderTest, VisibilityEvent) { ...@@ -355,15 +350,11 @@ TEST_F(SiteDataRecorderTest, VisibilityEvent) {
// Test that the visibility events get forwarded to the writer. // Test that the visibility events get forwarded to the writer.
EXPECT_CALL(*mock_writer, EXPECT_CALL(*mock_writer, NotifySiteForegrounded(false));
NotifySiteVisibilityChanged(
performance_manager::TabVisibility::kForeground));
node_impl->SetIsVisible(true); node_impl->SetIsVisible(true);
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
EXPECT_CALL(*mock_writer, EXPECT_CALL(*mock_writer, NotifySiteBackgrounded(false));
NotifySiteVisibilityChanged(
performance_manager::TabVisibility::kBackground));
node_impl->SetIsVisible(false); node_impl->SetIsVisible(false);
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
})); }));
...@@ -380,11 +371,11 @@ TEST_F(SiteDataRecorderTest, LoadEvent) { ...@@ -380,11 +371,11 @@ TEST_F(SiteDataRecorderTest, LoadEvent) {
// Test that the load/unload events get forwarded to the writer. // Test that the load/unload events get forwarded to the writer.
EXPECT_CALL(*mock_writer, NotifySiteLoaded()); EXPECT_CALL(*mock_writer, NotifySiteLoaded(TabVisibility::kBackground));
node_impl->SetIsLoading(false); node_impl->SetIsLoading(false);
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
EXPECT_CALL(*mock_writer, NotifySiteUnloaded()); EXPECT_CALL(*mock_writer, NotifySiteUnloaded(TabVisibility::kBackground));
node_impl->SetIsLoading(true); node_impl->SetIsLoading(true);
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
})); }));
......
...@@ -36,14 +36,13 @@ std::unique_ptr<SiteDataReader> NonRecordingSiteDataCache::GetReaderForOrigin( ...@@ -36,14 +36,13 @@ std::unique_ptr<SiteDataReader> NonRecordingSiteDataCache::GetReaderForOrigin(
} }
std::unique_ptr<SiteDataWriter> NonRecordingSiteDataCache::GetWriterForOrigin( std::unique_ptr<SiteDataWriter> NonRecordingSiteDataCache::GetWriterForOrigin(
const url::Origin& origin, const url::Origin& origin) {
performance_manager::TabVisibility tab_visibility) {
// Return a fake data writer. // Return a fake data writer.
SiteDataWriter* writer = new NoopSiteDataWriter(); SiteDataWriter* writer = new NoopSiteDataWriter();
return base::WrapUnique(writer); return base::WrapUnique(writer);
} }
bool NonRecordingSiteDataCache::IsRecordingForTesting() const { bool NonRecordingSiteDataCache::IsRecording() const {
return false; return false;
} }
......
...@@ -30,9 +30,8 @@ class NonRecordingSiteDataCache : public SiteDataCache, ...@@ -30,9 +30,8 @@ class NonRecordingSiteDataCache : public SiteDataCache,
std::unique_ptr<SiteDataReader> GetReaderForOrigin( std::unique_ptr<SiteDataReader> GetReaderForOrigin(
const url::Origin& origin) override; const url::Origin& origin) override;
std::unique_ptr<SiteDataWriter> GetWriterForOrigin( std::unique_ptr<SiteDataWriter> GetWriterForOrigin(
const url::Origin& origin, const url::Origin& origin) override;
performance_manager::TabVisibility tab_visibility) override; bool IsRecording() const override;
bool IsRecordingForTesting() const override;
int Size() const override; int Size() const override;
// SiteDataCacheInspector: // SiteDataCacheInspector:
......
...@@ -74,31 +74,25 @@ TEST_F(NonRecordingSiteDataCacheTest, EndToEnd) { ...@@ -74,31 +74,25 @@ TEST_F(NonRecordingSiteDataCacheTest, EndToEnd) {
// recording data cache aren't recorded. // recording data cache aren't recorded.
auto reader = non_recording_data_cache_->GetReaderForOrigin(kTestOrigin); auto reader = non_recording_data_cache_->GetReaderForOrigin(kTestOrigin);
EXPECT_TRUE(reader); EXPECT_TRUE(reader);
auto fake_writer = non_recording_data_cache_->GetWriterForOrigin( auto fake_writer = non_recording_data_cache_->GetWriterForOrigin(kTestOrigin);
kTestOrigin, performance_manager::TabVisibility::kBackground);
EXPECT_TRUE(fake_writer); EXPECT_TRUE(fake_writer);
auto real_writer = recording_data_cache_->GetWriterForOrigin( auto real_writer = recording_data_cache_->GetWriterForOrigin(kTestOrigin);
kTestOrigin, performance_manager::TabVisibility::kBackground);
EXPECT_TRUE(real_writer); EXPECT_TRUE(real_writer);
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown,
reader->UpdatesTitleInBackground()); reader->UpdatesTitleInBackground());
fake_writer->NotifySiteLoaded(); fake_writer->NotifySiteLoaded(TabVisibility::kBackground);
fake_writer->NotifyUpdatesTitleInBackground(); fake_writer->NotifyUpdatesTitleInBackground();
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown,
reader->UpdatesTitleInBackground()); reader->UpdatesTitleInBackground());
real_writer->NotifySiteLoaded(); real_writer->NotifySiteLoaded(TabVisibility::kBackground);
real_writer->NotifyUpdatesTitleInBackground(); real_writer->NotifyUpdatesTitleInBackground();
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse,
reader->UpdatesTitleInBackground()); reader->UpdatesTitleInBackground());
// These unload events shouldn't be registered, make sure that they aren't by fake_writer->NotifySiteUnloaded(TabVisibility::kBackground);
// unloading the site more time than it has been loaded. real_writer->NotifySiteUnloaded(TabVisibility::kBackground);
fake_writer->NotifySiteUnloaded();
fake_writer->NotifySiteUnloaded();
real_writer->NotifySiteUnloaded();
} }
TEST_F(NonRecordingSiteDataCacheTest, InspectorWorks) { TEST_F(NonRecordingSiteDataCacheTest, InspectorWorks) {
...@@ -121,8 +115,7 @@ TEST_F(NonRecordingSiteDataCacheTest, InspectorWorks) { ...@@ -121,8 +115,7 @@ TEST_F(NonRecordingSiteDataCacheTest, InspectorWorks) {
{ {
// Add an entry through the writing data cache, see that it's reflected in // Add an entry through the writing data cache, see that it's reflected in
// the inspector interface. // the inspector interface.
auto writer = recording_data_cache_->GetWriterForOrigin( auto writer = recording_data_cache_->GetWriterForOrigin(kTestOrigin);
kTestOrigin, performance_manager::TabVisibility::kBackground);
EXPECT_EQ(1U, inspector->GetAllInMemoryOrigins().size()); EXPECT_EQ(1U, inspector->GetAllInMemoryOrigins().size());
EXPECT_TRUE(inspector->GetDataForOrigin(kTestOrigin, &is_dirty, &data)); EXPECT_TRUE(inspector->GetDataForOrigin(kTestOrigin, &is_dirty, &data));
...@@ -130,8 +123,9 @@ TEST_F(NonRecordingSiteDataCacheTest, InspectorWorks) { ...@@ -130,8 +123,9 @@ TEST_F(NonRecordingSiteDataCacheTest, InspectorWorks) {
ASSERT_NE(nullptr, data.get()); ASSERT_NE(nullptr, data.get());
// Touch the underlying data, see that the dirty bit updates. // Touch the underlying data, see that the dirty bit updates.
writer->NotifySiteLoaded(); writer->NotifySiteLoaded(TabVisibility::kBackground);
EXPECT_TRUE(inspector->GetDataForOrigin(kTestOrigin, &is_dirty, &data)); EXPECT_TRUE(inspector->GetDataForOrigin(kTestOrigin, &is_dirty, &data));
writer->NotifySiteUnloaded(TabVisibility::kBackground);
} }
// Make sure the interface is unregistered from the browser context on // Make sure the interface is unregistered from the browser context on
......
...@@ -8,12 +8,13 @@ namespace performance_manager { ...@@ -8,12 +8,13 @@ namespace performance_manager {
NoopSiteDataWriter::~NoopSiteDataWriter() = default; NoopSiteDataWriter::~NoopSiteDataWriter() = default;
void NoopSiteDataWriter::NotifySiteLoaded() {} void NoopSiteDataWriter::NotifySiteLoaded(TabVisibility visibility) {}
void NoopSiteDataWriter::NotifySiteUnloaded() {} void NoopSiteDataWriter::NotifySiteUnloaded(TabVisibility visibility) {}
void NoopSiteDataWriter::NotifySiteVisibilityChanged( void NoopSiteDataWriter::NotifySiteForegrounded(bool is_loaded) {}
performance_manager::TabVisibility visibility) {}
void NoopSiteDataWriter::NotifySiteBackgrounded(bool is_loaded) {}
void NoopSiteDataWriter::NotifyUpdatesFaviconInBackground() {} void NoopSiteDataWriter::NotifyUpdatesFaviconInBackground() {}
...@@ -26,8 +27,11 @@ void NoopSiteDataWriter::NotifyLoadTimePerformanceMeasurement( ...@@ -26,8 +27,11 @@ void NoopSiteDataWriter::NotifyLoadTimePerformanceMeasurement(
base::TimeDelta cpu_usage_estimate, base::TimeDelta cpu_usage_estimate,
uint64_t private_footprint_kb_estimate) {} uint64_t private_footprint_kb_estimate) {}
NoopSiteDataWriter::NoopSiteDataWriter() const url::Origin& NoopSiteDataWriter::Origin() const {
: SiteDataWriter(nullptr, performance_manager::TabVisibility::kForeground) { static url::Origin dummy_origin;
return dummy_origin;
} }
NoopSiteDataWriter::NoopSiteDataWriter() : SiteDataWriter(nullptr) {}
} // namespace performance_manager } // namespace performance_manager
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "components/performance_manager/persistence/site_data/site_data_writer.h" #include "components/performance_manager/persistence/site_data/site_data_writer.h"
#include "url/origin.h"
namespace performance_manager { namespace performance_manager {
...@@ -16,10 +17,10 @@ class NoopSiteDataWriter : public SiteDataWriter { ...@@ -16,10 +17,10 @@ class NoopSiteDataWriter : public SiteDataWriter {
~NoopSiteDataWriter() override; ~NoopSiteDataWriter() override;
// Implementation of SiteDataWriter: // Implementation of SiteDataWriter:
void NotifySiteLoaded() override; void NotifySiteLoaded(TabVisibility visibility) override;
void NotifySiteUnloaded() override; void NotifySiteUnloaded(TabVisibility visibility) override;
void NotifySiteVisibilityChanged( void NotifySiteForegrounded(bool is_loaded) override;
performance_manager::TabVisibility visibility) override; void NotifySiteBackgrounded(bool is_loaded) override;
void NotifyUpdatesFaviconInBackground() override; void NotifyUpdatesFaviconInBackground() override;
void NotifyUpdatesTitleInBackground() override; void NotifyUpdatesTitleInBackground() override;
void NotifyUsesAudioInBackground() override; void NotifyUsesAudioInBackground() override;
...@@ -27,6 +28,7 @@ class NoopSiteDataWriter : public SiteDataWriter { ...@@ -27,6 +28,7 @@ class NoopSiteDataWriter : public SiteDataWriter {
base::TimeDelta load_duration, base::TimeDelta load_duration,
base::TimeDelta cpu_usage_estimate, base::TimeDelta cpu_usage_estimate,
uint64_t private_footprint_kb_estimate) override; uint64_t private_footprint_kb_estimate) override;
const url::Origin& Origin() const override;
private: private:
friend class NonRecordingSiteDataCache; friend class NonRecordingSiteDataCache;
......
...@@ -26,17 +26,12 @@ class SiteDataCache { ...@@ -26,17 +26,12 @@ class SiteDataCache {
const url::Origin& origin) = 0; const url::Origin& origin) = 0;
// Returns a SiteDataWriter for the given origin. // Returns a SiteDataWriter for the given origin.
//
// |tab_visibility| indicates the current visibility of the tab. The writer
// starts in an unloaded state, NotifyTabLoaded() must be called explicitly
// afterwards if the site is loaded.
virtual std::unique_ptr<SiteDataWriter> GetWriterForOrigin( virtual std::unique_ptr<SiteDataWriter> GetWriterForOrigin(
const url::Origin& origin, const url::Origin& origin) = 0;
performance_manager::TabVisibility tab_visibility) = 0;
// Indicate if the SiteDataWriter served by this data cache // Indicate if the SiteDataWriter served by this data cache actually persists
// actually persist information. // information.
virtual bool IsRecordingForTesting() const = 0; virtual bool IsRecording() const = 0;
// Returns the number of element in the cache. // Returns the number of element in the cache.
virtual int Size() const = 0; virtual int Size() const = 0;
......
...@@ -81,7 +81,7 @@ bool SiteDataCacheFactory::IsDataCacheRecordingForTesting( ...@@ -81,7 +81,7 @@ bool SiteDataCacheFactory::IsDataCacheRecordingForTesting(
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto it = data_cache_map_.find(browser_context_id); auto it = data_cache_map_.find(browser_context_id);
CHECK(it != data_cache_map_.end()); CHECK(it != data_cache_map_.end());
return it->second->IsRecordingForTesting(); return it->second->IsRecording();
} }
void SiteDataCacheFactory::SetCacheForTesting( void SiteDataCacheFactory::SetCacheForTesting(
......
...@@ -54,16 +54,15 @@ std::unique_ptr<SiteDataReader> SiteDataCacheImpl::GetReaderForOrigin( ...@@ -54,16 +54,15 @@ std::unique_ptr<SiteDataReader> SiteDataCacheImpl::GetReaderForOrigin(
} }
std::unique_ptr<SiteDataWriter> SiteDataCacheImpl::GetWriterForOrigin( std::unique_ptr<SiteDataWriter> SiteDataCacheImpl::GetWriterForOrigin(
const url::Origin& origin, const url::Origin& origin) {
performance_manager::TabVisibility tab_visibility) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
internal::SiteDataImpl* impl = GetOrCreateFeatureImpl(origin); internal::SiteDataImpl* impl = GetOrCreateFeatureImpl(origin);
DCHECK(impl); DCHECK(impl);
SiteDataWriter* data_writer = new SiteDataWriter(impl, tab_visibility); SiteDataWriter* data_writer = new SiteDataWriter(impl);
return base::WrapUnique(data_writer); return base::WrapUnique(data_writer);
} }
bool SiteDataCacheImpl::IsRecordingForTesting() const { bool SiteDataCacheImpl::IsRecording() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return true; return true;
} }
......
...@@ -41,9 +41,8 @@ class SiteDataCacheImpl : public SiteDataCache, ...@@ -41,9 +41,8 @@ class SiteDataCacheImpl : public SiteDataCache,
std::unique_ptr<SiteDataReader> GetReaderForOrigin( std::unique_ptr<SiteDataReader> GetReaderForOrigin(
const url::Origin& origin) override; const url::Origin& origin) override;
std::unique_ptr<SiteDataWriter> GetWriterForOrigin( std::unique_ptr<SiteDataWriter> GetWriterForOrigin(
const url::Origin& origin, const url::Origin& origin) override;
performance_manager::TabVisibility tab_visibility) override; bool IsRecording() const override;
bool IsRecordingForTesting() const override;
int Size() const override; int Size() const override;
const SiteDataMap& origin_data_map_for_testing() const { const SiteDataMap& origin_data_map_for_testing() const {
......
...@@ -78,8 +78,7 @@ class SiteDataCacheImplTest : public ::testing::Test { ...@@ -78,8 +78,7 @@ class SiteDataCacheImplTest : public ::testing::Test {
EXPECT_TRUE(reader_); EXPECT_TRUE(reader_);
ASSERT_FALSE(writer_); ASSERT_FALSE(writer_);
writer_ = data_cache_->GetWriterForOrigin( writer_ = data_cache_->GetWriterForOrigin(TestOrigin1());
TestOrigin1(), performance_manager::TabVisibility::kBackground);
EXPECT_TRUE(writer_); EXPECT_TRUE(writer_);
ASSERT_FALSE(data_); ASSERT_FALSE(data_);
...@@ -89,7 +88,7 @@ class SiteDataCacheImplTest : public ::testing::Test { ...@@ -89,7 +88,7 @@ class SiteDataCacheImplTest : public ::testing::Test {
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown,
reader_->UpdatesTitleInBackground()); reader_->UpdatesTitleInBackground());
writer_->NotifySiteLoaded(); writer_->NotifySiteLoaded(TabVisibility::kBackground);
writer_->NotifyUpdatesTitleInBackground(); writer_->NotifyUpdatesTitleInBackground();
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse,
reader_->UpdatesTitleInBackground()); reader_->UpdatesTitleInBackground());
...@@ -101,8 +100,7 @@ class SiteDataCacheImplTest : public ::testing::Test { ...@@ -101,8 +100,7 @@ class SiteDataCacheImplTest : public ::testing::Test {
EXPECT_TRUE(reader2_); EXPECT_TRUE(reader2_);
ASSERT_FALSE(writer2_); ASSERT_FALSE(writer2_);
writer2_ = data_cache_->GetWriterForOrigin( writer2_ = data_cache_->GetWriterForOrigin(TestOrigin2());
TestOrigin2(), performance_manager::TabVisibility::kBackground);
EXPECT_TRUE(writer2_); EXPECT_TRUE(writer2_);
ASSERT_FALSE(data2_); ASSERT_FALSE(data2_);
...@@ -112,7 +110,7 @@ class SiteDataCacheImplTest : public ::testing::Test { ...@@ -112,7 +110,7 @@ class SiteDataCacheImplTest : public ::testing::Test {
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown,
reader2_->UpdatesFaviconInBackground()); reader2_->UpdatesFaviconInBackground());
writer2_->NotifySiteLoaded(); writer2_->NotifySiteLoaded(TabVisibility::kBackground);
writer2_->NotifyUpdatesFaviconInBackground(); writer2_->NotifyUpdatesFaviconInBackground();
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse,
reader2_->UpdatesFaviconInBackground()); reader2_->UpdatesFaviconInBackground());
...@@ -141,19 +139,18 @@ class SiteDataCacheImplTest : public ::testing::Test { ...@@ -141,19 +139,18 @@ class SiteDataCacheImplTest : public ::testing::Test {
TEST_F(SiteDataCacheImplTest, EndToEnd) { TEST_F(SiteDataCacheImplTest, EndToEnd) {
auto reader = data_cache_->GetReaderForOrigin(TestOrigin1()); auto reader = data_cache_->GetReaderForOrigin(TestOrigin1());
EXPECT_TRUE(reader); EXPECT_TRUE(reader);
auto writer = data_cache_->GetWriterForOrigin( auto writer = data_cache_->GetWriterForOrigin(TestOrigin1());
TestOrigin1(), performance_manager::TabVisibility::kBackground);
EXPECT_TRUE(writer); EXPECT_TRUE(writer);
EXPECT_EQ(1U, data_cache_->origin_data_map_for_testing().size()); EXPECT_EQ(1U, data_cache_->origin_data_map_for_testing().size());
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown,
reader->UpdatesTitleInBackground()); reader->UpdatesTitleInBackground());
writer->NotifySiteLoaded(); writer->NotifySiteLoaded(TabVisibility::kBackground);
writer->NotifyUpdatesTitleInBackground(); writer->NotifyUpdatesTitleInBackground();
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse,
reader->UpdatesTitleInBackground()); reader->UpdatesTitleInBackground());
writer->NotifySiteUnloaded(); writer->NotifySiteUnloaded(TabVisibility::kBackground);
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse,
reader->UpdatesTitleInBackground()); reader->UpdatesTitleInBackground());
...@@ -204,8 +201,8 @@ TEST_F(SiteDataCacheImplTest, ClearSiteDataForOrigins) { ...@@ -204,8 +201,8 @@ TEST_F(SiteDataCacheImplTest, ClearSiteDataForOrigins) {
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse,
reader2_->UpdatesFaviconInBackground()); reader2_->UpdatesFaviconInBackground());
writer_->NotifySiteUnloaded(); writer_->NotifySiteUnloaded(TabVisibility::kBackground);
writer2_->NotifySiteUnloaded(); writer2_->NotifySiteUnloaded(TabVisibility::kBackground);
} }
TEST_F(SiteDataCacheImplTest, ClearAllSiteData) { TEST_F(SiteDataCacheImplTest, ClearAllSiteData) {
...@@ -226,8 +223,8 @@ TEST_F(SiteDataCacheImplTest, ClearAllSiteData) { ...@@ -226,8 +223,8 @@ TEST_F(SiteDataCacheImplTest, ClearAllSiteData) {
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown,
reader2_->UpdatesFaviconInBackground()); reader2_->UpdatesFaviconInBackground());
writer_->NotifySiteUnloaded(); writer_->NotifySiteUnloaded(TabVisibility::kBackground);
writer2_->NotifySiteUnloaded(); writer2_->NotifySiteUnloaded(TabVisibility::kBackground);
} }
TEST_F(SiteDataCacheImplTest, InspectorWorks) { TEST_F(SiteDataCacheImplTest, InspectorWorks) {
...@@ -250,8 +247,7 @@ TEST_F(SiteDataCacheImplTest, InspectorWorks) { ...@@ -250,8 +247,7 @@ TEST_F(SiteDataCacheImplTest, InspectorWorks) {
{ {
// Add an entry, see that it's reflected in the inspector interface. // Add an entry, see that it's reflected in the inspector interface.
auto writer = data_cache_->GetWriterForOrigin( auto writer = data_cache_->GetWriterForOrigin(TestOrigin1());
TestOrigin1(), performance_manager::TabVisibility::kBackground);
EXPECT_EQ(1U, inspector->GetAllInMemoryOrigins().size()); EXPECT_EQ(1U, inspector->GetAllInMemoryOrigins().size());
EXPECT_TRUE(inspector->GetDataForOrigin(TestOrigin1(), &is_dirty, &data)); EXPECT_TRUE(inspector->GetDataForOrigin(TestOrigin1(), &is_dirty, &data));
...@@ -259,9 +255,10 @@ TEST_F(SiteDataCacheImplTest, InspectorWorks) { ...@@ -259,9 +255,10 @@ TEST_F(SiteDataCacheImplTest, InspectorWorks) {
ASSERT_NE(nullptr, data.get()); ASSERT_NE(nullptr, data.get());
// Touch the underlying data, see that the dirty bit updates. // Touch the underlying data, see that the dirty bit updates.
writer->NotifySiteLoaded(); writer->NotifySiteLoaded(TabVisibility::kBackground);
EXPECT_TRUE(inspector->GetDataForOrigin(TestOrigin1(), &is_dirty, &data)); EXPECT_TRUE(inspector->GetDataForOrigin(TestOrigin1(), &is_dirty, &data));
EXPECT_TRUE(is_dirty); EXPECT_TRUE(is_dirty);
writer->NotifySiteUnloaded(TabVisibility::kBackground);
} }
// Make sure the interface is unregistered from the browser context on // Make sure the interface is unregistered from the browser context on
......
...@@ -67,6 +67,7 @@ void SiteDataImpl::NotifySiteUnloaded(TabVisibility tab_visibility) { ...@@ -67,6 +67,7 @@ void SiteDataImpl::NotifySiteUnloaded(TabVisibility tab_visibility) {
if (tab_visibility == TabVisibility::kBackground) if (tab_visibility == TabVisibility::kBackground)
DecrementNumLoadedBackgroundTabs(); DecrementNumLoadedBackgroundTabs();
DCHECK_GT(loaded_tabs_count_, 0U);
loaded_tabs_count_--; loaded_tabs_count_--;
// Only update the last loaded time when there's no more loaded instance of // Only update the last loaded time when there's no more loaded instance of
// this origin. // this origin.
......
...@@ -8,66 +8,49 @@ ...@@ -8,66 +8,49 @@
namespace performance_manager { namespace performance_manager {
SiteDataWriter::~SiteDataWriter() { SiteDataWriter::~SiteDataWriter() = default;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (is_loaded_)
NotifySiteUnloaded();
}
void SiteDataWriter::NotifySiteLoaded() { void SiteDataWriter::NotifySiteLoaded(TabVisibility visibility) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!is_loaded_);
is_loaded_ = true;
impl_->NotifySiteLoaded(); impl_->NotifySiteLoaded();
if (tab_visibility_ == performance_manager::TabVisibility::kBackground) if (visibility == TabVisibility::kBackground)
impl_->NotifyLoadedSiteBackgrounded(); impl_->NotifyLoadedSiteBackgrounded();
} }
void SiteDataWriter::NotifySiteUnloaded() { void SiteDataWriter::NotifySiteUnloaded(TabVisibility visibility) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(is_loaded_);
is_loaded_ = false;
impl_->NotifySiteUnloaded(tab_visibility_); impl_->NotifySiteUnloaded(visibility);
} }
void SiteDataWriter::NotifySiteVisibilityChanged( void SiteDataWriter::NotifySiteForegrounded(bool is_loaded) {
performance_manager::TabVisibility visibility) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Ignore this if we receive the same event multiple times. if (is_loaded)
if (tab_visibility_ == visibility) impl_->NotifyLoadedSiteForegrounded();
return; }
tab_visibility_ = visibility; void SiteDataWriter::NotifySiteBackgrounded(bool is_loaded) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (is_loaded_) { if (is_loaded)
if (visibility == performance_manager::TabVisibility::kBackground) {
impl_->NotifyLoadedSiteBackgrounded(); impl_->NotifyLoadedSiteBackgrounded();
} else {
impl_->NotifyLoadedSiteForegrounded();
}
}
} }
void SiteDataWriter::NotifyUpdatesFaviconInBackground() { void SiteDataWriter::NotifyUpdatesFaviconInBackground() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(performance_manager::TabVisibility::kBackground, tab_visibility_);
impl_->NotifyUpdatesFaviconInBackground(); impl_->NotifyUpdatesFaviconInBackground();
} }
void SiteDataWriter::NotifyUpdatesTitleInBackground() { void SiteDataWriter::NotifyUpdatesTitleInBackground() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(performance_manager::TabVisibility::kBackground, tab_visibility_);
impl_->NotifyUpdatesTitleInBackground(); impl_->NotifyUpdatesTitleInBackground();
} }
void SiteDataWriter::NotifyUsesAudioInBackground() { void SiteDataWriter::NotifyUsesAudioInBackground() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(performance_manager::TabVisibility::kBackground, tab_visibility_);
// TODO(sebmarchand): Do not advance the background audio observation time // TODO(sebmarchand): Do not advance the background audio observation time
// when the WebContents has never played audio. // when the WebContents has never played audio.
impl_->NotifyUsesAudioInBackground(); impl_->NotifyUsesAudioInBackground();
...@@ -82,10 +65,12 @@ void SiteDataWriter::NotifyLoadTimePerformanceMeasurement( ...@@ -82,10 +65,12 @@ void SiteDataWriter::NotifyLoadTimePerformanceMeasurement(
private_footprint_kb_estimate); private_footprint_kb_estimate);
} }
SiteDataWriter::SiteDataWriter( const url::Origin& SiteDataWriter::Origin() const {
scoped_refptr<internal::SiteDataImpl> impl, return impl_->origin();
performance_manager::TabVisibility tab_visibility) }
: impl_(std::move(impl)), tab_visibility_(tab_visibility) {
SiteDataWriter::SiteDataWriter(scoped_refptr<internal::SiteDataImpl> impl)
: impl_(std::move(impl)) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
} }
......
...@@ -20,12 +20,12 @@ class SiteDataWriter { ...@@ -20,12 +20,12 @@ class SiteDataWriter {
virtual ~SiteDataWriter(); virtual ~SiteDataWriter();
// Records tab load/unload events. // Records tab load/unload events.
virtual void NotifySiteLoaded(); virtual void NotifySiteLoaded(TabVisibility visibility);
virtual void NotifySiteUnloaded(); virtual void NotifySiteUnloaded(TabVisibility visibility);
// Records visibility change events. // Records visibility change events.
virtual void NotifySiteVisibilityChanged( virtual void NotifySiteForegrounded(bool is_loaded);
performance_manager::TabVisibility visibility); virtual void NotifySiteBackgrounded(bool is_loaded);
// Records feature usage. // Records feature usage.
virtual void NotifyUpdatesFaviconInBackground(); virtual void NotifyUpdatesFaviconInBackground();
...@@ -38,6 +38,8 @@ class SiteDataWriter { ...@@ -38,6 +38,8 @@ class SiteDataWriter {
base::TimeDelta cpu_usage_estimate, base::TimeDelta cpu_usage_estimate,
uint64_t private_footprint_kb_estimate); uint64_t private_footprint_kb_estimate);
virtual const url::Origin& Origin() const;
internal::SiteDataImpl* impl_for_testing() const { return impl_.get(); } internal::SiteDataImpl* impl_for_testing() const { return impl_.get(); }
protected: protected:
...@@ -47,19 +49,12 @@ class SiteDataWriter { ...@@ -47,19 +49,12 @@ class SiteDataWriter {
// Protected constructor, these objects are meant to be created by a site data // Protected constructor, these objects are meant to be created by a site data
// store. // store.
SiteDataWriter(scoped_refptr<internal::SiteDataImpl> impl, explicit SiteDataWriter(scoped_refptr<internal::SiteDataImpl> impl);
performance_manager::TabVisibility tab_visibility);
private: private:
// The SiteDataImpl object we delegate to. // The SiteDataImpl object we delegate to.
const scoped_refptr<internal::SiteDataImpl> impl_; const scoped_refptr<internal::SiteDataImpl> impl_;
// The visibility of the tab using this writer.
performance_manager::TabVisibility tab_visibility_;
// Indicates if the tab using this writer is loaded.
bool is_loaded_ = false;
SEQUENCE_CHECKER(sequence_checker_); SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(SiteDataWriter); DISALLOW_COPY_AND_ASSIGN(SiteDataWriter);
......
...@@ -26,8 +26,7 @@ class SiteDataWriterTest : public ::testing::Test { ...@@ -26,8 +26,7 @@ class SiteDataWriterTest : public ::testing::Test {
new internal::SiteDataImpl(url::Origin::Create(GURL("foo.com")), new internal::SiteDataImpl(url::Origin::Create(GURL("foo.com")),
&delegate_, &delegate_,
&data_store_))) { &data_store_))) {
SiteDataWriter* writer = new SiteDataWriter( SiteDataWriter* writer = new SiteDataWriter(test_impl_.get());
test_impl_.get(), performance_manager::TabVisibility::kBackground);
writer_ = base::WrapUnique(writer); writer_ = base::WrapUnique(writer);
} }
...@@ -59,42 +58,42 @@ class SiteDataWriterTest : public ::testing::Test { ...@@ -59,42 +58,42 @@ class SiteDataWriterTest : public ::testing::Test {
TEST_F(SiteDataWriterTest, TestModifiers) { TEST_F(SiteDataWriterTest, TestModifiers) {
// Make sure that we initially have no information about any of the features // Make sure that we initially have no information about any of the features
// and that the site is in an unloaded state. // and that the site is in an unloaded state.
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureUsageUnknown,
test_impl_->UpdatesFaviconInBackground()); test_impl_->UpdatesFaviconInBackground());
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureUsageUnknown,
test_impl_->UpdatesTitleInBackground()); test_impl_->UpdatesTitleInBackground());
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureUsageUnknown,
test_impl_->UsesAudioInBackground()); test_impl_->UsesAudioInBackground());
// Test the OnTabLoaded function. // Test the OnTabLoaded function.
EXPECT_FALSE(TabIsLoaded()); EXPECT_FALSE(TabIsLoaded());
writer_->NotifySiteLoaded(); writer_->NotifySiteLoaded(TabVisibility::kBackground);
EXPECT_TRUE(TabIsLoaded()); EXPECT_TRUE(TabIsLoaded());
// Test all the modifiers. // Test all the modifiers.
writer_->NotifyUpdatesFaviconInBackground(); writer_->NotifyUpdatesFaviconInBackground();
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureInUse,
test_impl_->UpdatesFaviconInBackground()); test_impl_->UpdatesFaviconInBackground());
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureUsageUnknown,
test_impl_->UpdatesTitleInBackground()); test_impl_->UpdatesTitleInBackground());
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureUsageUnknown,
test_impl_->UsesAudioInBackground()); test_impl_->UsesAudioInBackground());
writer_->NotifyUpdatesTitleInBackground(); writer_->NotifyUpdatesTitleInBackground();
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureInUse,
test_impl_->UpdatesFaviconInBackground()); test_impl_->UpdatesFaviconInBackground());
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureInUse,
test_impl_->UpdatesTitleInBackground()); test_impl_->UpdatesTitleInBackground());
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureUsageUnknown,
test_impl_->UsesAudioInBackground()); test_impl_->UsesAudioInBackground());
writer_->NotifyUsesAudioInBackground(); writer_->NotifyUsesAudioInBackground();
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureInUse,
test_impl_->UpdatesFaviconInBackground()); test_impl_->UpdatesFaviconInBackground());
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureInUse,
test_impl_->UpdatesTitleInBackground()); test_impl_->UpdatesTitleInBackground());
EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureInUse, EXPECT_EQ(SiteFeatureUsage::kSiteFeatureInUse,
test_impl_->UsesAudioInBackground()); test_impl_->UsesAudioInBackground());
writer_->NotifyLoadTimePerformanceMeasurement( writer_->NotifyLoadTimePerformanceMeasurement(
...@@ -108,7 +107,7 @@ TEST_F(SiteDataWriterTest, TestModifiers) { ...@@ -108,7 +107,7 @@ TEST_F(SiteDataWriterTest, TestModifiers) {
EXPECT_EQ(1u, test_impl_->private_footprint_kb_estimate().num_datums()); EXPECT_EQ(1u, test_impl_->private_footprint_kb_estimate().num_datums());
EXPECT_EQ(1005.0, test_impl_->private_footprint_kb_estimate().value()); EXPECT_EQ(1005.0, test_impl_->private_footprint_kb_estimate().value());
writer_->NotifySiteUnloaded(); writer_->NotifySiteUnloaded(TabVisibility::kBackground);
} }
TEST_F(SiteDataWriterTest, LoadAndBackgroundStateTransitions) { TEST_F(SiteDataWriterTest, LoadAndBackgroundStateTransitions) {
...@@ -154,42 +153,38 @@ TEST_F(SiteDataWriterTest, LoadAndBackgroundStateTransitions) { ...@@ -154,42 +153,38 @@ TEST_F(SiteDataWriterTest, LoadAndBackgroundStateTransitions) {
EXPECT_FALSE(TabIsLoaded()); EXPECT_FALSE(TabIsLoaded());
// Transition #4: Unloaded + Bg -> Loaded + Bg. // Transition #4: Unloaded + Bg -> Loaded + Bg.
writer_->NotifySiteLoaded(); writer_->NotifySiteLoaded(TabVisibility::kBackground);
EXPECT_TRUE(TabIsLoadedAndInBackground()); EXPECT_TRUE(TabIsLoadedAndInBackground());
// Transition #8: Loaded + Bg -> Loaded + Fg. // Transition #8: Loaded + Bg -> Loaded + Fg.
writer_->NotifySiteVisibilityChanged( writer_->NotifySiteForegrounded(true);
performance_manager::TabVisibility::kForeground);
EXPECT_TRUE(TabIsLoaded()); EXPECT_TRUE(TabIsLoaded());
EXPECT_FALSE(TabIsLoadedAndInBackground()); EXPECT_FALSE(TabIsLoadedAndInBackground());
// Transition #5: Loaded + Fg -> Unloaded + Fg. // Transition #5: Loaded + Fg -> Unloaded + Fg.
writer_->NotifySiteUnloaded(); writer_->NotifySiteUnloaded(TabVisibility::kForeground);
EXPECT_FALSE(TabIsLoaded()); EXPECT_FALSE(TabIsLoaded());
// Transition #1: Unloaded + Fg -> Unloaded + Bg. // Transition #1: Unloaded + Fg -> Unloaded + Bg.
writer_->NotifySiteVisibilityChanged( writer_->NotifySiteBackgrounded(false);
performance_manager::TabVisibility::kBackground);
EXPECT_FALSE(TabIsLoaded()); EXPECT_FALSE(TabIsLoaded());
// Transition #2: Unloaded + Bg -> Unloaded + Fg. // Transition #2: Unloaded + Bg -> Unloaded + Fg.
writer_->NotifySiteVisibilityChanged( writer_->NotifySiteForegrounded(false);
performance_manager::TabVisibility::kForeground);
EXPECT_FALSE(TabIsLoaded()); EXPECT_FALSE(TabIsLoaded());
// Transition #6: Unloaded + Fg -> Loaded + Fg. // Transition #6: Unloaded + Fg -> Loaded + Fg.
writer_->NotifySiteLoaded(); writer_->NotifySiteLoaded(TabVisibility::kForeground);
EXPECT_TRUE(TabIsLoaded()); EXPECT_TRUE(TabIsLoaded());
EXPECT_FALSE(TabIsLoadedAndInBackground()); EXPECT_FALSE(TabIsLoadedAndInBackground());
// Transition #7: Loaded + Fg -> Loaded + Bg. // Transition #7: Loaded + Fg -> Loaded + Bg.
writer_->NotifySiteVisibilityChanged( writer_->NotifySiteBackgrounded(true);
performance_manager::TabVisibility::kBackground);
EXPECT_TRUE(TabIsLoaded()); EXPECT_TRUE(TabIsLoaded());
EXPECT_TRUE(TabIsLoadedAndInBackground()); EXPECT_TRUE(TabIsLoadedAndInBackground());
// Transition #3: Loaded + Bg -> Unloaded + Bg. // Transition #3: Loaded + Bg -> Unloaded + Bg.
writer_->NotifySiteUnloaded(); writer_->NotifySiteUnloaded(TabVisibility::kBackground);
EXPECT_FALSE(TabIsLoaded()); EXPECT_FALSE(TabIsLoaded());
EXPECT_FALSE(TabIsLoadedAndInBackground()); EXPECT_FALSE(TabIsLoadedAndInBackground());
} }
......
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