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 } // namespace |
| 40 |
| 41 static jlong CreateSupervisedUserContentProvider( |
| 42 JNIEnv* env, |
| 43 const JavaParamRef<jobject>& caller) { |
| 44 return reinterpret_cast<intptr_t>( |
| 45 new SupervisedUserContentProvider(env, caller)); |
| 46 } |
| 47 |
| 48 SupervisedUserContentProvider::SupervisedUserContentProvider( |
| 49 JNIEnv* env, |
| 50 const JavaParamRef<jobject>& caller) |
| 51 : profile_(ProfileManager::GetLastUsedProfile()), |
| 52 java_content_provider_(env, caller), |
| 53 weak_factory_(this) { |
| 54 if (profile_->IsSupervised()) { |
| 55 SupervisedUserService* supervised_user_service = |
| 56 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 57 SupervisedUserURLFilter* url_filter = |
| 58 supervised_user_service->GetURLFilterForUIThread(); |
| 59 url_filter->AddObserver(new UrlFilterObserver(env, java_content_provider_)); |
| 60 } |
| 61 } |
| 62 |
| 63 SupervisedUserContentProvider::~SupervisedUserContentProvider() {} |
| 64 |
| 65 void SupervisedUserContentProvider::ShouldProceed( |
| 66 JNIEnv* env, |
| 67 jobject caller, |
| 68 const JavaParamRef<jstring>& url) { |
| 69 if (!profile_->IsSupervised()) { |
| 70 // User isn't supervised |
| 71 Java_SupervisedUserContentProvider_onQueryComplete( |
| 72 AttachCurrentThread(), java_content_provider_.obj(), true, nullptr); |
| 73 return; |
| 74 } |
| 75 SupervisedUserService* supervised_user_service = |
| 76 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 77 SupervisedUserURLFilter* url_filter = |
| 78 supervised_user_service->GetURLFilterForUIThread(); |
| 79 url_filter->GetFilteringBehaviorForURLWithAsyncChecks( |
| 80 GURL(base::android::ConvertJavaStringToUTF16(env, url)), |
| 81 base::Bind(&SupervisedUserContentProvider::OnQueryComplete, |
| 82 weak_factory_.GetWeakPtr())); |
| 83 } |
| 84 |
| 85 void SupervisedUserContentProvider::RequestInsert( |
| 86 JNIEnv* env, |
| 87 jobject caller, |
| 88 const JavaParamRef<jstring>& url) { |
| 89 if (!profile_->IsSupervised()) |
| 90 return; |
| 91 SupervisedUserService* supervised_user_service = |
| 92 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 93 supervised_user_service->AddURLAccessRequest( |
| 94 GURL(base::android::ConvertJavaStringToUTF16(env, url)), |
| 95 base::Bind(&SupervisedUserContentProvider::OnInsertRequestSendComplete, |
| 96 weak_factory_.GetWeakPtr())); |
| 97 } |
| 98 |
| 99 void SupervisedUserContentProvider::OnQueryComplete( |
| 100 SupervisedUserURLFilter::FilteringBehavior behavior, |
| 101 SupervisedUserURLFilter::FilteringBehaviorReason reason, |
| 102 bool /* uncertain */) { |
| 103 if (behavior != SupervisedUserURLFilter::BLOCK) { |
| 104 Java_SupervisedUserContentProvider_onQueryComplete( |
| 105 AttachCurrentThread(), java_content_provider_.obj(), true, nullptr); |
| 106 } else { |
| 107 JNIEnv* env = AttachCurrentThread(); |
| 108 Java_SupervisedUserContentProvider_onQueryComplete( |
| 109 env, java_content_provider_.obj(), false, |
| 110 base::android::ConvertUTF8ToJavaString( |
| 111 env, SupervisedUserInterstitial::GetHTMLContents(profile_, reason)) |
| 112 .obj()); |
| 113 } |
| 114 } |
| 115 |
| 116 void SupervisedUserContentProvider::SetFilterForTesting(JNIEnv* env, |
| 117 jobject caller) { |
| 118 if (!profile_->IsSupervised()) |
| 119 return; |
| 120 SupervisedUserService* supervised_user_service = |
| 121 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 122 SupervisedUserURLFilter* url_filter = |
| 123 supervised_user_service->GetURLFilterForUIThread(); |
| 124 url_filter->SetDefaultFilteringBehavior(SupervisedUserURLFilter::BLOCK); |
| 125 } |
| 126 |
| 127 void SupervisedUserContentProvider::OnInsertRequestSendComplete(bool sentOk) { |
| 128 Java_SupervisedUserContentProvider_onInsertRequestSendComplete( |
| 129 AttachCurrentThread(), java_content_provider_.obj(), sentOk); |
| 130 } |
| 131 |
| 132 bool SupervisedUserContentProvider::Register(JNIEnv* env) { |
| 133 return RegisterNativesImpl(env); |
| 134 } |
OLD | NEW |