Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(551)

Side by Side Diff: android_webview/native/aw_settings.cc

Issue 14271024: [Android WebView] Move the most of WebSettings into AwSettings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated findbugs issues Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "android_webview/native/aw_settings.h" 5 #include "android_webview/native/aw_settings.h"
6 6
7 #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h" 7 #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h"
8 #include "android_webview/native/aw_contents.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 "content/public/browser/navigation_controller.h"
12 #include "content/public/browser/navigation_entry.h"
9 #include "content/public/browser/render_view_host.h" 13 #include "content/public/browser/render_view_host.h"
10 #include "content/public/browser/web_contents.h" 14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/content_client.h"
11 #include "jni/AwSettings_jni.h" 16 #include "jni/AwSettings_jni.h"
12 #include "webkit/glue/webkit_glue.h" 17 #include "webkit/glue/webkit_glue.h"
18 #include "webkit/glue/webpreferences.h"
19 #include "webkit/user_agent/user_agent.h"
20
21 using base::android::CheckException;
22 using base::android::ConvertJavaStringToUTF16;
23 using base::android::ConvertUTF16ToJavaString;
24 using base::android::ConvertUTF8ToJavaString;
25 using base::android::GetClass;
26 using base::android::GetFieldID;
27 using base::android::GetMethodIDFromClassName;
28 using base::android::ScopedJavaLocalRef;
29 using webkit_glue::WebPreferences;
13 30
14 namespace android_webview { 31 namespace android_webview {
15 32
33 struct AwSettings::FieldIds {
34 // Note on speed. One may think that an approach that reads field values via
35 // JNI is ineffective and should not be used. Please keep in mind that in the
36 // legacy WebView the whole Sync method took <1ms on Xoom, and no one is
37 // expected to modify settings in performance-critical code.
38 FieldIds() { }
39
40 FieldIds(JNIEnv* env) {
41 const char* kStringClassName = "Ljava/lang/String;";
42
43 // FIXME: we should be using a new GetFieldIDFromClassName() with caching.
bulach 2013/04/25 12:55:57 given the "note on speed" above, would it be possi
44 ScopedJavaLocalRef<jclass> clazz(
45 GetClass(env, "org/chromium/android_webview/AwSettings"));
46 text_size_percent = GetFieldID(env, clazz, "mTextSizePercent", "I");
47 standard_fond_family =
48 GetFieldID(env, clazz, "mStandardFontFamily", kStringClassName);
49 fixed_font_family =
50 GetFieldID(env, clazz, "mFixedFontFamily", kStringClassName);
51 sans_serif_font_family =
52 GetFieldID(env, clazz, "mSansSerifFontFamily", kStringClassName);
53 serif_font_family =
54 GetFieldID(env, clazz, "mSerifFontFamily", kStringClassName);
55 cursive_font_family =
56 GetFieldID(env, clazz, "mCursiveFontFamily", kStringClassName);
57 fantasy_font_family =
58 GetFieldID(env, clazz, "mFantasyFontFamily", kStringClassName);
59 default_text_encoding =
60 GetFieldID(env, clazz, "mDefaultTextEncoding", kStringClassName);
61 user_agent =
62 GetFieldID(env, clazz, "mUserAgent", kStringClassName);
63 minimum_font_size = GetFieldID(env, clazz, "mMinimumFontSize", "I");
64 minimum_logical_font_size =
65 GetFieldID(env, clazz, "mMinimumLogicalFontSize", "I");
66 default_font_size = GetFieldID(env, clazz, "mDefaultFontSize", "I");
67 default_fixed_font_size =
68 GetFieldID(env, clazz, "mDefaultFixedFontSize", "I");
69 load_images_automatically =
70 GetFieldID(env, clazz, "mLoadsImagesAutomatically", "Z");
71 images_enabled =
72 GetFieldID(env, clazz, "mImagesEnabled", "Z");
73 java_script_enabled =
74 GetFieldID(env, clazz, "mJavaScriptEnabled", "Z");
75 allow_universal_access_from_file_urls =
76 GetFieldID(env, clazz, "mAllowUniversalAccessFromFileURLs", "Z");
77 allow_file_access_from_file_urls =
78 GetFieldID(env, clazz, "mAllowFileAccessFromFileURLs", "Z");
79 java_script_can_open_windows_automatically =
80 GetFieldID(env, clazz, "mJavaScriptCanOpenWindowsAutomatically", "Z");
81 support_multiple_windows =
82 GetFieldID(env, clazz, "mSupportMultipleWindows", "Z");
83 dom_storage_enabled =
84 GetFieldID(env, clazz, "mDomStorageEnabled", "Z");
85 database_enabled =
86 GetFieldID(env, clazz, "mDatabaseEnabled", "Z");
87 use_wide_viewport =
88 GetFieldID(env, clazz, "mUseWideViewport", "Z");
89 load_with_overview_mode =
90 GetFieldID(env, clazz, "mLoadWithOverviewMode", "Z");
91 media_playback_requires_user_gesture =
92 GetFieldID(env, clazz, "mMediaPlaybackRequiresUserGesture", "Z");
93 default_video_poster_url =
94 GetFieldID(env, clazz, "mDefaultVideoPosterURL", kStringClassName);
95 support_deprecated_target_density_dpi =
96 GetFieldID(env, clazz, "mSupportDeprecatedTargetDensityDPI", "Z");
97 dip_scale =
98 GetFieldID(env, clazz, "mDIPScale", "D");
99 initial_page_scale_percent =
100 GetFieldID(env, clazz, "mInitialPageScalePercent", "F");
101 }
102
103 // Field ids
104 jfieldID text_size_percent;
105 jfieldID standard_fond_family;
106 jfieldID fixed_font_family;
107 jfieldID sans_serif_font_family;
108 jfieldID serif_font_family;
109 jfieldID cursive_font_family;
110 jfieldID fantasy_font_family;
111 jfieldID default_text_encoding;
112 jfieldID user_agent;
113 jfieldID minimum_font_size;
114 jfieldID minimum_logical_font_size;
115 jfieldID default_font_size;
116 jfieldID default_fixed_font_size;
117 jfieldID load_images_automatically;
118 jfieldID images_enabled;
119 jfieldID java_script_enabled;
120 jfieldID allow_universal_access_from_file_urls;
121 jfieldID allow_file_access_from_file_urls;
122 jfieldID java_script_can_open_windows_automatically;
123 jfieldID support_multiple_windows;
124 jfieldID dom_storage_enabled;
125 jfieldID database_enabled;
126 jfieldID use_wide_viewport;
127 jfieldID load_with_overview_mode;
128 jfieldID media_playback_requires_user_gesture;
129 jfieldID default_video_poster_url;
130 jfieldID support_deprecated_target_density_dpi;
131 jfieldID dip_scale;
132 jfieldID initial_page_scale_percent;
133 };
134
16 AwSettings::AwSettings(JNIEnv* env, jobject obj) 135 AwSettings::AwSettings(JNIEnv* env, jobject obj)
17 : java_ref_(env, obj), 136 : aw_settings_(env, obj) {
18 initial_page_scale_percent_(0),
19 text_zoom_percent_(100) {
20 } 137 }
21 138
22 AwSettings::~AwSettings() { 139 AwSettings::~AwSettings() {
23 } 140 }
24 141
25 void AwSettings::Destroy(JNIEnv* env, jobject obj) { 142 void AwSettings::Destroy(JNIEnv* env, jobject obj) {
26 delete this; 143 delete this;
27 } 144 }
28 145
29 AwRenderViewHostExt* AwSettings::GetAwRenderViewHostExt() { 146 AwRenderViewHostExt* AwSettings::GetAwRenderViewHostExt() {
30 if (!web_contents()) return NULL; 147 if (!web_contents()) return NULL;
31 AwContents* contents = AwContents::FromWebContents(web_contents()); 148 AwContents* contents = AwContents::FromWebContents(web_contents());
32 if (!contents) return NULL; 149 if (!contents) return NULL;
33 return contents->render_view_host_ext(); 150 return contents->render_view_host_ext();
34 } 151 }
35 152
36 void AwSettings::ResetScrollAndScaleState(JNIEnv* env, jobject obj) { 153 void AwSettings::ResetScrollAndScaleState(JNIEnv* env, jobject obj) {
37 AwRenderViewHostExt* rvhe = GetAwRenderViewHostExt(); 154 AwRenderViewHostExt* rvhe = GetAwRenderViewHostExt();
38 if (!rvhe) return; 155 if (!rvhe) return;
39 rvhe->ResetScrollAndScaleState(); 156 rvhe->ResetScrollAndScaleState();
40 } 157 }
41 158
42 void AwSettings::SetInitialPageScale(
43 JNIEnv* env, jobject obj, jfloat page_scale_percent) {
44 if (initial_page_scale_percent_ == page_scale_percent) return;
45 initial_page_scale_percent_ = page_scale_percent;
46 UpdateInitialPageScale();
47 }
48
49 void AwSettings::SetTextZoom(JNIEnv* env, jobject obj, jint text_zoom_percent) {
50 if (text_zoom_percent_ == text_zoom_percent) return;
51 text_zoom_percent_ = text_zoom_percent;
52 UpdateTextZoom();
53 }
54
55 void AwSettings::SetWebContents(JNIEnv* env, jobject obj, jint jweb_contents) { 159 void AwSettings::SetWebContents(JNIEnv* env, jobject obj, jint jweb_contents) {
56 content::WebContents* web_contents = 160 content::WebContents* web_contents =
57 reinterpret_cast<content::WebContents*>(jweb_contents); 161 reinterpret_cast<content::WebContents*>(jweb_contents);
58 Observe(web_contents); 162 Observe(web_contents);
59 163
60 UpdateRenderViewHostExtSettings(); 164 UpdateEverything(env, obj);
61 if (web_contents->GetRenderViewHost()) { 165 }
62 UpdateRenderViewHostSettings(web_contents->GetRenderViewHost()); 166
167 void AwSettings::UpdateEverything() {
168 JNIEnv* env = base::android::AttachCurrentThread();
169 CHECK(env);
170 ScopedJavaLocalRef<jobject> scoped_obj = aw_settings_.get(env);
171 jobject obj = scoped_obj.obj();
172 if (!obj) return;
173 UpdateEverything(env, obj);
174 }
175
176 void AwSettings::UpdateEverything(JNIEnv* env, jobject obj) {
177 UpdateInitialPageScale(env, obj);
178 UpdateWebkitPreferences(env, obj);
179 UpdateUserAgent(env, obj);
180 ResetScrollAndScaleState(env, obj);
181 UpdatePreferredSizeMode();
182 }
183
184 void AwSettings::UpdateUserAgent(JNIEnv* env, jobject obj) {
185 if (!web_contents()) return;
186
187 if (!field_ids_)
188 field_ids_.reset(new FieldIds(env));
189
190 ScopedJavaLocalRef<jstring> str(env, static_cast<jstring>(
191 env->GetObjectField(obj, field_ids_->user_agent)));
192 std::string override = base::android::ConvertJavaStringToUTF8(str);
193 web_contents()->SetUserAgentOverride(override);
194
195 const content::NavigationController& controller =
196 web_contents()->GetController();
197 for (int i = 0; i < controller.GetEntryCount(); ++i)
198 controller.GetEntryAtIndex(i)->SetIsOverridingUserAgent(true);
199 }
200
201 void AwSettings::UpdateWebkitPreferences(JNIEnv* env, jobject obj) {
202 if (!web_contents()) return;
203 AwRenderViewHostExt* render_view_host_ext = GetAwRenderViewHostExt();
204 if (!render_view_host_ext) return;
205
206 if (!field_ids_)
207 field_ids_.reset(new FieldIds(env));
208
209 content::RenderViewHost* render_view_host =
210 web_contents()->GetRenderViewHost();
211 if (!render_view_host) return;
212 WebPreferences prefs = render_view_host->GetWebkitPreferences();
213
214 prefs.text_autosizing_enabled =
215 Java_AwSettings_getTextAutosizingEnabled(env, obj);
216
217 int text_size_percent = env->GetIntField(obj, field_ids_->text_size_percent);
218 if (prefs.text_autosizing_enabled) {
219 prefs.font_scale_factor = text_size_percent / 100.0f;
220 prefs.force_enable_zoom = text_size_percent >= 130;
221 // Use the default zoom level value when Text Autosizer is turned on.
222 render_view_host_ext->SetTextZoomLevel(0);
223 } else {
224 prefs.force_enable_zoom = false;
225 render_view_host_ext->SetTextZoomLevel(webkit_glue::ZoomFactorToZoomLevel(
226 text_size_percent / 100.0f));
63 } 227 }
64 } 228
65 229 ScopedJavaLocalRef<jstring> str(
66 void AwSettings::UpdateInitialPageScale() { 230 env, static_cast<jstring>(
231 env->GetObjectField(obj, field_ids_->standard_fond_family)));
232 prefs.standard_font_family_map[WebPreferences::kCommonScript] =
233 ConvertJavaStringToUTF16(str);
234
235 str.Reset(
236 env, static_cast<jstring>(
237 env->GetObjectField(obj, field_ids_->fixed_font_family)));
238 prefs.fixed_font_family_map[WebPreferences::kCommonScript] =
239 ConvertJavaStringToUTF16(str);
240
241 str.Reset(
242 env, static_cast<jstring>(
243 env->GetObjectField(obj, field_ids_->sans_serif_font_family)));
244 prefs.sans_serif_font_family_map[WebPreferences::kCommonScript] =
245 ConvertJavaStringToUTF16(str);
246
247 str.Reset(
248 env, static_cast<jstring>(
249 env->GetObjectField(obj, field_ids_->serif_font_family)));
250 prefs.serif_font_family_map[WebPreferences::kCommonScript] =
251 ConvertJavaStringToUTF16(str);
252
253 str.Reset(
254 env, static_cast<jstring>(
255 env->GetObjectField(obj, field_ids_->cursive_font_family)));
256 prefs.cursive_font_family_map[WebPreferences::kCommonScript] =
257 ConvertJavaStringToUTF16(str);
258
259 str.Reset(
260 env, static_cast<jstring>(
261 env->GetObjectField(obj, field_ids_->fantasy_font_family)));
262 prefs.fantasy_font_family_map[WebPreferences::kCommonScript] =
263 ConvertJavaStringToUTF16(str);
264
265 str.Reset(
266 env, static_cast<jstring>(
267 env->GetObjectField(obj, field_ids_->default_text_encoding)));
268 prefs.default_encoding = ConvertJavaStringToUTF8(str);
269
270 prefs.minimum_font_size =
271 env->GetIntField(obj, field_ids_->minimum_font_size);
272
273 prefs.minimum_logical_font_size =
274 env->GetIntField(obj, field_ids_->minimum_logical_font_size);
275
276 prefs.default_font_size =
277 env->GetIntField(obj, field_ids_->default_font_size);
278
279 prefs.default_fixed_font_size =
280 env->GetIntField(obj, field_ids_->default_fixed_font_size);
281
282 prefs.loads_images_automatically =
283 env->GetBooleanField(obj, field_ids_->load_images_automatically);
284
285 prefs.images_enabled =
286 env->GetBooleanField(obj, field_ids_->images_enabled);
287
288 prefs.javascript_enabled =
289 env->GetBooleanField(obj, field_ids_->java_script_enabled);
290
291 prefs.allow_universal_access_from_file_urls = env->GetBooleanField(
292 obj, field_ids_->allow_universal_access_from_file_urls);
293
294 prefs.allow_file_access_from_file_urls = env->GetBooleanField(
295 obj, field_ids_->allow_file_access_from_file_urls);
296
297 prefs.javascript_can_open_windows_automatically = env->GetBooleanField(
298 obj, field_ids_->java_script_can_open_windows_automatically);
299
300 prefs.supports_multiple_windows = env->GetBooleanField(
301 obj, field_ids_->support_multiple_windows);
302
303 prefs.plugins_enabled = !Java_AwSettings_getPluginsDisabled(env, obj);
304
305 prefs.application_cache_enabled =
306 Java_AwSettings_getAppCacheEnabled(env, obj);
307
308 prefs.local_storage_enabled = env->GetBooleanField(
309 obj, field_ids_->dom_storage_enabled);
310
311 prefs.databases_enabled = env->GetBooleanField(
312 obj, field_ids_->database_enabled);
313
314 prefs.double_tap_to_zoom_enabled = prefs.use_wide_viewport =
315 env->GetBooleanField(obj, field_ids_->use_wide_viewport);
316
317 prefs.initialize_at_minimum_page_scale = env->GetBooleanField(
318 obj, field_ids_->load_with_overview_mode);
319
320 prefs.user_gesture_required_for_media_playback = env->GetBooleanField(
321 obj, field_ids_->media_playback_requires_user_gesture);
322
323 str.Reset(
324 env, static_cast<jstring>(
325 env->GetObjectField(obj, field_ids_->default_video_poster_url)));
326 prefs.default_video_poster_url = str.obj() ?
327 GURL(ConvertJavaStringToUTF8(str)) : GURL();
328
329 prefs.support_deprecated_target_density_dpi = env->GetBooleanField(
330 obj, field_ids_->support_deprecated_target_density_dpi);
331
332 render_view_host->UpdateWebkitPreferences(prefs);
333 }
334
335 void AwSettings::UpdateInitialPageScale(JNIEnv* env, jobject obj) {
67 AwRenderViewHostExt* rvhe = GetAwRenderViewHostExt(); 336 AwRenderViewHostExt* rvhe = GetAwRenderViewHostExt();
68 if (!rvhe) return; 337 if (!rvhe) return;
69 if (initial_page_scale_percent_ == 0) { 338
339 if (!field_ids_)
340 field_ids_.reset(new FieldIds(env));
341
342 float initial_page_scale_percent =
343 env->GetFloatField(obj, field_ids_->initial_page_scale_percent);
344 if (initial_page_scale_percent == 0) {
70 rvhe->SetInitialPageScale(-1); 345 rvhe->SetInitialPageScale(-1);
71 } else { 346 } else {
72 rvhe->SetInitialPageScale(initial_page_scale_percent_ / 100.0f); 347 float dip_scale = static_cast<float>(
348 env->GetDoubleField(obj, field_ids_->dip_scale));
349 rvhe->SetInitialPageScale(initial_page_scale_percent / dip_scale / 100.0f);
73 } 350 }
74 } 351 }
75 352
76 void AwSettings::UpdateTextZoom() { 353 void AwSettings::UpdatePreferredSizeMode() {
77 AwRenderViewHostExt* rvhe = GetAwRenderViewHostExt(); 354 if (web_contents()->GetRenderViewHost()) {
78 if (!rvhe) return; 355 web_contents()->GetRenderViewHost()->EnablePreferredSizeMode();
79 if (text_zoom_percent_ > 0) {
80 rvhe->SetTextZoomLevel(webkit_glue::ZoomFactorToZoomLevel(
81 text_zoom_percent_ / 100.0f));
82 } else {
83 // Use the default zoom level value when Text Autosizer is turned on.
84 rvhe->SetTextZoomLevel(0);
85 } 356 }
86 } 357 }
87 358
88 void AwSettings::UpdatePreferredSizeMode(
89 content::RenderViewHost* render_view_host) {
90 render_view_host->EnablePreferredSizeMode();
91 }
92
93 void AwSettings::UpdateRenderViewHostExtSettings() {
94 UpdateInitialPageScale();
95 UpdateTextZoom();
96 }
97
98 void AwSettings::UpdateRenderViewHostSettings(
99 content::RenderViewHost* render_view_host) {
100 UpdatePreferredSizeMode(render_view_host);
101 }
102
103 void AwSettings::RenderViewCreated(content::RenderViewHost* render_view_host) { 359 void AwSettings::RenderViewCreated(content::RenderViewHost* render_view_host) {
104 // A single WebContents can normally have 0, 1 or 2 RenderViewHost instances 360 // A single WebContents can normally have 0 to many RenderViewHost instances
105 // associated with it. 361 // associated with it.
106 // This is important since there is only one RenderViewHostExt instance per 362 // This is important since there is only one RenderViewHostExt instance per
107 // WebContents (and not one RVHExt per RVH, as you might expect) and updating 363 // WebContents (and not one RVHExt per RVH, as you might expect) and updating
108 // settings via RVHExt only ever updates the 'current' RVH. 364 // settings via RVHExt only ever updates the 'current' RVH.
109 // In android_webview we don't swap out the RVH on cross-site navigations, so 365 // In android_webview we don't swap out the RVH on cross-site navigations, so
110 // we shouldn't have to deal with the multiple RVH per WebContents case. That 366 // we shouldn't have to deal with the multiple RVH per WebContents case. That
111 // in turn means that the newly created RVH is always the 'current' RVH 367 // in turn means that the newly created RVH is always the 'current' RVH
112 // (since we only ever go from 0 to 1 RVH instances) and hence the DCHECK. 368 // (since we only ever go from 0 to 1 RVH instances) and hence the DCHECK.
113 DCHECK(web_contents()->GetRenderViewHost() == render_view_host); 369 DCHECK(web_contents()->GetRenderViewHost() == render_view_host);
114 370
115 UpdateRenderViewHostExtSettings(); 371 UpdateEverything();
116 UpdateRenderViewHostSettings(render_view_host);
117 } 372 }
118 373
119 static jint Init(JNIEnv* env, 374 static jint Init(JNIEnv* env,
120 jobject obj, 375 jobject obj,
121 jint web_contents) { 376 jint web_contents) {
122 AwSettings* settings = new AwSettings(env, obj); 377 AwSettings* settings = new AwSettings(env, obj);
123 settings->SetWebContents(env, obj, web_contents); 378 settings->SetWebContents(env, obj, web_contents);
124 return reinterpret_cast<jint>(settings); 379 return reinterpret_cast<jint>(settings);
125 } 380 }
126 381
382 static jstring GetDefaultUserAgent(JNIEnv* env, jclass clazz) {
383 return base::android::ConvertUTF8ToJavaString(
384 env, content::GetUserAgent(GURL())).Release();
385 }
386
127 bool RegisterAwSettings(JNIEnv* env) { 387 bool RegisterAwSettings(JNIEnv* env) {
128 return RegisterNativesImpl(env) >= 0; 388 return RegisterNativesImpl(env) >= 0;
129 } 389 }
130 390
131 } // namespace android_webview 391 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698