Home | History | Annotate | Download | only in android
      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.
      4 
      5 #include "content/browser/android/interstitial_page_delegate_android.h"
      6 
      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"
     12 
     13 using base::android::AttachCurrentThread;
     14 using base::android::ScopedJavaLocalRef;
     15 
     16 namespace content {
     17 
     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) {
     25 }
     26 
     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());
     32 }
     33 
     34 void InterstitialPageDelegateAndroid::Proceed(JNIEnv* env, jobject obj) {
     35   if (page_)
     36     page_->Proceed();
     37 }
     38 
     39 void InterstitialPageDelegateAndroid::DontProceed(JNIEnv* env,
     40                                                   jobject obj) {
     41   if (page_)
     42     page_->DontProceed();
     43 }
     44 
     45 std::string InterstitialPageDelegateAndroid::GetHTMLContents() {
     46   return html_content_;
     47 }
     48 
     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());
     54 }
     55 
     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());
     61 }
     62 
     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);
     73     }
     74 
     75     Java_InterstitialPageDelegateAndroid_commandReceived(
     76         env, obj.obj(),
     77         base::android::ConvertUTF8ToJavaString(env, sanitized_command).obj());
     78   }
     79 }
     80 
     81 // static
     82 bool InterstitialPageDelegateAndroid
     83     ::RegisterInterstitialPageDelegateAndroid(JNIEnv* env) {
     84   return RegisterNativesImpl(env);
     85 }
     86 
     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);
     92 }
     93 
     94 }  // namespace content
     95