summaryrefslogtreecommitdiffstats
path: root/content/browser/android/interstitial_page_delegate_android.cc
blob: 4ed48907bed7ba469a5ac4e6f7170d3368a2ac32 (plain)
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
92
93
94
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/android/interstitial_page_delegate_android.h"

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "content/public/browser/interstitial_page.h"
#include "jni/InterstitialPageDelegateAndroid_jni.h"

using base::android::AttachCurrentThread;
using base::android::ScopedJavaLocalRef;

namespace content {

InterstitialPageDelegateAndroid::InterstitialPageDelegateAndroid(
    JNIEnv* env,
    jobject obj,
    const std::string& html_content)
    : weak_java_obj_(env, obj),
      html_content_(html_content),
      page_(NULL) {
}

InterstitialPageDelegateAndroid::~InterstitialPageDelegateAndroid() {
  JNIEnv* env = AttachCurrentThread();
  ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env);
  if (obj.obj())
    Java_InterstitialPageDelegateAndroid_onNativeDestroyed(env, obj.obj());
}

void InterstitialPageDelegateAndroid::Proceed(JNIEnv* env, jobject obj) {
  if (page_)
    page_->Proceed();
}

void InterstitialPageDelegateAndroid::DontProceed(JNIEnv* env,
                                                  jobject obj) {
  if (page_)
    page_->DontProceed();
}

std::string InterstitialPageDelegateAndroid::GetHTMLContents() {
  return html_content_;
}

void InterstitialPageDelegateAndroid::OnProceed() {
  JNIEnv* env = AttachCurrentThread();
  ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env);
  if (obj.obj())
    Java_InterstitialPageDelegateAndroid_onProceed(env, obj.obj());
}

void InterstitialPageDelegateAndroid::OnDontProceed() {
  JNIEnv* env = AttachCurrentThread();
  ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env);
  if (obj.obj())
    Java_InterstitialPageDelegateAndroid_onDontProceed(env, obj.obj());
}

void InterstitialPageDelegateAndroid::CommandReceived(
    const std::string& command) {
  JNIEnv* env = AttachCurrentThread();
  ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env);
  if (obj.obj()) {
    std::string sanitized_command(command);
    // The JSONified response has quotes, remove them.
    if (sanitized_command.length() > 1 && sanitized_command[0] == '"') {
      sanitized_command = sanitized_command.substr(
          1, sanitized_command.length() - 2);
    }

    Java_InterstitialPageDelegateAndroid_commandReceived(
        env, obj.obj(),
        base::android::ConvertUTF8ToJavaString(env, sanitized_command).obj());
  }
}

// static
bool InterstitialPageDelegateAndroid
    ::RegisterInterstitialPageDelegateAndroid(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

static jint Init(JNIEnv* env, jobject obj, jstring html_content) {
  InterstitialPageDelegateAndroid* delegate =
      new InterstitialPageDelegateAndroid(
          env, obj, base::android::ConvertJavaStringToUTF8(env, html_content));
  return reinterpret_cast<jint>(delegate);
}

}  // namespace content