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
11using base::android::AttachCurrentThread;
12using base::android::ConvertUTF8ToJavaString;
13using base::android::ScopedJavaGlobalRef;
14using base::android::ScopedJavaLocalRef;
15
16namespace android_webview {
17
18AwPermissionRequest::AwPermissionRequest(
19    scoped_ptr<AwPermissionRequestDelegate> delegate)
20    : delegate_(delegate.Pass()),
21      weak_factory_(this) {
22  DCHECK(delegate_.get());
23}
24
25AwPermissionRequest::~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
33void AwPermissionRequest::OnAccept(JNIEnv* env,
34                                   jobject jcaller,
35                                   jboolean accept) {
36  delegate_->NotifyRequestResult(accept);
37  delete this;
38}
39
40ScopedJavaLocalRef<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
49ScopedJavaLocalRef<jobject> AwPermissionRequest::GetJavaObject() {
50  return java_ref_.get(AttachCurrentThread());
51}
52
53const GURL& AwPermissionRequest::GetOrigin() {
54  return delegate_->GetOrigin();
55}
56
57int64 AwPermissionRequest::GetResources() {
58  return delegate_->GetResources();
59}
60
61bool RegisterAwPermissionRequest(JNIEnv* env) {
62  return RegisterNativesImpl(env);
63}
64
65}  // namespace android_webivew
66