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

Side by Side Diff: content/browser/android/content_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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/android/content_settings.h" 5 #include "content/browser/android/content_settings.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "content/browser/android/content_view_core_impl.h" 8 #include "content/browser/android/content_view_core_impl.h"
10 #include "content/browser/renderer_host/render_view_host_delegate.h" 9 #include "content/browser/renderer_host/render_view_host_delegate.h"
11 #include "content/browser/renderer_host/render_view_host_impl.h" 10 #include "content/browser/renderer_host/render_view_host_impl.h"
12 #include "content/public/browser/web_contents.h" 11 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/content_client.h" 12 #include "content/public/common/content_client.h"
14 #include "jni/ContentSettings_jni.h" 13 #include "jni/ContentSettings_jni.h"
15 #include "webkit/glue/webkit_glue.h" 14 #include "webkit/glue/webkit_glue.h"
16 #include "webkit/glue/webpreferences.h" 15 #include "webkit/glue/webpreferences.h"
17 #include "webkit/user_agent/user_agent.h"
18 16
19 using base::android::CheckException;
20 using base::android::ConvertJavaStringToUTF16;
21 using base::android::ConvertUTF16ToJavaString;
22 using base::android::ConvertUTF8ToJavaString;
23 using base::android::GetClass;
24 using base::android::GetFieldID; 17 using base::android::GetFieldID;
25 using base::android::GetMethodIDFromClassName;
26 using base::android::ScopedJavaLocalRef; 18 using base::android::ScopedJavaLocalRef;
27 using webkit_glue::WebPreferences;
28 19
29 namespace content { 20 namespace content {
30 21
31 struct ContentSettings::FieldIds { 22 struct ContentSettings::FieldIds {
32 // Note on speed. One may think that an approach that reads field values via
33 // JNI is ineffective and should not be used. Please keep in mind that in the
34 // legacy WebView the whole Sync method took <1ms on Xoom, and no one is
35 // expected to modify settings in performance-critical code.
36 FieldIds() { } 23 FieldIds() { }
37 24
38 FieldIds(JNIEnv* env) { 25 FieldIds(JNIEnv* env) {
39 const char* kStringClassName = "Ljava/lang/String;";
40
41 // FIXME: we should be using a new GetFieldIDFromClassName() with caching. 26 // FIXME: we should be using a new GetFieldIDFromClassName() with caching.
bulach 2013/04/25 12:55:57 ditto..
42 ScopedJavaLocalRef<jclass> clazz( 27 ScopedJavaLocalRef<jclass> clazz(base::android::GetClass(
43 GetClass(env, "org/chromium/content/browser/ContentSettings")); 28 env, "org/chromium/content/browser/ContentSettings"));
44 text_size_percent = GetFieldID(env, clazz, "mTextSizePercent", "I");
45 standard_fond_family =
46 GetFieldID(env, clazz, "mStandardFontFamily", kStringClassName);
47 fixed_font_family =
48 GetFieldID(env, clazz, "mFixedFontFamily", kStringClassName);
49 sans_serif_font_family =
50 GetFieldID(env, clazz, "mSansSerifFontFamily", kStringClassName);
51 serif_font_family =
52 GetFieldID(env, clazz, "mSerifFontFamily", kStringClassName);
53 cursive_font_family =
54 GetFieldID(env, clazz, "mCursiveFontFamily", kStringClassName);
55 fantasy_font_family =
56 GetFieldID(env, clazz, "mFantasyFontFamily", kStringClassName);
57 default_text_encoding =
58 GetFieldID(env, clazz, "mDefaultTextEncoding", kStringClassName);
59 user_agent =
60 GetFieldID(env, clazz, "mUserAgent", kStringClassName);
61 minimum_font_size = GetFieldID(env, clazz, "mMinimumFontSize", "I");
62 minimum_logical_font_size =
63 GetFieldID(env, clazz, "mMinimumLogicalFontSize", "I");
64 default_font_size = GetFieldID(env, clazz, "mDefaultFontSize", "I");
65 default_fixed_font_size =
66 GetFieldID(env, clazz, "mDefaultFixedFontSize", "I");
67 load_images_automatically =
68 GetFieldID(env, clazz, "mLoadsImagesAutomatically", "Z");
69 images_enabled =
70 GetFieldID(env, clazz, "mImagesEnabled", "Z");
71 java_script_enabled = 29 java_script_enabled =
72 GetFieldID(env, clazz, "mJavaScriptEnabled", "Z"); 30 GetFieldID(env, clazz, "mJavaScriptEnabled", "Z");
73 allow_universal_access_from_file_urls =
74 GetFieldID(env, clazz, "mAllowUniversalAccessFromFileURLs", "Z");
75 allow_file_access_from_file_urls =
76 GetFieldID(env, clazz, "mAllowFileAccessFromFileURLs", "Z");
77 java_script_can_open_windows_automatically =
78 GetFieldID(env, clazz, "mJavaScriptCanOpenWindowsAutomatically", "Z");
79 support_multiple_windows =
80 GetFieldID(env, clazz, "mSupportMultipleWindows", "Z");
81 dom_storage_enabled =
82 GetFieldID(env, clazz, "mDomStorageEnabled", "Z");
83 database_enabled =
84 GetFieldID(env, clazz, "mDatabaseEnabled", "Z");
85 use_wide_viewport =
86 GetFieldID(env, clazz, "mUseWideViewport", "Z");
87 load_with_overview_mode =
88 GetFieldID(env, clazz, "mLoadWithOverviewMode", "Z");
89 media_playback_requires_user_gesture =
90 GetFieldID(env, clazz, "mMediaPlaybackRequiresUserGesture", "Z");
91 default_video_poster_url =
92 GetFieldID(env, clazz, "mDefaultVideoPosterURL", kStringClassName);
93 support_deprecated_target_density_dpi =
94 GetFieldID(env, clazz, "mSupportDeprecatedTargetDensityDPI", "Z");
95 } 31 }
96 32
97 // Field ids 33 // Field ids
98 jfieldID text_size_percent;
99 jfieldID standard_fond_family;
100 jfieldID fixed_font_family;
101 jfieldID sans_serif_font_family;
102 jfieldID serif_font_family;
103 jfieldID cursive_font_family;
104 jfieldID fantasy_font_family;
105 jfieldID default_text_encoding;
106 jfieldID user_agent;
107 jfieldID minimum_font_size;
108 jfieldID minimum_logical_font_size;
109 jfieldID default_font_size;
110 jfieldID default_fixed_font_size;
111 jfieldID load_images_automatically;
112 jfieldID images_enabled;
113 jfieldID java_script_enabled; 34 jfieldID java_script_enabled;
114 jfieldID allow_universal_access_from_file_urls;
115 jfieldID allow_file_access_from_file_urls;
116 jfieldID java_script_can_open_windows_automatically;
117 jfieldID support_multiple_windows;
118 jfieldID dom_storage_enabled;
119 jfieldID database_enabled;
120 jfieldID use_wide_viewport;
121 jfieldID load_with_overview_mode;
122 jfieldID media_playback_requires_user_gesture;
123 jfieldID default_video_poster_url;
124 jfieldID support_deprecated_target_density_dpi;
125 }; 35 };
126 36
127 ContentSettings::ContentSettings(JNIEnv* env, 37 ContentSettings::ContentSettings(JNIEnv* env,
128 jobject obj, 38 jobject obj,
129 WebContents* contents, 39 WebContents* contents)
130 bool is_master_mode)
131 : WebContentsObserver(contents), 40 : WebContentsObserver(contents),
132 is_master_mode_(is_master_mode),
133 content_settings_(env, obj) { 41 content_settings_(env, obj) {
134 } 42 }
135 43
136 ContentSettings::~ContentSettings() { 44 ContentSettings::~ContentSettings() {
137 JNIEnv* env = base::android::AttachCurrentThread(); 45 JNIEnv* env = base::android::AttachCurrentThread();
138 ScopedJavaLocalRef<jobject> obj = content_settings_.get(env); 46 ScopedJavaLocalRef<jobject> obj = content_settings_.get(env);
139 if (obj.obj()) { 47 if (obj.obj()) {
140 Java_ContentSettings_onNativeContentSettingsDestroyed(env, obj.obj(), 48 Java_ContentSettings_onNativeContentSettingsDestroyed(env, obj.obj(),
141 reinterpret_cast<jint>(this)); 49 reinterpret_cast<jint>(this));
142 } 50 }
143 } 51 }
144 52
145 // static 53 // static
146 bool ContentSettings::RegisterContentSettings(JNIEnv* env) { 54 bool ContentSettings::RegisterContentSettings(JNIEnv* env) {
147 return RegisterNativesImpl(env); 55 return RegisterNativesImpl(env);
148 } 56 }
149 57
150 void ContentSettings::SyncFromNativeImpl() { 58 void ContentSettings::SyncFromNativeImpl() {
151 JNIEnv* env = base::android::AttachCurrentThread(); 59 JNIEnv* env = base::android::AttachCurrentThread();
152 CHECK(env); 60 CHECK(env);
153 if (!field_ids_) 61 if (!field_ids_)
154 field_ids_.reset(new FieldIds(env)); 62 field_ids_.reset(new FieldIds(env));
155 63
156 ScopedJavaLocalRef<jobject> scoped_obj = content_settings_.get(env); 64 ScopedJavaLocalRef<jobject> scoped_obj = content_settings_.get(env);
157 jobject obj = scoped_obj.obj(); 65 jobject obj = scoped_obj.obj();
158 if (!obj) 66 if (!obj)
159 return; 67 return;
160 RenderViewHost* render_view_host = web_contents()->GetRenderViewHost(); 68 RenderViewHost* render_view_host = web_contents()->GetRenderViewHost();
161 WebPreferences prefs = render_view_host->GetDelegate()->GetWebkitPrefs(); 69 webkit_glue::WebPreferences prefs =
162 70 render_view_host->GetDelegate()->GetWebkitPrefs();
163 Java_ContentSettings_setTextAutosizingEnabled(
164 env, obj, prefs.text_autosizing_enabled);
165 CheckException(env);
166
167 env->SetIntField(
168 obj,
169 field_ids_->text_size_percent,
170 prefs.font_scale_factor * 100.0f);
171 CheckException(env);
172
173 ScopedJavaLocalRef<jstring> str =
174 ConvertUTF16ToJavaString(env,
175 prefs.standard_font_family_map[WebPreferences::kCommonScript]);
176 env->SetObjectField(obj, field_ids_->standard_fond_family, str.obj());
177 CheckException(env);
178
179 str.Reset(ConvertUTF16ToJavaString(env,
180 prefs.fixed_font_family_map[WebPreferences::kCommonScript]));
181 env->SetObjectField(obj, field_ids_->fixed_font_family, str.obj());
182 CheckException(env);
183
184 str.Reset(ConvertUTF16ToJavaString(env,
185 prefs.sans_serif_font_family_map[WebPreferences::kCommonScript]));
186 env->SetObjectField(obj, field_ids_->sans_serif_font_family, str.obj());
187 CheckException(env);
188
189 str.Reset(ConvertUTF16ToJavaString(env,
190 prefs.serif_font_family_map[WebPreferences::kCommonScript]));
191 env->SetObjectField(obj, field_ids_->serif_font_family, str.obj());
192 CheckException(env);
193
194 str.Reset(ConvertUTF16ToJavaString(env,
195 prefs.cursive_font_family_map[WebPreferences::kCommonScript]));
196 env->SetObjectField(obj, field_ids_->cursive_font_family, str.obj());
197 CheckException(env);
198
199 str.Reset(ConvertUTF16ToJavaString(env,
200 prefs.fantasy_font_family_map[WebPreferences::kCommonScript]));
201 env->SetObjectField(obj, field_ids_->fantasy_font_family, str.obj());
202 CheckException(env);
203
204 str.Reset(ConvertUTF8ToJavaString(env, prefs.default_encoding));
205 env->SetObjectField(obj, field_ids_->default_text_encoding, str.obj());
206 CheckException(env);
207
208 str.Reset(ConvertUTF8ToJavaString(env, webkit_glue::GetUserAgent(GURL(""))));
209 env->SetObjectField(obj, field_ids_->user_agent, str.obj());
210 CheckException(env);
211
212 env->SetIntField(obj, field_ids_->minimum_font_size,
213 prefs.minimum_font_size);
214 CheckException(env);
215
216 env->SetIntField(
217 obj,
218 field_ids_->minimum_logical_font_size, prefs.minimum_logical_font_size);
219 CheckException(env);
220
221 env->SetIntField(obj, field_ids_->default_font_size,
222 prefs.default_font_size);
223 CheckException(env);
224
225 env->SetIntField(
226 obj, field_ids_->default_fixed_font_size, prefs.default_fixed_font_size);
227 CheckException(env);
228
229 env->SetBooleanField(
230 obj,
231 field_ids_->load_images_automatically, prefs.loads_images_automatically);
232 CheckException(env);
233
234 env->SetBooleanField(
235 obj,
236 field_ids_->images_enabled, prefs.images_enabled);
237 CheckException(env);
238 71
239 env->SetBooleanField( 72 env->SetBooleanField(
240 obj, field_ids_->java_script_enabled, prefs.javascript_enabled); 73 obj, field_ids_->java_script_enabled, prefs.javascript_enabled);
241 CheckException(env); 74 base::android::CheckException(env);
242
243 env->SetBooleanField(
244 obj,
245 field_ids_->allow_universal_access_from_file_urls,
246 prefs.allow_universal_access_from_file_urls);
247 CheckException(env);
248
249 env->SetBooleanField(
250 obj,
251 field_ids_->allow_file_access_from_file_urls,
252 prefs.allow_file_access_from_file_urls);
253 CheckException(env);
254
255 env->SetBooleanField(
256 obj,
257 field_ids_->java_script_can_open_windows_automatically,
258 prefs.javascript_can_open_windows_automatically);
259 CheckException(env);
260
261 env->SetBooleanField(
262 obj,
263 field_ids_->support_multiple_windows,
264 prefs.supports_multiple_windows);
265 CheckException(env);
266
267 Java_ContentSettings_setPluginsDisabled(env, obj, !prefs.plugins_enabled);
268 CheckException(env);
269
270 // We don't need to sync AppCache settings to Java, because there are
271 // no getters for them in the API.
272
273 env->SetBooleanField(
274 obj,
275 field_ids_->dom_storage_enabled,
276 prefs.local_storage_enabled);
277 CheckException(env);
278
279 env->SetBooleanField(
280 obj,
281 field_ids_->database_enabled,
282 prefs.databases_enabled);
283 CheckException(env);
284
285 env->SetBooleanField(
286 obj,
287 field_ids_->use_wide_viewport,
288 prefs.use_wide_viewport);
289 CheckException(env);
290
291 env->SetBooleanField(
292 obj,
293 field_ids_->load_with_overview_mode,
294 prefs.initialize_at_minimum_page_scale);
295 CheckException(env);
296
297 env->SetBooleanField(
298 obj,
299 field_ids_->media_playback_requires_user_gesture,
300 prefs.user_gesture_required_for_media_playback);
301 CheckException(env);
302
303 str.Reset(
304 ConvertUTF8ToJavaString(env, prefs.default_video_poster_url.spec()));
305 env->SetObjectField(obj, field_ids_->default_video_poster_url, str.obj());
306 CheckException(env);
307 }
308
309 void ContentSettings::SyncToNativeImpl() {
310 JNIEnv* env = base::android::AttachCurrentThread();
311 CHECK(env);
312 if (!field_ids_)
313 field_ids_.reset(new FieldIds(env));
314
315 ScopedJavaLocalRef<jobject> scoped_obj = content_settings_.get(env);
316 jobject obj = scoped_obj.obj();
317 if (!obj)
318 return;
319 RenderViewHost* render_view_host = web_contents()->GetRenderViewHost();
320 WebPreferences prefs = render_view_host->GetDelegate()->GetWebkitPrefs();
321
322 prefs.text_autosizing_enabled =
323 Java_ContentSettings_getTextAutosizingEnabled(env, obj);
324
325 int text_size_percent = env->GetIntField(obj, field_ids_->text_size_percent);
326 if (prefs.text_autosizing_enabled) {
327 prefs.font_scale_factor = text_size_percent / 100.0f;
328 prefs.force_enable_zoom = text_size_percent >= 130;
329 } else {
330 prefs.force_enable_zoom = false;
331 }
332
333 ScopedJavaLocalRef<jstring> str(
334 env, static_cast<jstring>(
335 env->GetObjectField(obj, field_ids_->standard_fond_family)));
336 prefs.standard_font_family_map[WebPreferences::kCommonScript] =
337 ConvertJavaStringToUTF16(str);
338
339 str.Reset(
340 env, static_cast<jstring>(
341 env->GetObjectField(obj, field_ids_->fixed_font_family)));
342 prefs.fixed_font_family_map[WebPreferences::kCommonScript] =
343 ConvertJavaStringToUTF16(str);
344
345 str.Reset(
346 env, static_cast<jstring>(
347 env->GetObjectField(obj, field_ids_->sans_serif_font_family)));
348 prefs.sans_serif_font_family_map[WebPreferences::kCommonScript] =
349 ConvertJavaStringToUTF16(str);
350
351 str.Reset(
352 env, static_cast<jstring>(
353 env->GetObjectField(obj, field_ids_->serif_font_family)));
354 prefs.serif_font_family_map[WebPreferences::kCommonScript] =
355 ConvertJavaStringToUTF16(str);
356
357 str.Reset(
358 env, static_cast<jstring>(
359 env->GetObjectField(obj, field_ids_->cursive_font_family)));
360 prefs.cursive_font_family_map[WebPreferences::kCommonScript] =
361 ConvertJavaStringToUTF16(str);
362
363 str.Reset(
364 env, static_cast<jstring>(
365 env->GetObjectField(obj, field_ids_->fantasy_font_family)));
366 prefs.fantasy_font_family_map[WebPreferences::kCommonScript] =
367 ConvertJavaStringToUTF16(str);
368
369 str.Reset(
370 env, static_cast<jstring>(
371 env->GetObjectField(obj, field_ids_->default_text_encoding)));
372 prefs.default_encoding = ConvertJavaStringToUTF8(str);
373
374 prefs.minimum_font_size =
375 env->GetIntField(obj, field_ids_->minimum_font_size);
376
377 prefs.minimum_logical_font_size =
378 env->GetIntField(obj, field_ids_->minimum_logical_font_size);
379
380 prefs.default_font_size =
381 env->GetIntField(obj, field_ids_->default_font_size);
382
383 prefs.default_fixed_font_size =
384 env->GetIntField(obj, field_ids_->default_fixed_font_size);
385
386 prefs.loads_images_automatically =
387 env->GetBooleanField(obj, field_ids_->load_images_automatically);
388
389 prefs.images_enabled =
390 env->GetBooleanField(obj, field_ids_->images_enabled);
391
392 prefs.javascript_enabled =
393 env->GetBooleanField(obj, field_ids_->java_script_enabled);
394
395 prefs.allow_universal_access_from_file_urls = env->GetBooleanField(
396 obj, field_ids_->allow_universal_access_from_file_urls);
397
398 prefs.allow_file_access_from_file_urls = env->GetBooleanField(
399 obj, field_ids_->allow_file_access_from_file_urls);
400
401 prefs.javascript_can_open_windows_automatically = env->GetBooleanField(
402 obj, field_ids_->java_script_can_open_windows_automatically);
403
404 prefs.supports_multiple_windows = env->GetBooleanField(
405 obj, field_ids_->support_multiple_windows);
406
407 prefs.plugins_enabled = !Java_ContentSettings_getPluginsDisabled(env, obj);
408
409 prefs.application_cache_enabled =
410 Java_ContentSettings_getAppCacheEnabled(env, obj);
411
412 prefs.local_storage_enabled = env->GetBooleanField(
413 obj, field_ids_->dom_storage_enabled);
414
415 prefs.databases_enabled = env->GetBooleanField(
416 obj, field_ids_->database_enabled);
417
418 prefs.double_tap_to_zoom_enabled = prefs.use_wide_viewport =
419 env->GetBooleanField(obj, field_ids_->use_wide_viewport);
420
421 prefs.initialize_at_minimum_page_scale = env->GetBooleanField(
422 obj, field_ids_->load_with_overview_mode);
423
424 prefs.user_gesture_required_for_media_playback = env->GetBooleanField(
425 obj, field_ids_->media_playback_requires_user_gesture);
426
427 str.Reset(
428 env, static_cast<jstring>(
429 env->GetObjectField(obj, field_ids_->default_video_poster_url)));
430 prefs.default_video_poster_url = str.obj() ?
431 GURL(ConvertJavaStringToUTF8(str)) : GURL();
432
433 prefs.support_deprecated_target_density_dpi = env->GetBooleanField(
434 obj, field_ids_->support_deprecated_target_density_dpi);
435
436 render_view_host->UpdateWebkitPreferences(prefs);
437 } 75 }
438 76
439 void ContentSettings::SyncFromNative(JNIEnv* env, jobject obj) { 77 void ContentSettings::SyncFromNative(JNIEnv* env, jobject obj) {
440 SyncFromNativeImpl(); 78 SyncFromNativeImpl();
441 } 79 }
442 80
443 void ContentSettings::SyncToNative(JNIEnv* env, jobject obj) {
444 SyncToNativeImpl();
445 }
446
447 void ContentSettings::RenderViewCreated(RenderViewHost* render_view_host) {
448 if (is_master_mode_)
449 SyncToNativeImpl();
450 }
451
452 void ContentSettings::WebContentsDestroyed(WebContents* web_contents) { 81 void ContentSettings::WebContentsDestroyed(WebContents* web_contents) {
453 delete this; 82 delete this;
454 } 83 }
455 84
456 static jint Init(JNIEnv* env, jobject obj, jint nativeContentViewCore, 85 static jint Init(JNIEnv* env, jobject obj, jint nativeContentViewCore) {
457 jboolean is_master_mode) {
458 WebContents* web_contents = 86 WebContents* web_contents =
459 reinterpret_cast<ContentViewCoreImpl*>(nativeContentViewCore) 87 reinterpret_cast<ContentViewCoreImpl*>(nativeContentViewCore)
460 ->GetWebContents(); 88 ->GetWebContents();
461 ContentSettings* content_settings = 89 ContentSettings* content_settings =
462 new ContentSettings(env, obj, web_contents, is_master_mode); 90 new ContentSettings(env, obj, web_contents);
463 return reinterpret_cast<jint>(content_settings); 91 return reinterpret_cast<jint>(content_settings);
464 } 92 }
465 93
466 static jstring GetDefaultUserAgent(JNIEnv* env, jclass clazz) {
467 return base::android::ConvertUTF8ToJavaString(
468 env, GetContentClient()->GetUserAgent()).Release();
469 }
470
471 } // namespace content 94 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698