OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/supervised_user/supervised_user_content_provider_androi d.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/browser/profiles/profile_manager.h" | |
11 #include "chrome/browser/supervised_user/supervised_user_interstitial.h" | |
12 #include "chrome/browser/supervised_user/supervised_user_service.h" | |
13 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | |
14 #include "jni/SupervisedUserContentProvider_jni.h" | |
15 | |
16 using base::android::JavaRef; | |
17 using base::android::JavaParamRef; | |
18 using base::android::ScopedJavaGlobalRef; | |
19 using base::android::AttachCurrentThread; | |
20 | |
21 namespace { | |
22 | |
23 class UrlFilterObserver : public SupervisedUserURLFilter::Observer { | |
24 public: | |
25 UrlFilterObserver(JNIEnv* env, | |
26 const ScopedJavaGlobalRef<jobject>& java_content_provider) | |
27 : java_content_provider_(java_content_provider) {} | |
28 | |
29 virtual ~UrlFilterObserver() {} | |
30 | |
31 private: | |
32 void OnSiteListUpdated() override { | |
33 Java_SupervisedUserContentProvider_onSupervisedUserFilterUpdated( | |
34 AttachCurrentThread(), java_content_provider_.obj()); | |
35 } | |
36 ScopedJavaGlobalRef<jobject> java_content_provider_; | |
37 }; | |
38 | |
39 void DoNothingCallback(bool /*b*/) {} | |
Bernhard Bauer
2015/12/14 17:58:38
This is available as base::DoNothing() (in base/bi
aberent
2015/12/16 15:20:37
No it isn't. Base::DoNothing() doesn't take an arg
aberent
2015/12/16 15:22:54
Actually no longer does nothing (hence renamed), s
| |
40 | |
41 } // namespace | |
42 | |
43 static jlong CreateSupervisedUserContentProvider( | |
44 JNIEnv* env, | |
45 const JavaParamRef<jobject>& caller) { | |
46 return reinterpret_cast<intptr_t>( | |
47 new SupervisedUserContentProvider(env, caller)); | |
48 } | |
49 | |
50 SupervisedUserContentProvider::SupervisedUserContentProvider( | |
51 JNIEnv* env, | |
52 const JavaParamRef<jobject>& caller) | |
53 : profile_(ProfileManager::GetLastUsedProfile()), | |
54 java_content_provider_(env, caller), | |
55 weak_factory_(this) { | |
56 if (profile_->IsSupervised()) { | |
57 SupervisedUserService* supervised_user_service = | |
58 SupervisedUserServiceFactory::GetForProfile(profile_); | |
59 SupervisedUserURLFilter* url_filter = | |
60 supervised_user_service->GetURLFilterForUIThread(); | |
61 url_filter->AddObserver(new UrlFilterObserver(env, java_content_provider_)); | |
62 } | |
63 } | |
64 | |
65 SupervisedUserContentProvider::~SupervisedUserContentProvider() {} | |
66 | |
67 void SupervisedUserContentProvider::ShouldProceed( | |
68 JNIEnv* env, | |
69 jobject caller, | |
70 const JavaParamRef<jstring>& url) { | |
71 if (!profile_->IsSupervised()) { | |
72 // User isn't supervised | |
73 Java_SupervisedUserContentProvider_onQueryComplete( | |
74 AttachCurrentThread(), java_content_provider_.obj(), true, nullptr); | |
75 return; | |
76 } | |
77 SupervisedUserService* supervised_user_service = | |
78 SupervisedUserServiceFactory::GetForProfile(profile_); | |
79 SupervisedUserURLFilter* url_filter = | |
80 supervised_user_service->GetURLFilterForUIThread(); | |
81 url_filter->GetFilteringBehaviorForURLWithAsyncChecks( | |
82 GURL(base::android::ConvertJavaStringToUTF16(env, url)), | |
83 base::Bind(&SupervisedUserContentProvider::OnQueryComplete, | |
84 weak_factory_.GetWeakPtr())); | |
85 } | |
86 | |
87 void SupervisedUserContentProvider::RequestInsert( | |
88 JNIEnv* env, | |
89 jobject caller, | |
90 const JavaParamRef<jstring>& url) { | |
91 if (!profile_->IsSupervised()) | |
92 return; | |
93 SupervisedUserService* supervised_user_service = | |
94 SupervisedUserServiceFactory::GetForProfile(profile_); | |
95 supervised_user_service->AddURLAccessRequest( | |
96 GURL(base::android::ConvertJavaStringToUTF16(env, url)), | |
97 base::Bind(DoNothingCallback)); | |
Bernhard Bauer
2015/12/14 17:58:37
Hm... in principle we could return an error if cre
aberent
2015/12/16 15:20:37
Done.
| |
98 } | |
99 | |
100 void SupervisedUserContentProvider::OnQueryComplete( | |
101 SupervisedUserURLFilter::FilteringBehavior behavior, | |
102 SupervisedUserURLFilter::FilteringBehaviorReason reason, | |
103 bool /* uncertain */) { | |
104 if (behavior != SupervisedUserURLFilter::BLOCK) { | |
105 Java_SupervisedUserContentProvider_onQueryComplete( | |
106 AttachCurrentThread(), java_content_provider_.obj(), true, nullptr); | |
107 } else { | |
108 JNIEnv* env = AttachCurrentThread(); | |
109 Java_SupervisedUserContentProvider_onQueryComplete( | |
110 env, java_content_provider_.obj(), false, | |
111 base::android::ConvertUTF8ToJavaString( | |
112 env, SupervisedUserInterstitial::GetHTMLContents(profile_, reason)) | |
113 .obj()); | |
114 } | |
115 } | |
116 | |
117 void SupervisedUserContentProvider::SetFilterForTesting(JNIEnv* env, | |
118 jobject caller) { | |
119 if (!profile_->IsSupervised()) { | |
Bernhard Bauer
2015/12/14 17:58:37
Leave out the braces.
aberent
2015/12/16 15:20:37
Done.
| |
120 return; | |
121 } | |
122 SupervisedUserService* supervised_user_service = | |
123 SupervisedUserServiceFactory::GetForProfile(profile_); | |
124 SupervisedUserURLFilter* url_filter = | |
125 supervised_user_service->GetURLFilterForUIThread(); | |
126 url_filter->SetDefaultFilteringBehavior(SupervisedUserURLFilter::BLOCK); | |
127 } | |
128 | |
129 bool RegisterSupervisedUserContentProvider(JNIEnv* env) { | |
130 return RegisterNativesImpl(env); | |
131 } | |
OLD | NEW |