15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/android/content_settings.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/android/jni_android.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/android/content_view_core_impl.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/renderer_host/render_view_host_delegate.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/renderer_host/render_view_host_impl.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/common/web_preferences.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jni/ContentSettings_jni.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ContentSettings::ContentSettings(JNIEnv* env,
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         jobject obj,
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         WebContents* contents)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : WebContentsObserver(contents),
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content_settings_(env, obj) {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ContentSettings::~ContentSettings() {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = content_settings_.get(env);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (obj.obj()) {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Java_ContentSettings_onNativeContentSettingsDestroyed(env, obj.obj(),
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        reinterpret_cast<intptr_t>(this));
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ContentSettings::RegisterContentSettings(JNIEnv* env) {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return RegisterNativesImpl(env);
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ContentSettings::GetJavaScriptEnabled(JNIEnv* env, jobject obj) {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderViewHost* render_view_host = web_contents()->GetRenderViewHost();
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!render_view_host)
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return render_view_host->GetWebkitPreferences().javascript_enabled;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
45010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void ContentSettings::WebContentsDestroyed() {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  delete this;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)static jlong Init(JNIEnv* env, jobject obj, jlong nativeContentViewCore) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* web_contents =
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      reinterpret_cast<ContentViewCoreImpl*>(nativeContentViewCore)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          ->GetWebContents();
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ContentSettings* content_settings =
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      new ContentSettings(env, obj, web_contents);
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return reinterpret_cast<intptr_t>(content_settings);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
59