Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / android / interstitial_page_delegate_android.cc
blobf598cd36a5c6dfee342ddbad2632f2394343366f
1 // Copyright (c) 2012 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 "content/browser/android/interstitial_page_delegate_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "content/public/browser/interstitial_page.h"
11 #include "jni/InterstitialPageDelegateAndroid_jni.h"
13 using base::android::AttachCurrentThread;
14 using base::android::ScopedJavaLocalRef;
16 namespace content {
18 InterstitialPageDelegateAndroid::InterstitialPageDelegateAndroid(
19 JNIEnv* env,
20 jobject obj,
21 const std::string& html_content)
22 : weak_java_obj_(env, obj),
23 html_content_(html_content),
24 page_(NULL) {
27 InterstitialPageDelegateAndroid::~InterstitialPageDelegateAndroid() {
28 JNIEnv* env = AttachCurrentThread();
29 ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env);
30 if (obj.obj())
31 Java_InterstitialPageDelegateAndroid_onNativeDestroyed(env, obj.obj());
34 void InterstitialPageDelegateAndroid::Proceed(JNIEnv* env, jobject obj) {
35 if (page_)
36 page_->Proceed();
39 void InterstitialPageDelegateAndroid::DontProceed(JNIEnv* env,
40 jobject obj) {
41 if (page_)
42 page_->DontProceed();
45 std::string InterstitialPageDelegateAndroid::GetHTMLContents() {
46 return html_content_;
49 void InterstitialPageDelegateAndroid::OnProceed() {
50 JNIEnv* env = AttachCurrentThread();
51 ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env);
52 if (obj.obj())
53 Java_InterstitialPageDelegateAndroid_onProceed(env, obj.obj());
56 void InterstitialPageDelegateAndroid::OnDontProceed() {
57 JNIEnv* env = AttachCurrentThread();
58 ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env);
59 if (obj.obj())
60 Java_InterstitialPageDelegateAndroid_onDontProceed(env, obj.obj());
63 void InterstitialPageDelegateAndroid::CommandReceived(
64 const std::string& command) {
65 JNIEnv* env = AttachCurrentThread();
66 ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env);
67 if (obj.obj()) {
68 std::string sanitized_command(command);
69 // The JSONified response has quotes, remove them.
70 if (sanitized_command.length() > 1 && sanitized_command[0] == '"') {
71 sanitized_command = sanitized_command.substr(
72 1, sanitized_command.length() - 2);
75 Java_InterstitialPageDelegateAndroid_commandReceived(
76 env, obj.obj(),
77 base::android::ConvertUTF8ToJavaString(env, sanitized_command).obj());
81 // static
82 bool InterstitialPageDelegateAndroid
83 ::RegisterInterstitialPageDelegateAndroid(JNIEnv* env) {
84 return RegisterNativesImpl(env);
87 static jlong Init(JNIEnv* env, jobject obj, jstring html_content) {
88 InterstitialPageDelegateAndroid* delegate =
89 new InterstitialPageDelegateAndroid(
90 env, obj, base::android::ConvertJavaStringToUTF8(env, html_content));
91 return reinterpret_cast<intptr_t>(delegate);
94 } // namespace content