[Topics] add {observe: true/false} option

It'd be useful to split apart the get/set functionalities:
https://github.com/patcg-individual-drafts/topics/pull/80

Also remove dead code: ../modules/browsing_topics/idls.gni

Bug: 1334682
Change-Id: I9ebb0b4bdb7c3f398b6a69ab632eecfd2af494eb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3696816
Reviewed-by: Joe Mason <[email protected]>
Reviewed-by: Josh Karlin <[email protected]>
Reviewed-by: Avi Drissman <[email protected]>
Commit-Queue: Yao Xiao <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1051859}
diff --git a/components/browsing_topics/browsing_topics_service.h b/components/browsing_topics/browsing_topics_service.h
index 53e688e..2aa6f9d 100644
--- a/components/browsing_topics/browsing_topics_service.h
+++ b/components/browsing_topics/browsing_topics_service.h
@@ -22,10 +22,13 @@
   // Return the browsing topics for a particular requesting context. The
   // calling context and top context information will also be used for the
   // access permission check, and for the `BrowsingTopicsPageLoadDataTracker` to
-  // track the API usage.
+  // track the API usage. If `observe` is true, record the observation
+  // (i.e. the <calling context site, top level site> pair) to the
+  // `BrowsingTopicsSiteDataStorage` database.
   virtual std::vector<blink::mojom::EpochTopicPtr> GetBrowsingTopicsForJsApi(
       const url::Origin& context_origin,
-      content::RenderFrameHost* main_frame) = 0;
+      content::RenderFrameHost* main_frame,
+      bool observe) = 0;
 
   // Get the topics state to show in the chrome://topics-internals page. If
   // `calculate_now` is true, this will first trigger a calculation before
diff --git a/components/browsing_topics/browsing_topics_service_impl.cc b/components/browsing_topics/browsing_topics_service_impl.cc
index 60e2f7a..2852fe1 100644
--- a/components/browsing_topics/browsing_topics_service_impl.cc
+++ b/components/browsing_topics/browsing_topics_service_impl.cc
@@ -226,7 +226,8 @@
 std::vector<blink::mojom::EpochTopicPtr>
 BrowsingTopicsServiceImpl::GetBrowsingTopicsForJsApi(
     const url::Origin& context_origin,
-    content::RenderFrameHost* main_frame) {
+    content::RenderFrameHost* main_frame,
+    bool observe) {
   if (!browsing_topics_state_loaded_) {
     RecordBrowsingTopicsApiResultUkmMetrics(
         EmptyApiResultReason::kStateNotReady, main_frame);
@@ -254,9 +255,11 @@
   HashedDomain hashed_context_domain = HashContextDomainForStorage(
       browsing_topics_state_.hmac_key(), context_domain);
 
-  // Track the API usage context after the permissions check.
-  BrowsingTopicsPageLoadDataTracker::GetOrCreateForPage(main_frame->GetPage())
-      ->OnBrowsingTopicsApiUsed(hashed_context_domain, history_service_);
+  if (observe) {
+    // Track the API usage context after the permissions check.
+    BrowsingTopicsPageLoadDataTracker::GetOrCreateForPage(main_frame->GetPage())
+        ->OnBrowsingTopicsApiUsed(hashed_context_domain, history_service_);
+  }
 
   std::string top_domain =
       net::registry_controlled_domains::GetDomainAndRegistry(
diff --git a/components/browsing_topics/browsing_topics_service_impl.h b/components/browsing_topics/browsing_topics_service_impl.h
index 0451c23..ddf7942 100644
--- a/components/browsing_topics/browsing_topics_service_impl.h
+++ b/components/browsing_topics/browsing_topics_service_impl.h
@@ -46,7 +46,8 @@
 
   std::vector<blink::mojom::EpochTopicPtr> GetBrowsingTopicsForJsApi(
       const url::Origin& context_origin,
-      content::RenderFrameHost* main_frame) override;
+      content::RenderFrameHost* main_frame,
+      bool observe) override;
 
   void GetBrowsingTopicsStateForWebUi(
       bool calculate_now,
diff --git a/components/browsing_topics/browsing_topics_service_impl_unittest.cc b/components/browsing_topics/browsing_topics_service_impl_unittest.cc
index 7ba445f..1df0906 100644
--- a/components/browsing_topics/browsing_topics_service_impl_unittest.cc
+++ b/components/browsing_topics/browsing_topics_service_impl_unittest.cc
@@ -568,7 +568,8 @@
                   ->GetBrowsingTopicsForJsApi(
                       /*context_origin=*/url::Origin::Create(
                           GURL("https://www.bar.com")),
-                      web_contents()->GetPrimaryMainFrame())
+                      web_contents()->GetPrimaryMainFrame(),
+                      /*observe=*/true)
                   .empty());
   auto entries = ukm_recorder.GetEntriesByName(
       ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
@@ -600,7 +601,8 @@
                    ->GetBrowsingTopicsForJsApi(
                        /*context_origin=*/url::Origin::Create(
                            GURL("https://www.bar.com")),
-                       web_contents()->GetPrimaryMainFrame())
+                       web_contents()->GetPrimaryMainFrame(),
+                       /*observe=*/true)
                    .empty());
   EXPECT_FALSE(browsing_topics_service_
                    ->GetTopicsForSiteForDisplay(
@@ -817,7 +819,8 @@
                   ->GetBrowsingTopicsForJsApi(
                       /*context_origin=*/url::Origin::Create(
                           GURL("https://www.bar.com")),
-                      web_contents()->GetPrimaryMainFrame())
+                      web_contents()->GetPrimaryMainFrame(),
+                      /*observe=*/true)
                   .empty());
 
   auto entries = ukm_recorder.GetEntriesByName(
@@ -853,7 +856,7 @@
   std::vector<blink::mojom::EpochTopicPtr> result =
       browsing_topics_service_->GetBrowsingTopicsForJsApi(
           /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-          web_contents()->GetPrimaryMainFrame());
+          web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   EXPECT_TRUE(result.empty());
 
@@ -872,7 +875,7 @@
 
   result = browsing_topics_service_->GetBrowsingTopicsForJsApi(
       /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-      web_contents()->GetPrimaryMainFrame());
+      web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   EXPECT_EQ(result.size(), 1u);
   EXPECT_EQ(result[0]->topic, 2);
@@ -906,7 +909,7 @@
   std::vector<blink::mojom::EpochTopicPtr> result =
       browsing_topics_service_->GetBrowsingTopicsForJsApi(
           /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-          web_contents()->GetPrimaryMainFrame());
+          web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   EXPECT_TRUE(result.empty());
 
@@ -942,7 +945,7 @@
   std::vector<blink::mojom::EpochTopicPtr> result =
       browsing_topics_service_->GetBrowsingTopicsForJsApi(
           /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-          web_contents()->GetPrimaryMainFrame());
+          web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   EXPECT_TRUE(result.empty());
 
@@ -954,7 +957,7 @@
 
   result = browsing_topics_service_->GetBrowsingTopicsForJsApi(
       /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-      web_contents()->GetPrimaryMainFrame());
+      web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   EXPECT_TRUE(result.empty());
 }
@@ -1003,7 +1006,7 @@
   std::vector<blink::mojom::EpochTopicPtr> result =
       browsing_topics_service_->GetBrowsingTopicsForJsApi(
           /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-          web_contents()->GetPrimaryMainFrame());
+          web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   EXPECT_EQ(result.size(), 3u);
   std::set<int> result_set;
@@ -1017,7 +1020,7 @@
 
   result = browsing_topics_service_->GetBrowsingTopicsForJsApi(
       /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-      web_contents()->GetPrimaryMainFrame());
+      web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   EXPECT_EQ(result.size(), 3u);
   result_set.clear();
@@ -1072,7 +1075,7 @@
   std::vector<blink::mojom::EpochTopicPtr> result =
       browsing_topics_service_->GetBrowsingTopicsForJsApi(
           /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-          web_contents()->GetPrimaryMainFrame());
+          web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   EXPECT_EQ(result.size(), 2u);
   std::set<int> result_set;
@@ -1085,7 +1088,7 @@
 
   result = browsing_topics_service_->GetBrowsingTopicsForJsApi(
       /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-      web_contents()->GetPrimaryMainFrame());
+      web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   EXPECT_EQ(result.size(), 2u);
   result_set.clear();
@@ -1148,7 +1151,7 @@
     std::vector<blink::mojom::EpochTopicPtr> result =
         browsing_topics_service_->GetBrowsingTopicsForJsApi(
             /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-            web_contents()->GetPrimaryMainFrame());
+            web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
     EXPECT_EQ(result.size(), 2u);
     std::set<int> result_set;
@@ -1185,7 +1188,7 @@
   NavigateToPage(GURL("https://www.foo.com"));
   browsing_topics_service_->GetBrowsingTopicsForJsApi(
       /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-      web_contents()->GetPrimaryMainFrame());
+      web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   std::vector<ApiUsageContext> api_usage_contexts =
       content::GetBrowsingTopicsApiUsage(topics_site_data_manager());
@@ -1196,6 +1199,30 @@
             GetHashedDomain("bar.com"));
 }
 
+TEST_F(BrowsingTopicsServiceImplTest,
+       GetBrowsingTopicsForJsApi_DoesNotObserve) {
+  base::queue<EpochTopics> mock_calculator_results;
+  mock_calculator_results.push(
+      CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
+                             {Topic(2), {GetHashedDomain("bar.com")}},
+                             {Topic(3), {GetHashedDomain("bar.com")}},
+                             {Topic(4), {GetHashedDomain("bar.com")}},
+                             {Topic(5), {GetHashedDomain("bar.com")}}},
+                            kTime1));
+  InitializeBrowsingTopicsService(std::move(mock_calculator_results));
+
+  task_environment()->RunUntilIdle();
+
+  NavigateToPage(GURL("https://www.foo.com"));
+  browsing_topics_service_->GetBrowsingTopicsForJsApi(
+      /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
+      web_contents()->GetPrimaryMainFrame(), /*observe=*/false);
+
+  std::vector<ApiUsageContext> api_usage_contexts =
+      content::GetBrowsingTopicsApiUsage(topics_site_data_manager());
+  EXPECT_TRUE(api_usage_contexts.empty());
+}
+
 TEST_F(BrowsingTopicsServiceImplTest, ApiResultUkm_ZeroAndOneTopic) {
   ukm::TestAutoSetUkmRecorder ukm_recorder;
 
@@ -1225,7 +1252,7 @@
   // any metrics.
   browsing_topics_service_->GetBrowsingTopicsForJsApi(
       /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-      web_contents()->GetPrimaryMainFrame());
+      web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   entries = ukm_recorder.GetEntriesByName(
       ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
@@ -1247,7 +1274,7 @@
 
   browsing_topics_service_->GetBrowsingTopicsForJsApi(
       /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-      web_contents()->GetPrimaryMainFrame());
+      web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   entries = ukm_recorder.GetEntriesByName(
       ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
@@ -1330,7 +1357,7 @@
 
   browsing_topics_service_->GetBrowsingTopicsForJsApi(
       /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
-      web_contents()->GetPrimaryMainFrame());
+      web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   auto entries = ukm_recorder.GetEntriesByName(
       ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
@@ -1836,7 +1863,7 @@
   std::vector<blink::mojom::EpochTopicPtr> api_call_result =
       browsing_topics_service_->GetBrowsingTopicsForJsApi(
           /*context_origin=*/url::Origin::Create(GURL("https://b.com")),
-          web_contents()->GetPrimaryMainFrame());
+          web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   EXPECT_EQ(api_call_result.size(), 1u);
   EXPECT_EQ(api_call_result[0]->topic, 3);
@@ -1864,7 +1891,7 @@
   // the same context domain was seen in the page before.
   api_call_result = browsing_topics_service_->GetBrowsingTopicsForJsApi(
       /*context_origin=*/url::Origin::Create(GURL("https://b.com")),
-      web_contents()->GetPrimaryMainFrame());
+      web_contents()->GetPrimaryMainFrame(), /*observe=*/true);
 
   // Since the domain "b.com" is removed. The candidate topic won't be returned.
   EXPECT_TRUE(api_call_result.empty());
diff --git a/components/browsing_topics/test_util.h b/components/browsing_topics/test_util.h
index 22a0e9e..9807d0e 100644
--- a/components/browsing_topics/test_util.h
+++ b/components/browsing_topics/test_util.h
@@ -87,7 +87,7 @@
 
   MOCK_METHOD(std::vector<blink::mojom::EpochTopicPtr>,
               GetBrowsingTopicsForJsApi,
-              (const url::Origin&, content::RenderFrameHost*),
+              (const url::Origin&, content::RenderFrameHost*, bool),
               (override));
   MOCK_METHOD(void,
               GetBrowsingTopicsStateForWebUi,