Skip to content

Commit

Permalink
Privacy Sandbox Settings: Provide accessed topics to PSCS
Browse files Browse the repository at this point in the history
CL provides the topics accessed by the current page to the associated
Page Specific Content Settings, which in turn already makes this info
available to Page Info.

Prior to this CL the only information actually provided to Page Info
was dummy info. This CL preserves this dummy information, which remains
behind a feature parameter.

(cherry picked from commit 530c1e6)

Bug: 1311516
Change-Id: I68fbb781aa274d1917d0f0441076bd784c2f51ed
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3557871
Reviewed-by: Balazs Engedy <engedy@chromium.org>
Reviewed-by: Yao Xiao <yaoxia@chromium.org>
Commit-Queue: Theodore Olsauskas-Warren <sauski@google.com>
Cr-Original-Commit-Position: refs/heads/main@{#987059}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3561562
Owners-Override: Krishna Govind <govind@chromium.org>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/branch-heads/4974@{#5}
Cr-Branched-From: 89f4767-refs/heads/main@{#986924}
  • Loading branch information
sauski-alternative authored and Krishna Govind committed Mar 30, 2022
1 parent da88782 commit 3e222d4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "components/browsing_topics/browsing_topics_service_impl.h"
#include "components/browsing_topics/epoch_topics.h"
#include "components/browsing_topics/test_util.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/optimization_guide/content/browser/page_content_annotations_service.h"
#include "components/optimization_guide/content/browser/test_page_content_annotator.h"
Expand Down Expand Up @@ -54,7 +55,8 @@ constexpr size_t kTaxonomySize = 349;
constexpr int kTaxonomyVersion = 1;
constexpr int64_t kModelVersion = 2;
constexpr size_t kPaddedTopTopicsStartIndex = 5;

constexpr Topic kExpectedTopic1 = Topic(1);
constexpr Topic kExpectedTopic2 = Topic(10);
constexpr char kExpectedResultOrder1[] =
"[{\"configVersion\":\"chrome.1\",\"modelVersion\":\"2\","
"\"taxonomyVersion\":\"1\",\"topic\":1,\"version\":\"chrome.1:1:2\"};{"
Expand Down Expand Up @@ -561,6 +563,20 @@ IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest,

EXPECT_TRUE(result == kExpectedResultOrder1 ||
result == kExpectedResultOrder2);

// Ensure access has been reported to the Page Specific Content Settings.
auto* pscs = content_settings::PageSpecificContentSettings::GetForPage(
web_contents()->GetPrimaryPage());
EXPECT_TRUE(pscs->HasAccessedTopics());
auto topics = pscs->GetAccessedTopics();
ASSERT_EQ(2u, topics.size());

// No ordering is enforced by the PSCS.
ASSERT_NE(topics[0].topic_id(), topics[1].topic_id());
ASSERT_TRUE(topics[0].topic_id() == kExpectedTopic1 ||
topics[0].topic_id() == kExpectedTopic2);
ASSERT_TRUE(topics[1].topic_id() == kExpectedTopic1 ||
topics[1].topic_id() == kExpectedTopic2);
}

IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest,
Expand Down Expand Up @@ -601,6 +617,9 @@ IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest,
// b.test has yet to call the API so it shouldn't receive a topic.
EXPECT_EQ("[]", InvokeTopicsAPI(content::ChildFrameAt(
web_contents()->GetMainFrame(), 0)));
auto* pscs = content_settings::PageSpecificContentSettings::GetForPage(
web_contents()->GetPrimaryPage());
EXPECT_FALSE(pscs->HasAccessedTopics());
}

IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest,
Expand Down
28 changes: 26 additions & 2 deletions chrome/browser/chrome_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5998,8 +5998,32 @@ ChromeContentBrowserClient::GetBrowsingTopicsForJsApi(
if (!browsing_topics_service)
return {};

return browsing_topics_service->GetBrowsingTopicsForJsApi(context_origin,
main_frame);
auto topics = browsing_topics_service->GetBrowsingTopicsForJsApi(
context_origin, main_frame);

// Compare the provided topics to the real (i.e. non random) topics available
// for the site, this allows filtering out of the randomly generated topics
// for passing to the Page Specific Content Settings.
auto real_topics = browsing_topics_service->GetTopicsForSiteForDisplay(
main_frame->GetLastCommittedOrigin());

// |topics| and |real_topics| will contain only a handful of entries,
// and |topics| order must be preserved. A simple loop is thus appropriate.
for (const auto& topic : topics) {
int taxonomy_version = 0;
base::StringToInt(topic->taxonomy_version, &taxonomy_version);
DCHECK(taxonomy_version);

privacy_sandbox::CanonicalTopic canonical_topic(
browsing_topics::Topic(topic->topic), taxonomy_version);
if (base::Contains(real_topics, canonical_topic)) {
content_settings::PageSpecificContentSettings::TopicAccessed(
main_frame, context_origin, /*blocked_by_policy=*/false,
canonical_topic);
}
}

return topics;
}

bool ChromeContentBrowserClient::IsBluetoothScanningBlocked(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ void PageSpecificContentSettings::InterestGroupJoined(
settings->OnInterestGroupJoined(api_origin, blocked_by_policy);
}

// static
void PageSpecificContentSettings::TopicAccessed(
content::RenderFrameHost* rfh,
const url::Origin api_origin,
bool blocked_by_policy,
privacy_sandbox::CanonicalTopic topic) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
PageSpecificContentSettings* settings = GetForFrame(rfh);
if (settings)
settings->OnTopicAccessed(api_origin, blocked_by_policy, topic);
}

// static
content::WebContentsObserver*
PageSpecificContentSettings::GetWebContentsObserverForTest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ class PageSpecificContentSettings
const url::Origin api_origin,
bool blocked_by_policy);

// Called when |api_origin| attempts to access browsing topics.
static void TopicAccessed(content::RenderFrameHost* rfh,
const url::Origin api_origin,
bool blocked_by_policy,
privacy_sandbox::CanonicalTopic topic);

static content::WebContentsObserver* GetWebContentsObserverForTest(
content::WebContents* web_contents);

Expand Down

0 comments on commit 3e222d4

Please sign in to comment.