1 // Copyright (c) 2013 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.
5 #include "android_webview/native/aw_settings.h"
7 #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h"
8 #include "android_webview/common/aw_content_client.h"
9 #include "android_webview/native/aw_contents.h"
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_string.h"
12 #include "base/macros.h"
13 #include "base/supports_user_data.h"
14 #include "content/public/browser/navigation_controller.h"
15 #include "content/public/browser/navigation_entry.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/renderer_preferences.h"
19 #include "content/public/common/web_preferences.h"
20 #include "jni/AwSettings_jni.h"
21 #include "ui/gfx/font_render_params.h"
23 using base::android::ConvertJavaStringToUTF16
;
24 using base::android::ConvertUTF8ToJavaString
;
25 using base::android::ScopedJavaLocalRef
;
26 using content::RendererPreferences
;
27 using content::WebPreferences
;
29 namespace android_webview
{
33 void PopulateFixedRendererPreferences(RendererPreferences
* prefs
) {
34 prefs
->tap_multiple_targets_strategy
=
35 content::TAP_MULTIPLE_TARGETS_STRATEGY_NONE
;
37 // TODO(boliu): Deduplicate with chrome/ code.
38 CR_DEFINE_STATIC_LOCAL(const gfx::FontRenderParams
, params
,
39 (gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(true), NULL
)));
40 prefs
->should_antialias_text
= params
.antialiasing
;
41 prefs
->use_subpixel_positioning
= params
.subpixel_positioning
;
42 prefs
->hinting
= params
.hinting
;
43 prefs
->use_autohinter
= params
.autohinter
;
44 prefs
->use_bitmaps
= params
.use_bitmaps
;
45 prefs
->subpixel_rendering
= params
.subpixel_rendering
;
48 void PopulateFixedWebPreferences(WebPreferences
* web_prefs
) {
49 web_prefs
->shrinks_standalone_images_to_fit
= false;
50 web_prefs
->should_clear_document_background
= false;
55 const void* kAwSettingsUserDataKey
= &kAwSettingsUserDataKey
;
57 class AwSettingsUserData
: public base::SupportsUserData::Data
{
59 AwSettingsUserData(AwSettings
* ptr
) : settings_(ptr
) {}
61 static AwSettings
* GetSettings(content::WebContents
* web_contents
) {
64 AwSettingsUserData
* data
= static_cast<AwSettingsUserData
*>(
65 web_contents
->GetUserData(kAwSettingsUserDataKey
));
66 return data
? data
->settings_
: NULL
;
70 AwSettings
* settings_
;
73 AwSettings::AwSettings(JNIEnv
* env
,
75 content::WebContents
* web_contents
)
76 : WebContentsObserver(web_contents
),
77 renderer_prefs_initialized_(false),
78 aw_settings_(env
, obj
) {
79 web_contents
->SetUserData(kAwSettingsUserDataKey
,
80 new AwSettingsUserData(this));
83 AwSettings::~AwSettings() {
85 web_contents()->SetUserData(kAwSettingsUserDataKey
, NULL
);
88 JNIEnv
* env
= base::android::AttachCurrentThread();
89 ScopedJavaLocalRef
<jobject
> scoped_obj
= aw_settings_
.get(env
);
90 jobject obj
= scoped_obj
.obj();
92 Java_AwSettings_nativeAwSettingsGone(env
, obj
,
93 reinterpret_cast<intptr_t>(this));
96 void AwSettings::Destroy(JNIEnv
* env
, jobject obj
) {
100 AwSettings
* AwSettings::FromWebContents(content::WebContents
* web_contents
) {
101 return AwSettingsUserData::GetSettings(web_contents
);
104 AwRenderViewHostExt
* AwSettings::GetAwRenderViewHostExt() {
105 if (!web_contents()) return NULL
;
106 AwContents
* contents
= AwContents::FromWebContents(web_contents());
107 if (!contents
) return NULL
;
108 return contents
->render_view_host_ext();
111 void AwSettings::ResetScrollAndScaleState(JNIEnv
* env
, jobject obj
) {
112 AwRenderViewHostExt
* rvhe
= GetAwRenderViewHostExt();
114 rvhe
->ResetScrollAndScaleState();
117 void AwSettings::UpdateEverything() {
118 JNIEnv
* env
= base::android::AttachCurrentThread();
120 ScopedJavaLocalRef
<jobject
> scoped_obj
= aw_settings_
.get(env
);
121 jobject obj
= scoped_obj
.obj();
123 // Grab the lock and call UpdateEverythingLocked.
124 Java_AwSettings_updateEverything(env
, obj
);
127 void AwSettings::UpdateEverythingLocked(JNIEnv
* env
, jobject obj
) {
128 UpdateInitialPageScaleLocked(env
, obj
);
129 UpdateWebkitPreferencesLocked(env
, obj
);
130 UpdateUserAgentLocked(env
, obj
);
131 ResetScrollAndScaleState(env
, obj
);
132 UpdateFormDataPreferencesLocked(env
, obj
);
133 UpdateRendererPreferencesLocked(env
, obj
);
136 void AwSettings::UpdateUserAgentLocked(JNIEnv
* env
, jobject obj
) {
137 if (!web_contents()) return;
139 ScopedJavaLocalRef
<jstring
> str
=
140 Java_AwSettings_getUserAgentLocked(env
, obj
);
141 bool ua_overidden
= str
.obj() != NULL
;
144 std::string override
= base::android::ConvertJavaStringToUTF8(str
);
145 web_contents()->SetUserAgentOverride(override
);
148 const content::NavigationController
& controller
=
149 web_contents()->GetController();
150 for (int i
= 0; i
< controller
.GetEntryCount(); ++i
)
151 controller
.GetEntryAtIndex(i
)->SetIsOverridingUserAgent(ua_overidden
);
154 void AwSettings::UpdateWebkitPreferencesLocked(JNIEnv
* env
, jobject obj
) {
155 if (!web_contents()) return;
156 AwRenderViewHostExt
* render_view_host_ext
= GetAwRenderViewHostExt();
157 if (!render_view_host_ext
) return;
159 content::RenderViewHost
* render_view_host
=
160 web_contents()->GetRenderViewHost();
161 if (!render_view_host
) return;
162 render_view_host
->OnWebkitPreferencesChanged();
165 void AwSettings::UpdateInitialPageScaleLocked(JNIEnv
* env
, jobject obj
) {
166 AwRenderViewHostExt
* rvhe
= GetAwRenderViewHostExt();
169 float initial_page_scale_percent
=
170 Java_AwSettings_getInitialPageScalePercentLocked(env
, obj
);
171 if (initial_page_scale_percent
== 0) {
172 rvhe
->SetInitialPageScale(-1);
174 float dip_scale
= static_cast<float>(
175 Java_AwSettings_getDIPScaleLocked(env
, obj
));
176 rvhe
->SetInitialPageScale(initial_page_scale_percent
/ dip_scale
/ 100.0f
);
180 void AwSettings::UpdateFormDataPreferencesLocked(JNIEnv
* env
, jobject obj
) {
181 if (!web_contents()) return;
182 AwContents
* contents
= AwContents::FromWebContents(web_contents());
183 if (!contents
) return;
185 contents
->SetSaveFormData(Java_AwSettings_getSaveFormDataLocked(env
, obj
));
188 void AwSettings::UpdateRendererPreferencesLocked(JNIEnv
* env
, jobject obj
) {
189 if (!web_contents()) return;
191 bool update_prefs
= false;
192 RendererPreferences
* prefs
= web_contents()->GetMutableRendererPrefs();
194 if (!renderer_prefs_initialized_
) {
195 PopulateFixedRendererPreferences(prefs
);
196 renderer_prefs_initialized_
= true;
201 Java_AwSettings_getVideoOverlayForEmbeddedVideoEnabledLocked(env
, obj
);
202 if (video_overlay
!= prefs
->use_video_overlay_for_embedded_encrypted_video
) {
203 prefs
->use_video_overlay_for_embedded_encrypted_video
= video_overlay
;
207 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
208 if (update_prefs
&& host
)
209 host
->SyncRendererPrefs();
212 void AwSettings::RenderViewCreated(content::RenderViewHost
* render_view_host
) {
213 // A single WebContents can normally have 0 to many RenderViewHost instances
214 // associated with it.
215 // This is important since there is only one RenderViewHostExt instance per
216 // WebContents (and not one RVHExt per RVH, as you might expect) and updating
217 // settings via RVHExt only ever updates the 'current' RVH.
218 // In android_webview we don't swap out the RVH on cross-site navigations, so
219 // we shouldn't have to deal with the multiple RVH per WebContents case. That
220 // in turn means that the newly created RVH is always the 'current' RVH
221 // (since we only ever go from 0 to 1 RVH instances) and hence the DCHECK.
222 DCHECK_EQ(render_view_host
, web_contents()->GetRenderViewHost());
227 void AwSettings::WebContentsDestroyed() {
231 void AwSettings::PopulateWebPreferences(WebPreferences
* web_prefs
) {
232 JNIEnv
* env
= base::android::AttachCurrentThread();
234 ScopedJavaLocalRef
<jobject
> scoped_obj
= aw_settings_
.get(env
);
235 jobject obj
= scoped_obj
.obj();
237 // Grab the lock and call PopulateWebPreferencesLocked.
238 Java_AwSettings_populateWebPreferences(
239 env
, obj
, reinterpret_cast<jlong
>(web_prefs
));
242 void AwSettings::PopulateWebPreferencesLocked(
243 JNIEnv
* env
, jobject obj
, jlong web_prefs_ptr
) {
244 AwRenderViewHostExt
* render_view_host_ext
= GetAwRenderViewHostExt();
245 if (!render_view_host_ext
) return;
247 WebPreferences
* web_prefs
= reinterpret_cast<WebPreferences
*>(web_prefs_ptr
);
248 PopulateFixedWebPreferences(web_prefs
);
250 web_prefs
->text_autosizing_enabled
=
251 Java_AwSettings_getTextAutosizingEnabledLocked(env
, obj
);
253 int text_size_percent
= Java_AwSettings_getTextSizePercentLocked(env
, obj
);
254 if (web_prefs
->text_autosizing_enabled
) {
255 web_prefs
->font_scale_factor
= text_size_percent
/ 100.0f
;
256 web_prefs
->force_enable_zoom
= text_size_percent
>= 130;
257 // Use the default zoom factor value when Text Autosizer is turned on.
258 render_view_host_ext
->SetTextZoomFactor(1);
260 web_prefs
->force_enable_zoom
= false;
261 render_view_host_ext
->SetTextZoomFactor(text_size_percent
/ 100.0f
);
264 web_prefs
->standard_font_family_map
[content::kCommonScript
] =
265 ConvertJavaStringToUTF16(
266 Java_AwSettings_getStandardFontFamilyLocked(env
, obj
));
268 web_prefs
->fixed_font_family_map
[content::kCommonScript
] =
269 ConvertJavaStringToUTF16(
270 Java_AwSettings_getFixedFontFamilyLocked(env
, obj
));
272 web_prefs
->sans_serif_font_family_map
[content::kCommonScript
] =
273 ConvertJavaStringToUTF16(
274 Java_AwSettings_getSansSerifFontFamilyLocked(env
, obj
));
276 web_prefs
->serif_font_family_map
[content::kCommonScript
] =
277 ConvertJavaStringToUTF16(
278 Java_AwSettings_getSerifFontFamilyLocked(env
, obj
));
280 web_prefs
->cursive_font_family_map
[content::kCommonScript
] =
281 ConvertJavaStringToUTF16(
282 Java_AwSettings_getCursiveFontFamilyLocked(env
, obj
));
284 web_prefs
->fantasy_font_family_map
[content::kCommonScript
] =
285 ConvertJavaStringToUTF16(
286 Java_AwSettings_getFantasyFontFamilyLocked(env
, obj
));
288 web_prefs
->default_encoding
= ConvertJavaStringToUTF8(
289 Java_AwSettings_getDefaultTextEncodingLocked(env
, obj
));
291 web_prefs
->minimum_font_size
=
292 Java_AwSettings_getMinimumFontSizeLocked(env
, obj
);
294 web_prefs
->minimum_logical_font_size
=
295 Java_AwSettings_getMinimumLogicalFontSizeLocked(env
, obj
);
297 web_prefs
->default_font_size
=
298 Java_AwSettings_getDefaultFontSizeLocked(env
, obj
);
300 web_prefs
->default_fixed_font_size
=
301 Java_AwSettings_getDefaultFixedFontSizeLocked(env
, obj
);
303 // Blink's LoadsImagesAutomatically and ImagesEnabled must be
304 // set cris-cross to Android's. See
305 // https://code.google.com/p/chromium/issues/detail?id=224317#c26
306 web_prefs
->loads_images_automatically
=
307 Java_AwSettings_getImagesEnabledLocked(env
, obj
);
308 web_prefs
->images_enabled
=
309 Java_AwSettings_getLoadsImagesAutomaticallyLocked(env
, obj
);
311 web_prefs
->javascript_enabled
=
312 Java_AwSettings_getJavaScriptEnabledLocked(env
, obj
);
314 web_prefs
->allow_universal_access_from_file_urls
=
315 Java_AwSettings_getAllowUniversalAccessFromFileURLsLocked(env
, obj
);
317 web_prefs
->allow_file_access_from_file_urls
=
318 Java_AwSettings_getAllowFileAccessFromFileURLsLocked(env
, obj
);
320 web_prefs
->javascript_can_open_windows_automatically
=
321 Java_AwSettings_getJavaScriptCanOpenWindowsAutomaticallyLocked(env
, obj
);
323 web_prefs
->supports_multiple_windows
=
324 Java_AwSettings_getSupportMultipleWindowsLocked(env
, obj
);
326 web_prefs
->plugins_enabled
=
327 !Java_AwSettings_getPluginsDisabledLocked(env
, obj
);
329 web_prefs
->application_cache_enabled
=
330 Java_AwSettings_getAppCacheEnabledLocked(env
, obj
);
332 web_prefs
->local_storage_enabled
=
333 Java_AwSettings_getDomStorageEnabledLocked(env
, obj
);
335 web_prefs
->databases_enabled
=
336 Java_AwSettings_getDatabaseEnabledLocked(env
, obj
);
338 web_prefs
->wide_viewport_quirk
= true;
339 web_prefs
->use_wide_viewport
=
340 Java_AwSettings_getUseWideViewportLocked(env
, obj
);
342 web_prefs
->force_zero_layout_height
=
343 Java_AwSettings_getForceZeroLayoutHeightLocked(env
, obj
);
345 const bool zero_layout_height_disables_viewport_quirk
=
346 Java_AwSettings_getZeroLayoutHeightDisablesViewportQuirkLocked(env
, obj
);
347 web_prefs
->viewport_enabled
= !(zero_layout_height_disables_viewport_quirk
&&
348 web_prefs
->force_zero_layout_height
);
350 web_prefs
->double_tap_to_zoom_enabled
=
351 Java_AwSettings_supportsDoubleTapZoomLocked(env
, obj
);
353 web_prefs
->initialize_at_minimum_page_scale
=
354 Java_AwSettings_getLoadWithOverviewModeLocked(env
, obj
);
356 web_prefs
->user_gesture_required_for_media_playback
=
357 Java_AwSettings_getMediaPlaybackRequiresUserGestureLocked(env
, obj
);
359 ScopedJavaLocalRef
<jstring
> url
=
360 Java_AwSettings_getDefaultVideoPosterURLLocked(env
, obj
);
361 web_prefs
->default_video_poster_url
= url
.obj() ?
362 GURL(ConvertJavaStringToUTF8(url
)) : GURL();
364 bool support_quirks
= Java_AwSettings_getSupportLegacyQuirksLocked(env
, obj
);
365 // Please see the corresponding Blink settings for bug references.
366 web_prefs
->support_deprecated_target_density_dpi
= support_quirks
;
367 web_prefs
->use_legacy_background_size_shorthand_behavior
= support_quirks
;
368 web_prefs
->viewport_meta_layout_size_quirk
= support_quirks
;
369 web_prefs
->viewport_meta_merge_content_quirk
= support_quirks
;
370 web_prefs
->viewport_meta_non_user_scalable_quirk
= support_quirks
;
371 web_prefs
->viewport_meta_zero_values_quirk
= support_quirks
;
372 web_prefs
->clobber_user_agent_initial_scale_quirk
= support_quirks
;
373 web_prefs
->ignore_main_frame_overflow_hidden_quirk
= support_quirks
;
374 web_prefs
->report_screen_size_in_physical_pixels_quirk
= support_quirks
;
376 web_prefs
->password_echo_enabled
=
377 Java_AwSettings_getPasswordEchoEnabledLocked(env
, obj
);
378 web_prefs
->spatial_navigation_enabled
=
379 Java_AwSettings_getSpatialNavigationLocked(env
, obj
);
381 bool enable_supported_hardware_accelerated_features
=
382 Java_AwSettings_getEnableSupportedHardwareAcceleratedFeaturesLocked(
385 bool accelerated_2d_canvas_enabled_by_switch
=
386 web_prefs
->accelerated_2d_canvas_enabled
;
387 web_prefs
->accelerated_2d_canvas_enabled
= true;
388 if (!accelerated_2d_canvas_enabled_by_switch
||
389 !enable_supported_hardware_accelerated_features
) {
390 // Any canvas smaller than this will fallback to software. Abusing this
391 // slightly to turn canvas off without changing
392 // accelerated_2d_canvas_enabled, which also affects compositing mode.
393 // Using 100M instead of max int to avoid overflows.
394 web_prefs
->minimum_accelerated_2d_canvas_size
= 100 * 1000 * 1000;
396 web_prefs
->experimental_webgl_enabled
=
397 web_prefs
->experimental_webgl_enabled
&&
398 enable_supported_hardware_accelerated_features
;
400 web_prefs
->allow_displaying_insecure_content
=
401 Java_AwSettings_getAllowDisplayingInsecureContentLocked(env
, obj
);
402 web_prefs
->allow_running_insecure_content
=
403 Java_AwSettings_getAllowRunningInsecureContentLocked(env
, obj
);
405 web_prefs
->fullscreen_supported
=
406 Java_AwSettings_getFullscreenSupportedLocked(env
, obj
);
409 static jlong
Init(JNIEnv
* env
,
411 jobject web_contents
) {
412 content::WebContents
* contents
= content::WebContents::FromJavaWebContents(
414 AwSettings
* settings
= new AwSettings(env
, obj
, contents
);
415 return reinterpret_cast<intptr_t>(settings
);
418 static jstring
GetDefaultUserAgent(JNIEnv
* env
, jclass clazz
) {
419 return base::android::ConvertUTF8ToJavaString(env
, GetUserAgent()).Release();
422 bool RegisterAwSettings(JNIEnv
* env
) {
423 return RegisterNativesImpl(env
);
426 } // namespace android_webview