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.
5 #include "content/browser/web_contents/web_contents_android.h"
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"
14 using base::android::AttachCurrentThread;
19 WebContents* WebContents::FromJavaWebContents(
20 jobject jweb_contents_android) {
21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
22 if (!jweb_contents_android)
25 WebContentsAndroid* web_contents_android =
26 reinterpret_cast<WebContentsAndroid*>(
27 Java_WebContentsImpl_getNativePointer(AttachCurrentThread(),
28 jweb_contents_android));
29 if (!web_contents_android)
31 return web_contents_android->web_contents();
35 bool WebContentsAndroid::Register(JNIEnv* env) {
36 return RegisterNativesImpl(env);
39 WebContentsAndroid::WebContentsAndroid(WebContents* web_contents)
40 : web_contents_(web_contents),
41 navigation_controller_(&(web_contents->GetController())) {
42 JNIEnv* env = AttachCurrentThread();
44 Java_WebContentsImpl_create(
46 reinterpret_cast<intptr_t>(this),
47 navigation_controller_.GetJavaObject().obj()).obj());
50 WebContentsAndroid::~WebContentsAndroid() {
51 Java_WebContentsImpl_destroy(AttachCurrentThread(), obj_.obj());
54 base::android::ScopedJavaLocalRef<jobject>
55 WebContentsAndroid::GetJavaObject() {
56 return base::android::ScopedJavaLocalRef<jobject>(obj_);
59 ScopedJavaLocalRef<jstring> WebContentsAndroid::GetTitle(
60 JNIEnv* env, jobject obj) const {
61 return base::android::ConvertUTF16ToJavaString(env,
62 web_contents_->GetTitle());
65 void WebContentsAndroid::Stop(JNIEnv* env, jobject obj) {
66 web_contents_->Stop();
69 } // namespace content