File: supervised_user_settings_test_bridge.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (136 lines) | stat: -rw-r--r-- 6,005 bytes parent folder | download | duplicates (4)
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
// Copyright 2022 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/jni_string.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "base/version_info/channel.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
#include "chrome/browser/supervised_user/supervised_user_test_util.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/supervised_user/core/browser/kids_chrome_management_url_checker_client.h"
#include "components/supervised_user/core/browser/proto/kidsmanagement_messages.pb.h"
#include "components/supervised_user/core/browser/supervised_user_service.h"
#include "components/supervised_user/core/browser/supervised_user_settings_service.h"
#include "components/supervised_user/core/browser/supervised_user_url_filter.h"
#include "components/supervised_user/core/common/supervised_user_constants.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/supervised_user/test_support_jni_headers/SupervisedUserSettingsTestBridge_jni.h"

using base::android::JavaParamRef;

void JNI_SupervisedUserSettingsTestBridge_SetFilteringBehavior(JNIEnv* env,
                                                               Profile* profile,
                                                               jint setting) {
  supervised_user::SupervisedUserSettingsService*
      supervised_user_settings_service =
          SupervisedUserSettingsServiceFactory::GetForKey(
              profile->GetProfileKey());
  supervised_user_settings_service->SetLocalSetting(
      supervised_user::kContentPackDefaultFilteringBehavior,
      base::Value(setting));
}

void JNI_SupervisedUserSettingsTestBridge_SetManualFilterForHost(
    JNIEnv* env,
    Profile* profile,
    const JavaParamRef<jstring>& host,
    jboolean allowlist) {
  std::string host_string(base::android::ConvertJavaStringToUTF8(env, host));
  supervised_user_test_util::SetManualFilterForHost(profile, host_string,
                                                    allowlist);
}
class TestUrlLoaderFactoryHelper {
 public:
  static TestUrlLoaderFactoryHelper* SharedInstance() {
    return base::Singleton<TestUrlLoaderFactoryHelper>::get();
  }

  void SetUp() {
    test_url_loader_factory_ =
        std::make_unique<network::TestURLLoaderFactory>();
    shared_url_loader_factory_ =
        base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
            test_url_loader_factory_.get());
  }

  void TearDown() { test_url_loader_factory_.reset(); }

  scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory() {
    return std::ref(shared_url_loader_factory_);
  }
  network::TestURLLoaderFactory* test_url_loader_factory() {
    return test_url_loader_factory_.get();
  }

 private:
  std::unique_ptr<network::TestURLLoaderFactory> test_url_loader_factory_;
  scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;
};

void JNI_SupervisedUserSettingsTestBridge_SetKidsManagementResponseForTesting(  // IN-TEST
    JNIEnv* env,
    Profile* profile,
    jboolean is_allowed) {
  kidsmanagement::ClassifyUrlResponse response;
  auto url_classification =
      is_allowed ? kidsmanagement::ClassifyUrlResponse::ALLOWED
                 : kidsmanagement::ClassifyUrlResponse::RESTRICTED;

  response.set_display_classification(url_classification);
  std::string classify_url_service_url =
      "https://kidsmanagement-pa.googleapis.com/kidsmanagement/v1/people/"
      "me:classifyUrl?alt=proto";
  network::TestURLLoaderFactory* test_url_loader_factory_ =
      TestUrlLoaderFactoryHelper::SharedInstance()->test_url_loader_factory();
  scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory =
      TestUrlLoaderFactoryHelper::SharedInstance()->shared_url_loader_factory();

  test_url_loader_factory_->AddResponse(classify_url_service_url,
                                        response.SerializeAsString());

  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile);

  supervised_user::SupervisedUserService* supervised_user_service =
      SupervisedUserServiceFactory::GetForProfile(profile);

  std::unique_ptr<safe_search_api::URLCheckerClient> url_checker_client =
      std::make_unique<supervised_user::KidsChromeManagementURLCheckerClient>(
          identity_manager, shared_url_loader_factory, /*country=*/"",
          version_info::Channel::UNKNOWN);
  supervised_user_service->GetURLFilter()->SetURLCheckerClient(
      std::move(url_checker_client));
}

void JNI_SupervisedUserSettingsTestBridge_SetSafeSearchResponseForTesting(  // IN-TEST
    JNIEnv* env,
    Profile* profile,
    jboolean is_allowed) {
  TestUrlLoaderFactoryHelper::SharedInstance()
      ->test_url_loader_factory()
      ->AddResponse(
          "https://safesearch.googleapis.com/v1:classify",
          base::StringPrintf(R"json({"displayClassification": "%s"})json",
                             (is_allowed ? "allowed" : "restricted")));
}

void JNI_SupervisedUserSettingsTestBridge_SetUpTestUrlLoaderFactoryHelper(
    JNIEnv* env) {
  TestUrlLoaderFactoryHelper::SharedInstance()->SetUp();
}

void JNI_SupervisedUserSettingsTestBridge_TearDownTestUrlLoaderFactoryHelper(
    JNIEnv* env) {
  TestUrlLoaderFactoryHelper::SharedInstance()->TearDown();
}