Revert of Output closure-compiled JavaScript files (patchset #10 id:180001 of https...
[chromium-blink-merge.git] / android_webview / native / aw_settings.cc
blob91c506433427b749f3cc2fbe89fa1d0b8d3d338f
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 {
31 namespace {
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;
53 }; // namespace
55 const void* kAwSettingsUserDataKey = &kAwSettingsUserDataKey;
57 class AwSettingsUserData : public base::SupportsUserData::Data {
58 public:
59 AwSettingsUserData(AwSettings* ptr) : settings_(ptr) {}
61 static AwSettings* GetSettings(content::WebContents* web_contents) {
62 if (!web_contents)
63 return NULL;
64 AwSettingsUserData* data = static_cast<AwSettingsUserData*>(
65 web_contents->GetUserData(kAwSettingsUserDataKey));
66 return data ? data->settings_ : NULL;
69 private:
70 AwSettings* settings_;
73 AwSettings::AwSettings(JNIEnv* env,
74 jobject obj,
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() {
84 if (web_contents()) {
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();
91 if (!obj) return;
92 Java_AwSettings_nativeAwSettingsGone(env, obj,
93 reinterpret_cast<intptr_t>(this));
96 void AwSettings::Destroy(JNIEnv* env, jobject obj) {
97 delete this;
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();
113 if (!rvhe) return;
114 rvhe->ResetScrollAndScaleState();
117 void AwSettings::UpdateEverything() {
118 JNIEnv* env = base::android::AttachCurrentThread();
119 CHECK(env);
120 ScopedJavaLocalRef<jobject> scoped_obj = aw_settings_.get(env);
121 jobject obj = scoped_obj.obj();
122 if (!obj) return;
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);
134 UpdateOffscreenPreRasterLocked(env, obj);
137 void AwSettings::UpdateUserAgentLocked(JNIEnv* env, jobject obj) {
138 if (!web_contents()) return;
140 ScopedJavaLocalRef<jstring> str =
141 Java_AwSettings_getUserAgentLocked(env, obj);
142 bool ua_overidden = str.obj() != NULL;
144 if (ua_overidden) {
145 std::string override = base::android::ConvertJavaStringToUTF8(str);
146 web_contents()->SetUserAgentOverride(override);
149 const content::NavigationController& controller =
150 web_contents()->GetController();
151 for (int i = 0; i < controller.GetEntryCount(); ++i)
152 controller.GetEntryAtIndex(i)->SetIsOverridingUserAgent(ua_overidden);
155 void AwSettings::UpdateWebkitPreferencesLocked(JNIEnv* env, jobject obj) {
156 if (!web_contents()) return;
157 AwRenderViewHostExt* render_view_host_ext = GetAwRenderViewHostExt();
158 if (!render_view_host_ext) return;
160 content::RenderViewHost* render_view_host =
161 web_contents()->GetRenderViewHost();
162 if (!render_view_host) return;
163 render_view_host->OnWebkitPreferencesChanged();
166 void AwSettings::UpdateInitialPageScaleLocked(JNIEnv* env, jobject obj) {
167 AwRenderViewHostExt* rvhe = GetAwRenderViewHostExt();
168 if (!rvhe) return;
170 float initial_page_scale_percent =
171 Java_AwSettings_getInitialPageScalePercentLocked(env, obj);
172 if (initial_page_scale_percent == 0) {
173 rvhe->SetInitialPageScale(-1);
174 } else {
175 float dip_scale = static_cast<float>(
176 Java_AwSettings_getDIPScaleLocked(env, obj));
177 rvhe->SetInitialPageScale(initial_page_scale_percent / dip_scale / 100.0f);
181 void AwSettings::UpdateFormDataPreferencesLocked(JNIEnv* env, jobject obj) {
182 if (!web_contents()) return;
183 AwContents* contents = AwContents::FromWebContents(web_contents());
184 if (!contents) return;
186 contents->SetSaveFormData(Java_AwSettings_getSaveFormDataLocked(env, obj));
189 void AwSettings::UpdateRendererPreferencesLocked(JNIEnv* env, jobject obj) {
190 if (!web_contents()) return;
192 bool update_prefs = false;
193 RendererPreferences* prefs = web_contents()->GetMutableRendererPrefs();
195 if (!renderer_prefs_initialized_) {
196 PopulateFixedRendererPreferences(prefs);
197 renderer_prefs_initialized_ = true;
198 update_prefs = true;
201 bool video_overlay =
202 Java_AwSettings_getVideoOverlayForEmbeddedVideoEnabledLocked(env, obj);
203 if (video_overlay != prefs->use_video_overlay_for_embedded_encrypted_video) {
204 prefs->use_video_overlay_for_embedded_encrypted_video = video_overlay;
205 update_prefs = true;
208 content::RenderViewHost* host = web_contents()->GetRenderViewHost();
209 if (update_prefs && host)
210 host->SyncRendererPrefs();
213 void AwSettings::UpdateOffscreenPreRasterLocked(JNIEnv* env, jobject obj) {
214 AwContents* contents = AwContents::FromWebContents(web_contents());
215 if (contents) {
216 contents->SetOffscreenPreRaster(
217 Java_AwSettings_getOffscreenPreRasterLocked(env, obj));
221 void AwSettings::RenderViewCreated(content::RenderViewHost* render_view_host) {
222 // A single WebContents can normally have 0 to many RenderViewHost instances
223 // associated with it.
224 // This is important since there is only one RenderViewHostExt instance per
225 // WebContents (and not one RVHExt per RVH, as you might expect) and updating
226 // settings via RVHExt only ever updates the 'current' RVH.
227 // In android_webview we don't swap out the RVH on cross-site navigations, so
228 // we shouldn't have to deal with the multiple RVH per WebContents case. That
229 // in turn means that the newly created RVH is always the 'current' RVH
230 // (since we only ever go from 0 to 1 RVH instances) and hence the DCHECK.
231 DCHECK_EQ(render_view_host, web_contents()->GetRenderViewHost());
233 UpdateEverything();
236 void AwSettings::WebContentsDestroyed() {
237 delete this;
240 void AwSettings::PopulateWebPreferences(WebPreferences* web_prefs) {
241 JNIEnv* env = base::android::AttachCurrentThread();
242 CHECK(env);
243 ScopedJavaLocalRef<jobject> scoped_obj = aw_settings_.get(env);
244 jobject obj = scoped_obj.obj();
245 if (!obj) return;
246 // Grab the lock and call PopulateWebPreferencesLocked.
247 Java_AwSettings_populateWebPreferences(
248 env, obj, reinterpret_cast<jlong>(web_prefs));
251 void AwSettings::PopulateWebPreferencesLocked(
252 JNIEnv* env, jobject obj, jlong web_prefs_ptr) {
253 AwRenderViewHostExt* render_view_host_ext = GetAwRenderViewHostExt();
254 if (!render_view_host_ext) return;
256 WebPreferences* web_prefs = reinterpret_cast<WebPreferences*>(web_prefs_ptr);
257 PopulateFixedWebPreferences(web_prefs);
259 web_prefs->text_autosizing_enabled =
260 Java_AwSettings_getTextAutosizingEnabledLocked(env, obj);
262 int text_size_percent = Java_AwSettings_getTextSizePercentLocked(env, obj);
263 if (web_prefs->text_autosizing_enabled) {
264 web_prefs->font_scale_factor = text_size_percent / 100.0f;
265 web_prefs->force_enable_zoom = text_size_percent >= 130;
266 // Use the default zoom factor value when Text Autosizer is turned on.
267 render_view_host_ext->SetTextZoomFactor(1);
268 } else {
269 web_prefs->force_enable_zoom = false;
270 render_view_host_ext->SetTextZoomFactor(text_size_percent / 100.0f);
273 web_prefs->standard_font_family_map[content::kCommonScript] =
274 ConvertJavaStringToUTF16(
275 Java_AwSettings_getStandardFontFamilyLocked(env, obj));
277 web_prefs->fixed_font_family_map[content::kCommonScript] =
278 ConvertJavaStringToUTF16(
279 Java_AwSettings_getFixedFontFamilyLocked(env, obj));
281 web_prefs->sans_serif_font_family_map[content::kCommonScript] =
282 ConvertJavaStringToUTF16(
283 Java_AwSettings_getSansSerifFontFamilyLocked(env, obj));
285 web_prefs->serif_font_family_map[content::kCommonScript] =
286 ConvertJavaStringToUTF16(
287 Java_AwSettings_getSerifFontFamilyLocked(env, obj));
289 web_prefs->cursive_font_family_map[content::kCommonScript] =
290 ConvertJavaStringToUTF16(
291 Java_AwSettings_getCursiveFontFamilyLocked(env, obj));
293 web_prefs->fantasy_font_family_map[content::kCommonScript] =
294 ConvertJavaStringToUTF16(
295 Java_AwSettings_getFantasyFontFamilyLocked(env, obj));
297 web_prefs->default_encoding = ConvertJavaStringToUTF8(
298 Java_AwSettings_getDefaultTextEncodingLocked(env, obj));
300 web_prefs->minimum_font_size =
301 Java_AwSettings_getMinimumFontSizeLocked(env, obj);
303 web_prefs->minimum_logical_font_size =
304 Java_AwSettings_getMinimumLogicalFontSizeLocked(env, obj);
306 web_prefs->default_font_size =
307 Java_AwSettings_getDefaultFontSizeLocked(env, obj);
309 web_prefs->default_fixed_font_size =
310 Java_AwSettings_getDefaultFixedFontSizeLocked(env, obj);
312 // Blink's LoadsImagesAutomatically and ImagesEnabled must be
313 // set cris-cross to Android's. See
314 // https://code.google.com/p/chromium/issues/detail?id=224317#c26
315 web_prefs->loads_images_automatically =
316 Java_AwSettings_getImagesEnabledLocked(env, obj);
317 web_prefs->images_enabled =
318 Java_AwSettings_getLoadsImagesAutomaticallyLocked(env, obj);
320 web_prefs->javascript_enabled =
321 Java_AwSettings_getJavaScriptEnabledLocked(env, obj);
323 web_prefs->allow_universal_access_from_file_urls =
324 Java_AwSettings_getAllowUniversalAccessFromFileURLsLocked(env, obj);
326 web_prefs->allow_file_access_from_file_urls =
327 Java_AwSettings_getAllowFileAccessFromFileURLsLocked(env, obj);
329 web_prefs->javascript_can_open_windows_automatically =
330 Java_AwSettings_getJavaScriptCanOpenWindowsAutomaticallyLocked(env, obj);
332 web_prefs->supports_multiple_windows =
333 Java_AwSettings_getSupportMultipleWindowsLocked(env, obj);
335 web_prefs->plugins_enabled =
336 !Java_AwSettings_getPluginsDisabledLocked(env, obj);
338 web_prefs->application_cache_enabled =
339 Java_AwSettings_getAppCacheEnabledLocked(env, obj);
341 web_prefs->local_storage_enabled =
342 Java_AwSettings_getDomStorageEnabledLocked(env, obj);
344 web_prefs->databases_enabled =
345 Java_AwSettings_getDatabaseEnabledLocked(env, obj);
347 web_prefs->wide_viewport_quirk = true;
348 web_prefs->use_wide_viewport =
349 Java_AwSettings_getUseWideViewportLocked(env, obj);
351 web_prefs->force_zero_layout_height =
352 Java_AwSettings_getForceZeroLayoutHeightLocked(env, obj);
354 const bool zero_layout_height_disables_viewport_quirk =
355 Java_AwSettings_getZeroLayoutHeightDisablesViewportQuirkLocked(env, obj);
356 web_prefs->viewport_enabled = !(zero_layout_height_disables_viewport_quirk &&
357 web_prefs->force_zero_layout_height);
359 web_prefs->double_tap_to_zoom_enabled =
360 Java_AwSettings_supportsDoubleTapZoomLocked(env, obj);
362 web_prefs->initialize_at_minimum_page_scale =
363 Java_AwSettings_getLoadWithOverviewModeLocked(env, obj);
365 web_prefs->user_gesture_required_for_media_playback =
366 Java_AwSettings_getMediaPlaybackRequiresUserGestureLocked(env, obj);
368 ScopedJavaLocalRef<jstring> url =
369 Java_AwSettings_getDefaultVideoPosterURLLocked(env, obj);
370 web_prefs->default_video_poster_url = url.obj() ?
371 GURL(ConvertJavaStringToUTF8(url)) : GURL();
373 bool support_quirks = Java_AwSettings_getSupportLegacyQuirksLocked(env, obj);
374 // Please see the corresponding Blink settings for bug references.
375 web_prefs->support_deprecated_target_density_dpi = support_quirks;
376 web_prefs->use_legacy_background_size_shorthand_behavior = support_quirks;
377 web_prefs->viewport_meta_layout_size_quirk = support_quirks;
378 web_prefs->viewport_meta_merge_content_quirk = support_quirks;
379 web_prefs->viewport_meta_non_user_scalable_quirk = support_quirks;
380 web_prefs->viewport_meta_zero_values_quirk = support_quirks;
381 web_prefs->clobber_user_agent_initial_scale_quirk = support_quirks;
382 web_prefs->ignore_main_frame_overflow_hidden_quirk = support_quirks;
383 web_prefs->report_screen_size_in_physical_pixels_quirk = support_quirks;
385 web_prefs->password_echo_enabled =
386 Java_AwSettings_getPasswordEchoEnabledLocked(env, obj);
387 web_prefs->spatial_navigation_enabled =
388 Java_AwSettings_getSpatialNavigationLocked(env, obj);
390 bool enable_supported_hardware_accelerated_features =
391 Java_AwSettings_getEnableSupportedHardwareAcceleratedFeaturesLocked(
392 env, obj);
394 bool accelerated_2d_canvas_enabled_by_switch =
395 web_prefs->accelerated_2d_canvas_enabled;
396 web_prefs->accelerated_2d_canvas_enabled = true;
397 if (!accelerated_2d_canvas_enabled_by_switch ||
398 !enable_supported_hardware_accelerated_features) {
399 // Any canvas smaller than this will fallback to software. Abusing this
400 // slightly to turn canvas off without changing
401 // accelerated_2d_canvas_enabled, which also affects compositing mode.
402 // Using 100M instead of max int to avoid overflows.
403 web_prefs->minimum_accelerated_2d_canvas_size = 100 * 1000 * 1000;
405 web_prefs->experimental_webgl_enabled =
406 web_prefs->experimental_webgl_enabled &&
407 enable_supported_hardware_accelerated_features;
409 web_prefs->allow_displaying_insecure_content =
410 Java_AwSettings_getAllowDisplayingInsecureContentLocked(env, obj);
411 web_prefs->allow_running_insecure_content =
412 Java_AwSettings_getAllowRunningInsecureContentLocked(env, obj);
414 web_prefs->fullscreen_supported =
415 Java_AwSettings_getFullscreenSupportedLocked(env, obj);
418 static jlong Init(JNIEnv* env,
419 jobject obj,
420 jobject web_contents) {
421 content::WebContents* contents = content::WebContents::FromJavaWebContents(
422 web_contents);
423 AwSettings* settings = new AwSettings(env, obj, contents);
424 return reinterpret_cast<intptr_t>(settings);
427 static jstring GetDefaultUserAgent(JNIEnv* env, jclass clazz) {
428 return base::android::ConvertUTF8ToJavaString(env, GetUserAgent()).Release();
431 bool RegisterAwSettings(JNIEnv* env) {
432 return RegisterNativesImpl(env);
435 } // namespace android_webview