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/native/aw_contents.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/command_line.h"
12 #include "base/supports_user_data.h"
13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/content_client.h"
18 #include "content/public/common/content_switches.h"
19 #include "jni/AwSettings_jni.h"
20 #include "webkit/common/user_agent/user_agent.h"
21 #include "webkit/common/webpreferences.h"
23 using base::android::ConvertJavaStringToUTF16
;
24 using base::android::ConvertUTF8ToJavaString
;
25 using base::android::ScopedJavaLocalRef
;
27 namespace android_webview
{
29 const void* kAwSettingsUserDataKey
= &kAwSettingsUserDataKey
;
31 class AwSettingsUserData
: public base::SupportsUserData::Data
{
33 AwSettingsUserData(AwSettings
* ptr
) : settings_(ptr
) {}
35 static AwSettings
* GetSettings(content::WebContents
* web_contents
) {
38 AwSettingsUserData
* data
= reinterpret_cast<AwSettingsUserData
*>(
39 web_contents
->GetUserData(kAwSettingsUserDataKey
));
40 return data
? data
->settings_
: NULL
;
44 AwSettings
* settings_
;
47 AwSettings::AwSettings(JNIEnv
* env
, jobject obj
, jlong web_contents
)
48 : WebContentsObserver(
49 reinterpret_cast<content::WebContents
*>(web_contents
)),
50 accelerated_2d_canvas_disabled_by_switch_(
51 CommandLine::ForCurrentProcess()->HasSwitch(
52 switches::kDisableAccelerated2dCanvas
)),
53 aw_settings_(env
, obj
) {
54 reinterpret_cast<content::WebContents
*>(web_contents
)->
55 SetUserData(kAwSettingsUserDataKey
, new AwSettingsUserData(this));
58 AwSettings::~AwSettings() {
60 web_contents()->SetUserData(kAwSettingsUserDataKey
, NULL
);
63 JNIEnv
* env
= base::android::AttachCurrentThread();
64 ScopedJavaLocalRef
<jobject
> scoped_obj
= aw_settings_
.get(env
);
65 jobject obj
= scoped_obj
.obj();
67 Java_AwSettings_nativeAwSettingsGone(env
, obj
,
68 reinterpret_cast<intptr_t>(this));
71 void AwSettings::Destroy(JNIEnv
* env
, jobject obj
) {
75 AwSettings
* AwSettings::FromWebContents(content::WebContents
* web_contents
) {
76 return AwSettingsUserData::GetSettings(web_contents
);
79 AwRenderViewHostExt
* AwSettings::GetAwRenderViewHostExt() {
80 if (!web_contents()) return NULL
;
81 AwContents
* contents
= AwContents::FromWebContents(web_contents());
82 if (!contents
) return NULL
;
83 return contents
->render_view_host_ext();
86 void AwSettings::ResetScrollAndScaleState(JNIEnv
* env
, jobject obj
) {
87 AwRenderViewHostExt
* rvhe
= GetAwRenderViewHostExt();
89 rvhe
->ResetScrollAndScaleState();
92 void AwSettings::UpdateEverything() {
93 JNIEnv
* env
= base::android::AttachCurrentThread();
95 ScopedJavaLocalRef
<jobject
> scoped_obj
= aw_settings_
.get(env
);
96 jobject obj
= scoped_obj
.obj();
98 // Grab the lock and call UpdateEverythingLocked.
99 Java_AwSettings_updateEverything(env
, obj
);
102 void AwSettings::UpdateEverythingLocked(JNIEnv
* env
, jobject obj
) {
103 UpdateInitialPageScaleLocked(env
, obj
);
104 UpdateWebkitPreferencesLocked(env
, obj
);
105 UpdateUserAgentLocked(env
, obj
);
106 ResetScrollAndScaleState(env
, obj
);
107 UpdateFormDataPreferencesLocked(env
, obj
);
110 void AwSettings::UpdateUserAgentLocked(JNIEnv
* env
, jobject obj
) {
111 if (!web_contents()) return;
113 ScopedJavaLocalRef
<jstring
> str
=
114 Java_AwSettings_getUserAgentLocked(env
, obj
);
115 bool ua_overidden
= str
.obj() != NULL
;
118 std::string override
= base::android::ConvertJavaStringToUTF8(str
);
119 web_contents()->SetUserAgentOverride(override
);
122 const content::NavigationController
& controller
=
123 web_contents()->GetController();
124 for (int i
= 0; i
< controller
.GetEntryCount(); ++i
)
125 controller
.GetEntryAtIndex(i
)->SetIsOverridingUserAgent(ua_overidden
);
128 void AwSettings::UpdateWebkitPreferencesLocked(JNIEnv
* env
, jobject obj
) {
129 if (!web_contents()) return;
130 AwRenderViewHostExt
* render_view_host_ext
= GetAwRenderViewHostExt();
131 if (!render_view_host_ext
) return;
133 content::RenderViewHost
* render_view_host
=
134 web_contents()->GetRenderViewHost();
135 if (!render_view_host
) return;
136 render_view_host
->UpdateWebkitPreferences(
137 render_view_host
->GetWebkitPreferences());
140 void AwSettings::UpdateInitialPageScaleLocked(JNIEnv
* env
, jobject obj
) {
141 AwRenderViewHostExt
* rvhe
= GetAwRenderViewHostExt();
144 float initial_page_scale_percent
=
145 Java_AwSettings_getInitialPageScalePercentLocked(env
, obj
);
146 if (initial_page_scale_percent
== 0) {
147 rvhe
->SetInitialPageScale(-1);
149 float dip_scale
= static_cast<float>(
150 Java_AwSettings_getDIPScaleLocked(env
, obj
));
151 rvhe
->SetInitialPageScale(initial_page_scale_percent
/ dip_scale
/ 100.0f
);
155 void AwSettings::UpdateFormDataPreferencesLocked(JNIEnv
* env
, jobject obj
) {
156 if (!web_contents()) return;
157 AwContents
* contents
= AwContents::FromWebContents(web_contents());
158 if (!contents
) return;
160 contents
->SetSaveFormData(Java_AwSettings_getSaveFormDataLocked(env
, obj
));
163 void AwSettings::RenderViewCreated(content::RenderViewHost
* render_view_host
) {
164 // A single WebContents can normally have 0 to many RenderViewHost instances
165 // associated with it.
166 // This is important since there is only one RenderViewHostExt instance per
167 // WebContents (and not one RVHExt per RVH, as you might expect) and updating
168 // settings via RVHExt only ever updates the 'current' RVH.
169 // In android_webview we don't swap out the RVH on cross-site navigations, so
170 // we shouldn't have to deal with the multiple RVH per WebContents case. That
171 // in turn means that the newly created RVH is always the 'current' RVH
172 // (since we only ever go from 0 to 1 RVH instances) and hence the DCHECK.
173 DCHECK(web_contents()->GetRenderViewHost() == render_view_host
);
178 void AwSettings::WebContentsDestroyed(content::WebContents
* web_contents
) {
183 void AwSettings::PopulateFixedPreferences(WebPreferences
* web_prefs
) {
184 web_prefs
->shrinks_standalone_images_to_fit
= false;
185 web_prefs
->should_clear_document_background
= false;
188 void AwSettings::PopulateWebPreferences(WebPreferences
* web_prefs
) {
189 JNIEnv
* env
= base::android::AttachCurrentThread();
192 AwRenderViewHostExt
* render_view_host_ext
= GetAwRenderViewHostExt();
193 if (!render_view_host_ext
) return;
195 ScopedJavaLocalRef
<jobject
> scoped_obj
= aw_settings_
.get(env
);
196 jobject obj
= scoped_obj
.obj();
199 PopulateFixedPreferences(web_prefs
);
201 web_prefs
->text_autosizing_enabled
=
202 Java_AwSettings_getTextAutosizingEnabledLocked(env
, obj
);
204 int text_size_percent
= Java_AwSettings_getTextSizePercentLocked(env
, obj
);
205 if (web_prefs
->text_autosizing_enabled
) {
206 web_prefs
->font_scale_factor
= text_size_percent
/ 100.0f
;
207 web_prefs
->force_enable_zoom
= text_size_percent
>= 130;
208 // Use the default zoom factor value when Text Autosizer is turned on.
209 render_view_host_ext
->SetTextZoomFactor(1);
211 web_prefs
->force_enable_zoom
= false;
212 render_view_host_ext
->SetTextZoomFactor(text_size_percent
/ 100.0f
);
215 web_prefs
->standard_font_family_map
[webkit_glue::kCommonScript
] =
216 ConvertJavaStringToUTF16(
217 Java_AwSettings_getStandardFontFamilyLocked(env
, obj
));
219 web_prefs
->fixed_font_family_map
[webkit_glue::kCommonScript
] =
220 ConvertJavaStringToUTF16(
221 Java_AwSettings_getFixedFontFamilyLocked(env
, obj
));
223 web_prefs
->sans_serif_font_family_map
[webkit_glue::kCommonScript
] =
224 ConvertJavaStringToUTF16(
225 Java_AwSettings_getSansSerifFontFamilyLocked(env
, obj
));
227 web_prefs
->serif_font_family_map
[webkit_glue::kCommonScript
] =
228 ConvertJavaStringToUTF16(
229 Java_AwSettings_getSerifFontFamilyLocked(env
, obj
));
231 web_prefs
->cursive_font_family_map
[webkit_glue::kCommonScript
] =
232 ConvertJavaStringToUTF16(
233 Java_AwSettings_getCursiveFontFamilyLocked(env
, obj
));
235 web_prefs
->fantasy_font_family_map
[webkit_glue::kCommonScript
] =
236 ConvertJavaStringToUTF16(
237 Java_AwSettings_getFantasyFontFamilyLocked(env
, obj
));
239 web_prefs
->default_encoding
= ConvertJavaStringToUTF8(
240 Java_AwSettings_getDefaultTextEncodingLocked(env
, obj
));
242 web_prefs
->minimum_font_size
=
243 Java_AwSettings_getMinimumFontSizeLocked(env
, obj
);
245 web_prefs
->minimum_logical_font_size
=
246 Java_AwSettings_getMinimumLogicalFontSizeLocked(env
, obj
);
248 web_prefs
->default_font_size
=
249 Java_AwSettings_getDefaultFontSizeLocked(env
, obj
);
251 web_prefs
->default_fixed_font_size
=
252 Java_AwSettings_getDefaultFixedFontSizeLocked(env
, obj
);
254 // Blink's LoadsImagesAutomatically and ImagesEnabled must be
255 // set cris-cross to Android's. See
256 // https://code.google.com/p/chromium/issues/detail?id=224317#c26
257 web_prefs
->loads_images_automatically
=
258 Java_AwSettings_getImagesEnabledLocked(env
, obj
);
259 web_prefs
->images_enabled
=
260 Java_AwSettings_getLoadsImagesAutomaticallyLocked(env
, obj
);
262 web_prefs
->javascript_enabled
=
263 Java_AwSettings_getJavaScriptEnabledLocked(env
, obj
);
265 web_prefs
->allow_universal_access_from_file_urls
=
266 Java_AwSettings_getAllowUniversalAccessFromFileURLsLocked(env
, obj
);
268 web_prefs
->allow_file_access_from_file_urls
=
269 Java_AwSettings_getAllowFileAccessFromFileURLsLocked(env
, obj
);
271 web_prefs
->javascript_can_open_windows_automatically
=
272 Java_AwSettings_getJavaScriptCanOpenWindowsAutomaticallyLocked(env
, obj
);
274 web_prefs
->supports_multiple_windows
=
275 Java_AwSettings_getSupportMultipleWindowsLocked(env
, obj
);
277 web_prefs
->plugins_enabled
=
278 !Java_AwSettings_getPluginsDisabledLocked(env
, obj
);
280 web_prefs
->application_cache_enabled
=
281 Java_AwSettings_getAppCacheEnabledLocked(env
, obj
);
283 web_prefs
->local_storage_enabled
=
284 Java_AwSettings_getDomStorageEnabledLocked(env
, obj
);
286 web_prefs
->databases_enabled
=
287 Java_AwSettings_getDatabaseEnabledLocked(env
, obj
);
289 web_prefs
->wide_viewport_quirk
= true;
290 web_prefs
->double_tap_to_zoom_enabled
= web_prefs
->use_wide_viewport
=
291 Java_AwSettings_getUseWideViewportLocked(env
, obj
);
293 web_prefs
->initialize_at_minimum_page_scale
=
294 Java_AwSettings_getLoadWithOverviewModeLocked(env
, obj
);
296 web_prefs
->user_gesture_required_for_media_playback
=
297 Java_AwSettings_getMediaPlaybackRequiresUserGestureLocked(env
, obj
);
299 ScopedJavaLocalRef
<jstring
> url
=
300 Java_AwSettings_getDefaultVideoPosterURLLocked(env
, obj
);
301 web_prefs
->default_video_poster_url
= url
.obj() ?
302 GURL(ConvertJavaStringToUTF8(url
)) : GURL();
304 bool support_quirks
= Java_AwSettings_getSupportLegacyQuirksLocked(env
, obj
);
305 // Please see the corresponding Blink settings for bug references.
306 web_prefs
->support_deprecated_target_density_dpi
= support_quirks
;
307 web_prefs
->use_legacy_background_size_shorthand_behavior
= support_quirks
;
308 web_prefs
->viewport_meta_layout_size_quirk
= support_quirks
;
309 web_prefs
->viewport_meta_merge_content_quirk
= support_quirks
;
310 web_prefs
->viewport_meta_non_user_scalable_quirk
= support_quirks
;
311 web_prefs
->viewport_meta_zero_values_quirk
= support_quirks
;
312 web_prefs
->clobber_user_agent_initial_scale_quirk
= support_quirks
;
313 web_prefs
->ignore_main_frame_overflow_hidden_quirk
= support_quirks
;
314 web_prefs
->report_screen_size_in_physical_pixels_quirk
= support_quirks
;
316 web_prefs
->password_echo_enabled
=
317 Java_AwSettings_getPasswordEchoEnabledLocked(env
, obj
);
318 web_prefs
->spatial_navigation_enabled
=
319 Java_AwSettings_getSpatialNavigationLocked(env
, obj
);
321 web_prefs
->accelerated_2d_canvas_enabled
=
322 !accelerated_2d_canvas_disabled_by_switch_
&&
323 Java_AwSettings_getEnableSupportedHardwareAcceleratedFeaturesLocked(
327 static jlong
Init(JNIEnv
* env
,
329 jlong web_contents
) {
330 AwSettings
* settings
= new AwSettings(env
, obj
, web_contents
);
331 return reinterpret_cast<intptr_t>(settings
);
334 static jstring
GetDefaultUserAgent(JNIEnv
* env
, jclass clazz
) {
335 return base::android::ConvertUTF8ToJavaString(
336 env
, content::GetUserAgent(GURL())).Release();
339 bool RegisterAwSettings(JNIEnv
* env
) {
340 return RegisterNativesImpl(env
) >= 0;
343 } // namespace android_webview