1 // Copyright (c) 2012 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 "android_webview/native/aw_contents.h"
7 #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h"
8 #include "android_webview/native/aw_browser_dependency_factory.h"
9 #include "android_webview/native/aw_contents_container.h"
10 #include "android_webview/native/aw_web_contents_delegate.h"
11 #include "base/android/jni_android.h"
12 #include "base/android/jni_string.h"
13 #include "base/bind.h"
14 #include "base/callback.h"
15 #include "base/supports_user_data.h"
16 #include "content/public/browser/android/content_view_core.h"
17 #include "content/public/browser/web_contents.h"
18 #include "jni/AwContents_jni.h"
20 using base::android::AttachCurrentThread
;
21 using base::android::ConvertUTF16ToJavaString
;
22 using base::android::ScopedJavaGlobalRef
;
23 using base::android::ScopedJavaLocalRef
;
24 using content::ContentViewCore
;
25 using content::WebContents
;
27 namespace android_webview
{
31 const void* kAwContentsUserDataKey
= &kAwContentsUserDataKey
;
33 class AwContentsUserData
: public base::SupportsUserData::Data
{
35 AwContentsUserData(AwContents
* ptr
) : contents_(ptr
) {}
36 AwContents
* get() { return contents_
; }
39 AwContents
* contents_
;
45 AwContents
* AwContents::FromWebContents(content::WebContents
* web_contents
) {
47 AwContentsUserData
* data
= reinterpret_cast<AwContentsUserData
*>(
48 web_contents
->GetUserData(kAwContentsUserDataKey
));
49 if (data
) return data
->get();
54 AwContents::AwContents(JNIEnv
* env
,
56 jobject web_contents_delegate
,
57 bool private_browsing
)
58 : java_ref_(env
, obj
),
59 web_contents_delegate_(
60 new AwWebContentsDelegate(env
, web_contents_delegate
)) {
61 android_webview::AwBrowserDependencyFactory
* dependency_factory
=
62 android_webview::AwBrowserDependencyFactory::GetInstance();
63 content::WebContents
* web_contents
=
64 dependency_factory
->CreateWebContents(private_browsing
);
65 contents_container_
.reset(dependency_factory
->CreateContentsContainer(
67 web_contents
->SetDelegate(web_contents_delegate_
.get());
68 web_contents_delegate_
->SetJavaScriptDialogCreator(
69 dependency_factory
->GetJavaScriptDialogCreator());
70 web_contents
->SetUserData(kAwContentsUserDataKey
,
71 new AwContentsUserData(this));
72 render_view_host_ext_
.reset(new AwRenderViewHostExt(web_contents
));
75 AwContents::~AwContents() {
76 content::WebContents
* web_contents
= contents_container_
->GetWebContents();
77 DCHECK(AwContents::FromWebContents(web_contents
) == this);
78 web_contents
->RemoveUserData(kAwContentsUserDataKey
);
81 jint
AwContents::GetWebContents(JNIEnv
* env
, jobject obj
) {
82 return reinterpret_cast<jint
>(contents_container_
->GetWebContents());
85 void AwContents::Destroy(JNIEnv
* env
, jobject obj
) {
90 // |message| is passed as base::Owned, so it will automatically be deleted
91 // when the callback goes out of scope.
92 void DocumentHasImagesCallback(ScopedJavaGlobalRef
<jobject
>* message
,
94 Java_AwContents_onDocumentHasImagesResponse(AttachCurrentThread(),
100 void AwContents::DocumentHasImages(JNIEnv
* env
, jobject obj
, jobject message
) {
101 render_view_host_ext_
->DocumentHasImages(
102 base::Bind(&DocumentHasImagesCallback
,
103 base::Owned(new ScopedJavaGlobalRef
<jobject
>(
104 ScopedJavaLocalRef
<jobject
>(env
, message
)))));
107 static jint
Init(JNIEnv
* env
,
109 jobject web_contents_delegate
,
110 jboolean private_browsing
) {
111 AwContents
* tab
= new AwContents(env
, obj
, web_contents_delegate
,
113 return reinterpret_cast<jint
>(tab
);
116 bool RegisterAwContents(JNIEnv
* env
) {
117 return RegisterNativesImpl(env
) >= 0;
121 } // namespace android_webview