File: privacy_sandbox_bridge.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (285 lines) | stat: -rw-r--r-- 10,657 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/android/callback_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/command_line.h"
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_activity_types_factory.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_activity_types_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service_factory.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings_factory.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "components/privacy_sandbox/canonical_topic.h"
#include "components/privacy_sandbox/privacy_sandbox_settings.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/privacy_sandbox/android/jni_headers/PrivacySandboxBridge_jni.h"

using base::android::ConvertUTF16ToJavaString;
using base::android::ConvertUTF8ToJavaString;
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;

namespace {

PrivacySandboxService* GetPrivacySandboxService(
    const base::android::JavaRef<jobject>& j_profile) {
  return PrivacySandboxServiceFactory::GetForProfile(
      Profile::FromJavaObject(j_profile));
}

std::vector<jni_zero::ScopedJavaLocalRef<jobject>> ToJavaTopicsArray(
    JNIEnv* env,
    const std::vector<privacy_sandbox::CanonicalTopic>& topics) {
  std::vector<ScopedJavaLocalRef<jobject>> j_topics;
  for (const auto& topic : topics) {
    j_topics.push_back(Java_PrivacySandboxBridge_createTopic(
        env, topic.topic_id().value(), topic.taxonomy_version(),
        ConvertUTF16ToJavaString(env, topic.GetLocalizedRepresentation()),
        ConvertUTF16ToJavaString(env, topic.GetLocalizedDescription())));
  }
  return j_topics;
}
}  // namespace

static jboolean JNI_PrivacySandboxBridge_IsPrivacySandboxRestricted(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  return GetPrivacySandboxService(j_profile)->IsPrivacySandboxRestricted();
}

static jboolean JNI_PrivacySandboxBridge_IsRestrictedNoticeEnabled(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  return GetPrivacySandboxService(j_profile)->IsRestrictedNoticeEnabled();
}

static std::vector<jni_zero::ScopedJavaLocalRef<jobject>>
JNI_PrivacySandboxBridge_GetCurrentTopTopics(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  return ToJavaTopicsArray(
      env, GetPrivacySandboxService(j_profile)->GetCurrentTopTopics());
}

static std::vector<jni_zero::ScopedJavaLocalRef<jobject>>
JNI_PrivacySandboxBridge_GetBlockedTopics(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  return ToJavaTopicsArray(
      env, GetPrivacySandboxService(j_profile)->GetBlockedTopics());
}

static std::vector<jni_zero::ScopedJavaLocalRef<jobject>>
JNI_PrivacySandboxBridge_GetFirstLevelTopics(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  return ToJavaTopicsArray(
      env, GetPrivacySandboxService(j_profile)->GetFirstLevelTopics());
}

static std::vector<jni_zero::ScopedJavaLocalRef<jobject>>
JNI_PrivacySandboxBridge_GetChildTopicsCurrentlyAssigned(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    jint topic_id,
    jint taxonomy_version) {
  return ToJavaTopicsArray(
      env, GetPrivacySandboxService(j_profile)->GetChildTopicsCurrentlyAssigned(
               privacy_sandbox::CanonicalTopic(browsing_topics::Topic(topic_id),
                                               taxonomy_version)));
}

static void JNI_PrivacySandboxBridge_SetTopicAllowed(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    jint topic_id,
    jint taxonomy_version,
    jboolean allowed) {
  GetPrivacySandboxService(j_profile)->SetTopicAllowed(
      privacy_sandbox::CanonicalTopic(browsing_topics::Topic(topic_id),
                                      taxonomy_version),
      allowed);
}

static void JNI_PrivacySandboxBridge_GetFledgeJoiningEtldPlusOneForDisplay(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    const JavaParamRef<jobject>& j_callback) {
  GetPrivacySandboxService(j_profile)->GetFledgeJoiningEtldPlusOneForDisplay(
      base::BindOnce(
          [](const base::android::JavaRef<jobject>& j_callback,
             std::vector<std::string> strings) {
            DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
            JNIEnv* env = base::android::AttachCurrentThread();
            base::android::RunObjectCallbackAndroid(
                j_callback, base::android::ToJavaArrayOfStrings(env, strings));
          },
          base::android::ScopedJavaGlobalRef<jobject>(j_callback)));
}

static std::vector<std::string>
JNI_PrivacySandboxBridge_GetBlockedFledgeJoiningTopFramesForDisplay(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  return GetPrivacySandboxService(j_profile)
      ->GetBlockedFledgeJoiningTopFramesForDisplay();
}

static void JNI_PrivacySandboxBridge_SetFledgeJoiningAllowed(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    const JavaParamRef<jstring>& top_frame_etld_plus1,
    jboolean allowed) {
  GetPrivacySandboxService(j_profile)->SetFledgeJoiningAllowed(
      base::android::ConvertJavaStringToUTF8(top_frame_etld_plus1), allowed);
}

static jint JNI_PrivacySandboxBridge_GetRequiredPromptType(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    jint surface_type) {
  // If the FRE is disabled, as it is in tests which must not be interrupted
  // with dialogs, do not attempt to show a dialog.
  const auto& command_line = *base::CommandLine::ForCurrentProcess();
  if (command_line.HasSwitch("disable-fre"))
    return static_cast<int>(PrivacySandboxService::PromptType::kNone);

  return static_cast<int>(
      GetPrivacySandboxService(j_profile)->GetRequiredPromptType(
          static_cast<PrivacySandboxService::SurfaceType>(surface_type)));
}

static void JNI_PrivacySandboxBridge_PromptActionOccurred(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    jint action,
    jint surface_type) {
  GetPrivacySandboxService(j_profile)->PromptActionOccurred(
      static_cast<PrivacySandboxService::PromptAction>(action),
      static_cast<PrivacySandboxService::SurfaceType>(surface_type));
}

static jboolean JNI_PrivacySandboxBridge_IsRelatedWebsiteSetsDataAccessEnabled(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  return GetPrivacySandboxService(j_profile)
      ->IsRelatedWebsiteSetsDataAccessEnabled();
}

static jboolean JNI_PrivacySandboxBridge_IsRelatedWebsiteSetsDataAccessManaged(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  return GetPrivacySandboxService(j_profile)
      ->IsRelatedWebsiteSetsDataAccessManaged();
}

static void JNI_PrivacySandboxBridge_SetRelatedWebsiteSetsDataAccessEnabled(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    jboolean enabled) {
  GetPrivacySandboxService(j_profile)->SetRelatedWebsiteSetsDataAccessEnabled(
      enabled);
}

static ScopedJavaLocalRef<jstring>
JNI_PrivacySandboxBridge_GetRelatedWebsiteSetOwner(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    const JavaParamRef<jstring>& memberOrigin) {
  auto rwsOwner =
      GetPrivacySandboxService(j_profile)->GetRelatedWebsiteSetOwner(
          GURL(base::android::ConvertJavaStringToUTF8(env, memberOrigin)));

  if (!rwsOwner.has_value()) {
    return nullptr;
  }

  return ConvertUTF8ToJavaString(env, rwsOwner->GetURL().host());
}

static jboolean JNI_PrivacySandboxBridge_IsPartOfManagedRelatedWebsiteSet(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    const JavaParamRef<jstring>& origin) {
  auto schemefulSite = net::SchemefulSite(
      GURL(base::android::ConvertJavaStringToUTF8(env, origin)));

  return GetPrivacySandboxService(j_profile)->IsPartOfManagedRelatedWebsiteSet(
      schemefulSite);
}

static void JNI_PrivacySandboxBridge_TopicsToggleChanged(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    jboolean new_value) {
  GetPrivacySandboxService(j_profile)->TopicsToggleChanged(new_value);
}

static void
JNI_PrivacySandboxBridge_SetAllPrivacySandboxAllowedForTesting(  // IN-TEST
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  PrivacySandboxSettingsFactory::GetForProfile(
      Profile::FromJavaObject(j_profile))
      ->SetAllPrivacySandboxAllowedForTesting();  // IN-TEST
}

static void JNI_PrivacySandboxBridge_RecordActivityType(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile,
    jint activity_type) {
  privacy_sandbox::PrivacySandboxActivityTypesService*
      privacy_sandbox_activity_types_service =
          PrivacySandboxActivityTypesFactory::GetForProfile(
              Profile::FromJavaObject(j_profile));
  if (!privacy_sandbox_activity_types_service) {
    return;
  }
  privacy_sandbox_activity_types_service->RecordActivityType(
      static_cast<privacy_sandbox::PrivacySandboxActivityTypesService::
                      PrivacySandboxStorageActivityType>(activity_type));
}

static jboolean
JNI_PrivacySandboxBridge_PrivacySandboxPrivacyGuideShouldShowAdTopicsCard(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  return GetPrivacySandboxService(j_profile)
      ->PrivacySandboxPrivacyGuideShouldShowAdTopicsCard();
}

static jboolean JNI_PrivacySandboxBridge_ShouldUsePrivacyPolicyChinaDomain(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_profile) {
  return GetPrivacySandboxService(j_profile)
      ->ShouldUsePrivacyPolicyChinaDomain();
}

static ScopedJavaLocalRef<jstring>
JNI_PrivacySandboxBridge_GetEmbeddedPrivacyPolicyURL(
    JNIEnv* env,
    jint domain_type,
    jint color_scheme,
    const JavaParamRef<jstring>& locale) {
  return ConvertUTF8ToJavaString(
      env,
      privacy_sandbox::GetEmbeddedPrivacyPolicyURL(
          static_cast<privacy_sandbox::PrivacyPolicyDomainType>(domain_type),
          static_cast<privacy_sandbox::PrivacyPolicyColorScheme>(color_scheme),
          base::android::ConvertJavaStringToUTF8(env, locale)));
}