Commit b463c39f authored by Steven Holte's avatar Steven Holte Committed by Commit Bot

Add ukm::GetSourceIdForWebContentsDocument helper.

Bug: 754889
Change-Id: I60bc810728bed4759dac3a406f0eae36a1e11820
Reviewed-on: https://chromium-review.googlesource.com/794491Reviewed-by: default avatarBryan McQuade <bmcquade@chromium.org>
Commit-Queue: Steven Holte <holte@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521853}
parent ad730d93
......@@ -41,6 +41,8 @@ class SourceUrlRecorderWebContentsObserver
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
ukm::SourceId GetLastCommittedSourceId();
private:
explicit SourceUrlRecorderWebContentsObserver(
content::WebContents* web_contents);
......@@ -53,12 +55,15 @@ class SourceUrlRecorderWebContentsObserver
// Map from navigation ID to the initial URL for that navigation.
base::flat_map<int64_t, GURL> pending_navigations_;
int64_t last_committed_source_id_;
DISALLOW_COPY_AND_ASSIGN(SourceUrlRecorderWebContentsObserver);
};
SourceUrlRecorderWebContentsObserver::SourceUrlRecorderWebContentsObserver(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {}
: content::WebContentsObserver(web_contents),
last_committed_source_id_(ukm::kInvalidSourceId) {}
void SourceUrlRecorderWebContentsObserver::DidStartNavigation(
content::NavigationHandle* navigation_handle) {
......@@ -86,6 +91,14 @@ void SourceUrlRecorderWebContentsObserver::DidFinishNavigation(
if (it == pending_navigations_.end())
return;
DCHECK(navigation_handle->IsInMainFrame());
DCHECK(!navigation_handle->IsSameDocument());
if (navigation_handle->HasCommitted()) {
last_committed_source_id_ = ukm::ConvertToSourceId(
navigation_handle->GetNavigationId(), ukm::SourceIdType::NAVIGATION_ID);
}
GURL initial_url = std::move(it->second);
pending_navigations_.erase(it);
......@@ -96,6 +109,10 @@ void SourceUrlRecorderWebContentsObserver::DidFinishNavigation(
MaybeRecordUrl(navigation_handle, initial_url);
}
ukm::SourceId SourceUrlRecorderWebContentsObserver::GetLastCommittedSourceId() {
return last_committed_source_id_;
}
void SourceUrlRecorderWebContentsObserver::MaybeRecordUrl(
content::NavigationHandle* navigation_handle,
const GURL& initial_url) {
......@@ -136,4 +153,10 @@ void InitializeSourceUrlRecorderForWebContents(
SourceUrlRecorderWebContentsObserver::CreateForWebContents(web_contents);
}
SourceId GetSourceIdForWebContentsDocument(content::WebContents* web_contents) {
SourceUrlRecorderWebContentsObserver* obs =
SourceUrlRecorderWebContentsObserver::FromWebContents(web_contents);
return obs ? obs->GetLastCommittedSourceId() : kInvalidSourceId;
}
} // namespace ukm
......@@ -5,6 +5,8 @@
#ifndef COMPONENTS_UKM_CONTENT_SOURCE_URL_RECORDER_H_
#define COMPONENTS_UKM_CONTENT_SOURCE_URL_RECORDER_H_
#include "services/metrics/public/cpp/ukm_source_id.h"
namespace content {
class WebContents;
} // namespace content
......@@ -15,6 +17,10 @@ namespace ukm {
void InitializeSourceUrlRecorderForWebContents(
content::WebContents* web_contents);
// Get a UKM SourceId for the currently committed document of web contents.
// Returns kInvalidSourceId if no commit has been observed.
SourceId GetSourceIdForWebContentsDocument(content::WebContents* web_contents);
} // namespace ukm
#endif // COMPONENTS_UKM_CONTENT_SOURCE_URL_RECORDER_H_
......@@ -40,6 +40,12 @@ class SourceUrlRecorderWebContentsObserverBrowserTest
return test_ukm_recorder_->GetSourceForSourceId(source_id);
}
GURL GetAssociatedURLForWebContentsDocument() {
const ukm::UkmSource* src = test_ukm_recorder_->GetSourceForSourceId(
ukm::GetSourceIdForWebContentsDocument(shell()->web_contents()));
return src ? src->url() : GURL();
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<ukm::TestAutoSetUkmRecorder> test_ukm_recorder_;
......@@ -79,6 +85,8 @@ IN_PROC_BROWSER_TEST_F(SourceUrlRecorderWebContentsObserverBrowserTest, Basic) {
EXPECT_NE(nullptr, source);
EXPECT_EQ(url, source->url());
EXPECT_TRUE(source->initial_url().is_empty());
EXPECT_EQ(url, GetAssociatedURLForWebContentsDocument());
}
IN_PROC_BROWSER_TEST_F(SourceUrlRecorderWebContentsObserverBrowserTest,
......@@ -88,6 +96,7 @@ IN_PROC_BROWSER_TEST_F(SourceUrlRecorderWebContentsObserverBrowserTest,
content::NavigateToURL(shell(), url);
EXPECT_TRUE(observer.has_committed());
EXPECT_EQ(nullptr, GetSourceForNavigationId(observer.navigation_id()));
EXPECT_EQ(GURL(), GetAssociatedURLForWebContentsDocument());
}
IN_PROC_BROWSER_TEST_F(SourceUrlRecorderWebContentsObserverBrowserTest,
......@@ -111,6 +120,8 @@ IN_PROC_BROWSER_TEST_F(SourceUrlRecorderWebContentsObserverBrowserTest,
EXPECT_EQ(main_url, source->url());
EXPECT_EQ(nullptr,
GetSourceForNavigationId(subframe_observer.navigation_id()));
EXPECT_EQ(main_url, GetAssociatedURLForWebContentsDocument());
}
IN_PROC_BROWSER_TEST_F(SourceUrlRecorderWebContentsObserverDownloadBrowserTest,
......@@ -121,4 +132,5 @@ IN_PROC_BROWSER_TEST_F(SourceUrlRecorderWebContentsObserverDownloadBrowserTest,
EXPECT_FALSE(observer.has_committed());
EXPECT_TRUE(observer.is_download());
EXPECT_EQ(nullptr, GetSourceForNavigationId(observer.navigation_id()));
EXPECT_EQ(GURL(), GetAssociatedURLForWebContentsDocument());
}
......@@ -22,6 +22,12 @@ class SourceUrlRecorderWebContentsObserverTest
ukm::InitializeSourceUrlRecorderForWebContents(web_contents());
}
GURL GetAssociatedURLForWebContentsDocument() {
const ukm::UkmSource* src = test_ukm_recorder_.GetSourceForSourceId(
ukm::GetSourceIdForWebContentsDocument(web_contents()));
return src ? src->url() : GURL();
}
protected:
ukm::TestAutoSetUkmRecorder test_ukm_recorder_;
};
......@@ -52,12 +58,16 @@ TEST_F(SourceUrlRecorderWebContentsObserverTest, InitialUrl) {
EXPECT_EQ(final_url, kv.second->url());
EXPECT_EQ(initial_url, kv.second->initial_url());
}
EXPECT_EQ(final_url, GetAssociatedURLForWebContentsDocument());
}
TEST_F(SourceUrlRecorderWebContentsObserverTest, IgnoreUnsupportedScheme) {
NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(),
GURL("about:blank"));
EXPECT_EQ(0ul, test_ukm_recorder_.sources_count());
EXPECT_EQ(GURL(), GetAssociatedURLForWebContentsDocument());
}
TEST_F(SourceUrlRecorderWebContentsObserverTest, IgnoreUrlInSubframe) {
......@@ -75,6 +85,8 @@ TEST_F(SourceUrlRecorderWebContentsObserverTest, IgnoreUrlInSubframe) {
EXPECT_EQ(main_frame_url, kv.second->url());
EXPECT_TRUE(kv.second->initial_url().is_empty());
}
EXPECT_EQ(main_frame_url, GetAssociatedURLForWebContentsDocument());
}
TEST_F(SourceUrlRecorderWebContentsObserverTest, IgnoreSameDocumentNavigation) {
......@@ -92,4 +104,6 @@ TEST_F(SourceUrlRecorderWebContentsObserverTest, IgnoreSameDocumentNavigation) {
EXPECT_EQ(url, kv.second->url());
EXPECT_TRUE(kv.second->initial_url().is_empty());
}
EXPECT_EQ(url, GetAssociatedURLForWebContentsDocument());
}
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