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