| // Copyright 2017 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "content/browser/android/javascript_injector.h" | |
| #include <vector> | |
| #include "base/android/jni_string.h" | |
| #include "base/memory/ptr_util.h" | |
| #include "base/memory/scoped_refptr.h" | |
| #include "components/origin_matcher/origin_matcher.h" | |
| #include "content/browser/android/java/gin_java_bridge_dispatcher_host.h" | |
| #include "content/browser/preloading/prerender/prerender_final_status.h" | |
| #include "content/browser/preloading/prerender/prerender_host_registry.h" | |
| #include "content/browser/web_contents/web_contents_impl.h" | |
| #include "content/public/browser/web_contents.h" | |
| // Must come after all headers that specialize FromJniType() / ToJniType(). | |
| #include "content/public/android/content_jni_headers/JavascriptInjectorImpl_jni.h" | |
| using base::android::AttachCurrentThread; | |
| using base::android::ConvertJavaStringToUTF8; | |
| using base::android::JavaRef; | |
| using base::android::ScopedJavaLocalRef; | |
| namespace content { | |
| JavascriptInjector::JavascriptInjector( | |
| JNIEnv* env, | |
| const base::android::JavaRef<jobject>& obj, | |
| const base::android::JavaRef<jobject>& retained_objects, | |
| WebContents* web_contents) | |
| : WebContentsUserData<JavascriptInjector>(*web_contents), | |
| java_ref_(env, obj) { | |
| java_bridge_dispatcher_host_ = | |
| base::MakeRefCounted<GinJavaBridgeDispatcherHost>(web_contents, | |
| retained_objects); | |
| web_contents->SetUserData(UserDataKey(), base::WrapUnique(this)); | |
| } | |
| JavascriptInjector::~JavascriptInjector() { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); | |
| if (j_obj.is_null()) | |
| return; | |
| Java_JavascriptInjectorImpl_onDestroy(env, j_obj); | |
| } | |
| void JavascriptInjector::SetAllowInspection(JNIEnv* env, bool allow) { | |
| CHECK(java_bridge_dispatcher_host_, base::NotFatalUntil::M159); | |
| java_bridge_dispatcher_host_->SetAllowObjectContentsInspection(allow); | |
| } | |
| void JavascriptInjector::AddInterface( | |
| JNIEnv* env, | |
| const JavaRef<jobject>& object, | |
| const JavaRef<jstring>& name, | |
| const JavaRef<jclass>& safe_annotation_clazz, | |
| origin_matcher::OriginMatcher matcher) { | |
| CHECK(java_bridge_dispatcher_host_, base::NotFatalUntil::M159); | |
| // If a new js object is added or removed when a page is in BFCache or | |
| // prerendered, the change won't apply after activating the page. To avoid | |
| // this behavior difference when these features are involved vs not, | |
| // evict all BFCached and prerendered pages so that we won't activate any | |
| // pages that don't have this object modified. | |
| // Same for RemoveInterface below. | |
| GetWebContents().GetController().GetBackForwardCache().Flush( | |
| content::BackForwardCache::NotRestoredReason:: | |
| kWebViewJavaScriptObjectChanged); | |
| GetWebContentsImpl().GetPrerenderHostRegistry()->CancelAllHosts( | |
| PrerenderFinalStatus::kJavaScriptInterfaceAdded); | |
| java_bridge_dispatcher_host_->AddNamedObject( | |
| ConvertJavaStringToUTF8(env, name), object, safe_annotation_clazz, | |
| std::move(matcher)); | |
| } | |
| void JavascriptInjector::RemoveInterface(JNIEnv* env, | |
| const JavaRef<jstring>& name) { | |
| CHECK(java_bridge_dispatcher_host_, base::NotFatalUntil::M159); | |
| GetWebContents().GetController().GetBackForwardCache().Flush( | |
| content::BackForwardCache::NotRestoredReason:: | |
| kWebViewJavaScriptObjectChanged); | |
| GetWebContentsImpl().GetPrerenderHostRegistry()->CancelAllHosts( | |
| PrerenderFinalStatus::kJavaScriptInterfaceRemoved); | |
| java_bridge_dispatcher_host_->RemoveNamedObject( | |
| ConvertJavaStringToUTF8(env, name)); | |
| } | |
| WebContentsImpl& JavascriptInjector::GetWebContentsImpl() { | |
| return static_cast<WebContentsImpl&>(GetWebContents()); | |
| } | |
| static void JNI_JavascriptInjectorImpl_Init( | |
| JNIEnv* env, | |
| const JavaRef<jobject>& obj, | |
| const JavaRef<jobject>& jweb_contents, | |
| const JavaRef<jobject>& retained_objects) { | |
| auto* web_contents = WebContents::FromJavaWebContents(jweb_contents); | |
| CHECK(web_contents) << "Should be created with a valid WebContents."; | |
| CHECK(!JavascriptInjector::FromWebContents(web_contents), | |
| base::NotFatalUntil::M159); | |
| // Owned by |web_contents|. | |
| new JavascriptInjector(env, obj, retained_objects, web_contents); | |
| } | |
| static void JNI_JavascriptInjectorImpl_SetAllowInspection( | |
| JNIEnv* env, | |
| WebContents* web_contents, | |
| bool allow) { | |
| if (!web_contents) { | |
| return; | |
| } | |
| if (auto* injector = JavascriptInjector::FromWebContents(web_contents)) { | |
| injector->SetAllowInspection(env, allow); | |
| } | |
| } | |
| static void JNI_JavascriptInjectorImpl_AddInterface( | |
| JNIEnv* env, | |
| WebContents* web_contents, | |
| const JavaRef<jobject>& object, | |
| const JavaRef<jstring>& name, | |
| const JavaRef<jclass>& safe_annotation_clazz, | |
| origin_matcher::OriginMatcher matcher) { | |
| if (!web_contents) { | |
| return; | |
| } | |
| if (auto* injector = JavascriptInjector::FromWebContents(web_contents)) { | |
| injector->AddInterface(env, object, name, safe_annotation_clazz, | |
| std::move(matcher)); | |
| } | |
| } | |
| static void JNI_JavascriptInjectorImpl_RemoveInterface( | |
| JNIEnv* env, | |
| WebContents* web_contents, | |
| const JavaRef<jstring>& name) { | |
| if (!web_contents) { | |
| return; | |
| } | |
| if (auto* injector = JavascriptInjector::FromWebContents(web_contents)) { | |
| injector->RemoveInterface(env, name); | |
| } | |
| } | |
| WEB_CONTENTS_USER_DATA_KEY_IMPL(JavascriptInjector); | |
| } // namespace content | |
| DEFINE_JNI(JavascriptInjectorImpl) |