// Copyright 2018 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 "content/browser/accessibility/captioning_controller.h" #include "base/android/jni_string.h" #include "content/browser/web_contents/web_contents_impl.h" #include "content/common/frame_messages.h" #include "content/public/android/content_jni_headers/CaptioningController_jni.h" #include "content/public/common/web_preferences.h" using base::android::AttachCurrentThread; using base::android::ConvertJavaStringToUTF8; using base::android::JavaParamRef; using base::android::JavaRef; using base::android::ScopedJavaLocalRef; namespace content { namespace { int GetRenderProcessIdFromRenderViewHost(RenderViewHost* host) { DCHECK(host); RenderProcessHost* render_process = host->GetProcess(); DCHECK(render_process); if (render_process->IsInitializedAndNotDead()) return render_process->GetProcess().Handle(); return 0; } // Adds !important to all captions styles. They should always override any // styles added by the video author or by a user stylesheet. This is because in // Chrome, there is an option to turn off captions styles, so any time the // captions are on, the styles should take priority. std::string AddCSSImportant(std::string css_string) { return css_string + " !important"; } } // namespace CaptioningController::CaptioningController(JNIEnv* env, const JavaRef& obj, WebContents* web_contents) : WebContentsObserver(web_contents), java_ref_(env, obj) {} CaptioningController::~CaptioningController() { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (!obj.is_null()) Java_CaptioningController_onDestroy(env, obj); } void CaptioningController::RenderViewReady() { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (!obj.is_null()) Java_CaptioningController_onRenderProcessChange(env, obj); } void CaptioningController::RenderViewHostChanged(RenderViewHost* old_host, RenderViewHost* new_host) { int old_pid = 0; if (old_host) { old_pid = GetRenderProcessIdFromRenderViewHost(old_host); } int new_pid = GetRenderProcessIdFromRenderViewHost(web_contents()->GetRenderViewHost()); if (new_pid != old_pid) { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (!obj.is_null()) Java_CaptioningController_onRenderProcessChange(env, obj); } } void CaptioningController::WebContentsDestroyed() { delete this; } void CaptioningController::SetTextTrackSettings( JNIEnv* env, const JavaParamRef& obj, jboolean textTracksEnabled, const JavaParamRef& textTrackBackgroundColor, const JavaParamRef& textTrackFontFamily, const JavaParamRef& textTrackFontStyle, const JavaParamRef& textTrackFontVariant, const JavaParamRef& textTrackTextColor, const JavaParamRef& textTrackTextShadow, const JavaParamRef& textTrackTextSize) { auto web_prefs = web_contents() ->GetMainFrame() ->GetRenderViewHost() ->GetWebkitPreferences(); web_prefs.text_tracks_enabled = textTracksEnabled; web_prefs.text_track_background_color = AddCSSImportant(ConvertJavaStringToUTF8(env, textTrackBackgroundColor)); web_prefs.text_track_font_family = AddCSSImportant(ConvertJavaStringToUTF8(env, textTrackFontFamily)); web_prefs.text_track_font_style = AddCSSImportant(ConvertJavaStringToUTF8(env, textTrackFontStyle)); web_prefs.text_track_font_variant = AddCSSImportant(ConvertJavaStringToUTF8(env, textTrackFontVariant)); web_prefs.text_track_text_color = AddCSSImportant(ConvertJavaStringToUTF8(env, textTrackTextColor)); web_prefs.text_track_text_shadow = AddCSSImportant(ConvertJavaStringToUTF8(env, textTrackTextShadow)); web_prefs.text_track_text_size = AddCSSImportant(ConvertJavaStringToUTF8(env, textTrackTextSize)); web_contents()->GetMainFrame()->GetRenderViewHost()->UpdateWebkitPreferences( web_prefs); } jlong JNI_CaptioningController_Init( JNIEnv* env, const JavaParamRef& obj, const JavaParamRef& jweb_contents) { WebContentsImpl* web_contents = static_cast( WebContents::FromJavaWebContents(jweb_contents)); CHECK(web_contents); return reinterpret_cast( new CaptioningController(env, obj, web_contents)); } } // namespace content