11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Copyright 2014 The Chromium Authors. All rights reserved.
21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Use of this source code is governed by a BSD-style license that can be
31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// found in the LICENSE file.
41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/shell/browser/android/cast_window_manager.h"
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include <jni.h>
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/android/jni_android.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/android/jni_string.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/android/scoped_java_ref.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/bind.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/lazy_instance.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/common/chromecast_config.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/common/pref_names.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/shell/browser/android/cast_window_android.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/shell/browser/cast_browser_context.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/shell/browser/cast_browser_main_parts.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/shell/browser/cast_browser_process.h"
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/shell/browser/cast_content_browser_client.h"
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/browser/browser_thread.h"
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/browser/web_contents.h"
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ipc/ipc_channel.h"
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "jni/CastWindowManager_jni.h"
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "url/gurl.h"
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace {
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibase::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    g_window_manager = LAZY_INSTANCE_INITIALIZER;
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace chromecast {
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace shell {
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibase::android::ScopedJavaLocalRef<jobject>
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciCreateCastWindowView(CastWindowAndroid* shell) {
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  JNIEnv* env = base::android::AttachCurrentThread();
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  jobject j_window_manager = g_window_manager.Get().obj();
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return Java_CastWindowManager_createCastWindow(env, j_window_manager);
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid CloseCastWindowView(jobject shell_wrapper) {
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  JNIEnv* env = base::android::AttachCurrentThread();
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  jobject j_window_manager = g_window_manager.Get().obj();
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Java_CastWindowManager_closeCastWindow(env, j_window_manager, shell_wrapper);
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Register native methods
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool RegisterCastWindowManager(JNIEnv* env) {
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return RegisterNativesImpl(env);
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid Init(JNIEnv* env, jclass clazz, jobject obj) {
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  g_window_manager.Get().Reset(
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      base::android::ScopedJavaLocalRef<jobject>(env, obj));
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccijlong LaunchCastWindow(JNIEnv* env, jclass clazz, jstring jurl) {
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  GURL url(base::android::ConvertJavaStringToUTF8(env, jurl));
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return reinterpret_cast<jlong>(
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      CastWindowAndroid::CreateNewWindow(
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          CastBrowserProcess::GetInstance()->browser_context(),
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          url));
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid StopCastWindow(JNIEnv* env, jclass clazz,
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                    jlong nativeCastWindow, jboolean gracefully) {
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CastWindowAndroid* window =
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      reinterpret_cast<CastWindowAndroid*>(nativeCastWindow);
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(window);
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (gracefully)
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    window->Close();
751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  else
761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    window->Destroy();
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid EnableDevTools(JNIEnv* env, jclass clazz, jboolean enable) {
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // The specific port value doesn't matter since Android uses Unix domain
821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // sockets, only whether or not it is zero.
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  chromecast::ChromecastConfig::GetInstance()->pref_service()->
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      SetInteger(prefs::kRemoteDebuggingPort, enable ? 1 : 0);
851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace shell
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace chromecast
89