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.
4
5 #include "android_webview/native/permission/aw_permission_request.h"
6
7 #include "android_webview/native/permission/aw_permission_request_delegate.h"
8 #include "base/android/jni_string.h"
9 #include "jni/AwPermissionRequest_jni.h"
10
11 using base::android::AttachCurrentThread;
12 using base::android::ConvertUTF8ToJavaString;
13 using base::android::ScopedJavaGlobalRef;
14 using base::android::ScopedJavaLocalRef;
15
16 namespace android_webview {
17
AwPermissionRequest(scoped_ptr<AwPermissionRequestDelegate> delegate)18 AwPermissionRequest::AwPermissionRequest(
19 scoped_ptr<AwPermissionRequestDelegate> delegate)
20 : delegate_(delegate.Pass()),
21 weak_factory_(this) {
22 DCHECK(delegate_.get());
23 }
24
~AwPermissionRequest()25 AwPermissionRequest::~AwPermissionRequest() {
26 JNIEnv* env = AttachCurrentThread();
27 ScopedJavaLocalRef<jobject> j_object = java_ref_.get(env);
28 if (j_object.is_null())
29 return;
30 Java_AwPermissionRequest_detachNativeInstance(env, j_object.obj());
31 }
32
OnAccept(JNIEnv * env,jobject jcaller,jboolean accept)33 void AwPermissionRequest::OnAccept(JNIEnv* env,
34 jobject jcaller,
35 jboolean accept) {
36 delegate_->NotifyRequestResult(accept);
37 delete this;
38 }
39
CreateJavaPeer()40 ScopedJavaLocalRef<jobject> AwPermissionRequest::CreateJavaPeer() {
41 JNIEnv* env = AttachCurrentThread();
42 ScopedJavaLocalRef<jobject> j_object = Java_AwPermissionRequest_create(
43 env, reinterpret_cast<jlong>(this),
44 ConvertUTF8ToJavaString(env, GetOrigin().spec()).obj(), GetResources());
45 java_ref_ = JavaObjectWeakGlobalRef(env, j_object.obj());
46 return j_object;
47 }
48
GetJavaObject()49 ScopedJavaLocalRef<jobject> AwPermissionRequest::GetJavaObject() {
50 return java_ref_.get(AttachCurrentThread());
51 }
52
GetOrigin()53 const GURL& AwPermissionRequest::GetOrigin() {
54 return delegate_->GetOrigin();
55 }
56
GetResources()57 int64 AwPermissionRequest::GetResources() {
58 return delegate_->GetResources();
59 }
60
RegisterAwPermissionRequest(JNIEnv * env)61 bool RegisterAwPermissionRequest(JNIEnv* env) {
62 return RegisterNativesImpl(env);
63 }
64
65 } // namespace android_webivew
66