summaryrefslogtreecommitdiffstats
path: root/content/browser/android/content_settings.cc
blob: b023f2b1acd3e0621e335c83a5d30c5ff7d08b4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/android/content_settings.h"

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "content/browser/android/content_view_core_impl.h"
#include "content/browser/renderer_host/render_view_host_delegate.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/public/browser/web_contents.h"
#include "jni/ContentSettings_jni.h"
#include "webkit/glue/user_agent.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/glue/webpreferences.h"

using base::android::CheckException;
using base::android::ConvertJavaStringToUTF16;
using base::android::ConvertUTF16ToJavaString;
using base::android::ConvertUTF8ToJavaString;
using base::android::GetClass;
using base::android::GetFieldID;
using base::android::GetMethodIDFromClassName;
using base::android::ScopedJavaLocalRef;
using webkit_glue::WebPreferences;

namespace content {

struct ContentSettings::FieldIds {
  // Note on speed. One may think that an approach that reads field values via
  // JNI is ineffective and should not be used. Please keep in mind that in the
  // legacy WebView the whole Sync method took <1ms on Xoom, and no one is
  // expected to modify settings in performance-critical code.
  FieldIds() { }

  FieldIds(JNIEnv* env) {
    const char* kStringClassName = "Ljava/lang/String;";

    // FIXME: we should be using a new GetFieldIDFromClassName() with caching.
    ScopedJavaLocalRef<jclass> clazz(
        GetClass(env, "org/chromium/content/browser/ContentSettings"));
    standard_fond_family =
        GetFieldID(env, clazz, "mStandardFontFamily", kStringClassName);
    fixed_font_family =
        GetFieldID(env, clazz, "mFixedFontFamily", kStringClassName);
    sans_serif_font_family =
        GetFieldID(env, clazz, "mSansSerifFontFamily", kStringClassName);
    serif_font_family =
        GetFieldID(env, clazz, "mSerifFontFamily", kStringClassName);
    cursive_font_family =
        GetFieldID(env, clazz, "mCursiveFontFamily", kStringClassName);
    fantasy_font_family =
        GetFieldID(env, clazz, "mFantasyFontFamily", kStringClassName);
    default_text_encoding =
        GetFieldID(env, clazz, "mDefaultTextEncoding", kStringClassName);
    user_agent =
        GetFieldID(env, clazz, "mUserAgent", kStringClassName);
    minimum_font_size = GetFieldID(env, clazz, "mMinimumFontSize", "I");
    minimum_logical_font_size =
        GetFieldID(env, clazz, "mMinimumLogicalFontSize", "I");
    default_font_size = GetFieldID(env, clazz, "mDefaultFontSize", "I");
    default_fixed_font_size =
        GetFieldID(env, clazz, "mDefaultFixedFontSize", "I");
    load_images_automatically =
        GetFieldID(env, clazz, "mLoadsImagesAutomatically", "Z");
    java_script_enabled =
        GetFieldID(env, clazz, "mJavaScriptEnabled", "Z");
    allow_universal_access_from_file_urls =
        GetFieldID(env, clazz, "mAllowUniversalAccessFromFileURLs", "Z");
    allow_file_access_from_file_urls =
        GetFieldID(env, clazz, "mAllowFileAccessFromFileURLs", "Z");
    java_script_can_open_windows_automatically =
        GetFieldID(env, clazz, "mJavaScriptCanOpenWindowsAutomatically", "Z");
    dom_storage_enabled =
        GetFieldID(env, clazz, "mDomStorageEnabled", "Z");
  }

  // Field ids
  jfieldID standard_fond_family;
  jfieldID fixed_font_family;
  jfieldID sans_serif_font_family;
  jfieldID serif_font_family;
  jfieldID cursive_font_family;
  jfieldID fantasy_font_family;
  jfieldID default_text_encoding;
  jfieldID user_agent;
  jfieldID minimum_font_size;
  jfieldID minimum_logical_font_size;
  jfieldID default_font_size;
  jfieldID default_fixed_font_size;
  jfieldID load_images_automatically;
  jfieldID java_script_enabled;
  jfieldID allow_universal_access_from_file_urls;
  jfieldID allow_file_access_from_file_urls;
  jfieldID java_script_can_open_windows_automatically;
  jfieldID dom_storage_enabled;
};

ContentSettings::ContentSettings(JNIEnv* env,
                         jobject obj,
                         WebContents* contents,
                         bool is_master_mode)
    : WebContentsObserver(contents),
      is_master_mode_(is_master_mode) {
  content_settings_.Reset(env, obj);
}

ContentSettings::~ContentSettings() {
}

// static
bool ContentSettings::RegisterContentSettings(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

void ContentSettings::Destroy(JNIEnv* env, jobject obj) {
  delete this;
}

void ContentSettings::SyncFromNativeImpl() {
  JNIEnv* env = base::android::AttachCurrentThread();
  CHECK(env);
  if (!field_ids_.get())
    field_ids_.reset(new FieldIds(env));

  jobject obj = content_settings_.obj();
  RenderViewHost* render_view_host = web_contents()->GetRenderViewHost();
  WebPreferences prefs = render_view_host->GetDelegate()->GetWebkitPrefs();

  ScopedJavaLocalRef<jstring> str =
      ConvertUTF16ToJavaString(env,
          prefs.standard_font_family_map[WebPreferences::kCommonScript]);
  env->SetObjectField(obj, field_ids_->standard_fond_family, str.obj());
  CheckException(env);

  str.Reset(ConvertUTF16ToJavaString(env,
      prefs.fixed_font_family_map[WebPreferences::kCommonScript]));
  env->SetObjectField(obj, field_ids_->fixed_font_family, str.obj());
  CheckException(env);

  str.Reset(ConvertUTF16ToJavaString(env,
      prefs.sans_serif_font_family_map[WebPreferences::kCommonScript]));
  env->SetObjectField(obj, field_ids_->sans_serif_font_family, str.obj());
  CheckException(env);

  str.Reset(ConvertUTF16ToJavaString(env,
      prefs.serif_font_family_map[WebPreferences::kCommonScript]));
  env->SetObjectField(obj, field_ids_->serif_font_family, str.obj());
  CheckException(env);

  str.Reset(ConvertUTF16ToJavaString(env,
      prefs.cursive_font_family_map[WebPreferences::kCommonScript]));
  env->SetObjectField(obj, field_ids_->cursive_font_family, str.obj());
  CheckException(env);

  str.Reset(ConvertUTF16ToJavaString(env,
      prefs.fantasy_font_family_map[WebPreferences::kCommonScript]));
  env->SetObjectField(obj, field_ids_->fantasy_font_family, str.obj());
  CheckException(env);

  str.Reset(ConvertUTF8ToJavaString(env, prefs.default_encoding));
  env->SetObjectField(obj, field_ids_->default_text_encoding, str.obj());
  CheckException(env);

  str.Reset(ConvertUTF8ToJavaString(env, webkit_glue::GetUserAgent(GURL(""))));
  env->SetObjectField(obj, field_ids_->user_agent, str.obj());
  CheckException(env);

  env->SetIntField(obj, field_ids_->minimum_font_size,
                   prefs.minimum_font_size);
  CheckException(env);

  env->SetIntField(
      obj,
      field_ids_->minimum_logical_font_size, prefs.minimum_logical_font_size);
  CheckException(env);

  env->SetIntField(obj, field_ids_->default_font_size,
                   prefs.default_font_size);
  CheckException(env);

  env->SetIntField(
      obj, field_ids_->default_fixed_font_size, prefs.default_fixed_font_size);
  CheckException(env);

  env->SetBooleanField(
      obj,
      field_ids_->load_images_automatically, prefs.loads_images_automatically);
  CheckException(env);

  env->SetBooleanField(
      obj, field_ids_->java_script_enabled, prefs.javascript_enabled);
  CheckException(env);

  env->SetBooleanField(
      obj,
      field_ids_->allow_universal_access_from_file_urls,
      prefs.allow_universal_access_from_file_urls);
  CheckException(env);

  env->SetBooleanField(
      obj,
      field_ids_->allow_file_access_from_file_urls,
      prefs.allow_file_access_from_file_urls);
  CheckException(env);

  env->SetBooleanField(
      obj,
      field_ids_->java_script_can_open_windows_automatically,
      prefs.javascript_can_open_windows_automatically);
  CheckException(env);

  Java_ContentSettings_setPluginsDisabled(env, obj, !prefs.plugins_enabled);
  CheckException(env);

  env->SetBooleanField(
      obj,
      field_ids_->dom_storage_enabled,
      prefs.local_storage_enabled);
  CheckException(env);
}

void ContentSettings::SyncToNativeImpl() {
  JNIEnv* env = base::android::AttachCurrentThread();
  CHECK(env);
  if (!field_ids_.get())
    field_ids_.reset(new FieldIds(env));

  jobject obj = content_settings_.obj();
  RenderViewHost* render_view_host = web_contents()->GetRenderViewHost();
  WebPreferences prefs = render_view_host->GetDelegate()->GetWebkitPrefs();

  ScopedJavaLocalRef<jstring> str(
      env, static_cast<jstring>(
          env->GetObjectField(obj, field_ids_->standard_fond_family)));
  prefs.standard_font_family_map[WebPreferences::kCommonScript] =
      ConvertJavaStringToUTF16(str);

  str.Reset(
      env, static_cast<jstring>(
          env->GetObjectField(obj, field_ids_->fixed_font_family)));
  prefs.fixed_font_family_map[WebPreferences::kCommonScript] =
      ConvertJavaStringToUTF16(str);

  str.Reset(
      env, static_cast<jstring>(
          env->GetObjectField(obj, field_ids_->sans_serif_font_family)));
  prefs.sans_serif_font_family_map[WebPreferences::kCommonScript] =
      ConvertJavaStringToUTF16(str);

  str.Reset(
      env, static_cast<jstring>(
          env->GetObjectField(obj, field_ids_->serif_font_family)));
  prefs.serif_font_family_map[WebPreferences::kCommonScript] =
      ConvertJavaStringToUTF16(str);

  str.Reset(
      env, static_cast<jstring>(
          env->GetObjectField(obj, field_ids_->cursive_font_family)));
  prefs.cursive_font_family_map[WebPreferences::kCommonScript] =
      ConvertJavaStringToUTF16(str);

  str.Reset(
      env, static_cast<jstring>(
          env->GetObjectField(obj, field_ids_->fantasy_font_family)));
  prefs.fantasy_font_family_map[WebPreferences::kCommonScript] =
      ConvertJavaStringToUTF16(str);

  str.Reset(
      env, static_cast<jstring>(
          env->GetObjectField(obj, field_ids_->default_text_encoding)));
  prefs.default_encoding = ConvertJavaStringToUTF8(str);

  prefs.minimum_font_size =
      env->GetIntField(obj, field_ids_->minimum_font_size);

  prefs.minimum_logical_font_size =
      env->GetIntField(obj, field_ids_->minimum_logical_font_size);

  prefs.default_font_size =
      env->GetIntField(obj, field_ids_->default_font_size);

  prefs.default_fixed_font_size =
      env->GetIntField(obj, field_ids_->default_fixed_font_size);

  prefs.loads_images_automatically =
      env->GetBooleanField(obj, field_ids_->load_images_automatically);

  prefs.javascript_enabled =
      env->GetBooleanField(obj, field_ids_->java_script_enabled);

  prefs.allow_universal_access_from_file_urls = env->GetBooleanField(
      obj, field_ids_->allow_universal_access_from_file_urls);

  prefs.allow_file_access_from_file_urls = env->GetBooleanField(
      obj, field_ids_->allow_file_access_from_file_urls);

  prefs.javascript_can_open_windows_automatically = env->GetBooleanField(
      obj, field_ids_->java_script_can_open_windows_automatically);

  prefs.plugins_enabled = !Java_ContentSettings_getPluginsDisabled(env, obj);

  prefs.local_storage_enabled = env->GetBooleanField(
      obj, field_ids_->dom_storage_enabled);

  render_view_host->UpdateWebkitPreferences(prefs);
}

void ContentSettings::SyncFromNative(JNIEnv* env, jobject obj) {
  SyncFromNativeImpl();
}

void ContentSettings::SyncToNative(JNIEnv* env, jobject obj) {
  SyncToNativeImpl();
}

void ContentSettings::RenderViewCreated(RenderViewHost* render_view_host) {
  if (is_master_mode_)
    SyncToNativeImpl();
}

static jint Init(JNIEnv* env, jobject obj, jint nativeContentViewCore,
                 jboolean is_master_mode) {
  WebContents* web_contents =
      reinterpret_cast<ContentViewCoreImpl*>(nativeContentViewCore)
          ->web_contents();
  ContentSettings* content_settings =
      new ContentSettings(env, obj, web_contents, is_master_mode);
  return reinterpret_cast<jint>(content_settings);
}

static jstring GetDefaultUserAgent(JNIEnv* env, jclass clazz) {
  // "Version/4.0" had been hardcoded in the legacy WebView.
  std::string ua = webkit_glue::BuildUserAgentFromProduct("Version/4.0");
  return base::android::ConvertUTF8ToJavaString(env, ua).Release();
}

}  // namespace content