Home | History | Annotate | Download | only in web_contents
      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 
     14 using base::android::AttachCurrentThread;
     15 
     16 namespace content {
     17 
     18 // static
     19 WebContents* 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
     35 bool WebContentsAndroid::Register(JNIEnv* env) {
     36   return RegisterNativesImpl(env);
     37 }
     38 
     39 WebContentsAndroid::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 
     50 WebContentsAndroid::~WebContentsAndroid() {
     51   Java_WebContentsImpl_destroy(AttachCurrentThread(), obj_.obj());
     52 }
     53 
     54 base::android::ScopedJavaLocalRef<jobject>
     55 WebContentsAndroid::GetJavaObject() {
     56   return base::android::ScopedJavaLocalRef<jobject>(obj_);
     57 }
     58 
     59 ScopedJavaLocalRef<jstring> WebContentsAndroid::GetTitle(
     60     JNIEnv* env, jobject obj) const {
     61   return base::android::ConvertUTF16ToJavaString(env,
     62                                                  web_contents_->GetTitle());
     63 }
     64 
     65 ScopedJavaLocalRef<jstring> WebContentsAndroid::GetVisibleURL(
     66     JNIEnv* env, jobject obj) const {
     67   return base::android::ConvertUTF8ToJavaString(
     68       env, web_contents_->GetVisibleURL().spec());
     69 }
     70 
     71 void WebContentsAndroid::Stop(JNIEnv* env, jobject obj) {
     72   web_contents_->Stop();
     73 }
     74 
     75 }  // namespace content
     76