1 // Copyright 2014 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/permission/aw_permission_request.h"
7 #include "android_webview/native/permission/aw_permission_request_delegate.h"
8 #include "base/android/jni_string.h"
9 #include "jni/AwPermissionRequest_jni.h"
11 using base::android::AttachCurrentThread
;
12 using base::android::ConvertUTF8ToJavaString
;
13 using base::android::ScopedJavaLocalRef
;
15 namespace android_webview
{
18 base::android::ScopedJavaLocalRef
<jobject
> AwPermissionRequest::Create(
19 scoped_ptr
<AwPermissionRequestDelegate
> delegate
,
20 base::WeakPtr
<AwPermissionRequest
>* weak_ptr
) {
21 base::android::ScopedJavaLocalRef
<jobject
> java_peer
;
22 AwPermissionRequest
* permission_request
=
23 new AwPermissionRequest(delegate
.Pass(), &java_peer
);
24 *weak_ptr
= permission_request
->weak_factory_
.GetWeakPtr();
28 AwPermissionRequest::AwPermissionRequest(
29 scoped_ptr
<AwPermissionRequestDelegate
> delegate
,
30 ScopedJavaLocalRef
<jobject
>* java_peer
)
31 : delegate_(delegate
.Pass()),
34 DCHECK(delegate_
.get());
37 JNIEnv
* env
= AttachCurrentThread();
38 *java_peer
= Java_AwPermissionRequest_create(
39 env
, reinterpret_cast<jlong
>(this),
40 ConvertUTF8ToJavaString(env
, GetOrigin().spec()).obj(), GetResources());
41 java_ref_
= JavaObjectWeakGlobalRef(env
, java_peer
->obj());
44 AwPermissionRequest::~AwPermissionRequest() {
45 OnAcceptInternal(false);
48 void AwPermissionRequest::OnAccept(JNIEnv
* env
,
51 OnAcceptInternal(accept
);
54 void AwPermissionRequest::OnAcceptInternal(bool accept
) {
56 delegate_
->NotifyRequestResult(accept
);
61 void AwPermissionRequest::DeleteThis() {
62 ScopedJavaLocalRef
<jobject
> j_request
= GetJavaObject();
63 if (j_request
.is_null())
65 Java_AwPermissionRequest_destroyNative(AttachCurrentThread(),
69 void AwPermissionRequest::Destroy(JNIEnv
* env
, jobject obj
) {
73 ScopedJavaLocalRef
<jobject
> AwPermissionRequest::GetJavaObject() {
74 return java_ref_
.get(AttachCurrentThread());
77 const GURL
& AwPermissionRequest::GetOrigin() {
78 return delegate_
->GetOrigin();
81 int64
AwPermissionRequest::GetResources() {
82 return delegate_
->GetResources();
85 void AwPermissionRequest::CancelAndDelete() {
90 bool RegisterAwPermissionRequest(JNIEnv
* env
) {
91 return RegisterNativesImpl(env
);
94 } // namespace android_webivew