1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91

android_webview / browser / permission / aw_permission_request.cc [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "android_webview/browser/permission/aw_permission_request.h"

#include <utility>

#include "android_webview/browser/permission/aw_permission_request_delegate.h"
#include "base/android/jni_string.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "android_webview/browser_jni_headers/AwPermissionRequest_jni.h"

using base::android::ConvertUTF8ToJavaString;
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
using jni_zero::AttachCurrentThread;

namespace android_webview {

// static
base::android::ScopedJavaLocalRef<jobject> AwPermissionRequest::Create(
    std::unique_ptr<AwPermissionRequestDelegate> delegate,
    base::WeakPtr<AwPermissionRequest>* weak_ptr) {
  base::android::ScopedJavaLocalRef<jobject> java_peer;
  AwPermissionRequest* permission_request =
      new AwPermissionRequest(std::move(delegate), &java_peer);
  *weak_ptr = permission_request->weak_factory_.GetWeakPtr();
  return java_peer;
}

AwPermissionRequest::AwPermissionRequest(
    std::unique_ptr<AwPermissionRequestDelegate> delegate,
    ScopedJavaLocalRef<jobject>* java_peer)
    : delegate_(std::move(delegate)), processed_(false) {
  DCHECK(delegate_.get());
  DCHECK(java_peer);

  JNIEnv* env = AttachCurrentThread();
  *java_peer = Java_AwPermissionRequest_create(
      env, reinterpret_cast<jlong>(this), GetOrigin().spec(), GetResources());
  java_ref_ = JavaObjectWeakGlobalRef(env, *java_peer);
}

AwPermissionRequest::~AwPermissionRequest() {
  OnAcceptInternal(false);
}

void AwPermissionRequest::OnAccept(JNIEnv* env,
                                   const JavaParamRef<jobject>& jcaller,
                                   jboolean accept) {
  OnAcceptInternal(accept);
}

void AwPermissionRequest::OnAcceptInternal(bool accept) {
  if (!processed_) {
    delegate_->NotifyRequestResult(accept);
    processed_ = true;
  }
}

void AwPermissionRequest::DeleteThis() {
  ScopedJavaLocalRef<jobject> j_request = GetJavaObject();
  if (!j_request)
    return;
  Java_AwPermissionRequest_destroyNative(AttachCurrentThread(), j_request);
}

void AwPermissionRequest::Destroy(JNIEnv* env) {
  delete this;
}

ScopedJavaLocalRef<jobject> AwPermissionRequest::GetJavaObject() {
  return java_ref_.get(AttachCurrentThread());
}

const GURL& AwPermissionRequest::GetOrigin() {
  return delegate_->GetOrigin();
}

int64_t AwPermissionRequest::GetResources() {
  return delegate_->GetResources();
}

void AwPermissionRequest::CancelAndDelete() {
  processed_ = true;
  DeleteThis();
}

}  // namespace android_webview