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
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/supervised_user/supervised_user_content_provider_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/supervised_user/supervised_user_interstitial.h"
#include "chrome/browser/supervised_user/supervised_user_service.h"
#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
#include "jni/SupervisedUserContentProvider_jni.h"
using base::android::JavaRef;
using base::android::JavaParamRef;
using base::android::ScopedJavaGlobalRef;
using base::android::AttachCurrentThread;
namespace {
class UrlFilterObserver : public SupervisedUserURLFilter::Observer {
public:
UrlFilterObserver(JNIEnv* env,
const ScopedJavaGlobalRef<jobject>& java_content_provider)
: java_content_provider_(java_content_provider) {}
virtual ~UrlFilterObserver() {}
private:
void OnSiteListUpdated() override {
Java_SupervisedUserContentProvider_onSupervisedUserFilterUpdated(
AttachCurrentThread(), java_content_provider_.obj());
}
ScopedJavaGlobalRef<jobject> java_content_provider_;
};
} // namespace
static jlong CreateSupervisedUserContentProvider(
JNIEnv* env,
const JavaParamRef<jobject>& caller) {
return reinterpret_cast<intptr_t>(
new SupervisedUserContentProvider(env, caller));
}
SupervisedUserContentProvider::SupervisedUserContentProvider(
JNIEnv* env,
const JavaParamRef<jobject>& caller)
: profile_(ProfileManager::GetLastUsedProfile()),
java_content_provider_(env, caller),
weak_factory_(this) {
if (profile_->IsSupervised()) {
SupervisedUserService* supervised_user_service =
SupervisedUserServiceFactory::GetForProfile(profile_);
SupervisedUserURLFilter* url_filter =
supervised_user_service->GetURLFilterForUIThread();
url_filter->AddObserver(new UrlFilterObserver(env, java_content_provider_));
}
}
SupervisedUserContentProvider::~SupervisedUserContentProvider() {}
void SupervisedUserContentProvider::ShouldProceed(
JNIEnv* env,
const JavaParamRef<jobject>& caller,
const JavaParamRef<jobject>& query_result_jobj,
const JavaParamRef<jstring>& url) {
if (!profile_->IsSupervised()) {
// User isn't supervised
Java_SupervisedUserQueryReply_onQueryComplete(env, query_result_jobj.obj(),
true, nullptr);
return;
}
SupervisedUserService* supervised_user_service =
SupervisedUserServiceFactory::GetForProfile(profile_);
SupervisedUserURLFilter* url_filter =
supervised_user_service->GetURLFilterForUIThread();
url_filter->GetFilteringBehaviorForURLWithAsyncChecks(
GURL(base::android::ConvertJavaStringToUTF16(env, url)),
base::Bind(&SupervisedUserContentProvider::OnQueryComplete,
weak_factory_.GetWeakPtr(),
ScopedJavaGlobalRef<jobject>(env, query_result_jobj.obj())));
}
void SupervisedUserContentProvider::RequestInsert(
JNIEnv* env,
const JavaParamRef<jobject>& caller,
const JavaParamRef<jobject>& insert_result_jobj,
const JavaParamRef<jstring>& url) {
if (!profile_->IsSupervised())
return;
SupervisedUserService* supervised_user_service =
SupervisedUserServiceFactory::GetForProfile(profile_);
supervised_user_service->AddURLAccessRequest(
GURL(base::android::ConvertJavaStringToUTF16(env, url)),
base::Bind(&SupervisedUserContentProvider::OnInsertRequestSendComplete,
weak_factory_.GetWeakPtr(),
ScopedJavaGlobalRef<jobject>(env, insert_result_jobj.obj())));
}
void SupervisedUserContentProvider::OnQueryComplete(
ScopedJavaGlobalRef<jobject> query_reply_jobj,
SupervisedUserURLFilter::FilteringBehavior behavior,
SupervisedUserURLFilter::FilteringBehaviorReason reason,
bool /* uncertain */) {
if (behavior != SupervisedUserURLFilter::BLOCK) {
Java_SupervisedUserQueryReply_onQueryComplete(
AttachCurrentThread(), query_reply_jobj.obj(), true, nullptr);
} else {
JNIEnv* env = AttachCurrentThread();
Java_SupervisedUserQueryReply_onQueryComplete(
env, query_reply_jobj.obj(), false,
base::android::ConvertUTF8ToJavaString(
env, SupervisedUserInterstitial::GetHTMLContents(profile_, reason))
.obj());
}
}
void SupervisedUserContentProvider::SetFilterForTesting(JNIEnv* env,
jobject caller) {
if (!profile_->IsSupervised())
return;
SupervisedUserService* supervised_user_service =
SupervisedUserServiceFactory::GetForProfile(profile_);
SupervisedUserURLFilter* url_filter =
supervised_user_service->GetURLFilterForUIThread();
url_filter->SetDefaultFilteringBehavior(SupervisedUserURLFilter::BLOCK);
}
void SupervisedUserContentProvider::OnInsertRequestSendComplete(
ScopedJavaGlobalRef<jobject> insert_reply_jobj,
bool sent_ok) {
Java_SupervisedUserInsertReply_onInsertRequestSendComplete(
AttachCurrentThread(), insert_reply_jobj.obj(), sent_ok);
}
bool SupervisedUserContentProvider::Register(JNIEnv* env) {
return RegisterNativesImpl(env);
}
|