12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file. 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/browser/android/interstitial_page_delegate_android.h" 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/android/jni_android.h" 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/android/jni_string.h" 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/android/scoped_java_ref.h" 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/interstitial_page.h" 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "jni/InterstitialPageDelegateAndroid_jni.h" 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using base::android::AttachCurrentThread; 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using base::android::ScopedJavaLocalRef; 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace content { 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)InterstitialPageDelegateAndroid::InterstitialPageDelegateAndroid( 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) JNIEnv* env, 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) jobject obj, 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& html_content) 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : weak_java_obj_(env, obj), 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) html_content_(html_content), 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) page_(NULL) { 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)InterstitialPageDelegateAndroid::~InterstitialPageDelegateAndroid() { 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) JNIEnv* env = AttachCurrentThread(); 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env); 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (obj.obj()) 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Java_InterstitialPageDelegateAndroid_onNativeDestroyed(env, obj.obj()); 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void InterstitialPageDelegateAndroid::Proceed(JNIEnv* env, jobject obj) { 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (page_) 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) page_->Proceed(); 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void InterstitialPageDelegateAndroid::DontProceed(JNIEnv* env, 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) jobject obj) { 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (page_) 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) page_->DontProceed(); 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string InterstitialPageDelegateAndroid::GetHTMLContents() { 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return html_content_; 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void InterstitialPageDelegateAndroid::OnProceed() { 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) JNIEnv* env = AttachCurrentThread(); 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env); 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (obj.obj()) 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Java_InterstitialPageDelegateAndroid_onProceed(env, obj.obj()); 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void InterstitialPageDelegateAndroid::OnDontProceed() { 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) JNIEnv* env = AttachCurrentThread(); 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env); 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (obj.obj()) 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Java_InterstitialPageDelegateAndroid_onDontProceed(env, obj.obj()); 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void InterstitialPageDelegateAndroid::CommandReceived( 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& command) { 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) JNIEnv* env = AttachCurrentThread(); 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ScopedJavaLocalRef<jobject> obj = weak_java_obj_.get(env); 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (obj.obj()) { 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string sanitized_command(command); 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // The JSONified response has quotes, remove them. 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (sanitized_command.length() > 1 && sanitized_command[0] == '"') { 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) sanitized_command = sanitized_command.substr( 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1, sanitized_command.length() - 2); 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Java_InterstitialPageDelegateAndroid_commandReceived( 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) env, obj.obj(), 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::android::ConvertUTF8ToJavaString(env, sanitized_command).obj()); 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool InterstitialPageDelegateAndroid 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ::RegisterInterstitialPageDelegateAndroid(JNIEnv* env) { 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return RegisterNativesImpl(env); 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)static jlong Init(JNIEnv* env, jobject obj, jstring html_content) { 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) InterstitialPageDelegateAndroid* delegate = 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) new InterstitialPageDelegateAndroid( 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) env, obj, base::android::ConvertJavaStringToUTF8(env, html_content)); 91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return reinterpret_cast<intptr_t>(delegate); 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace content 95