blob: ea3dde80c354826a1197c75b4233fe3acee93d21 [file] [log] [blame]
// Copyright 2016 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 "chromecast/browser/android/cast_content_window_android.h"
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/memory/ptr_util.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/renderer_preferences.h"
#include "jni/CastContentWindowAndroid_jni.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/events/keycodes/keyboard_code_conversion_android.h"
namespace chromecast {
namespace shell {
namespace {
base::android::ScopedJavaLocalRef<jobject>
CreateJavaWindow(jlong nativeWindow, bool isHeadless, bool enableTouchInput) {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_CastContentWindowAndroid_create(env, nativeWindow, isHeadless,
enableTouchInput);
}
} // namespace
// static
std::unique_ptr<CastContentWindow> CastContentWindow::Create(
CastContentWindow::Delegate* delegate,
bool isHeadless,
bool enable_touch_input) {
return base::WrapUnique(
new CastContentWindowAndroid(delegate, isHeadless, enable_touch_input));
}
CastContentWindowAndroid::CastContentWindowAndroid(
CastContentWindow::Delegate* delegate,
bool isHeadless,
bool enable_touch_input)
: delegate_(delegate),
java_window_(CreateJavaWindow(reinterpret_cast<jlong>(this),
isHeadless,
enable_touch_input)) {
DCHECK(delegate_);
}
CastContentWindowAndroid::~CastContentWindowAndroid() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_CastContentWindowAndroid_onNativeDestroyed(env, java_window_);
}
void CastContentWindowAndroid::ShowWebContents(
content::WebContents* web_contents,
CastWindowManager* window_manager) {
DCHECK(window_manager);
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobject> java_web_contents =
web_contents->GetJavaWebContents();
Java_CastContentWindowAndroid_showWebContents(env, java_window_,
java_web_contents);
}
void CastContentWindowAndroid::OnActivityStopped(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller) {
delegate_->OnWindowDestroyed();
}
void CastContentWindowAndroid::OnKeyDown(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
int keycode) {
ui::KeyEvent key_event(ui::ET_KEY_PRESSED,
ui::KeyboardCodeFromAndroidKeyCode(keycode),
ui::EF_NONE);
delegate_->OnKeyEvent(key_event);
}
} // namespace shell
} // namespace chromecast