web_contents_android.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1// Copyright 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. 4 5#include "content/browser/web_contents/web_contents_android.h" 6 7#include "base/android/jni_android.h" 8#include "base/android/jni_string.h" 9#include "base/logging.h" 10#include "content/public/browser/browser_thread.h" 11#include "content/public/browser/web_contents.h" 12#include "jni/WebContentsImpl_jni.h" 13 14using base::android::AttachCurrentThread; 15 16namespace content { 17 18// static 19WebContents* WebContents::FromJavaWebContents( 20 jobject jweb_contents_android) { 21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 22 if (!jweb_contents_android) 23 return NULL; 24 25 WebContentsAndroid* web_contents_android = 26 reinterpret_cast<WebContentsAndroid*>( 27 Java_WebContentsImpl_getNativePointer(AttachCurrentThread(), 28 jweb_contents_android)); 29 if (!web_contents_android) 30 return NULL; 31 return web_contents_android->web_contents(); 32} 33 34// static 35bool WebContentsAndroid::Register(JNIEnv* env) { 36 return RegisterNativesImpl(env); 37} 38 39WebContentsAndroid::WebContentsAndroid(WebContents* web_contents) 40 : web_contents_(web_contents), 41 navigation_controller_(&(web_contents->GetController())) { 42 JNIEnv* env = AttachCurrentThread(); 43 obj_.Reset(env, 44 Java_WebContentsImpl_create( 45 env, 46 reinterpret_cast<intptr_t>(this), 47 navigation_controller_.GetJavaObject().obj()).obj()); 48} 49 50WebContentsAndroid::~WebContentsAndroid() { 51 Java_WebContentsImpl_destroy(AttachCurrentThread(), obj_.obj()); 52} 53 54base::android::ScopedJavaLocalRef<jobject> 55WebContentsAndroid::GetJavaObject() { 56 return base::android::ScopedJavaLocalRef<jobject>(obj_); 57} 58 59ScopedJavaLocalRef<jstring> WebContentsAndroid::GetTitle( 60 JNIEnv* env, jobject obj) const { 61 return base::android::ConvertUTF16ToJavaString(env, 62 web_contents_->GetTitle()); 63} 64 65void WebContentsAndroid::Stop(JNIEnv* env, jobject obj) { 66 web_contents_->Stop(); 67} 68 69} // namespace content 70